Welcome to this brief description of Kubernetes services in 5 min. Reader would need some understanding of Kubernetes and YAML. If you want to know more about this sort things check my playlist “Great presentations and demos of K8s and tools” in our Youtube Channel and don’t forget to subscribe.
Kubernetes services are objects like pods: Replica sets or Deployments. Why would I need K8s Services? Quick answer: Because they enable communications with outsider elements to the applications that reside in pods. They help to connect Apps with other Apps or users.
One example of services: A group of front-end pods using services to connect a backend group, and and this backend is also using services to connect with other that have direct access to data.
- Services help to front end pods to be accessible from users and spread the load over the pods.
- Front-ends pods used back-end service to access this group of pods.
- Data pods can be in an internal service also to be accesible from the back-end pods.
- Pods can be located in any K8s cluster node.
We have three type of services:
Kubernetes Services in 5 min: Types
Users want to access a web page in a pod. As you know pods use and internal network as containers and they need to be exposed in some way to the external network where users are located. Kubernetes provides a service that can listen a request in the Node and then forward it to the requested pod with the App. This type of service is known as Node Port, because works in a specific port i the Node and it’s forwarded to a specific port in a group of pods.
We have three kinds of ports (from the point of view of the service):
- Target Port: Port located at the pod with the web App where the service forward any request to (i.e. 80 TCP)
- Port: It’s the port at the service itself. The service is actually a server with its own IP address. Then this port is located at this IP (i.e. 80 TCP)
- NodePort: It’s the port at the external interface in the node or k8s worker (i.e. 30280 TCP). Thos esports can be between 30000 – 32727
apiVersion: v1 kind: Service metadata: name: webapp-service spec: type: NodePort ports: - targetPort: 80 port: 80 nodePort: 30008 selector: app: webapp type: front-end
If you don’t define port in the YAML file of the service definition, then it will use the same as the targetort. If NodePort is not defined, it will use any in the range. You can use label or selectors to specify the pod to be mapped. Those selector can apply to the following pod.
apiVersion: v1 kind: Pod metadata: name: webapp-pod labels: app: webapp type: front-end spec: containers: - name: nginx01 image: ngnix
Create the service using :
kubectl create -f service-definiton.yml
kubectl get services
NodePort uses a random algorithm to balance traffic on all the pods that match with the selector ( and SessionAffinity: Yes). K8s also will expand the service over all nodes or workers in the cluster and uses exactly the same NodePort at all workers.
Kubernetes Services in 5 min: NodePort
Internal IP in the Cluster to enable services through IPs internally in the cluster to make them available to other services (i.e. frontend pods to backend ports)
apiVersion: v1 kind: Service metadata: name: webapp-service spec: type: NodePort ports: - targetPort: 8080 port: 80 selector: app: webapp
It provides a load balancers for applications. Load balancers are external in the cloud service providers, and it uses to distribute the load among different pods with applications. This feature is only available for cloud providers or environments which support external load balancers.
apiVersion: v1 kind: Service metadata: name: webapp-service spec: type: NodePort ports: - targetPort: 8080 port: 80 type: LoadBalancer selector: app: webapp
That’s it. Don’t forget to leave a comment. See ya!