快速访问 k8s work 节点
2022-10-10
创建一个特权容器,通过 nsenter
command 进入 node shell。示例 yaml 如下:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
labels:
run: nsenter-v0l86q
name: nsenter-v0l86q
namespace: default
spec:
containers:
- command:
- nsenter
- --target
- "1"
- --mount
- --uts
- --ipc
- --net
- --pid
- --
- bash
- -l
image: docker.io/library/alpine
imagePullPolicy: Always
name: nsenter
securityContext:
privileged: true
stdin: true
stdinOnce: true
tty: true
hostNetwork: true
hostPID: true
restartPolicy: Never
tolerations:
- key: CriticalAddonsOnly
operator: Exists
- effect: NoExecute
operator: Exists
nodeName: ip-10-25-114-196.ap-east-1.compute.internal
EOF
直接 kubectl apply -f node-shell.yaml
即可创建 node shell。
上面的 yaml,关键有这么几点:
进入 node shell 的命令:nsenter --target 1 --mount --uts --ipc --net --pid -- bash -l
,在 Linux 系统里, nsenter 是一个命令行工具,用于进入到另一个 namespace 。譬如, nsenter -n -t 1 bash
就是进入到 pid 为 1 的进程所在的网络 namespace 里。
以及进入 node shell 的权限:
•hostPID: true
共享 host 的 pid
•hostNetwork: true
共享 host 的网络
•privileged: true
: PSP 权限策略是 privileged
, 即完全无限制。
该pod在哪个node创建,登录该pod就是在哪个node,如果想登录特定node,可以结合 nodeName: 启动pod;
# 登录
k exec -it nsenter-v0l86q bash
# 查看node信息
hostname