consul_server.sh

#!/bin/bash
conf_dir=/opt/consul/conf
mkdir -p ${conf_dir}
#集群节点
nodes=(
172.17.252.103
172.17.252.104
172.17.252.105
)
consul_ver=1.4.2
retry_interval=15s
contain_svr_name=consul_server
privIP=$(/sbin/ifconfig eth0 | sed -n 's/.*inet \(addr:\)\?\([0-9.]\{7,15\}\) .*/\2/p')
if [[ !("${nodes[@]}" =~ $privIP) ]];
then
echo -e "Current node:${privIP} not in configured server nodes.\n"
exit
fi
svr_runing=$(docker ps -a | grep "${contain_svr_name}" | egrep "Up [About]|[0-9]{1,}")
if [[ ${svr_runing} != "" ]];
then
echo -e "Current container of consul server has been running.\n"
exit
else
svr_exists=$(docker ps -a | grep "${contain_svr_name}")
if [[ ${svr_exists} != "" ]];
then
echo -e "Now try to start the container as it stopped...\n"
docker start ${svr_exists}
sleep 2
docker ps -a grep "${contain_svr_name}"
exit
fi
fi
echo -e "To start a new container for consul...\n"
echo -e "To initialize configuration...\n"
nodels=""
for host in ${nodes[*]}
do
if [[ $nodels != "" ]];
then
nodels=$nodels,
fi
nodels=$nodels"\"$host\""
done
config="{\n
\"datacenter\": \"testkydhuabei2\",\n #数据中心
\"retry_join\": [${nodels}],\n
\"retry_interval\": \"${retry_interval}\",\n
\"rejoin_after_leave\": true,\n
\"start_join\": [${nodels}],\n
\"bootstrap_expect\": 3,\n
\"server\": true,\n
\"ui\": true,\n
\"dns_config\": {\"allow_stale\": true, \"max_stale\": \"5s\"},\n
\"node_name\": \"$HOSTNAME\"\n
}\n"
echo $config
echo -e ${config} > ${conf_dir}/server.json
echo -e ${config}
docker run -d -v ${conf_dir}:${conf_dir} \
-v /etc/localtime:/etc/localtime \
-v /consul/data:/consul/data \
--name ${contain_svr_name} \
--net=host consul:${consul_ver} agent \
-config-dir=${conf_dir} \
-client=0.0.0.0 \
-bind=0.0.0.0 \
-advertise=${privIP}
sleep 2
svr_runing=$(docker ps -a | grep "${contain_svr_name}" | egrep "Up [About]|[0-9]{1,}")
if [[ ${svr_runing} == "" ]];
then
echo -e "\nError: docker-consul failed to start...\n"
exit
fi
echo -e "\nOK: docker-consul has started as background server.\n"

consul_client.sh

#!/bin/bash
conf_dir=/opt/consul/conf
mkdir -p ${conf_dir}
svrnodes=(
172.17.252.102
172.17.252.103
172.17.252.105
)
privIP=$(/sbin/ifconfig eth0 | sed -n 's/.*inet \(addr:\)\?\([0-9.]\{7,15\}\) .*/\2/p')
consul_ver=1.4.2
retry_interval=15s
contain_cli_name=consul_client
if [[ "${svrnodes[@]}" =~ $privIP ]];
then
echo -e "Current node:${privIP} is configured for server consul, not to run client mode.\n"
exit
fi
svr_runing=$(docker ps -a | grep "${contain_cli_name}" | egrep "Up [About]|[0-9]{1,}")
if [[ ${svr_runing} != "" ]];
then
echo -e "Current container of consul client has been running.\n"
exit
else
svr_exists=$(docker ps -a | grep "${contain_cli_name}")
if [[ ${svr_exists} != "" ]];
then
echo -e "Now try to start the container as it stopped...\n"
docker start ${svr_exists}
sleep 2
docker ps -a grep "${contain_cli_name}"
exit
fi
fi
echo -e "To start a new container for consul...\n"
echo -e "To initialize configuration...\n"
nodels=""
for host in ${svrnodes[*]}
do
if [[ $nodels != "" ]];
then
nodels=$nodels,
fi
nodels=$nodels"\"$host\""
done
config="{\n
\"retry_join\": [${nodels}],\n
\"retry_interval\": \"${retry_interval}\",\n
\"rejoin_after_leave\": true,\n
\"start_join\": [${nodels}],\n
\"server\": false,\n
\"ui\": true,\n
\"node_name\": \"$HOSTNAME\"\n
}\n"
echo $config
echo -e ${config} > ${conf_dir}/client.json
echo -e ${config}
docker run -d -v ${conf_dir}:${conf_dir} \
-v /etc/localtime:/etc/localtime \
--name ${contain_cli_name} \
--net=host consul:${consul_ver} agent \
-datacenter=kydhuabei2 \
-config-dir=${conf_dir} \
-client=0.0.0.0 \
-advertise=${privIP}
sleep 2
svr_runing=$(docker ps -a | grep "${contain_cli_name}" | grep "Up")
if [[ ${svr_runing} == "" ]];
then
echo -e "\nError: docker-consul client node failed to start...\n"
exit
fi
echo -e "\nOK: docker-consul has started as a client node.\n"

注:此脚本与网络找寻到的,consul最好3台server组成集群