Bite-Sized Kubernetes Part 5 - Services
Tags: kubernetes, devops, containers
Published: 2023-03-08 19:57:37

## Heave Ho!☸️

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

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

## 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`:
        ```yaml
        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:
        ```bash
        $ kubectl create -f <file name>.yml
        ```
  - See list of services:
        ```bash
        $ kubectl get services
        ```
  - See detailed info about a service:
        ```bash
        $ kubectl describe service <service name>
        ```
  - To access the service from outside the cluster, use the node IP address and the node port:
        ```bash
        $ curl <node IP address>:<node port>
        ```
  - To access the service from inside the cluster, use the cluster IP address and the port:
        ```bash
        $ 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`:
        ```yaml
        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`:
        ```yaml
        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!!!**🚀