While My previous Tutorial “Kubernetes: Configuring Kubernetes Cluster” Provides detailed Information about how to set up the Kubernetes cluster, In this article, we are going to discuss Pods, Namespaces, Labels, and Scaling Up / Down your Deployments.
Let’s discuss each topic one by one in detail:
Pods are the smallest, most basic deployable objects in Kubernetes. A Pod represents a single instance of a running process in your cluster. Pods contain one or more containers, such as Docker containers. When a Pod runs multiple containers, the containers are managed as a single entity and share the Pod’s resources.
Namespaces are a way to organize clusters into virtual sub-clusters — they can be helpful when different teams or projects share a Kubernetes cluster. Any number of namespaces are supported within a cluster, each logically separated from others but with the ability to communicate with each other. Namespaces cannot be nested within each other.
Namespaces are intended for use in environments with many users spread across multiple teams, or projects. For clusters with a few to tens of users, you should not need to create or think about namespaces at all. Start using namespaces when you need the features they provide.
Labels are key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users but do not directly imply semantics to the core system. Labels can be used to organize and to select subsets of objects. Labels can be attached to objects at creation time and subsequently added and modified at any time
Now Let’s understand each of them practically:
For our practice, we will be using the same setup we have created in my previous article titled “Kubernetes: Configuring Kubernetes Cluster”.
Basically, we will be using 3 machines for our lab: 1 Kubernetes Master Node and 2 Worker Nodes.
The servers have IP and FQDN details as below. We have already Configured the Kubernetes cluster and have 2 worker nodes.
- Running “kubectl get nodes”will show the cluster status. It will show us the Node details, their status, roles, age, and Version.
2. Running “kubectl get pods” command shows no pods are currently running.
3. The “kubectl get pods — all-namespaces”commandwill show the namespaces at present. “Kube-System” namespace is created automatically when you configure Kubernetes Cluster and is being used by Kubernetes Cluster.
4. Now let’s create a pod by using Nginx image. Run the below command for the same.
kubectl create deployment nginx — image=nginx
5. The “kubectl get pods” command will show that the Nginx pod is up and running.
6. The “kubectl get pods — all-namespaces” command will show that the newly created Nginx pod is using the “default” namespace.
7. Now let’s create our custom namespaces for “Dev” and “Test” Environments. We can see below that our namespaces are created.
8. The following command — “kubectl get namespaces” will display all the namespaces in the cluster.
9. Now let’s Create a “httpd” webserver pod in “Dev” environment. Issue below command for this:
kubectl create deployment httpd-dev — image=httpd -n dev
10. Below command show that a new pod has been created in the dev namespace:
11. Now if we want to see all the Pods running for the dev environment, run the below command:
kubectl get pods -n dev
12. Now what if you want to delete a pod? The below command will do the same.
kubectl delete pod -n <Namespace Name> <Name of the Pod>
In our case:
kubectl delete pod -n dev httpd-dev-6b849c6b54-z6smg
Note: When you delete a pod, it will be recreated automatically by Kubernetes Cluster.
13. Running “kubectl get pods -n dev” will show that a new Pod has been created in the “dev” namespace.
14. Now what if you want to check the labels of our Pod? Use the below command instead:
kubectl get pods httpd-dev-6b849c6b54-srw85 -n dev — show-labels
Note: To see the “Label” of our Pods in “default” Namespace, we can achieve by below( No need to mention the Namespace name):
You can also notice that, “kubectl get pods” command will only show the pods running in the “default” namespace and will never show the pods running in other namespaces.
15. Now what if we want to add a label to our “Nginx” pod? Use the below command:
kubectl label pod nginx-6799fc88d8-w22zw version=v1
In the above example, we can see that it has added a label called “version=v1”
Scaling Up / Scaling Down our Deployment
We can see that currently, there is only one nginx pod running in default Namespace.
kubectl get pods — all-namespaces
Now, what if we want to scale it up to 3 pods. running below command will do the same:
kubectl scale deployment nginx — replicas=3
We can see that 3 replicas have been created for Nginx.
Now, what if we want to scale down our Nginx deployment to 2 pods? The below command will do the same.
kubectl scale deployment nginx — replicas=2
The below command shows that the “nginx” deployment has been scalled down to 2 pods.
That Concludes our Tutorial on Working with Pods, Namespaces, Labels, and Scaling Up / Down your Deployments.