k8s中如何用DaemonSet在大量节点上执行脚本
source link: https://blog.shell909090.org/blog/archives/2888/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何使用DaemonSet执行特权代码
简单点说,最低有三个条件。
spec.hostPID
is true.spec.containers.securityContext.privileged
is true.- 镜像里有
nsenter
.
条件1主要是为了让你能找到host的mnt namespace,然后你用条件3里的nsenter
来在这个namespace里执行代码。就像这样。
/usr/bin/nsenter -m/proc/1/ns/mnt bash /runOnHost.sh
host上如何执行脚本
nsenter
在host namespace下执行的时候,是不访问容器内文件系统的(或者很麻烦)。所以在host上执行脚本,最简单的办法就是拷过去。
cp /srv/runOnHost.sh /host/runOnHost.sh
/usr/bin/nsenter -m/proc/1/ns/mnt bash /runOnHost.sh
rm /host/runOnHost.sh
这就引出了一个新要求。如果要复制文件,pod需要挂载host文件系统。
containers:
volumeMounts:
- mountPath: /host
name: host-mount
volumes:
- hostPath:
path: /
type: ""
name: host-mount
如何访问网络
注意,虽然我们进入了host的mnt namespace,但是network用的还是容器的。如果你的代码不需要网络,或者容器网络就能工作,那很好。如果需要host网络,那么需要设置spec.hostNetwork
为true。
最简化镜像
很幸运的是,ubuntu官方镜像带了bash
和nsenter
,因此不单独pack一个image来执行脚本是可能的。
- 使用ubuntu镜像。
- 用文件目录创建一个ConfigMap。
在DaemonSet里引用这个ConfigMap并且mount。
containers: volumeMounts: - mountPath: /srv name: scripts volumes: - configMap: defaultMode: 0755 name: rce-testing name: scripts
Put all together
runOnHost.sh: 随意
runOnPod.sh:
#!/bin/bash
cp /srv/runOnHost.sh /host/runOnHost.sh
/usr/bin/nsenter -m/proc/1/ns/mnt bash /runOnHost.sh
rm /host/runOnHost.sh
sleep 864000000
用以下命令生成一个ConfigMap,内容是上述两个文件。
kubectl create configmap rce-testing --from-file=srv/
rce-testing.yaml:
apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: rce-testing
name: rce-testing
spec:
selector:
matchLabels:
app: rce-testing
template:
metadata:
labels:
app: rce-testing
spec:
hostPID: true
hostNetwork: true
containers:
- name: ubuntu
image: ubuntu
imagePullPolicy: IfNotPresent
command:
- /bin/bash
args:
- /srv/runOnPod.sh
securityContext:
privileged: true
volumeMounts:
- mountPath: /srv
name: scripts
- mountPath: /host
name: host-mount
volumes:
- configMap:
defaultMode: 0755
name: rce-testing
name: scripts
- hostPath:
path: /
type: ""
name: host-mount
其他注意事项
PodSecurityPolicy可能会阻止你开特权pod,注意解锁。
Recommend
-
78
本节介绍 DaemonSet 的几个典型应用场景。
-
40
在我们最近的文章中,我们介绍了 使用Linkerd作为服务网格代理 ,并发起了一系列的文章,记录了
-
47
-
22
异常的背景初识异常我们曾经的代码中已经接触了一些“异常”了.例如:除以0System.out.println(10/0);//执行结果Exceptioninthread"main"java.lang.ArithmeticException:/byzero数组下标越界int[]arr={1,2,3};System.out.println(arr[100]);//执行结果Exceptioninthre...
-
5
你身边有没有这种顽固的Eclipse忠实用户:IDEA不能一个窗口管理多个项目!太不方便了! 对于一个窗口同时管理多个项目的需求,在我们日常开发时候是经常需要的。尤其当我们在分布式环境下,在一个窗口中调试起来就能方便很多。
-
7
摘要:提到分布式算法,就不得不提 Paxos 算法,在过去几十年里,它基本上是分布式共识的代 名词,因为当前最常用的一批共识算法都是基于它改进的。比如,Fast Paxos 算法、 Cheap Paxos 算法、Raft 算法、ZAB 协议等等。
-
5
Emacs中如何在指定目录下运行shell命令 shell-command 与 shell-comand-to-string 可以用来在Emacs中运行shell命令,这两个函数都会自动根据shell命令是否以 & 结尾来自动同步/异步调度shell命令。 ...
-
5
爱码爱生活 vue中如何用js遍历map对象...
-
4
自定义事件tips推荐始终使用 kebab-case 的事件名。(v-on会将事件名自动转换为小写,避免匹配不到)changeData ×change-data √自定义组件的v-model
-
6
编程思考 : Java中如何用简单的数字描述更多的信息 精选 原创 小二上酒8 2022-09-...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK