Kubernetes----Pod常用操作

网友投稿 305 2022-09-11

Kubernetes----Pod常用操作

一、命令行查看pod

1.1 不指定命名查看pod

通过如下命令,不指定命名空间,查看的是default命名空间的pod,如下,default命名空间中没有pod

[root@master demo]# kubectl get nsNAME STATUS AGEdefault Active 5d23hkube-node-lease Active 5d23hkube-public Active 5d23hkube-system Active 5d23hkubernetes-dashboard Active 5d20h[root@master demo]# kubectl get podNo resources found in default namespace.[root@master demo]#

1.2 指定命名空间查看pod

如下,指定命名空间即查询kube-system命名空间的pod,如下可以看出kube-system命名空间中有许多自带的pod

[root@master demo]# kubectl get nsNAME STATUS AGEdefault Active 5d23hkube-node-lease Active 5d23hkube-public Active 5d23hkube-system Active 5d23hkubernetes-dashboard Active 5d20h[root@master demo]# kubectl get pod -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-558bd4d5db-7vbmq 1/1 Running 0 5d23hcoredns-558bd4d5db-sps22 1/1 Running 0 5d23hetcd-master 1/1 Running 0 5d23hkube-apiserver-master 1/1 Running 0 5d23hkube-controller-manager-master 1/1 Running 0 5d23hkube-flannel-ds-cd9qk 1/1 Running 0 5d23hkube-flannel-ds-gg4jq 1/1 Running 0 5d23hkube-flannel-ds-n76xj 1/1 Running 0 5d23hkube-proxy-g4j5g 1/1 Running 0 5d23hkube-proxy-h27ms 1/1 Running 0 5d23hkube-proxy-tqzjl 1/1 Running 0 5d23hkube-scheduler-master 1/1 Running 0 5d23h[root@master demo]#

二、命令行方式对Pod操作

2.1 命令行创建pod

如下,先创建dev命名空间,然后创建nginx的pod,使用nginx:latest 镜像,设置端口为80

[root@master demo]# kubectl create namespace devnamespace/dev created[root@master demo]# kubectl run nginx --image=nginx:latest --port=80 -n devpod/nginx created[root@master demo]#

2.2 命令行查询验证创建的pod

如下,查看上面创建的pod的信息

[root@master demo]# kubectl get pod -n devNAME READY STATUS RESTARTS AGEnginx 1/1 Running 0 77s[root@master demo]# kubectl get pod -n dev -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx 1/1 Running 0 85s 10.244.2.16 node2 [root@master demo]#

2.3 命令方式删除创建的pod

[root@master demo]# kubectl delete pod nginx -n devpod "nginx" deleted[root@master demo]# kubectl delete namespace devnamespace "dev" deleted[root@master demo]#

三、命令加配置文件方式对Pod操作

3.1 使用配置文件的方式创建pod

创建pod_nginx.yaml文件,内容如下,即创建一个dev的命名空间以及在dev的命名空间中创建一个nginx的pod

apiVersion: v1kind: Namespacemetadata: name: dev---apiVersion: v1kind: Podmetadata: name: nginx namespace: devspec: containers: - image: nginx:1.17.1 name: pod ports: - name: nginx-port containerPort: 80 protocol:

使用如下命令创建

[root@master demo]# kubectl create -f pod_nginx.yamlnamespace/dev createdpod/nginx created[root@master demo]#

3.2 使用配置文件的方式查看

如下,使用配置文件的方式查看创建的资源

[root@master demo]# kubectl get -f pod_nginx.yamlNAME STATUS AGEnamespace/dev Active 73sNAME READY STATUS RESTARTS AGEpod/nginx 1/1 Running 0 73s[root@master demo]#

3.3 使用配置文件的方式删除

如下,使用如下命令将创建的资源删除

[root@master demo]# kubectl apply -f pod_nginx.yamlnamespace/dev unchangedpod/nginx unchanged[root@master demo]#

4.1 使用apply和配置文件方式创建pod

创建pod_nginx.yaml文件,内容如下,即创建一个dev的命名空间以及在dev的命名空间中创建一个nginx的pod

apiVersion: v1kind: Namespacemetadata: name: dev---apiVersion: v1kind: Podmetadata: name: nginx namespace: devspec: containers: - image: nginx:1.17.1 name: pod ports: - name: nginx-port containerPort: 80 protocol:

使用如下命令创建

[root@master demo]# kubectl apply -f pod_nginx.yamlnamespace/dev createdpod/nginx created[root@master demo]#

4.2 使用apply和配置文件的方式对pod进行修改

如下,将pod_nginx.yaml文件中的镜像修改为1.17.3

apiVersion: v1kind: Namespacemetadata: name: dev---apiVersion: v1kind: Podmetadata: name: nginx namespace: devspec: containers: - image: nginx:1.17.3 name: pod ports: - name: nginx-port containerPort: 80 protocol:

然后使用如下命令修改更新

[root@master demo]# kubectl apply -f pod_nginx.yamlnamespace/dev unchangedpod/nginx configured[root@master demo]#

4.3 使用describe命令查看修改状态

如下,使用describe命令查看修改的状态

[root@master demo]# kubectl describe pod nginx -n devName: nginxNamespace: devPriority: 0Node: node2/192.168.16.42Start Time: Sun, 20 Mar 2022 14:43:51 +0800Labels: Annotations: Status: RunningIP: 10.244.2.18IPs: IP: 10.244.2.18Containers: pod: Container ID: docker://9edee9d176b0c6cdda342c161f51ac306211504d897755d7f2a1c874f10530c3 Image: nginx:1.17.3 Image ID: docker-pullable://nginx@sha256:9688d0dae8812dd2437947b756393eb0779487e361aa2ffbc3a529dca61f102c Port: 80/TCP Host Port: 0/TCP State: Running Started: Sun, 20 Mar 2022 14:52:36 +0800 Last State: Terminated Reason: Completed Exit Code: 0 Started: Sun, 20 Mar 2022 14:43:52 +0800 Finished: Sun, 20 Mar 2022 14:52:36 +0800 Ready: True Restart Count: 1 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-rclxs (ro)Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled TrueVolumes: kube-api-access-rclxs: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: DownwardAPI: trueQoS Class: BestEffortNode-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300sEvents: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 9m17s default-scheduler Successfully assigned dev/nginx to node2 Normal Pulled 9m16s kubelet Container image "nginx:1.17.1" already present on machine Normal Created 32s (x2 over 9m16s) kubelet Created container pod Normal Started 32s (x2 over 9m16s) kubelet Started container pod Normal Killing 32s kubelet Container pod definition changed, will be restarted Normal Pulled 32s kubelet Container image "nginx:1.17.3" already present on machine[root@master demo]#

4.4 删除Pod

apply命令不能删除操作,删除Pod还是要使用delete -f,如下:

[root@master demo]# kubectl delete -f pod_nginx.yamlnamespace "dev" deletedpod "nginx" deleted[root@master demo]#

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:DoMarketing-营销智库:范冰冰之后,“真性情”的郑爽已失去人心!
下一篇:Kubernetes----Kubernetes中三种对象管理方式对比
相关文章

 发表评论

暂时没有评论,来抢沙发吧~