在 kubernetes 中创建两个容器 pod

时间:2021-07-24 18:34:19

标签: kubernetes

创建一个运行两个容器的 pod,并确保该 pod 具有共享卷,两个容器都可以使用该卷相互通信 在一个容器中编写 HTML 文件并尝试从另一个容器访问它

谁能告诉我怎么做

1 个答案:

答案 0 :(得分:1)

具有多个容器的示例 pod

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

  restartPolicy: Never

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

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

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

官方文档:https://kubernetes.io/docs/tasks/access-application-cluster/communicate-containers-same-pod-shared-volume/

以上示例使用的是空目录,因此如果您重新启动 POD 或重新启动,您将丢失数据。

如果您有任何保存数据的要求,我建议您使用 PVC 而不是空目录。

如果可以,我建议使用 NFS

相关问题