k8s 编写pod yaml 文件 启动pod 查看pod详细信息 查看pod日志 连接pod容器 删除pod
#1 创建pod yaml 文件
# 使用帮助命令
[root@master01 pod]# kubectl explain pod.spec
[root@master01 pod]# cat pod-self.yaml
apiVersion: v1 kind: Pod metadata: name: pod-self namespace: default labels: app: my-self dev: pro spec: restartPolicy: Always containers: - name: my-self-tomcat image: tomcat:8.5-jre8-alpine imagePullPolicy: IfNotPresent ports: - containerPort: 8080 - name: my-slef-nginx image: nginx:1.16.0 imagePullPolicy: IfNotPresent ports: - containerPort: 80
#2 启动pod
[root@master01 pod]# kubectl apply -f pod-self.yaml
pod/pod-self unchanged
#3 查看pod
[root@master01 pod]# kubectl get pod -o wide | grep pod-self
pod-self 2/2 Running 0 3m33s 10.244.196.181 node01 <none> <none>
[root@master01 pod]# kubectl get pods -l app=my-self -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-self 2/2 Running 0 6m 10.244.196.181 node01 <none> <none>
#4 查看pod 详细信息
[root@master01 pod]# kubectl describe pods pod-self
Name: pod-self
Namespace: default
Priority: 0
Node: node01/192.168.10.203
Start Time: Sun, 12 Mar 2023 13:04:58 +0800
Labels: app=my-self
dev=pro
Annotations: cni.projectcalico.org/podIP: 10.244.196.181/32
cni.projectcalico.org/podIPs: 10.244.196.181/32
Status: Running
IP: 10.244.196.181
$5 查看指定pod里面容器的 日志
[root@master01 pod]# kubectl logs pod-self -c my-self-tomcat
12-Mar-2023 05:05:18.566 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.41
12-Mar-2023 05:05:18.655 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: May 4 2019 09:17:16 UTC
12-Mar-2023 05:05:18.656 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.41.0
#6 连接指定pod的容器
[root@master01 pod]# kubectl exec -it pod-self -c my-self-tomcat bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-4.4# exit
[root@master01 pod]# kubectl exec -it pod-self -c my-self-tomcat -- bash
bash-4.4#
#7 删除pod
##1 指定pod 的名字
[root@master01 pod]# kubectl delete pods pod-self
pod "pod-self" deleted
[root@master01 pod]# kubectl get pod | grep pod-self
##2 指定Pod 的yaml 文件
[root@master01 pod]# kubectl apply -f pod-self.yaml
pod/pod-self created
[root@master01 pod]# kubectl delete -f pod-self.yaml
pod "pod-self" deleted
[root@master01 pod]# kubectl get pod | grep pod-self
# 强制删除pod
[root@xuegod63 ~]# kubectl delete pod nginx --force --grace-period=0
# 创建podbu 运行,输入到yaml 模板
[root@master01 ~]# kubectl run nginx --image=nginx --image-pullpolicy='IfNotPresent'
--port=80 -o yaml --dry-run=client > 1.yam