DEV Community

Cover image for Bite-Sized Kubernetes Part 5 - Services
Hrittik Bhattacharjee
Hrittik Bhattacharjee

Posted on • Updated on

Bite-Sized Kubernetes Part 5 - Services

Heave Ho!☸️

In Part 4 we learnt about deployments in kubernetes.
Moseying along now, to Services!🏴‍☠️

OBJECT DESCRIPTION
Pods The lowest level, single instance of an application
ReplicaSets The next level, manages a set of pods
Deployments The next level, manages a set of ReplicaSets
👉 Services The next level, manages a set of deployments
Ingress The next level, manages a set of services
Volumes The next level, manages a set of ingress
Namespaces The next level, manages a set of volumes
Cluster The highest level, manages a set of namespaces

Services:

  • K8s services enable the pods to communicate with the outside world.
  • K8s services also enable the pods to communicate with each other.
  • Services types:
    • NodePort service: a service that listens for requests on a port on the k8s node and forwards them to the pods
    • ClusterIP service: a service that creates a virtual IP address that is accessible only within the cluster, for communication between various services within the cluster.
    • Load Balancer service: a service that creates a load balancer in the cloud provider that forwards requests to the pods accordingly to balance load.

NodePort Service:

  • Target port: the port on the pod that the service forwards requests to.
  • Node port: the port on the k8s node that the service listens for requests on.
  • Port: the port on the service that it listens on.
  • The service has its own IP address, called the cluster IP address of the service.
  • Creating a NodePort service:

    • Example service-definition.yml:

      apiVersion: v1
      kind: Service
      metadata:
        name: my-app-service
        labels:
          app: my-app
          type: my-type
      spec:
        type: NodePort
        ports:
          - targetPort: 8080
            port: 80
            nodePort: 30080
        # selector identifies the pods that the service will forward requests to
        selector:
          app: my-app
          type: my-type
      
    • The type property can be set to NodePort or ClusterIP or LoadBalancer depending on the type of service we're creating.

    • Once the .yml file is created, it can be used to create a service:

      $ kubectl create -f <file name>.yml
      
    • See list of services:

      $ kubectl get services
      
    • See detailed info about a service:

      $ kubectl describe service <service name>
      
    • To access the service from outside the cluster, use the node IP address and the node port:

      $ curl <node IP address>:<node port>
      
    • To access the service from inside the cluster, use the cluster IP address and the port:

      $ curl <cluster IP address>:<port>
      
  • If there are multiple nodes in the cluster with their own pods, the service created can be accessed from all the nodes in the cluster.

ClusterIP Service:

  • A service that creates a virtual IP address that is accessible only within the cluster, for communication between various services within the cluster.
  • It groups together a set of pods and exposes them as a single service e.g. a service for all pods running the frontend app, a service for all pods running the backend app, for the database pods, redis pods etc.
  • Creating a ClusterIP service:

    • Example service-definition.yml:

      apiVersion: v1
      kind: Service
      metadata:
        name: my-app-service
        labels:
          app: my-app
          type: my-type
      spec:
        type: ClusterIP
        ports:
            # target port: the port on the pod that the service forwards requests to
            # port: the port on the service that it listens on
            # no need to specify nodePort since this is a ClusterIP service
          - targetPort: 8080
            port: 80
        selector:
          app: my-app
          type: my-type
      
  • Commands are same as the ones above for NodePort service.

LoadBalancer Service:

  • A service that creates a load balancer in the cloud provider that forwards requests to the pods accordingly to balance load.
  • Creating a LoadBalancer service:

    • Example service-definition.yml:

      apiVersion: v1
      kind: Service
      metadata:
        name: my-app-service
        labels:
          app: my-app
          type: my-type
      spec:
        type: LoadBalancer
        ports:
          - targetPort: 8080
            port: 80
        selector:
          app: my-app
          type: my-type
      
  • Commands are same as the ones above for NodePort service.

You're doing great, Sailor! Captin Ahab would be proud!👨🏼‍✈️
Keep going! We'll look at "Ingress", "Volumes", "Namespaces" and revisit "Clusters" in the next and final part of this series!

Ahead Flank!!!🚀

Top comments (0)