1. 首页 > Linux教程 > 正文

Linux教程FG434-Kubernetes Pod管理

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

风哥提示:

本文档介绍Kubernetes Pod的创建和管理方法。

Part01-Pod创建

1.1 使用YAML创建Pod

# 创建Pod YAML文件
[root@k8s-master ~]# cat > fgedu-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: fgedu-web labels: app: fgedu-web env: production spec: containers: - name: nginx image: nginx:1.25 ports: - containerPort: 80 resources: requests: cpu: 100m memory: 128Mi limits: cpu: 500m memory: 512Mi livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 10 periodSeconds: 10 readinessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 5 volumeMounts: - name: html mountPath: /usr/share/nginx/html volumes: - name: html emptyDir: {} EOF # 创建Pod [root@k8s-master ~]# kubectl apply -f fgedu-pod.yaml pod/fgedu-web created # 查看Pod状态 [root@k8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE fgedu-web 1/1 Running 0 30s # 查看Pod详情 [root@k8s-master ~]# kubectl describe pod fgedu-web Name: fgedu-web Namespace: default Priority: 0 Service Account: default Node: k8s-node1/192.168.1.101 Start Time: Sat, 04 Apr 2026 08:30:00 +0800 Labels: app=fgedu-web env=production Annotations: cni.projectcalico.org/containerID: abc123def456789012345678901234567890123456789012345678901234 cni.projectcalico.org/podIP: 10.244.1.10/32 cni.projectcalico.org/podIPs: 10.244.1.10/32 Status: Running IP: 10.244.1.10 IPs: IP: 10.244.1.10 Containers: nginx: Container ID: containerd://abc123def456789012345678901234567890123456789012345678901234 Image: nginx:1.25 Image ID: docker.io/library/nginx@sha256:abc123def456789012345678901234567890123456789012345678901234 Port: 80/TCP Host Port: 0/TCP State: Running 学习交流加群风哥QQ113257174 Started: Sat, 04 Apr 2026 08:30:00 +0800 Ready: True Restart Count: 0 Limits: cpu: 500m memory: 512Mi Requests: cpu: 100m memory: 128Mi Liveness: http-get http://:80/ delay=10s timeout=1s period=10s #success=1 #failure=3 Readiness: http-get http://:80/ delay=5s timeout=1s period=5s #success=1 #failure=3 Environment:
Mounts:
/usr/share/nginx/html from html (rw)
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-abc12 (ro)
Volumes:
html:
Type: EmptyDir (a temporary directory that shares a pod’s lifetime)
Medium:
SizeLimit:
kube-api-access-abc12:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional:
DownwardAPI: true
QoS Class: Burstable
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
—- —— —- —- ——-
Normal Scheduled 60s default-scheduler Successfully assigned default/fgedu-web to k8s-node1
Normal Pulling 59s kubelet Pulling image “nginx:1.25”
Normal Pulled 30s kubelet Successfully pulled image “nginx:1.25” in 29.123456789s
Normal Created 30s kubelet Created container nginx
Normal Started 30s kubelet Started container nginx

Part02-Pod操作

2.1 Pod管理命令

# 查看Pod日志
[root@k8s-master ~]# kubectl logs fgedu-web
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

# 实时查看日志
[root@k8s-master ~]# kubectl logs -f fgedu-web

# 进入Pod执行命令
[root@k8s-master ~]# kubectl exec -it fgedu-web — bash
root@fgedu-web:/# hostname
fgedu-web
root@fgedu-web:/# cat /etc/os-release
PRETTY_NAME=”Debian GNU/Linux 12 (bookworm)”
NAME=”Debian GNU/Linux”
VERSION_ID=”12″
VERSION=”12 (bookworm)”
root@fgedu-web:/# exit
exit

# 在Pod中执行命令
[root@k8s-master ~]# kubectl exec fgedu-web — nginx -v
nginx version: nginx/1.25.4

# 查看Pod资源使用
[root@k8s-master ~]# kubectl top pod fgedu-web
NAME CPU(cores) MEMORY(bytes)
fgedu-web 1m 5Mi

# 删除Pod
[root@k8s-master ~]# kubectl delete pod fgedu-web
pod “fgedu-web” deleted

# 强制删除Pod
[root@k8s-master ~]# kubectl delete pod fgedu-web –force –grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod “fgedu-web” force deleted

# 使用命令行创建Pod
[root@k8s-master ~]# kubectl run fgedu-web –image=nginx:1.25 –port=80
pod/fgedu-web created

# 查看Pod YAML
[root@k8s-master ~]# kubectl get pod fgedu-web -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: “2026-04-04T00:30:00Z”
labels:
run: fgedu-web
name: fgedu-web
namespace: default
resourceVersion: “123456”
uid: abc123-def456-7890-1234-567890123456
spec:
containers:
– image: nginx:1.25
imagePullPolicy: IfNotPresent
name: fgedu-web
ports:
– containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
– mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-abc12
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: k8s-node1
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
– effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
– effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
– name: kube-api-access-abc12
projected:
defaultMode: 420
sources:
– serviceAccountToken:
expirationSeconds: 3607
path: token
– configMap:
items:
– key: ca.crt
path: ca.crt
name: kube-root-ca.crt
– downwardAPI:
items:
– fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
conditions:
– lastProbeTime: null
lastTransitionTime: “2026-04-04T00:30:00Z”
status: “True”
type: Initialized
– lastProbeTime: null
lastTransitionTime: “2026-04-04T00:30:30Z”
status: “True”
type: Ready
– lastProbeTime: null
lastTransitionTime: “2026-04-04T00:30:30Z”
status: “True”
type: ContainersReady
– lastProbeTime: null
lastTransitionTime: “2026-04-04T00:30:00Z”
status: “True”
type: PodScheduled
containerStatuses:
– containerID: containerd://abc123def456789012345678901234567890123456789012345678901234
image: docker.io/library/nginx:1.25
imageID: docker.io/library/nginx@sha256:abc123def456789012345678901234567890123456789012345678901234
lastState: {}
name: fgedu-web
ready: true
restartCount: 0
started: true
state:
running:
startedAt: “2026-04-04T00:30:00Z”
hostIP: 192.更多视频教程www.fgedu.net.cn168.1.101
phase: Running
podIP: 10.244.1.10
podIPs:
– ip: 10.244.1.10
qosClass: BestEffort
startTime: “2026-04-04T00:30:00Z”

风哥针对Pod管理建议:

  • 使用YAML文件管理Pod
  • 配置资源请求和限制
  • 设置健康检查探针
  • 使用标签组织Pod
  • 配置适当的重启策略

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息