github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/docs/site/src/zh/getting-started/plugin.md (about) 1 # 集群镜像插件使用 2 3 ## 插件类型列表 4 5 ### 主机名插件 6 7 主机名插件将帮助您更改所有主机名 8 9 ```yaml 10 --- 11 apiVersion: sealer.aliyun.com/v1alpha1 12 kind: Plugin 13 metadata: 14 name: MyHostname # 指定插件名称,将会转储到$rootfs/plugins目录下。 15 spec: 16 type: HOSTNAME #插件类型 17 action: PreInit # 指定运行阶段 18 data: | 19 192.168.0.2 master-0 20 192.168.0.3 master-1 21 192.168.0.4 master-2 22 192.168.0.5 node-0 23 192.168.0.6 node-1 24 192.168.0.7 node-2 25 ``` 26 27 ### 脚本插件 28 29 你可以在指定节点的任何阶段执行任何shell命令。 30 31 ```yaml 32 apiVersion: sealer.aliyun.com/v1alpha1 33 kind: Plugin 34 metadata: 35 name: MyShell 36 spec: 37 type: SHELL 38 action: PostInstall # # 指定运行阶段【PreInit | PostInstall | PostClean】 39 'on': node-role.kubernetes.io/master= 40 data: | 41 kubectl get nodes 42 ``` 43 44 ```shell 45 action : [PreInit| PostInstall] # 指定执行shell的时机 46 镜像挂载前阶段 | action: Originally 47 在初始化之前之前执行命令 | action: PreInit 48 在添加节点之前执行命令 | action: PreJoin 49 在添加节点之后执行命令 | action: PostJoin 50 在执行Kubefile CMD命令前 | action: PreGuest 51 在安装集群之后执行命令 | action: PostInstall 52 在清理集群前执行命令 | action: PreClean 53 在清理集群后执行命令 | action: PostClean 54 组合使用 | action: PreInit|PreJoin 55 on : #指定执行命令的机器 56 为空时默认在所有节点执行 57 在所有master节点上执行 'on': master 58 在所有node节点上执行 'on': node 59 在指定IP上执行 'on': 192.168.56.113,192.168.56.114,192.168.56.115,192.168.56.116 60 在有连续IP的机器上执行 'on': 192.168.56.113-192.168.56.116 61 在指定label节点上执行(action需为PostInstall或PreClean) 'on': node-role.kubernetes.io/master= 62 data : #指定执行的shell命令 63 例: 64 `data: echo "test shell plugin"` 65 66 调用cluster env 执行脚本: 67 执行shell 脚本命令: `data: echo $docker_dir $ips[@]` 68 执行shell 脚本文件: `data: . scripts/install.sh` 或者 `data: source scripts/install.sh` 69 ## 当sealer执行脚本时env的设置类似于:`docker_dir=/data/docker ips=(192.168.0.2;192.168.0.3) && source scripts/install.sh` 70 ## data中使用bash scripts/install.sh的方式将开启新的进程执行脚本导致无法获取当前环境env 71 ## 因此install.sh中如需使用env需要使用source scripts/install.sh 72 ``` 73 74 ### 标签插件 75 76 帮助您在安装kubernetes集群后设置标签 77 78 ```yaml 79 apiVersion: sealer.aliyun.com/v1alpha1 80 kind: Plugin 81 metadata: 82 name: MyLabel 83 spec: 84 type: LABEL 85 action: PostInstall 86 data: | 87 192.168.0.2 ssd=true 88 192.168.0.3 ssd=true 89 192.168.0.4 ssd=true 90 192.168.0.5 ssd=false,hdd=true 91 192.168.0.6 ssd=false,hdd=true 92 192.168.0.7 ssd=false,hdd=true 93 ``` 94 95 ### 集群检测插件 96 97 由于服务器以及环境因素(服务器磁盘性能差)可能会导致sealer安装完kubernetes集群后,立即部署应用服务,出现部署失败的情况。cluster check插件会等待kubernetes集群稳定后再部署应用服务。 98 99 ```yaml 100 apiVersion: sealer.aliyun.com/v1alpha1 101 kind: Plugin 102 metadata: 103 name: checkCluster 104 spec: 105 type: CLUSTERCHECK 106 action: PreGuest 107 ``` 108 109 ### 污点插件 110 111 如果你在Clusterfile后添加taint插件配置并应用它,sealer将帮助你添加污点和去污点: 112 113 ```yaml 114 apiVersion: sealer.aliyun.com/v1alpha1 115 kind: Plugin 116 metadata: 117 name: taint 118 spec: 119 type: TAINT 120 action: PreGuest 121 data: | 122 192.168.56.3 key1=value1:NoSchedule 123 192.168.56.4 key2=value2:NoSchedule- 124 192.168.56.3-192.168.56.7 key3:NoSchedule 125 192.168.56.3,192.168.56.4,192.168.56.5,192.168.56.6,192.168.56.7 key4:NoSchedule 126 192.168.56.3 key5=:NoSchedule 127 192.168.56.3 key6:NoSchedule- 128 192.168.56.4 key7:NoSchedule- 129 ``` 130 131 > data写法为`ips taint_argument` 132 > ips : 多个ip通过`,`连接,连续ip写法为 首ip-末尾ip 133 > taint_argument: 同kubernetes 添加或去污点写法(key=value:effect #effect必须为:NoSchedule, PreferNoSchedule 或 NoExecute)。 134 135 ### Etcd 备份插件 136 137 ```yaml 138 apiVersion: sealer.aliyun.com/v1alpha1 139 kind: Plugin 140 metadata: 141 name: MyBackup 142 spec: 143 type: ETCD 144 action: PostInstall 145 ``` 146 147 ### Out of tree plugin 148 149 at present, we only support the golang so file as out of tree plugin. More description about golang plugin 150 see [golang plugin website](https://pkg.go.dev/plugin). 151 152 copy the so file and the plugin config to your cloud image at build stage use `Kubefile`,sealer will parse and execute 153 it. develop your own out of tree plugin see [sealer plugin](../advanced/develop-plugin.md). 154 155 plugin config: 156 157 ```yaml 158 apiVersion: sealer.aliyun.com/v1alpha1 159 kind: Plugin 160 metadata: 161 name: label_nodes.so # out of tree plugin name 162 spec: 163 type: LABEL_TEST_SO # define your own plugin type. 164 action: PostInstall # which stage will this plugin be applied. 165 data: | 166 192.168.0.2 ssd=true 167 ``` 168 169 Kubefile: 170 171 ```shell script 172 FROM kubernetes:v1.19.8 173 COPY label_nodes.so plugin 174 COPY label_nodes.yaml plugin 175 ``` 176 177 Build a cluster image that contains the golang plugin (or more plugins): 178 179 ```shell script 180 sealer build -m lite -t kubernetes-post-install:v1.19.8 . 181 ``` 182 183 ## 如何使用插件 184 185 ### 通过Clusterfile使用插件 186 187 例如,在安装kubernetes集群后设置节点标签: 188 189 ```yaml 190 apiVersion: sealer.cloud/v2 191 kind: Cluster 192 metadata: 193 name: default-kubernetes-cluster 194 spec: 195 image: kubernetes:v1.19.8 196 ssh: 197 passwd: xxx 198 hosts: 199 - ips: [ 192.168.0.2,192.168.0.3,192.168.0.4 ] 200 roles: [ master ] 201 - ips: [ 192.168.0.5 ] 202 roles: [ node ] 203 --- 204 apiVersion: sealer.aliyun.com/v1alpha1 205 kind: Plugin 206 metadata: 207 name: LABEL 208 spec: 209 type: LABEL 210 action: PostInstall 211 data: | 212 172.20.126.8 ssd=false,hdd=true 213 ``` 214 215 ```shell script 216 sealer apply -f Clusterfile 217 ``` 218 219 ### 在Kubefile中使用默认插件 220 221 在很多情况下,可以不使用Clusterfile而使用插件,本质上是在使用插件之前存储了Clusterfile插件到$rootfs/plugins目录下 所以当我们构建镜像时可以添加自定义默认插件。 222 223 插件配置文件 shell.yaml: 224 225 ```yaml 226 apiVersion: sealer.aliyun.com/v1alpha1 227 kind: Plugin 228 metadata: 229 name: taint 230 spec: 231 type: SHELL 232 action: PostInstall 233 'on': node-role.kubernetes.io/master= 234 data: | 235 kubectl get nodes 236 --- 237 apiVersion: sealer.aliyun.com/v1alpha1 238 kind: Plugin 239 metadata: 240 name: SHELL 241 spec: 242 action: PostInstall 243 data: | 244 if type yum >/dev/null 2>&1;then 245 yum -y install iscsi-initiator-utils 246 systemctl enable iscsid 247 systemctl start iscsid 248 elif type apt-get >/dev/null 2>&1;then 249 apt-get update 250 apt-get -y install open-iscsi 251 systemctl enable iscsid 252 systemctl start iscsid 253 fi 254 ``` 255 256 Kubefile: 257 258 ```shell script 259 FROM kubernetes:v1.19.8 260 COPY shell.yaml plugin 261 ``` 262 263 构建一个包含安装iscsi的插件(或更多插件)的集群镜像: 264 265 ```shell script 266 sealer build -m lite -t kubernetes-iscsi:v1.19.8 . 267 ``` 268 269 通过镜像启动集群后插件也将被执行,而无需在Clusterfile中定义插件: 270 `sealer run kubernetes-iscsi:v1.19.8 -m x.x.x.x -p xxx`