Essential Kubernetes Pod Commands and Practical Examples
This article presents a comprehensive list of common Kubernetes pod management commands, detailed case studies for network sharing and shared storage, and an in‑depth explanation of pod YAML fields, helping readers master pod operations and troubleshooting.
1. k8s-pod common management commands
Create Pod:
kubectl apply -f pod.yaml
kubectl run nginx --image=nginxView Pod:
kubectl get pods
kubectl describe pod <PodName>View logs:
kubectl logs <PodName> [-c CONTAINER]
kubectl logs <PodName> [-c CONTAINER] -fEnter container terminal: kubectl exec <PodName> [-c CONTAINER] --bash Delete Pod: kubectl delete <PodName> Export pod YAML configuration:
# Example output of `kubectl get pods` and export to YAML
kubectl get pods -o yaml > web-pod.yaml2. k8s-pod case study
2.1 Implement network sharing
2.1.1 Export configuration file and write case
Write exported web-pod.yaml for testing:
# Sample pod definition
apiVersion: v1
kind: Pod
metadata:
name: pod-net-test
namespace: default
spec:
containers:
- name: pod-test
image: busybox
command: ["/bin/sh"]
args:
- "-c"
- "sleep 3000000"
- name: web
image: nginx2.1.2 Apply configuration file
# kubectl apply -f web-pod.yaml
pod/pod-net-test created2.1.3 Monitor pod startup
# kubectl get pods
# kubectl get pods -w (continuous watch)Note: using -w continuously watches pod status.
2.1.4 Enter pod
# kubectl exec -it pods/pod-net-test -c pod-test -- /bin/sh
Defaulting container name to pod-test.
# ifconfig ... (network info)
# netstat -lntup ... (listening ports)exec: command to enter container
-it: allocate a pseudo‑terminal
pod-net-test: pod name
-c pod-test: specify container name
-- /bin/sh: shell to run
2.1.5 Verify file is served by nginx
Enter nginx container and modify index.html :
# kubectl exec -it pod-net-test -c web -- /bin/bash
cd /usr/share/nginx/html/
ls
echo 'pod-test' > index.htmlExit nginx, enter busybox and wget the page to verify:
# kubectl exec -it pod-net-test -c pod-test -- /bin/sh
wget http://127.0.0.1:80 -O index.html
cat index.htmlSummary:
We verified the nginx service by modifying index.html.
When using kubectl exec, always specify the container with -c.
2.2 Implement shared storage
2.2.1 Export configuration file and write case
Navigate to directory and list files:
# cd /root/yaml/
# llCreate pod-volume-test.yaml defining a shared emptyDir volume:
apiVersion: v1
kind: Pod
metadata:
labels:
app: test
name: pod-volume-test
namespace: default
spec:
containers:
- image: busybox
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 /data2.2.3 Start service
# kubectl apply -f pod-volume-test.yaml
pod/pod-volume-test created2.2.4 Verify service started
# kubectl get pods
# (output shows pod-volume-test READY 2/2)2.2.5 Verify volume sharing
Create index.html in the web container:
# kubectl exec -it pod-volume-test -c web -- /bin/bash
cd /data
touch index.html
echo 'pod volume test' > index.html
lsCheck from the test container that the file is visible:
# kubectl exec -it pod-volume-test -c test -- /bin/sh
cd /data
ls
cat index.html2.2.6 View logs
View web container logs:
# kubectl logs pod-volume-test -c web -f
... startup logs ...
127.0.0.1 - - [29/Nov/2020:03:51:12 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-"Access the service from the test container:
# kubectl exec -it pod-volume-test -c test -- /bin/sh
wget http://127.0.0.1 -O index.html
cat index.html3. k8s-pod field details
# Full YAML pod definition example
apiVersion: v1 # required, version number
kind: Pod # required, Pod
metadata:
name: string # required, pod name
namespace: string # required, namespace
labels: [...] # custom labels
annotations: [...] # custom annotations
spec:
containers:
- name: string
image: string
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
path: string
secret:
scretname: string
items:
- key: string
path: string
configMap:
name: string
items:
- key: string
path: stringSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
