Cloud Native 18 min read

Master Essential k8s Pod Commands and Real-World Scenarios

This guide presents the most frequently used Kubernetes pod management commands, demonstrates practical examples of network sharing and shared storage with complete YAML configurations, walks through verification steps, and explains each field in a pod definition to help operators master pod operations.

Raymond Ops
Raymond Ops
Raymond Ops
Master Essential k8s Pod Commands and Real-World Scenarios

1. k8s-pod common management commands

Create Pod:

kubectl apply -f pod.yaml
kubectl run nginx --image=nginx

View Pods:

kubectl get pods
kubectl describe pod <PodName>

View logs:

kubectl logs <PodName> [-c CONTAINER]
kubectl logs <PodName> [-c CONTAINER] -f

Enter container terminal: kubectl exec <PodName> [-c CONTAINER] --bash Delete Pod: kubectl delete <PodName> Export Pod YAML configuration:

# Example output of kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-s5wvx   1/1     Running   1          40h
...

2. k8s-pod case

2.1 Implement network sharing

Network sharing diagram
Network sharing diagram

2.1.1 Export configuration file and write case

# Write web-pod.yaml for testing
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: pod-test
  name: pod-net-test
  namespace: default
spec:
  containers:
  - image: busybox
    imagePullPolicy: Always
    name: pod-test
    command: ["/bin/sh"]
    args:
      - "-c"
      - "sleep 3000000"
  - image: nginx
    name: web

2.1.2 Apply configuration file

# kubectl apply -f web-pod.yaml
pod/pod-net-test created

2.1.3 Monitor pod startup

# kubectl get pods
NAME                     READY   STATUS            RESTARTS   AGE
nginx-6799fc88d8-s5wvx   1/1     Running           1          41h
pod-net-test             0/2     ContainerCreating 0          19s
...
# kubectl get pods -w   # watch continuously

Note: using -w watches pod status continuously.

2.1.4 Enter pod

# kubectl exec -it pods/pod-net-test -c pod-test -- /bin/sh
Defaulting container name to pod-test.
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 5A:C1:FA:25:85:C0
          inet addr:10.244.169.139  Bcast:10.244.169.139  Mask:255.255.255.255
...
/ # netstat -lntup
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -

exec: command to enter container

-it: allocate a pseudo‑terminal

pod-net-test: pod name

-c pod-test: specify container

-- /bin/sh: shell to use

2.1.5 Verify file inside nginx

# kubectl exec -it pod-net-test -c web -- /bin/bash
root@pod-net-test:/# cd /usr/share/nginx/html/
root@pod-net-test:/usr/share/nginx/html# echo 'pod-test' >index.html
# kubectl exec -it pod-net-test -c pod-test -- /bin/sh
/ # wget http://127.0.0.1:80
... saved 'index.html'
/ # cat index.html
pod-test

Summary:

We modified index.html in the nginx container to verify network sharing.

When entering a container, always use the -c flag to specify which container.

2.2 Implement shared storage

2.2.1 Write pod-volume-test.yaml

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: test
  name: pod-volume-test
  namespace: default
spec:
  containers:
  - image: busybox
    imagePullPolicy: Always
    name: test
    command: ["/bin/sh"]
    args:
      - "-c"
      - "sleep 3000000"
    volumeMounts:
    - name: log
      mountPath: /data
  - image: nginx
    name: web
    volumeMounts:
    - name: log
      mountPath: /data
  volumes:
  - name: log
    emptyDir: {}

2.2.2 Create shared directory

# mkdir -p /data

2.2.3 Start service

# kubectl apply -f pod-volume-test.yaml
pod/pod-volume-test created

2.2.4 Verify service startup

# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-tfgfr   1/1     Running   0          30m
pod-volume-test          2/2     Running   0          2m37s
...

2.2.5 Verify data volume is shared

# kubectl exec -it pod-volume-test -c web -- /bin/bash
root@pod-volume-test:/# cd /data
root@pod-volume-test:/data# echo 'pod volume test' >index.html
root@pod-volume-test:/data# ls
index.html
# kubectl exec -it pod-volume-test -c test -- /bin/sh
/data # cat index.html
pod volume test

2.2.6 View logs

# kubectl logs pod-volume-test -c web -f
/docker-entrypoint.sh: Configuration complete; ready for start up
... (nginx access log) ...

3. k8s-pod field details

# Full pod definition in YAML
apiVersion: v1               # required, e.g., v1
kind: Pod                     # required, Pod
metadata:
  name: string               # required, pod name
  namespace: string          # required, namespace
  labels:                    # custom labels
    - name: string
  annotations:               # custom annotations
    - name: string
spec:
  containers:                # list of containers
    - name: string           # container name
      image: string          # container image
      imagePullPolicy: [Always|Never|IfNotPresent]
      command: [string]
      args: [string]
      workingDir: string
      volumeMounts:
        - name: string
          mountPath: string
          readOnly: boolean
      ports:
        - name: string
          containerPort: int
          hostPort: int
          protocol: string
      env:
        - name: string
          value: string
      resources:
        limits:
          cpu: string
          memory: string
        requests:
          cpu: string
          memory: string
      livenessProbe:
        exec:
          command: [string]
        httpGet:
          path: string
          port: number
          host: string
          scheme: string
          httpHeaders:
            - name: string
              value: string
        tcpSocket:
          port: number
        initialDelaySeconds: 0
        timeoutSeconds: 0
        periodSeconds: 0
        successThreshold: 0
        failureThreshold: 0
      securityContext:
        privileged: false
  restartPolicy: [Always|Never|OnFailure]
  nodeSelector: object
  imagePullSecrets:
    - name: string
  hostNetwork: false
  volumes:
    - name: string
      emptyDir: {}
      hostPath: string
      secret:
        secretName: string
        items:
          - key: string
            path: string
      configMap:
        name: string
        items:
          - key: string
            path: string
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

KubernetesYAMLPodshared-storagekubectl
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.