1 在一个pod中创建多个容器
1.1 YAML文件示例
# cat multi-pods.yaml 
---
apiVersion: v1
kind: Pod
metadata:name: multi-pods
spec:containers:- name: blue-pod-containerimage: busybox:1.31.1command: ["sleep"]args:    ["1000"]- name: green-pod-containerimage: busybox:1.30.1command: ["sleep"]args:    ["10000"]- name: yellow-pod-containerimage: busybox:1.30.1command: ["sleep"]args:    ["100000"]
...1.2 生成Pod
# kubectl create -f multi-pods.yaml 
pod/multi-pods created1.3 确认Pod信息
使用kubectl get pods命令可以获取此pod的详细信息,可以看到READY显示为3/3,说明包含三个容器全部正常启动,整体状态STATUS为Running状态,Pod本身的IP为10.254.176.3,Pod名称为multi-pods。
# 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>然后可以使用kubectl describe查看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://881da8a39e6a9999cce25ef227c97e8680379d97cd190e6a98ff25f49a3f7469Image:         busybox:1.31.1Image ID:      docker-pullable://busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115aPort:          <none>Host Port:     <none>Command:sleepArgs:1000State:          RunningStarted:      Sun, 09 Feb 2020 04:34:19 -0500Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from default-token-kzxwt (ro)green-pod-container:Container ID:  docker://b60d1cf06ff68ffae44e13f33d84b12b8525707af6de1836abdc5905a135166fImage:         busybox:1.30.1Image ID:      docker-pullable://busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3dPort:          <none>Host Port:     <none>Command:sleepArgs:10000State:          RunningStarted:      Sun, 09 Feb 2020 04:34:19 -0500Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from default-token-kzxwt (ro)yellow-pod-container:Container ID:  docker://8e500fef966ab861843d46076bdb07fa5443041bcc6b20c2a415ef5aed181cdfImage:         busybox:1.30.1Image ID:      docker-pullable://busybox@sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3dPort:          <none>Host Port:     <none>Command:sleepArgs:100000State:          RunningStarted:      Sun, 09 Feb 2020 04:34:19 -0500Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from default-token-kzxwt (ro)
Conditions:Type              StatusInitialized       True Ready             True ContainersReady   True PodScheduled      True 
Volumes:default-token-kzxwt:Type:        Secret (a volume populated by a Secret)SecretName:  default-token-kzxwtOptional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300snode.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.131Normal  Pulled     2m46s  kubelet, 192.168.163.131  Container image "busybox:1.31.1" already present on machineNormal  Created    2m46s  kubelet, 192.168.163.131  Created container blue-pod-containerNormal  Started    2m46s  kubelet, 192.168.163.131  Started container blue-pod-containerNormal  Pulled     2m46s  kubelet, 192.168.163.131  Container image "busybox:1.30.1" already present on machineNormal  Created    2m46s  kubelet, 192.168.163.131  Created container green-pod-containerNormal  Started    2m46s  kubelet, 192.168.163.131  Started container green-pod-containerNormal  Pulled     2m46s  kubelet, 192.168.163.131  Container image "busybox:1.30.1" already present on machineNormal  Created    2m46s  kubelet, 192.168.163.131  Created container yellow-pod-containerNormal  Started    2m46s  kubelet, 192.168.163.131  Started container yellow-pod-container可以看到在Containers段显示了三个容器的详细信息,而在Event中也可以看到三个容器的创建过程。
1.4 进入容器
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  COMMAND1 root      0:00 sleep 1000006 root      0:00 sh11 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 Unixlink 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首先确认pause的id
[root@host131 Pod]# docker inspect k8s_POD_multi-pods_default_3c744cee-6ce3-4de3-99ab-f8909fee0147_0 |grep Id"Id": "44d9b05a6d1b9f33e170a70a4db304b92e4dbbdd3d0a38efaca0513ad8b86acb",然后可以确认到在其余的容器中都和此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"另外如果不使用-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.
/ #