Goglides Dev 🌱

Cover image for Hierarchy in Kubernetes?
Rahul Gautam
Rahul Gautam

Posted on • Updated on

Hierarchy in Kubernetes?

We had talked about Pods in our previous blog. We created a pod using an image from Dockerhub that we uploaded. Now, we have a challenge. What if we had to make multiple numbers pods? Will it be easy to create n number of pods manually? Or is there an object that helps us create multiple Pods in an instance?

Today, in this blog, we will be looking at different hierarchies in k8s and try to answer all our questions.

Pods

While deploying an application, Kubernetes does not directly deploy containers on the worker nodes. Those containers are first encapsulated into objects known as Pods. Hence, Pods are the smallest object that contains a single instance of an application in Kubernetes.

I have written a blog about Pods so I will be skipping this part.

Now, we have created a pod. Now, if someone asks you to make at least four pods with the same service, how will you do it?

The simple answer is

ReplicaSet

ReplicaSet is a Kubernetes object which maintains duplicate pods whenever users want their Pod to be available. It replicates pods and creates identical pods.

Creating our first ReplicaSet

First, Let's check if we have any replica set created at the start.

kubectl get replicaset
Enter fullscreen mode Exit fullscreen mode

Image description

Hmm, no replica sets. Let's create one.

1) Let's create a yaml file for the replica set.

We are going to name it pyapirs.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: blogrs-example
  labels:
    app: blogapp
    type: api

spec:
  template:
    metadata:
      name: blogapi-example
      labels:
        app: blogapp
        type: api

    spec:
      containers: 
      - name: blogapi-py
        image: gamerited/docker-pythonapi

  replicas: 3
  selector:
    matchLabels:
      type: api

Enter fullscreen mode Exit fullscreen mode

Here, we use apiVersion: apps/v1 for replica sets. If you wonder how replica sets are created. I got a small representation of it :)

Image description

2) Let's apply our file for this; we have to use the following command

kubectl apply -f pyapirs.yaml
Enter fullscreen mode Exit fullscreen mode

Image description

Now,

3) Let's Check our replica set using the following command

kubectl get replicasets
Enter fullscreen mode Exit fullscreen mode

Image description

Here, Desired is the number of pods we wanted to create. So, let's check whether we have multiple pods or not.

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description

Nice! Multiple pods are created.

Replicas

Now, let's edit our yaml file and apply it again.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: blogrs-example
  labels:
    app: blogapp
    type: api

spec:
  template:
    metadata:
      name: blogapi-example
      labels:
        app: blogapp
        type: api

    spec:
      containers: 
      - name: blogapi-py
        image: gamerited/docker-pythonapi

  replicas: 4
  selector:
    matchLabels:
      type: api

Enter fullscreen mode Exit fullscreen mode

Now let's apply it again with the same command we used.

Image description

It says, Our replica set is configured, now lets see the stats

Image description

Also, let's check our number of Pods
Image description

You can see a new pod has been created.

Now, What if we wanted to update our pod? Is there anything in higher authority than replica sets? There sure is. Its called

Deployments

Deployments are the highest hierarchy in Kubernetes. Deployments ensure that your application is running and available for everyone. It provides an update for Pods and ReplicaSets. It also automatically replace instances when needed.

Creating our first Deployment

Just like ReplicaSet, let's check if any deployment is running in our cluster or not.

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

Image description

It seems like none is running, so let's create our first deployment

1) We need to create a YAML file so we can deploy our application.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deploy
  labels:
    tier: api
    app: blogapi

spec:
  selector:
    matchLabels:
      app: blogapi
  replicas: 4
  template:
    metadata:
      name: api-deploy
      labels:
        app: blogapi
    spec:
      containers:
        - name: blogapi-py
          image: gamerited/docker-pythonapi
Enter fullscreen mode Exit fullscreen mode

Here the template is similar, but the difference can be seen in the kind of the file, i.e., Deployment

2) Let's apply our yaml file using the command

kubectl apply -f pyapideploy.yaml
Enter fullscreen mode Exit fullscreen mode

Image description

Now, Once you look for deployments,

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

Image description

now,

Lets look at our replicasets

Image description

Here, We can see our replica sets are created. Now, let's check our pods

Image description

Congratulations! You now can create a Pod, Replicaset, and Deployment (independently). Next blog, we will discuss in detail about Deployments.

Top comments (0)