Friday, September 9, 2022

Kubernetes Service Type

Kubernetes Service Type:

 

Cluster-IP:


#kubectl get pods

#kubectl expose  pod  mypod   --port=9000  --target-port=80  --name myservice  ==> by default cluster-ip

#kubectl get services

#kubectl get service

#kubectl get svc

#curl ip:9000


Node Port:



#kubectl expose pod mypod --type=NodePort --port=8000  --target-port=80 --name myservice1



How service works?

vi myservice.yml

apiVersion: v1

kind: Service

metadata:

   name:  firstservice

   labels: 

       env: test

spec:

    type: NodePort

    ports: 

      - nodePort: 9000  ==> Node port 

        port:  8000   ==> Service Port

        targetPort: 80  ==> Container Port

   selectors:

       type: app











1)Node Port








Types of Services

By default, Kubernetes creates a ClusterIP type of service. We can build different kinds of services by having a spec.type property in the service YAML file.

The four types of services are:

ClusterIP

Accessible within the cluster. Dependent applications can interact with other applications internally using the ClusterIP service.

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  type: ClusterIP #optional for ClusterIP
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379

NodePort

NodePort services are accessible outside the cluster. It creates a mapping of pods to its hosting node/machine on a static port. For example, you have a node with IP address 10.0.0.20 and a Redis pod running under it. NodePort will expose 10.0.0.20:30038, assuming the port exposed is 30038, which you can then access outside the Kubernetes cluster.

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  type: NodePort
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
      nodePort: 30038

LoadBalancer

This service type creates load balancers in various Cloud providers like AWS, GCP, Azure, etc., to expose our application to the Internet. The Cloud provider will provide a mechanism for routing the traffic to the services. The most common example usage of this type is for a website or a web app.

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  type: LoadBalancer
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379


No comments:

Post a Comment

Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117