Shashikant shah

Sunday 28 April 2024

What is Endpoints Service in k8s cluster ?

 Endpoint Services :

When you create a service in Kubernetes, it automatically creates an endpoint associated with that service. This endpoint is essentially a list of IP addresses and ports of the pods that the service is directing traffic to. So, when one application wants to talk to another application within the cluster, it looks up the endpoint associated with the target service to find out where to send its requests.


Why are required endpoints?

There is a front-end pod and a back-end application pod and all requests from the front-end go to the backend pod, If there is a redeployment in the backend pod, then IP has been changed for backend pods and the front-end pod will not be able to send requests to the backend, Its solution is done from the end-point.



# kubectl run frontend-pod --image=curlimages/curl --command -- sleep 3600

# kubectl run backend-pod --image=nginx

# kubectl get pods -o wide

# kubectl exec  -it frontend-pod -- sh

Curl to backend pods server from frontend pods.

Redeployment then Ip change.


For declarative way

# vim service.yaml

apiVersion: v1

kind: Service

metadata:

   name: clusterip-service

spec:

   ports:

   - port: 8080

     targetPort: 80

# kubectl apply -f service.yaml

# kubectl describe svc clusterip-service


Add the backend IP in the endpoint. But it’s a manual task.

# vim endpoint.yaml

apiVersion: v1

kind: Endpoints

metadata:

  name: clusterip-service

subsets:

  - addresses:

      - ip: 10.244.1.55

    ports:

      - port: 80

# kubectl apply -f endpoint.yaml

# kubectl get ep

# kubectl describe svc clusterip-service

# kubectl exec  -it frontend-pod -- sh

Hit to Endpoint 10.103.190.232:8080



ii)Using Selectors with endpoint in service:

Use case: if there are 500 Pods, we will have to manually define each IP in the endpoint.

Kubernetes allows us to define the list of labels of PODS that need to be added as part of Endpoints.

All Pods that match those labels will be added.



1.     Create deployment.




# vim demo-deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-deployment

  labels:

    app: nginx

spec:

  replicas: 3

  selector:

    matchLabels:

      app: nginx

  template:

    metadata:

      labels:

        app: nginx

    spec:

      containers:

      - name: nginx

        image: nginx:1.14.2

        ports:

        - containerPort: 80

# kubectl apply -f demo-deployment.yaml

For pods status :-

# kubectl get pods --show-labels


For deployment status :-

# kubectl get deployments -o wide


For replicaset status :-

# kubectl get rs -o wide


2. Creating Service.

# vim service-selector.yaml

apiVersion: v1

kind: Service

metadata:

   name: service-selector

spec:

   selector:

     app: nginx

   ports:

   - port: 80

     targetPort: 80

# kubectl apply -f service-selector.yaml

# kubectl get svc


# kubectl describe service service-selector


# kubectl get endpoints service-selector

# kubectl scale deployment/nginx-deployment --replicas=10

# kubectl describe service service-selector

# kubectl describe endpoints service-selector

From nodes :-

# curl 10.96.248.224:8080


for Imperative way

Port forwarding of pods.

# kubectl port-forward --address 0.0.0.0 pod/firstpod 8091:80

Port forwarding of services.

# kubectl port-forward --address 0.0.0.0 service/myfistservice 8090:8001

No comments:

Post a Comment