Kubernetes Workloads Resources

Kubernetes Workloads Resources

#Day3 - #KubeWeek challenge

A workload is a component that runs inside a set of pods. A Pod, in itself, is a set of containers. Pods are created and destroyed as needed to manage the traffic by using controllers.

Controllers monitor the state of Kubernetes resources and are responsible for keeping them in the desired state. If the current state is different from the desired state, the controller would make the necessary requests to change the state.

Workload resources configure the controller to ensure correct pods are running and match the desired state. So, we do not manage each Pod individually but use “workload resources” to manage a set of pods. The workload resources configure the controllers to ensure the desired state is maintained.

There are many types of workload resources available in Kubernetes. It is described in a yaml file that also specifies the kind of workload resource it is. You can run any of the yaml files describing the workload resource with a kubectl apply command:

kubectl apply -f <yaml file>

Let’s have a brief look at the most important workload resources in Kubernetes and how they are defined within a yaml file.

▶ReplicaSet

ReplicaSet is used to ensure the continuous availability of Pods. It specifies the number of replicas of the Pods that should exist at all times. Here is how a ReplicaSet is defined in a yaml.

Example:

\>> Creating ReplicaSet using a manifest file (yaml)

$ vi ReplicaSet.yaml
apiVersion: apps/v1
kind: ReplicaSet  
metadata:
  name: replica-set
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: app-container
        image: nginx

Let's create(apply)

$ kubectl apply -f ReplicaSet.yaml

replicaset.apps/replica-set created

\>> View ReplicaSet

Similar to namespace and pods, we can get all the replicaset with command:

$ kubectl get replicaset

or

$ kubectl get rs

In the above manifest file, we have mentioned replicas:3 so that it runs 3 instances of Pods with the label app:backend.

$ kubectl get pods --show-labels

▶Scaling application

An application can be scaled up and down depending on the situation in two ways.

Method 1: By updating the value replicas in the configuration file(yaml) and applying the changes.

Let's update the value of replicas to 5 in the configuration file.

Apply the above changes:

$ kubectl apply -f ReplicaSet.yaml

replicaset.apps/replica-set configured

Similarly, we can scale down by assigning the lower value to the replicas in the configuration file(yaml).

Method 2: By using CLI

We can scale up or down our application by using kubectl command. Let's scale down the application with CLI kubectl scale --replicas=2 replicaset <replicaset-name>.

$ kubectl scale --replicas=2 replicaset replica-set

Let's check the number of pods again:

\>> Edit ReplicaSet

We can edit replicaset with command:

$ kubectl edit replicaset <replicaset-name>

\>> Delete ReplicaSet

We can delete replicaset with command:

$ kubectl delete replicaset <replicaset-name>

▶Deployment

Deployment is a Kubernetes object that is used to manage, organize, and combine pods. When deployment is created, then it creates replicaset. Replicaset creates pods according to the number specified in the replicas option.

Deployments are used to scale our application by increasing the number of running pods or updating the running application.

\>> Create a deployment object

We can create deployment objects in two ways:

Method 1: Using manifest file (yaml)

$ vi deployment.yaml

Let's apply the above file to create deployment:

$ kubectl apply -f deployment.yaml

  deployment.apps/portfolio-deployment created

or

$ kubectl create -f deployment.yaml

  deployment.apps/portfolio-deployment created

To view deployments in the Kubernetes cluster:

$ kubectl get deployment

The above output shows that 3 instances of pods are deployed.
Let's check those Pods,

$ kubectl get pods --show-labels

Pods are associated with deployment and replicaset with the help of labels.

\>> Delete deployment

We can delete deployment as follows:

$ kubectl delete deployment/portfolio-deployment-cli

  deployment.apps "portfolio-deployment-cli" deleted

Let's confirm:

$ kubectl get deployments

▶ReplicaSet Vs Deployment

ReplicaSet ensures the number of running pods in the cluster. Pods are the replicas and the mechanism of specifying the number of running pods in the cluster.

Deployment is a higher-level abstraction that manages one or more ReplicaSet to provide controlled rollout of a new version.

\>> Deployment resources make it easier for updating pods to new version.

\>> Deployment doesn't directly interact with Pods but it does rolling out update using ReplicaSets.

There are other types of Workloads:

This was a nutshell of Kubernetes and their Workloads.