What is a Service?

It allows exposing the application running on a pod/deployment to,

  • To other pods.
  • To an external use.
  • Service types.

Pictorial representation of a service

Image 1
Pictorial representation of a service (iptables proxy mode), from kubernetes.io

Service types

  • ClusterIP (default type)
  • NodePort
  • LoadBalancer

ClusterIP

We will be having some situations like we need the application container to reach the DB service. It happens within the cluster. We do not need the DB port to be accessible to the external user. In this scenario, we can make use of the clusterIP. Exposes the DB/application port to its internal IP. ClusterIP is the default ServiceType in Kubernetes.

NodePort

The default Kubernetes service(ClusterIP) exposes the port within the cluster. What if we need access to the application outside? NodePort service makes the application visible outside the Kubernetes cluster. You’ll be able to contact the NodePort outside the cluster. You can specify the : to access the exposed port. NodePort ranges (typically 30000–32767 )

LoadBalancer

In the NodePort service, the port will be listening to each node. We need a mechanism to distribute the external access to each node. This can be accomplished by keeping a network load balancer. The majority of the cloud providers provide LoadBalancer for Kubernetes without any additional configuration. That is what the service type ‘LoadBalancer’ does. Traffic from the external load balancer is directed to the backend pods. The cloud provider decides how it is load balanced.

Service Definition file

The structure of the service definition file looks like this.

apiVersion: 
kind: 
metadata:
  name: 
spec:
  type: 
  selector:
    app: 
  ports:
    - port: 
      targetPort: 
      nodePort: 

We will fill in the required information to create a service.

apiVersion: v1
kind: Service
metadata:
  name: test-service
spec:
  # By default the `service type ` is set to ClusterIp if it is not defined in the defenition file.
  type: NodePort
  selector:
    app: test-app
  ports:
      # By default the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 80
      # Optional field
      # By default Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007

Creating the Service

To create a service syntax is "kubectl create -f <service defenition file name >" 

We will create a service and have a look at it. Created the same service defenition file in to our Kubernetes setup.

rajith@k8s-master:~$ vi test-service.yaml
rajith@k8s-master:~$ cat test-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: test-service
spec:
  # By default the `service type ` is set to ClusterIp if it is not defined in the defenition file.
  type: NodePort
  selector:
    app: test-app
  ports:
      # By default the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 80
      # Optional field
      # By default Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007
rajith@k8s-master:~$ 
rajith@k8s-master:~$ kubectl create -f test-service.yaml
service/test-service created
rajith@k8s-master:~$

Created the service.We will validate the service.

rajith@k8s-master:~$ kubectl get svc 
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes     ClusterIP   10.96.0.1       <none>        443/TCP        24d
test-service   NodePort    10.99.149.101   <none>        80:30007/TCP   7s
rajith@k8s-master:~$ 
  • We have two services running on this cluster.
  • The service named ‘kubernetes’ is the default service coming along with the cluster deployment.
  • The name of the service which we created is ‘test-service ‘.
  • It is a ‘NodePort’ service.
  • It is listening to port 300007.

In the next module we will have a demo with the below steps.

  • Creating a Kubernetes deployment with Nginx image.
  • Verify the deployment details and try connecting to Nginx.
  • Create a NodePort service to expose the Nginx application.
  • Verify the Nginx connectivity after the service creation.
  • Verify the details of the service and the nginx deployment.
  • Delete the service and try accessing Nginx.
  • Destroy the deployment.

Next module, Service demo, click here