如何在k8s中的不同容器之间共享netns?

时间:2021-03-07 05:45:54

标签: kubernetes namespaces

我想在属于同一个 pod 的两个容器之间共享 netns。 我知道默认情况下同一个 pod 中的所有容器都共享网络。我尝试为两个容器将相同的主机路径安装到 /var/run/netns。我可以在第一个容器中创建 netns,但是当我访问第二个容器中的 netns 时,它显示“无效参数”。我运行"file /var/run/netns/n1",它报告正常的空文件。我能做什么?可能吗?

1 个答案:

答案 0 :(得分:0)

但在我的情况下,下面的清单文件工作得很好。您也可以按照以下步骤进行检查。

第一步:kubectl apply -f(在清单文件下面)

第 2 步:kubectl exec -ti test-pd -c test1 /bin/bash

步骤 3:进入 test-pd 目录并使用 touch 命令创建一个 .txt 文件并退出。

第四步:kubectl exec -ti test-pd -c test2 sh

Spep 5:转到 test-pd 目录并在此处检查创建的文件。我在这里找到了创建的文件。

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: nginx
    name: test1
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  - image: pkbhowmick/go-rest-api:2.0.1
    name: test2
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: DirectoryOrCreate