/    /  Kubernetes – Service

Kubernetes – Service

 

A service is a logical set of pods. This abstraction provides a single IP address and DNS name through which pods can be accessed. Pods can scale very easily with Service’s load balancing configuration.

Kubernetes services are REST objects whose definition can be posted to the Kubernetes API Server on the Kubernetes master.

Selectorless service

apiVersion: v1
kind: Service
metadata:
   name: Sample_service
spec:
   ports:
   - port: 8080
   targetPort: 31922

Sample_service will be created by the above configuration

Config file with selector

apiVersion: v1
kind: Service
metadata:
   name: Sample_service
spec:
   selector:
      application: "My Application" -------------------> (Selector)
   ports:
   - port: 8080
   targetPort: 31922

We have a selector in this example, so we need to create an endpoint manually to transfer traffic.

apiVersion: v1
kind: Endpoints
metadata:
   name: Sample_service
subnets:
   address:
      "ip": "192.157.157.30" -------------------> (Selector)
   ports:
      - port: 8080

The above code creates an endpoint that will route traffic to the endpoint defined as “192.157.157.30:8080 ”.

Multi-Port Service Creation

apiVersion: v1
kind: Service
metadata:
   name: Sample_service
spec:
   selector:
      application: “My Application” -------------------> (Selector)
   ClusterIP: 10.3.0.12
   ports:
      -name: http
      protocol: TCP
      port: 80
      targetPort: 31922
   -name:https
      Protocol: TCP
      Port: 443
      targetPort: 31921

Types of Services

ClusterIP − It exposes the service within the defined Kubernetes cluster. It restricts the service within the cluster.

spec:
   ports:
   - port: 8080
      nodePort: 31922
      name: NodeportService
      clusterIP: 10.20.30.40

NodePort − 

The service will be exposed on a static port. ClusterIP, to which NodePort will route, will be automatically created. NodeIP:nodePort lets you access the service outside of the cluster.

spec:
   type: NodePort
   ports:
   - port: 8080
      nodePort: 31922
      name: NodeportService

 

Load Balancer − The external load balancer routes traffic to NodePort and ClusterIP services automatically created by the cloud provider.

Try creating a full service yaml file with the service type as Node Port.

apiVersion: v1
kind: Service
metadata:
   name: appname
   labels:
      k8s-app: appname
spec:
   type: NodePort
   ports:
   - port: 8080
      nodePort: 31922
      name: omninginx
   selector:
      k8s-app: appname
      component: nginx
      env: env_name