Day 07/40 Days of K8s: Understanding Kubernetes Pod: 馃惓鉃★笍鈽革笍

Day 07/40 Days of K8s: Understanding Kubernetes Pod: 馃惓鉃★笍鈽革笍

The Need for Pods in Kubernetes? 馃

Containers: Docker vs Kubernetes

  1. Docker Approach 馃惓
  • Docker uses standalone containers, each run independently

  • Here is a docker command to run nginx container:

docker run -it -d --name nginx -p 80:80 nginx
  1. Kubernetes Approach 鈽革笍
  • Introduces the concept of "Pods"

  • A pod is the smallest object in Kubernetes, is an abstraction of containers. Each pod can contain one or more containers (ideally one app container per pod)

  • Brings declarative capability and standardization. The running specification of a container is Pod.

Why Pods? 馃

  1. Abstraction: Pods provide a higher-level abstraction, allowing Kubernetes to manage containerized applications more effectively.

  2. Multi-containers: Can run multiple containers that need to work together closely.

  3. Shared resources: Containers in a pod share resources like network and storage. Two containers talk to each other using local host.

  4. Declarative configuration: Pods are defined in YAML, bringing standardization and declarative capabilities.

That doesn鈥檛 mean pods can鈥檛 be created using commands in k8s. There are two methods to create k8s objects.

Kubernetes Request flow:

Client requests go to the API server on the master, which forwards them to other components for object management.

Pod Creation Methods:

  1. Imperative commands

  2. Declarative way (YAML manifests)

Imperative commands:

kubectl run nginx --image=nginx  #run nginx pod

# The below command creates yaml file automatically from k8s
kubectl run nginx --image=nginx --dry-run=client -o yaml

Yaml manifest(pod.yaml):

# This is an Nginx pod yaml.

apiVersion: v1               # Kubernetes API version
kind: Pod                    # Type of Kubernetes object created
metadata:                    # Information about the Pod
  name: nginx-pod            # Name of the Pod
  labels:                    # Key-value pairs for identifying the Pod
    env: demo
    type: frontend
spec:                        # specification of the Pod
  containers:                # List of containers in the Pod
  - name: nginx-container    # Name of the container
    image: nginx             # nginx image from Dockerhub
    ports:                   # Ports to expose
    - containerPort: 80      # Exposed Container port 80

Common kubectl Commands:

kubectl apply -f nginx.yaml        # applying config of nginx file 

kubectl get pods                   # to view the pods 

kubectl get pods -o wide           # to view the pod detailed view 

kubectl describe pod nginx         # will give more info about the pod 

kubectl logs pod nginx             # To check the logs of the pod

kubectl exec -it -d nginx /bin/sh  # to enter into the container shell 

kubectl edit nginx.yaml            # directly edit the object in the cluster.

We understood the concepts of container creation, comparing Docker and Kubernetes approaches, why k8s uses pod, how to create pods, and ends with essential kubectl commands.

#Containerization #Docker #K8s #Pod #CKASeries #40DaysofKubernetes