首页 > 解决方案 > 无法在单个 pod 中创建多个容器

问题描述

我正在尝试在单个 pod 中创建多个容器。我面临以下问题:

YAML 文件:

apiVersion: v1
kind: Pod
metadata:
  name: multi-containers
spec:

  restartPolicy: Never

  volumes:
  - name: multi-data
    emptyDir: {}

  containers:

  - name: nginx-multicontainerone
    image: nginx
    volumeMounts:
    - name: multi-data
      mountPath: /one

  - name: nginx-multicontainertwo
    image: nginx
    volumeMounts:
    - name: multi-data
      mountPath: /two

  - name: debian-container
    image: debian
    volumeMounts:
    - name: multi-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]

下面是 describe 命令的输出:

Kubectl 描述 pod 多容器

Name:         multi-containers
Namespace:    default
Priority:     0
Node:         docker-desktop/192.168.65.4
Start Time:   Mon, 01 Nov 2021 20:07:08 +0530
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.1.0.238
IPs:
  IP:  10.1.0.238
Containers:
  nginx-multicontainerone:
    Container ID:   docker://91561db271c29670880de55dda6a5f1724de42583d5712807f37dbc1597aa2ea
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 01 Nov 2021 20:07:28 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /one from multi-data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-n6hst (ro)
  nginx-multicontainertwo:
    Container ID:   docker://049e08584f49e7970fac5b1fbb60dbf67c9928944123336c62ec423a7c656239
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
    Port:           <none>
    Host Port:      <none>
    State:          Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Mon, 01 Nov 2021 20:07:32 +0530
      Finished:     Mon, 01 Nov 2021 20:07:34 +0530
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /two from multi-data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-n6hst (ro)
  debian-container:
    Container ID:  docker://8eb1b494e3da1672cef86bbe34af11f3e6f2e148ab56fbb969aca1f81205d5fa
    Image:         debian
    Image ID:      docker-pullable://debian@sha256:4d6ab716de467aad58e91b1b720f0badd7478847ec7a18f66027d0f8a329a43c
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
    Args:
      -c
      echo Hello from the debian container > /pod-data/index.html
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Mon, 01 Nov 2021 20:07:35 +0530
      Finished:     Mon, 01 Nov 2021 20:07:35 +0530
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /pod-data from multi-data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-n6hst (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  multi-data:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  <unset>
  kube-api-access-n6hst:
    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  66s   default-scheduler  Successfully assigned default/multi-containers to docker-desktop
  Normal  Pulling    65s   kubelet            Pulling image "nginx"
  Normal  Pulled     47s   kubelet            Successfully pulled image "nginx" in 18.2903658s
  Normal  Created    47s   kubelet            Created container nginx-multicontainerone
  Normal  Started    47s   kubelet            Started container nginx-multicontainerone
  Normal  Pulling    47s   kubelet            Pulling image "nginx"
  Normal  Pulled     44s   kubelet            Successfully pulled image "nginx" in 3.285316s
  Normal  Created    43s   kubelet            Created container nginx-multicontainertwo
  Normal  Started    43s   kubelet            Started container nginx-multicontainertwo
  Normal  Pulling    43s   kubelet            Pulling image "debian"
  Normal  Pulled     40s   kubelet            Successfully pulled image "debian" in 3.3076706s
  Normal  Created    40s   kubelet            Created container debian-container
  Normal  Started    40s   kubelet            Started container debian-container

命令:kubectl get pods 结果:多容器 1/3
NotReady 0 14m

本练习的主要目标:我正在使用共享的 emptyDir 卷并尝试访问容器“nginx-multicontainerone”、“nginx-multicontainertwo”中的 index.html

标签: kubernetes

解决方案


我在 Minikube 上运行了您的 pod,并且能够重现该问题。因为 pod 中的所有容器共享相同的网络接口,所以第一个容器获取 pod 中 Nginx 的默认端口 (80)。随后,另一个 Nginx 容器在尝试绑定到同一端口时失败。要检查失败容器的日志,可以使用以下命令。

kubectl logs multi-containers nginx-multicontainertwo

当我运行它时,我得到了以下输出。

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/11/01 15:02:10 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/11/01 15:02:10 [emerg] 1#1: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2021/11/01 15:02:10 [notice] 1#1: try again to bind() after 500ms
2021/11/01 15:02:10 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/11/01 15:02:10 [emerg] 1#1: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2021/11/01 15:02:10 [notice] 1#1: try again to bind() after 500ms
2021/11/01 15:02:10 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/11/01 15:02:10 [emerg] 1#1: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2021/11/01 15:02:10 [notice] 1#1: try again to bind() after 500ms
2021/11/01 15:02:10 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/11/01 15:02:10 [emerg] 1#1: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2021/11/01 15:02:10 [notice] 1#1: try again to bind() after 500ms
2021/11/01 15:02:10 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/11/01 15:02:10 [emerg] 1#1: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2021/11/01 15:02:10 [notice] 1#1: try again to bind() after 500ms
2021/11/01 15:02:10 [emerg] 1#1: still could not bind()
nginx: [emerg] still could not bind()

尝试为每个 Nginx 实例分配不同的端口配置。


推荐阅读