salt-master, 文件名Dockerfile
# VERSION 1.0 # TO_BUILD: docker build -t salt/master . FROM library/ubuntu:16.04 # Update & install packages RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && sed -i s/security.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && apt-get update && apt-get install software-properties-common -y && add-apt-repository ppa:saltstack/salt -y && apt-get install salt-master -y # COPY ./master /etc/salt/master COPY ./*.conf /etc/salt/master.d CMD ["salt-master","start"]
salt-minion, 文件名Dockerfile
# VERSION 1.0 # TO_BUILD: docker build -t salt/minion . FROM library/ubuntu:16.04 # Update & install packages RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && sed -i s/security.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && apt-get update && apt-get install software-properties-common -y && add-apt-repository ppa:saltstack/salt -y && apt-get install salt-minion -y CMD ["salt-minion","start"]
salt-master conf, 文件名master.conf
interface: 0.0.0.0 auto_accept: True hash_type: sha256 file_ignore_regex: - '/.svn($|/)' - '/.git($|/)' file_ignore_glob: - '*.pyc' - '*/somefolder/*.bak' - '*.swp'
salt-minion conf,文件名minion.conf
master: 172.17.0.2 id: minion-vm1 hash_type: sha256
使用shell脚本启动,文件名称create-cluster.sh
#!/bin/bash # must be 2 args if [ $# -lt 2 ];then echo "usage $0 master_name num_of_minion" exit fi name=$1 master_name='master__'$name # check the container name cname=$(docker ps -a| grep $master_name$ | awk '{print $NF}') cid=$(docker ps -a| grep $master_$name$ | awk '{print $1}') if [ "$cname" = "$master_name" ];then echo "contain name $name exist, id is $cid." exit fi # the 2nd arg must be digit if [ "$2" -gt 0 ] 2>/dev/null;then echo "there will be create $2 minion." num=$2 else echo "usage $0 master_name num_of_minion" echo "$2 is not a number" exit fi # create master mkdir -p /docker/cluster/$name/master/master.d cp /docker/Docker/salt master/master.conf /docker/cluster/$name/master/master.d/master.conf msg=$(docker run -d --name=$master_name -v /docker/cluster/$name/master/master.d:/etc/salt/master.d salt/master) # check if container is create success mcid=$(docker ps| grep $master_name$ | awk '{print $1}') if [ -z $mcid ];then echo "master create faild!" exit fi master_ip=$(docker exec -it $mcid ip a | grep "global eth0" | awk '{print $2}' | awk -F '/' '{print $1}') if [ -z $master_ip ];then echo "could not find master ip in container, id = $mcid !" exit fi echo "master ip is $master_ip" #create minion for i in `seq $num`; do minion_name='node__'${name}'__'${i} mkdir -p /docker/cluster/$name/node$i/minion.d cp /docker/Docker/salt minion/minion.conf /docker/cluster/$name/node$i/minion.d/minion.conf sed -i -e 's/id: minion-vm1/id: minion-'${minion_name}'/' /docker/cluster/$name/node$i/minion.d/minion.conf sed -i -e 's/master: 172.17.0.2/master: '"${master_ip}"'/' /docker/cluster/$name/node$i/minion.d/minion.conf cid=$(docker run -d --name=$minion_name -v /docker/cluster/$name/node$i/minion.d:/etc/salt/minion.d salt/minion) # check if container is create success cid=$(docker ps| grep $minion_name$ | awk '{print $1}') if [ -z $cid ];then echo "node$i create faild!" exit fi done # show the result x='#' echo "docker exec -it $mcid salt-key -L" minion_name="minion-$minion_name" for i in `seq 100`; do printf "starting:[%-100s]%d%% " $x $i x=#$x node=$(docker exec -it $mcid salt-key -L | grep $minion_name) if [ -n "$node" ];then break fi sleep 5 done if [ $i -le 100 ];then while [ $i -le 100 ]; do printf "starting:[%-100s]%d%% " $x $i x=#$x i=$(($i+1)) done fi printf " " if [ -n "$node" ];then echo "create cluster success" else echo "please check manually with command: docker exec -it $mcid salt-key -L" fi
执行脚本 sh ./create-cluster.sh master_name 5
# 创建一个集群,包含5个minion
docker exec -it 54e509d81808 salt-key -L
# 执行结果如下 Accepted Keys: minion-node__test__1 minion-node__test__2 minion-node__test__3 minion-node__test__4 minion-node__test__5 Denied Keys: Unaccepted Keys: Rejected Keys:
docker ps
# 执行结果如下 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 77fb1000c0b3 salt/minion "salt-minion start" 32 minutes ago Up 32 minutes node__test__5 557a50f73902 salt/minion "salt-minion start" 32 minutes ago Up 32 minutes node__test__4 0c5dca2b207a salt/minion "salt-minion start" 32 minutes ago Up 32 minutes node__test__3 b08afc8fe496 salt/minion "salt-minion start" 32 minutes ago Up 32 minutes node__test__2 480d9b517766 salt/minion "salt-minion start" 32 minutes ago Up 32 minutes node__test__1 54e509d81808 salt/master "salt-master start" 32 minutes ago Up 32 minutes master__test