bash - 如何执行到K8s pods 中,但从外部使用bash_profile?

标签 bash kubernetes

我的.bash_profile具有许多我经常使用的别名。但是,当我执行kubernetes Pane 时,这些别名变得(无法理解)变得不可访问。当我说“exec into”时,我的意思是:
kubectl exec -it [pod-name] -c [container-name] bash
有什么办法可以使我在执行后仍可以使用bash配置文件吗?

最佳答案

您说的只是别名。在这种情况下,只有在这种情况下,您才能保存。使用bash_profileConfigMap中的--from-env-file

kubectl create configmap bash-profile --from-env-file=.bash_profile

请记住,env文件中的每一行都必须采用VAR = VAL格式。

以#开头的行和空白行将被忽略。

然后,您可以将所有键值对作为容器环境变量加载:
apiVersion: v1
kind: Pod
metadata:
  name: bash-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: bash-profile
  restartPolicy: Never

Populate a Volume with data stored in a ConfigMap:
apiVersion: v1
kind: Pod
metadata:
  name: bash-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /root/.bash_profile
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: bash-profile
  restartPolicy: Never

@ Mark提到的想法也应该可行。

如果需要将kubectl cp .bash_profile <pod_name>:/root/放入特定的容器中,则可以添加-c, --container='': Container name. If omitted, the first container in the pod will be chosen选项。

关于bash - 如何执行到K8s pods 中,但从外部使用bash_profile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547457/

相关文章:

linux - 显示具有特定扩展名的所有文件的文件名

kubernetes - 在 storageclass.storage.k8s.io "fast"中未找到创建持久卷结果

kubernetes - 我们如何在图中可视化 Kubernetes 对象/拓扑?

kubernetes - 什么时候需要 kubectl 代理?

linux - 与标准环境相比,在 Docker 中使用 'script' 的“su”返回不同的结果

regex - 如何从两个文件中读取内容并合并到 bash shell 中的第三个文件

linux - 如何检查计划作业的进程ID或进程

linux - shell,删除名称末尾的空格

kubernetes - 如何将服务连接限制为Kubernetes中的Pod列表?

kubernetes - 配置 microk8s 使用 ~/.kube/config