方案一 ephemeralContainers
告警:ephemeralContainers 目前k8s1.22 还是处于alpha ,不适合生产集群使用
需要在master 的三大组件中都加入
--feature-gates=EphemeralContainers=true
kube-proxy的启动配置中也加入
--feature-gates=EphemeralContainers=true
kubelet的配置加入如下配置
featureGates:
EphemeralContainers: true
重启所有服务
kubectl debug example-foo-9bbb75dc8-klj2p -ti --image=busybox:stable
方案二 shareProcessNamespace
使用 shareProcessNamespace ,假如需要调试的容器为Nginx,控制器是deployment,
原本的样子
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-foo
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
想要通过共享容器命名空间方式访问只需要,在containers:同一层级添加shareProcessNamespace: true
然后加入调试容器,假如是busybox,通过kubectl edit 方式,编辑成如下模式
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-foo
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
shareProcessNamespace: true
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
- name: busybox
command:
- /bin/sh
- -c
- sleep 3600
image: busybox:stable
imagePullPolicy: IfNotPresent
然后就可以通过进入busybox 容器调试Nginx了