Kubernetes

Kubernetes

What is Kubernetes?

It is a system for running many different containers over multiple different machines.

Get a simple container running on our local Kubernetes cluster.

  1. The image must be hosted on docker hub.

  2. Make a config file to create a container.

  3. Make a config file to set up networking.

Then create a project directory and create a file 'client-pod.yaml'.

apiVersion: v1
kind: Pod
metadata:
  name: client-pod
  labels:
    components: web
spec:
  containers:
    - name: client
      image: dsouzareuben79964/multi-client
      ports:
        - containerPort: 3000

apiVersion: Specifies the version of the Kubernetes API that is used. In this case, it's using the v1 version of the API. Each API version gives access to different set of objects.

kind: Defines the type of Kubernetes resource being created. Here, it's a Pod.

metadata: Contains metadata for the Pod. In this case:

name: The name assigned to the Pod is "client-pod."

spec: Describes the desired state for the Pod. Inside the spec field:

containers: An array specifying the containers to run within the Pod. In this case, there is one container named "client."

  1. name: The name of the container is "client."

  2. image: The Docker image to be used for the container is "dsouzareuben79964/multi-client."

  3. ports: An array specifying the ports to open in the container. In this case, there is one port:

containerPort: The container will listen on port 3000.

Next create another file 'client-node-port.yaml'.

apiVersion: v1
kind: Service
metadata:
  name: client-node-port
spec:
  type: NodePort
  ports:
    - port: 3050
      targetPort: 3000
      nodePort: 31515
  selector:
    component: web

apiVersion: Specifies the version of the Kubernetes API. In this case, it's using the v1 version of the API.

kind: Defines the type of Kubernetes resource being created. Here, it's a Service.

metadata: Contains metadata for the Service. In this case:

name: The name assigned to the Service is "client-node-port."

spec: Describes the desired state for the Service. Inside the spec field:

type: Specifies the type of Service. Here, it's set to "NodePort," which exposes the Service on each Node's IP at a static port.

ports: An array specifying the ports to be exposed by the Service. In this case, there is one port defined:

  1. port: The port on which the Service will be exposed. It is set to 3050.

  2. targetPort: The port to which the Service will forward traffic inside the cluster. It is set to 3000, which corresponds to the containerPort in the Pod.

  3. nodePort: The port on each Node on which the Service is accessible. It is set to 31515.

selector: Specifies the set of Pods to which the Service should route traffic. In this case, the selector is looking for Pods with the label "component: web." This means that the Service will route traffic to Pods that have been labeled with "component: web."

Each object serves a different purpose.

A pod is grouping of containers with common purposes. These containers need to be deployed together. The containers inside a pod have tight integration and must be executed with each other.

Services is used to set up networking services in a Kubernetes cluster

Services are of type:

  1. Ingress

  2. NodePort

  3. ClusterIP

  4. LoadBalancer

Feeding the config file to kubectl.

Print the status of running pods.

If we want to connect to our running containers we cannot simply access them through the localhost. We have to ask miniKube to provide its ip address to connect to the containers.

Imperative VS Declarative deployments

Imperative Deployments

It follows the exact steps to setup the container.

Declarative Deployments

Provide a set of guiding rules to setup the container.

Updating Existing Objects

So when we want to make any modifications in the config file leave the name and kind of the file same. The master checks if it already has a file with same name and kind and updates it. If it does not have it will create a new container.

Declarative Updates

Lets make some changes to client-pod.

Now we check if the Kubernetes cluster runs the updated image.

To get all info of the pod.

DEPLOYMENT

It is an object that maintains a set of identical pods ensuring they have correct config and the right number exists.

Deployments create pods for us. Helpful in the production environment.

(client-deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: client-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: web
  template:
    metadata:
      labels:
        component: web
    spec:
      containers:
        - name: client
          image: stephengrider/multi-client
          ports:
            - containerPort: 3000

Lets increase no. of replicas.

Updating Deployment Images

Lets make some changes in client-deployment.yaml.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: client-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: web
  template:
    metadata:
      labels:
        component: web
    spec:
      containers:
        - name: client
          image: stephengrider/multi-worker
          ports:
            - containerPort: 3000

Triggering Deployment Updates

  1. Manually Delete The Pods

    On deleting the pods manually the master will identify that a pod is missing and rebuild it.

  2. Tag built images with version number

  3. Use an imperative command

    Tag the image with a version number.

    push it to docker hub.

    Then use the kubectl command

Did you find this article valuable?

Support Reuben D'souza by becoming a sponsor. Any amount is appreciated!