自从1年多前在阿里云上开通 k8s 并把 Spring Cloud 应用部署进去后,就很少去看 k8s 的东西了。期间应用的部署和运行一直很顺畅,直到昨天。。,有个项目发布总是失败,通过 k get no
查看时,发现有个节点的 status
显示 NotReady
。
想 ssh 进节点看看发生了什么,竟然忘记怎么进去。。
查了之前写的学习笔记,上面有写一种借助 Privileged Pod
的方法,这里分享一下。
整个 Pod 的配置如下:
apiVersion: v1
kind: Pod
metadata:
name: privileged-pod
namespace: default
spec:
containers:
- name: busybox
image: busybox
resources:
limits:
cpu: 200m
memory: 100Mi
requests:
cpu: 100m
memory: 50Mi
stdin: true
securityContext:
privileged: true
volumeMounts:
- name: host-root-volume
mountPath: /host
# 如果你要在 Node 上进行写操作,将此置为 false
readOnly: true
volumes:
- name: host-root-volume
hostPath:
path: /
hostNetwork: true
hostPID: true
restartPolicy: Never
nodeSelector:
# 指定在哪个 Node 上运行该 Pod
kubernetes.io/hostname: cn-hangzhou.172.16.181.222
要注意的地方有2点:
-
通过 volumeMounts
将宿主机的/
路径挂载到 Pod 的/host
路径下。 -
通过 nodeSelector
指定 Pod 运行在哪个 Node 上。
Apply 上面的 yaml 之后,执行 k exec -it privileged-pod -- chroot /host
即可进入指定的宿主机。
如果你想要每个节点上都运行该 Pod ,以方便对每个节点进行检查,将上面模板改成 DaemonSet
即可:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: privileged-daemon
spec:
selector:
matchLabels:
name: privileged-pod
template:
metadata:
labels:
name: privileged-pod
spec:
containers:
- name: busybox
image: busybox
resources:
limits:
cpu: 200m
memory: 100Mi
requests:
cpu: 100m
memory: 50Mi
stdin: true
securityContext:
privileged: true
volumeMounts:
- name: host-root-volume
mountPath: /host
# 如果你要在 Node 上进行写操作,将此置为 false
readOnly: true
volumes:
- name: host-root-volume
hostPath:
path: /
hostNetwork: true
hostPID: true
restartPolicy: Always
参考
-
The most pointess Kubernetes command ever[1] -
Privileged Pod – Debug kubernetes node[2]
参考资料
The most pointess Kubernetes command ever: https://raesene.github.io/blog/2019/04/01/The-most-pointless-kubernetes-command-ever/
[2]
Privileged Pod – Debug kubernetes node: https://dev.to/dannypsnl/privileged-pod-debug-kubernetes-node-5129
原文始发于微信公众号(背井):k8s: 从 Pod chroot 进 Node
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/246642.html