Services, Load Balancing, and Networking
apiVersion: v1
kind: Service
metadata:
name: echosvc
spec:
selector:
app: echo
ports:
- protocol: TCP
port: 9090 # Published Port
targetPort: 8080 # Exposed port in the container
Exercise
Save the above content in a file name svc.yaml
and run
k apply -f svc.yaml
k port-forward service/echosvc 9090:9090
kubectl get endpoints echosvc
Change the service type
NodePort
apiVersion: v1
kind: Service
metadata:
name: echosvc
spec:
type: NodePort
selector:
app: echo
ports:
- protocol: TCP
port: 9090 # Published Port
targetPort: 8080 # Exposed port in the container
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30007
LoadBalancer
apiVersion: v1
kind: Service
metadata:
name: echosvc
spec:
type: LoadBalancer
selector:
app: echo
ports:
- protocol: TCP
port: 9090 # Published Port
targetPort: 8080 # Exposed port in the container
Ingress
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
serviceName: test
servicePort: 80
References
- https://kubernetes.io/docs/concepts/services-networking/service/#service-resource
- https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/
- https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
- https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/
- https://docs.traefik.io/providers/kubernetes-ingress/