You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在K8s Pod内从busybox容器访问nginx容器的文件系统?

在K8s Pod的busybox容器中访问nginx容器内的文件的实现方法

当然可以实现,针对你调试无常用Linux工具容器的需求,有两种常用方案:

方案一:基于已配置的共享进程命名空间访问

你已经在Pod中设置了spec.shareProcessNamespace: true,这个配置允许Pod内所有容器共享同一个进程命名空间,busybox容器可通过/proc/<PID>/root/路径直接访问nginx容器的文件系统,操作步骤:

  • 进入busybox容器:
    kubectl exec -it nginxbusybox -c busybox -- sh
    
  • 查找nginx容器的主进程PID:
    ps aux | grep nginx
    
    通常nginx的主进程PID为1(容器内进程PID从1开始分配)
  • 访问nginx的目标目录:
    ls /proc/1/root/etc/nginx
    
    执行后就能看到nginx容器内/etc/nginx目录下的所有文件。

方案二:使用emptyDir共享卷(更直观的方式)

如果觉得查找PID麻烦,可通过配置共享卷,将nginx容器的目标目录挂载到临时共享卷,再让busybox容器挂载同一个卷直接访问文件。修改后的Pod YAML如下:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginxbusybox
  name: nginxbusybox
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - name: nginx-config
      mountPath: /etc/nginx
  - image: busybox
    name: busybox
    command:
    - sleep
    - '86400'
    volumeMounts:
    - name: nginx-config
      mountPath: /nginx-config
  volumes:
  - name: nginx-config
    emptyDir: {}

部署修改后的Pod后,进入busybox容器直接执行:

ls /nginx-config

就能看到nginx容器内/etc/nginx目录的内容。这种方式无需依赖进程命名空间,访问路径更直观,但需要重新部署Pod。

两种方案都能满足你的调试需求,可根据实际场景选择。

内容的提问来源于stack exchange,提问作者user1726366

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 08:15:52