Skip to main content

Kubernetes Pods: Create Pods with Run-Command and YAML Configuration; Single & Multi Container Pods, Port-Forwarding, Find Container on Worker Node

702 words·
Kubernetes Kubectl
Table of Contents
Kubernetes-Components - This article is part of a series.
Part 3: This Article

Example: Create Pod with Run Command
#

# Create a pod with Nginx container
kubectl run example-pod --image=nginx:alpine

# List pods
kubectl get pods

# List pod details
kubectl describe pod example-pod

# List pod details: Save output in YAML format into file
kubectl get pod example-pod -o yaml > example-pod.yaml

# Delete the pod
kubectl delete pod example-pod



Example: Create Pod with YAML Configuration
#

Create Configuration
#

# Create a pod configuration with dry-run
kubectl run nginx --image=nginx --dry-run=client -o yaml > example-pod.yaml
# Adopt the pod configuration and add a port
vi example-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    ports: # Add the port
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
# Create the pod
kubectl create -f example-pod.yaml

Port-Forwarding
#

# Create a port-forwarding
kubectl port-forward pod/nginx 8080:80


# Curl the Nginx container (in a new shell)
curl localhost:8080

# Shell output:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>



Example: Multi Container Pod
#

Overview
#

  • The following Pod configuration deploys a Nginx and an Alpine container within the same pod.

  • The Nginx container serves the default content on port “80” and the Alpine container is configured to curl the Nginx service at localhost port “80” every three seconds.

  • Containers within the same pod share the same network space and can communicate via localhost, which refers to other containers in the same pod.

  • All containers in the pod share the same IP address and each must ensure they use different port numbers if they serve network traffic.


Create Pod
#

# Create a configuration for the pod
vi nginx-alpine-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-alpine-pod
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80
  - name: alpine-container
    image: alpine
    command: ["/bin/ash", "-c"]
    args:
    - apk add --no-cache curl &&
      while true; do
        curl http://localhost:80/;
        sleep 3;
      done
  • "/bin/ash", "-c" Path to ash shell, the “-c” option tells the shell to take the next string as a command to execute.
# Create the pod
kubectl create -f nginx-alpine-pod.yaml

Pod Status & Details
#

List Pods & Pod Details
#

# Check pod status
kubectl get pods

# Shell output: Wait till both containers a ready
NAME                                    READY   STATUS    RESTARTS      AGE
nginx-alpine-pod                        2/2     Running   0             11s
# List pod details
kubectl describe pod nginx-alpine-pod

# Shell output:
...
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  26s   default-scheduler  Successfully assigned default/nginx-alpine-pod to ubuntu3
  Normal  Pulling    26s   kubelet            Pulling image "nginx"
  Normal  Pulled     25s   kubelet            Successfully pulled image "nginx" in 979ms (979ms including waiting). Image size: 72955450 bytes.
  Normal  Created    25s   kubelet            Created container nginx-container
  Normal  Started    25s   kubelet            Started container nginx-container
  Normal  Pulling    25s   kubelet            Pulling image "alpine"
  Normal  Pulled     23s   kubelet            Successfully pulled image "alpine" in 2.543s (2.543s including waiting). Image size: 3634744 bytes.
  Normal  Created    23s   kubelet            Created container alpine-container
  Normal  Started    23s   kubelet            Started container alpine-container

Alpine Container Logs
#

In the logs of the Alpine container, the curled output of the Nginx service is visible:

# Check the logs of the "alpine-container" in the "nginx-alpine-pod"
kubectl logs nginx-alpine-pod -c alpine-container --tail=30

# Shell output:
...
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>

Find Container on Kubernetes Node
#

Find Worker Node
#

# List on which node the pods runs
kubectl get pods -o wide

# Shell output:
NAME               READY   STATUS    RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
nginx-alpine-pod   2/2     Running   0          6m21s   10.233.74.71   node4   <none>           <none>

Find Container Runtime
#

# Use the following command to find the container runtime
kubectl describe node node4

# Shell output:
...
Container Runtime Version:  containerd://1.7.16

List Containers on Node
#

Log in to the Kubernetes node where the pod runs, to check the container status:

# List all running containers managed by containerd within the Kubernetes namespace (k8s.io)
sudo ctr -n k8s.io containers list

# Shell output:
CONTAINER                                                           IMAGE                                               RUNTIME
...
0a68e2f17ff0a92ed5d7ee14d4041362ab62851377a34a153c0a04c4e047a772    docker.io/library/alpine:latest                     io.containerd.runc.v2
1ea8b91d268c81819ccfd2a8d41936d53f545f2a8933a6de28e9c34a133c391d    docker.io/library/nginx:1.25.2-alpine               io.containerd.runc.v2
# List detailed information about a specific container
sudo ctr -n k8s.io containers info container-id
sudo ctr -n k8s.io containers info 0a68e2f17ff0a92ed5d7ee14d4041362ab62851377a34a153c0a04c4e047a772

Delete the Pod
#

# Delete the pod
kubectl delete pod nginx-alpine-pod
Kubernetes-Components - This article is part of a series.
Part 3: This Article