Kubernetes之Pod常用操作

Posted 爱上口袋的天空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kubernetes之Pod常用操作相关的知识,希望对你有一定的参考价值。

一、命令行查看pod

1、不指定命名查看pod

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

[root@master demo]# kubectl get ns
NAME                   STATUS   AGE
default                Active   5d23h
kube-node-lease        Active   5d23h
kube-public            Active   5d23h
kube-system            Active   5d23h
kubernetes-dashboard   Active   5d20h
[root@master demo]# kubectl get pod
No resources found in default namespace.
[root@master demo]#

2、指定命名空间查看pod

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

[root@master demo]# kubectl get ns
NAME                   STATUS   AGE
default                Active   5d23h
kube-node-lease        Active   5d23h
kube-public            Active   5d23h
kube-system            Active   5d23h
kubernetes-dashboard   Active   5d20h
[root@master demo]# kubectl get pod -n kube-system
NAME                             READY   STATUS    RESTARTS   AGE
coredns-558bd4d5db-7vbmq         1/1     Running   0          5d23h
coredns-558bd4d5db-sps22         1/1     Running   0          5d23h
etcd-master                      1/1     Running   0          5d23h
kube-apiserver-master            1/1     Running   0          5d23h
kube-controller-manager-master   1/1     Running   0          5d23h
kube-flannel-ds-cd9qk            1/1     Running   0          5d23h
kube-flannel-ds-gg4jq            1/1     Running   0          5d23h
kube-flannel-ds-n76xj            1/1     Running   0          5d23h
kube-proxy-g4j5g                 1/1     Running   0          5d23h
kube-proxy-h27ms                 1/1     Running   0          5d23h
kube-proxy-tqzjl                 1/1     Running   0          5d23h
kube-scheduler-master            1/1     Running   0          5d23h
[root@master demo]#

二、命令行方式对Pod操作

1、命令行创建pod

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

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

2、命令行查询验证创建的pod

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

[root@master demo]# kubectl get pod -n dev
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          77s
[root@master demo]# kubectl get pod -n dev -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          85s   10.244.2.16   node2   <none>           <none>
[root@master demo]#

3、命令方式删除创建的pod

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

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

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

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

apiVersion: v1
kind: Namespace
metadata:
  name: dev

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
spec:
  containers:
  - image: nginx:1.17.1
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP

使用如下命令创建

[root@master demo]# kubectl create -f pod_nginx.yaml
namespace/dev created
pod/nginx created
[root@master demo]#

2、使用配置文件的方式查看

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

[root@master demo]# kubectl get -f pod_nginx.yaml
NAME            STATUS   AGE
namespace/dev   Active   73s

NAME        READY   STATUS    RESTARTS   AGE
pod/nginx   1/1     Running   0          73s
[root@master demo]#

3、使用配置文件的方式删除

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

[root@master demo]# kubectl delete -f pod_nginx.yaml
namespace/dev unchanged
pod/nginx unchanged
[root@master demo]#

四、 声明式对象配置的方式操作Pod

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

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

apiVersion: v1
kind: Namespace
metadata:
  name: dev

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
spec:
  containers:
  - image: nginx:1.17.1
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP

使用如下命令创建

[root@master demo]# kubectl apply -f pod_nginx.yaml
namespace/dev created
pod/nginx created
[root@master demo]#

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

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

apiVersion: v1
kind: Namespace
metadata:
  name: dev

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
spec:
  containers:
  - image: nginx:1.17.3
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP

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

[root@master demo]# kubectl apply -f pod_nginx.yaml
namespace/dev unchanged
pod/nginx configured
[root@master demo]#

3、使用describe命令查看修改状态

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

[root@master demo]# kubectl describe pod nginx -n dev
Name:         nginx
Namespace:    dev
Priority:     0
Node:         node2/192.168.16.42
Start Time:   Sun, 20 Mar 2022 14:43:51 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.2.18
IPs:
  IP:  10.244.2.18
Containers:
  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:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-rclxs (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-rclxs:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
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  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、删除Pod

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

[root@master demo]# kubectl delete -f pod_nginx.yaml
namespace "dev" deleted
pod "nginx" deleted
[root@master demo]#
创作挑战赛 新人创作奖励来咯,坚持创作打卡瓜分现金大奖

以上是关于Kubernetes之Pod常用操作的主要内容,如果未能解决你的问题,请参考以下文章

kubernetes之kubectl命令用法总结

Kubernetes之资源清单定义

云计算时代操作系统Kubernetes之存储(中)

Kubernetes之标签与Pod控制器详解

Kubernetes之DaemonSet

Kubernetes对象之Pod详解(附安装部署方法)