赞
踩
在前面的文章中介绍了Pod的使用方法,示例中的Pod包含一个容器,这篇文章介绍一下包含多个容器的Pod的使用方法。
[root@host131 Pod]# cat multi-pods.yaml --- apiVersion: v1 kind: Pod metadata: name: multi-pods spec: containers: - name: blue-pod-container image: busybox:1.31.1 command: ["sleep"] args: ["1000"] - name: green-pod-container image: busybox:1.30.1 command: ["sleep"] args: ["10000"] - name: yellow-pod-container image: busybox:1.30.1 command: ["sleep"] args: ["100000"] ... [root@host131 Pod]#
[root@host131 Pod]# kubectl create -f multi-pods.yaml
pod/multi-pods created
[root@host131 Pod]#
使用kubectl get pods命令可以获取此pod的详细信息,可以看到READY显示为3/3,说明包含三个容器全部正常启动,整体状态STATUS为Running状态,Pod本身的IP为10.254.176.3,Pod名称为multi-pods。
[root@host131 Pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
multi-pods 3/3 Running 0 27s 10.254.176.3 192.168.163.131 <none> <none>
[root@host131 Pod]#
然后可以使用kubectl describe查看pod的详细信息:
[root@host131 Pod]# kubectl describe pod/multi-pods Name: multi-pods Namespace: default Priority: 0 Node: 192.168.163.131/192.168.163.131 Start Time: Sun, 09 Feb 2020 04:34:18 -0500 Labels: <none> Annotations: <none> Status: Running IP: 10.254.176.3 IPs: IP: 10.254.176.3 Containers: blue-pod-container: Container ID: docker://881da8a39e6a9999cce25ef227c97e8680379d97cd190e6a98ff25f49a3f7469 Image: busybox:1.31.1 Image ID: docker-pullable://busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a Port: <none> Host Port: <none> Command: sleep Args: 1000 State: Running Started: Sun, 09 Feb 2020 04:34:19 -0500 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-kzxwt (ro) green-pod-container: Container ID: docker://b60d1cf06ff68ffae44e13f33d84b12b8525707af6de1836abdc5905a135166f Image: busybox:1.30.1 Image ID: docker-pullable://busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d Port: <none> Host Port: <none> Command: sleep Args: 10000 State: Running Started: Sun, 09 Feb 2020 04:34:19 -0500 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-kzxwt (ro) yellow-pod-container: Container ID: docker://8e500fef966ab861843d46076bdb07fa5443041bcc6b20c2a415ef5aed181cdf Image: busybox:1.30.1 Image ID: docker-pullable://busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d Port: <none> Host Port: <none> Command: sleep Args: 100000 State: Running Started: Sun, 09 Feb 2020 04:34:19 -0500 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-kzxwt (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-kzxwt: Type: Secret (a volume populated by a Secret) SecretName: default-token-kzxwt Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m47s default-scheduler Successfully assigned default/multi-pods to 192.168.163.131 Normal Pulled 2m46s kubelet, 192.168.163.131 Container image "busybox:1.31.1" already present on machine Normal Created 2m46s kubelet, 192.168.163.131 Created container blue-pod-container Normal Started 2m46s kubelet, 192.168.163.131 Started container blue-pod-container Normal Pulled 2m46s kubelet, 192.168.163.131 Container image "busybox:1.30.1" already present on machine Normal Created 2m46s kubelet, 192.168.163.131 Created container green-pod-container Normal Started 2m46s kubelet, 192.168.163.131 Started container green-pod-container Normal Pulled 2m46s kubelet, 192.168.163.131 Container image "busybox:1.30.1" already present on machine Normal Created 2m46s kubelet, 192.168.163.131 Created container yellow-pod-container Normal Started 2m46s kubelet, 192.168.163.131 Started container yellow-pod-container [root@host131 Pod]#
可以看到在Containers段显示了三个容器的详细信息,而在Event中也可以看到三个容器的创建过程。
Pod中只有一个容器时,使用如下命令即可进入到容器之中:
执行命令:kubectl exec -it pod名称 sh或者bash
而如果有多个容器时,则需要使用-c指定容器名称:
执行命令:kubectl exec -it pod名称 -c 容器名称 sh或者bash
比如进入multi-pods中名为yellow-pod-container :
[root@host131 Pod]# kubectl exec -it multi-pods -c yellow-pod-container sh
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 sleep 100000
6 root 0:00 sh
11 root 0:00 ps -ef
/ # busybox |grep BusyBox
BusyBox v1.30.1 (2019-05-09 01:23:43 UTC) multi-call binary.
BusyBox is copyrighted by many authors between 1998-2015.
BusyBox is a multi-call binary that combines many common Unix
link to busybox for each function they wish to use and BusyBox
/ # hostname
multi-pods
/ #
pause镜像确认,使用docker ps命令可以确认到包含pause在内的4个容器,pause容器作为此Pod的根容器,负责整个Pod的网络信息
[root@host131 Pod]# docker ps |grep multi-pods
8e500fef966a 64f5d945efcc "sleep 100000" 8 minutes ago Up 8 minutes k8s_yellow-pod-container_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0
b60d1cf06ff6 64f5d945efcc "sleep 10000" 8 minutes ago Up 8 minutes k8s_green-pod-container_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0
881da8a39e6a 6d5fcfe5ff17 "sleep 1000" 8 minutes ago Up 8 minutes k8s_blue-pod-container_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0
44d9b05a6d1b gcr.io/google_containers/pause-amd64:3.1 "/pause" 8 minutes ago Up 8 minutes k8s_POD_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0
[root@host131 Pod]#
首先确认pause的id
[root@host131 Pod]# docker inspect k8s_POD_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0 |grep Id
"Id": "44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb",
[root@host131 Pod]#
然后可以确认到在其余的容器中都和此Pod有所关联,可以看到Pause作为根容器为其余容器所起到的作用。
[root@host131 Pod]# docker inspect k8s_yellow-pod-container_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0 |grep 44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb
"ResolvConfPath": "/var/lib/docker/containers/44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb/hostname",
"NetworkMode": "container:44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb",
"IpcMode": "container:44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb",
"io.kubernetes.sandbox.id": "44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb"
[root@host131 Pod]#
[root@host131 Pod]# docker inspect k8s_green-pod-container_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0 |grep 44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb
"ResolvConfPath": "/var/lib/docker/containers/44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb/hostname",
"NetworkMode": "container:44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb",
"IpcMode": "container:44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb",
"io.kubernetes.sandbox.id": "44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb"
[root@host131 Pod]#
另外如果不使用-c指定容器时,缺省会进入第一个容器中
[root@host131 Pod]# kubectl exec -it multi-pods sh
Defaulting container name to blue-pod-container.
Use 'kubectl describe pod/multi-pods -n default' to see all of the containers in this pod.
/ #
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。