Skip to main content

Command Palette

Search for a command to run...

Exploring Kubernetes: Services

Updated
•2 min read•View as Markdown

In k8s each pod gets its internal IP, but pods are ephemeral and every time a pod is destroyed and new is created Ip gets change, then it would be difficult to access applications running on that pod.

Services
-helps to create a stable IP address and keep application running smoothly.
-do the load balancing by sending traffic equally on all pods

How services work?
Each pod is assigned with labels and service contains a selector. Services use this selector keyword and search for pod with matching labels

Types of Services:

  1. NodePort: It is used to access the application outside the cluster but in the same network. NodePort service opens port(30000-32767) on each node.
    --Port of the pod(where you have to send traffic) is k/as Target Port.

    --Port: port of service.(It helps to communicate to n fro)

  2. Cluster IP: To access the IP within the cluster, say there is an application and its database in same cluster, so to access/communicate with each other they need to have cluster.

  3. Load Balancer: This service sends equal no of requests on each pod and it works at transport layer using round robin algorithm.

     apiVersion: v1
     kind: Service
     metadata:
         name: my-service
     spec:
        type: NodePort
        selector: my-app
     ports:
         - port: 80 (service's port node)
           targetPort: 9367 (pod's node port)
           nodePort: 30007
    

Creating services using imperative mode: It can be done using expose command.

kubectl expose deploy my-service --type=LoadBalancer --port=8080

Q1.Why we need multiple replicas of pod?/ ideal no of pod replicas?
It depends on the no of requests a pod can handle at any given time, if 1 rc can req 10 request and we have 100 req then 10 rc are needed.

Q2. can you have services with no selector?
Ans: Yes, create endpoint slice manually add it

Q3. Can services have internal IP?
Ans: yes, you cannot send traffic on service's IP. Ip of service and IP of node port has no relation