How To Deal With Kubernetes ClusterIP And NodePort?

How To Deal With Kubernetes ClusterIP And NodePort?

Table of contents

Kubernetes have an object type called Service. Service is used to create a connection between nodes, and expose applications outside the cluster to the internet. Kubernetes provides IPs to the pod after creating it, but the problem arrives when the pod stopped and a new pod starts which comes with a new IP. so it gets very difficult to access the application. to overcome this issue Kubernetes comes with a services object.

ClusterIP:

ClusterIP is a Virtual IP (VIP) that gets mapped with the pod's IPs. Kube-Proxy does this mapping operation. It is just a layer over the pod IP. Using cluster Ip it gets very easier to create networking between the different nodes.

Let's see how it works.

Create a deployment with an httpd running in a container and start the deployment.

kind: Deployment
apiVersion: apps/v1
metadata:
  name: depl
spec:
  selector:
    matchLabels:
      env: prod
  replicas: 2
  template:
    metadata:
      name: dpod
      labels:
        env: prod
    spec:
      containers:
        - name: httpcon
          image: httpd
          ports:
          - containerPort: 80

Now let's create a ClusterIP service.

kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    env: prod
  type: ClusterIP

Here be careful with selectors. it should same as the label of the pod.

To get the ClusterIP and check whether the connection is established or not.

$ kubectl get svc   #you can write service instead of svc

$ curl <ClusterIp:Port>

NodePort:

To expose the application outside the cluster the NodePort is used. we can access the application from the Internet. As the name suggests it's the node port, it exposes the port of each node. here also Kube proxy comes in the picture, it exposes ports from 30000-32767.

Let's expose the same deployment from above and create a new service for Nodeport

kind: Service
apiVersion: v1
metadata:
  name: nodeservice
spec:
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    env: prod
  type: NodePort

To get the exposed port

$ kubectl get svc
# Nodeport ranges from 30000-32767

To access the application from the browser.

If you are running the k8s cluster on the cloud, just get the public IP and type node port after the colon in the browser.

If you are using Minikube.

$ minikube svc <service-name>
# $ minikube svc nodeservice

OR

$ minikube svc <service-name> --url # You will get the url

Conclusion:

We learned the Kubernetes service and its types ClusterIP and NodePort

ClusterIP is used for internal communication between the nodes and Nodeport is used to access the application from outside the cluster or can say from the internet but still, we have to change the port numbers to access the application from different nodes.

The Next blog will be about Service called LoadBalancer.

Thank You!!!

Did you find this article valuable?

Support Pranav Thorve by becoming a sponsor. Any amount is appreciated!