๐ (Day 2) Namespace and Pod
๐What is a namespace in Kubernetes?
In Kubernetes, a namespace is a way to organize and isolate resources within a cluster. Think of namespaces as separate folders on a computer. In Kubernetes, itโs a way to divide a cluster into different sections. This helps manage resources and keeps different projects or teams organized. Each namespace acts like its own mini-cluster, allowing teams to work independently without interference.
๐กExample:
You might have a "development" namespace and a "production" namespace. This way, the apps you're developing won't interfere with the live production apps.
Key Points about Namespaces:
๐ Segmentation: Divides cluster resources between multiple users or teams.
๐ Resource Isolation: Each namespace has its own set of resources (pods, services, etc.) and can have separate access controls.
๐ Efficient Organization: Organize resources related to specific environments (development, testing, production) within separate namespaces.
๐ฆ Default Namespace: Kubernetes provides a default namespace,
default
, used when no specific namespace is provided.Predefined Namespaces: Kubernetes comes with some predefined namespaces such as:
default
: The default namespace for resources.kube-system
: For Kubernetes system-related components.kube-public
: Resources visible across all namespaces, typically for public access.
Let's start with some hands-on Kubernetes commands.
๐ ๏ธ Task-1: Understanding Namespace
To list all the available namespaces
kubectl get ns
To create a new namespace:
kubectl create ns <new ns name>
This command lists all namespaces in the Kubernetes cluster.
kubectl get ns
To list all components in the kube-system namespace (for system-related components):
This command lists all the components (like pods, services, deployments, etc.) running specifically in the kube-system namespace.
The kube-system namespace contains critical system components for Kubernetes itself, such as:
CoreDNS: Handles DNS resolution inside the cluster.
Kube-proxy: Manages network routing for services.
Etcd: Stores the cluster's state.
Controller-manager: Manages core Kubernetes functions.
Scheduler: Schedules pods to nodes.
kubectl -n kube-system get all
Detailed view of the kube-system namespace:
kubectl describe ns kube-system
๐ณ Pod in Kubernetes
What is Pod?
Pods are the smallest deployable units in Kubernetes. Think of it like a box that holds one or more containers (like Docker containers). These containers share the same resources (like storage, network) and work together. A pod ensures your application runs smoothly, whether it has one container or multiple containers working together.
๐ ๏ธ Task-2: Pod Creation and Interaction
Create a pod using the NGINX image:
kubectl run pod-1 --image nginx --port 80
Check the newly created Pod:
kubectl get pod
Get more detailed info (wide output) about the pod:
kubectl get pod -o wide
Get detailed information about a specific pod:
kubectl describe pod pod-1
This command is useful for troubleshooting and understanding the detailed state and history of your pod, especially if there are issues like crashes, restarts, or errors. It gives you in-depth insight into how the pod and its containers are functioning
๐ YAML for Pods
Generate a YAML configuration file for a pod without creating it:
Click here for a detailed explanation
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
View the generated YAML:
cat pod.yaml
Apply the YAML file to create the pod:
kubectl apply -f pod.yaml
๐ Exploring Kubernetes Resources and Their Versions
- Listing Available Kubernetes Resources with
kubectl api-resources
The command kubectl api-resources
lists all available resources in your Kubernetes cluster, like Pods, Services, and Deployments. It shows their names, shortcuts, API versions, whether they are namespaced, and their kinds.
kubectl api-resources
- Checking the Version and Details of a Pod
To see the version and details of a Pod, use this command:
kubectl explain pod
kubectl explain ReplicaSet
kubectl explain deployment
๐ ๏ธ Task-3: Pod Creation with YAML
- Open the
vi
editor to create the file:
vi httpd-pod.yaml
- Add the pod configuration inside the file. For example, for an HTTPD server:
Save and exit the vi
editor by pressing ESC
, then typing :wq
.
- you can apply the configuration using:
kubectl apply -f httpd-pod.yaml
- Check the newly created Pod and list the pods in the current namespace:
kubectl get pod
kubectl get pod -o wide
Describe Pod
kubectl describe pod httpd-pod
๐ Managing Pod Labels and Containers
- Check Pod Labels: To view the labels of all Pods, run:
kubectl get pod --show-labels
- Add a Label to a Running Pod: To add a label to a specific Pod:
kubectl label pod <pod-name> kubernetes=k8s
- Check Labels Again: To verify the label has been added:
kubectl get pod <pod-name> --show-labels
- To delete a label from a Pod
In the command kubectl label pod <pod-name> <label-key>-
, the <label-key>
represents the key of the label you want to delete. So if you want to delete a label like kubernetes=k8s
, you would run:
kubectl label pod httpd-pod <label-key>- # <label-key> is kubernetes and k8s is the value
kubectl label pod httpd-pod kubernetes- #kubernetes is the key of label kubernetes=k8s
- Add a Label When Creating a Pod: To create a Pod with a label:
kubectl run <pod-name> --image nginx --labels docker=containerization
- Enter a Single Container Pod: To access a shell in a single-container Pod:
kubectl exec -it <pod-name> -- /bin/bash
exit #To exit the shell
- Edit a Single Container Pod to Create a Multi-Container Pod: To modify an existing Pod using kubectl edit:
The kubectl edit
command allows you to edit live Kubernetes resources, such as pods, directly from your terminal. When you run this command, Kubernetes opens the pod's YAML configuration in an editor (usually vi
or another default editor). You can then modify it, such as adding a second container to the pod, and when you save and exit, Kubernetes will update the pod with the new configuration.
The purpose of using kubectl edit
is to:
Quickly modify a running pod (or any other resource) without needing to delete and recreate it.
Add, remove, or modify containers, environment variables, volumes, etc., directly in the YAML file.
Make live updates to your application configuration with immediate effect.
This method is especially useful for testing and troubleshooting small changes to running pods in Kubernetes.
Kubectl edit po <pod-name>
// Ex. Kubectl edit po httpd-pod
- Enter a Specific Container in a Multi-Container Pod: To access a shell in a specific container using
kubectl exec
:
This command allows you to enter a specific container in a multi-container pod to troubleshoot, run commands, or inspect logs and processes directly inside that container.
kubectl exec -it <pod-name> -c <container-name> -- /bin/bash
Example:
Let's say you have a multi-container pod named webapp-pod
with two containers: nginx-container
and redis-container
. To enter the nginx-container
, use the following command:
kubectl exec -it webapp-pod -c nginx-container -- /bin/bash
This command opens an interactive Bash shell inside the nginx-container
, allowing you to run commands directly within the container.
Clean up resources:
To forcefully remove all Pods in a namespace, you can use the following command:
kubectl delete pod --all --grace-period=0 --force
Subscribe to my newsletter
Read articles from Sandhya Babu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sandhya Babu
Sandhya Babu
๐ Aspiring DevOps Engineer | Cloud & DevOps Enthusiast๐ Hello! Iโm Sandhya Babu, deeply passionate about DevOps and cloud technologies. Currently in my exploring phase, Iโm learning something new every day, from tools like Jenkins, Docker, and Kubernetes to the concepts that drive modern tech infrastructures. I have hands-on experience with several Proof of Concept (POC) projects, where I've applied my skills in real-world scenarios. I love writing blogs about what I've learned and sharing my experiences with others, hoping to inspire and connect with fellow learners. With certifications in Azure DevOps and AWS SAA-C03, Iโm actively seeking opportunities to apply my knowledge, contribute to exciting projects, and continue growing in the tech industry. Letโs connect and explore the world of DevOps together!