将文件传递到 kubernetes Pod 中的 docker 容器

Pass file to docker container in a kubernetes Pod

我是 Kubernetes 的初学者,我想实现的是:

所以我想知道是否可以在 Kubectl apply 中做到这一点?

我的 pod.yaml 看起来像:

apiVersion: v1
kind: Pod
metadata:
  generateName: testing
  labels:
    type: testing
  namespace: ns-test
  name: testing-config
spec:
  restartPolicy: OnFailure
  hostNetwork: true
  containers:
    - name: mycontainer
      image: ".../mycontainer:latest"

首先,您使用您的密钥创建一个秘密:kubectl create secret generic mysecret-keys --from-file=privatekey=</path/to/the/key/file/on/your/host> --from-file=publickey=</path/to/the/key/file/on/your/host>

然后你使用 pod 中的秘密引用密钥文件:

apiVersion: v1
kind: Pod
metadata:
  ...
spec:
  ...
  containers:
  - name: mycontainer
    image: ".../mycontainer:latest"
    volumeMounts:
    - name: mysecret-keys
      mountPath: /path/in/the/container  # <-- privatekey & publickey will be mounted as file in this directory where your shell script can access
  volumes:
  - name: mysecret-keys
    secret:
      secretName: mysecret-keys  # <-- mount the secret resource you created above

您可以使用kubectl get secret mysecret-keys --output yaml查看秘密。您可以使用 kubectl describe pod testing-config.

检查 pod 及其安装

您必须将私钥/public 密钥存储在 kubernetes secret 对象中

apiVersion: v1
kind: Secret
metadata:
  name: mysshkey
  namespace: ns-test
data:
  id_rsa: {{ value }}
  id_rsa.pub: {{ value }}

现在您可以将这个秘密文件挂载到您的容器中:

      containers:
      - image: "my-image:latest"
        name: my-app
        ...
        volumeMounts:
          - mountPath: "/var/my-app"
            name: ssh-key
            readOnly: true
      volumes:
        - name: ssh-key
          secret:
            secretName: mysshkey

kuberentes的文档也提供了Using Secrets as files from a Pod

的一章

它没有经过测试,但我希望它有效。