Kubernetes Pods and Namespaces

Kubernetes Pods and Namespaces

What is Pod and Why Kuberenetes use Pods?

Pod is a running process in cluster. It contains one or more container such as docker container. In pod containers are managed as single entity and share the resources. You can have different types of containers in a single pod whether it is related to application or database.

image

Kubernetes does not run containers directly instead it run the pod and groups the containers and ensures that each of them use same resources and network. The main purpose of using pods is Replication. When containers are grouped together into pod then with replication Kubernetes can horizontally scale application as needed. In other words if a single pod is workload increases Kubernetes will make the replicas of it and will deploy to the cluster. This ensures that during heavy workload cluster will perform efficiently and also by making replicas it provides failure resistance to the cluster.

Pods in a Kubernetes cluster are used in two main ways :

1. Single container Pod

Single container pod refers to the pod which contains only one container. you can deploy such pod by writing a yaml file. And running the kubectl command

apiVersion: v1
kind: Pod
metadata:
  name: webserver
  namespace: websrvr
  labels:
    app: nginx
    tier: front
    version: v1
    env: production
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

Once the yaml file is created save the file let's give name nginx.yaml and run the create command to run the file.

kubectl create –f nginx.yml

It will create a pod with the name of nginx. We can use the describe command along with kubectl to describe the pod.

2. Multi container Pod

Multi container pod refers to the pod which contains one or more containers. you can deploy such pod by writing a yaml file. And running the kubectl command.

apiVersion: v1
kind: Pod
metadata:
  name: multicontainer-pods
  namespace: multi
  labels:
    app: httpd
    tier: frontend-backend
    version: v1
spec:
  containers:
  - name: web
    image: httpd
    ports:
    - containerPort: 80
  - name: db
    image: mysql
    ports:
    - containerPort: 3306
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: *******

In the above yaml file, we have created one pod with two containers inside it, one for httpd and the other for MySQL. You can execute the yaml file with kubectl create command.

Namespaces

Namespace are the logical separation for different environments with the help of namespace you can use same resources at a time as they are logically separated. you can use any number of namespaces supported in cluster and they have ability to communicate with each other.

Some of the use-case why to use Namespaces :

  • You can separate development, testing, and deployment environment of containerized applications enabling the entire lifecycle to take place on the same cluster.
  • With the namespace you can divide the resources of the cluster between multiple teams.
  • Allowing teams or projects to exist in their own virtual clusters without fear of impacting each other’s work.

There are following initial namespaces in Kubernetes

1. default This is the default namespace for every Kubernetes command and every Kubernetes resource resides in this default namespace until new namespace are created the entire cluster resides in default.

2. Kube-System This namespace is used for Kubernetes components which should be avoided for use.

3. Kube-Public This namespace is automatically created and its for only reserved for cluster usage but it is readable by all users.

Creating Namespace

Here we are creating two namespaces named as NP1 and NP2 like this you can create namespace according to you requirements.

apiVersion: v1
kind: Namespace
metadata:
  name: NP1
apiVersion: v1
kind: Namespace
metadata:
  name: NP2

Deleting Namespace

You can delete namespce by executing below command :

kubectl delete ns NP1

Basic Kubectl Commands

  • This is use to get all the namespace which are created.

    kubectl get ns
    
  • This command is used for get all the resources which are under default namespace.

    kubectl get all -n default
    
  • With this command you will all the components of the cluster.

    kubectl get all -n kube-system
    
  • This is also same command as above but it will display information in detail about the cluster components.

    kubectl get all -n kube-system -o wide
    

Community Asked Questions

  • How many Container can a Pod have?

Ans : We can have total 5000 nodes, Total 150000 Pods not more than that and not more than 300000 total containers.

  • Where is the container storage in the Kubernetes Pods ?

Ans : By Default Kubernetes has temporary storage (non-persistent). Any storage which is a part of a container in the pod is held in the host temporary storage which exists as long as the pod exists.

  • What is difference between Pod and Container ?

Ans : Pod encapsulates different containers and deploy as one while container is a single entity and it is deploy as a single entity. In Pod you can have multiple containers having different OS images whereas in container you can have only one OS image.

  • What is the Purpose of Pods ?

Ans : Basically Pods are designed to support multiple cooperating processes in the form of containers. Which form a cohesive unit of service and share same storage and network on the cluster.

Community and Social Footprints :

Did you find this article valuable?

Support Cloudnloud Tech Community by becoming a sponsor. Any amount is appreciated!