· 7 years ago · Feb 16, 2018, 06:02 PM
1#!/bin/bash
2
3conf_dir=/opt/consul/conf
4mkdir -p ${conf_dir}
5
6svrnodes=(
7192.168.1.10
8192.168.1.11
9192.168.1.12
10)
11privIP=$(/sbin/ifconfig eth0 | sed -n 's/.*inet \(addr:\)\?\([0-9.]\{7,15\}\) .*/\2/p')
12consul_ver=1.0.3
13retry_interval=15s
14contain_cli_name=consul_client
15
16if [[ "${svrnodes[@]}" =~ $privIP ]];
17then
18 echo -e "Current node:${privIP} is configured for server consul, not to run client mode.\n"
19 exit
20fi
21
22svr_runing=$(docker ps -a | grep "${contain_cli_name}" | egrep "Up [About]|[0-9]{1,}")
23if [[ ${svr_runing} != "" ]];
24then
25 echo -e "Current container of consul client has been running.\n"
26 exit
27else
28 svr_exists=$(docker ps -a | grep "${contain_cli_name}")
29 if [[ ${svr_exists} != "" ]];
30 then
31 echo -e "Now try to start the container as it stopped...\n"
32 docker start ${svr_exists}
33 sleep 2
34 docker ps -a grep "${contain_cli_name}"
35 exit
36 fi
37fi
38
39echo -e "To start a new container for consul...\n"
40echo -e "To initialize configuration...\n"
41
42nodels=""
43for host in ${svrnodes[*]}
44do
45 if [[ $nodels != "" ]];
46 then
47 nodels=$nodels,
48 fi
49 nodels=$nodels"\"$host\""
50done
51
52config="{\n
53\"retry_join\": [${nodels}],\n
54\"retry_interval\": \"${retry_interval}\",\n
55\"rejoin_after_leave\": true,\n
56\"start_join\": [${nodels}],\n
57\"server\": false,\n
58\"ui\": true,\n
59\"node_name\": \"$HOSTNAME\"\n
60}\n"
61
62echo $config
63echo -e ${config} > ${conf_dir}/client.json
64echo -e ${config}
65
66docker run -d -v ${conf_dir}:${conf_dir} \
67 --name ${contain_cli_name} \
68 --net=host consul:${consul_ver} agent \
69 -config-dir=${conf_dir} \
70 -client=0.0.0.0 \
71 -advertise=${privIP}
72
73sleep 2
74
75svr_runing=$(docker ps -a | grep "${contain_cli_name}" | egrep "Up [About]|[0-9]{1,}")
76if [[ ${svr_runing} == "" ]];
77then
78 echo -e "\nError: docker-consul client node failed to start...\n"
79 exit
80fi
81echo -e "\nOK: docker-consul has started as a client node.\n"