-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul-standard.sh
342 lines (257 loc) · 14.3 KB
/
consul-standard.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
function addConsulDnsDockerOpts()
{
printf "***** Adding Consul DNS DOCKER_OPTS if needed\n"
local toBeAppended="DOCKER_OPTS='--dns $(_dockerBridgeIp) --dns 8.8.8.8 --dns-search service.consul dns-search node.consul'"
if [ ! -f /etc/default/docker ] || [ "$(grep -c DOCKER_OPTS /etc/default/docker)" -eq "0" ]; then
echo $toBeAppended | sudo tee --append /etc/default/docker > /dev/null
sudo service docker restart
fi
}
function startRedis()
{
printf "***** Starting Redis\n"
local container=''
container='beredissrv1' && docker run --name $container -d redis && _printContainerNameAndIp $container
container='beredissrv2' && docker run --name $container -d redis && _printContainerNameAndIp $container
container='feredissrv1' && docker run --name $container -d redis && _printContainerNameAndIp $container
container='feredissrv2' && docker run --name $container -d redis && _printContainerNameAndIp $container
}
function pushItemsToRedis()
{
printf "***** Pushing items to Redis\n"
docker run -it --link beredissrv1:redis --rm redis redis-cli -h redis -p 6379 lpush myBeList1 BeL1First > /dev/null && \
printf "Server beredissrv1 - Key myBeList1 items:\n" && \
docker run -it --link beredissrv1:redis --rm redis redis-cli -h redis -p 6379 lrange myBeList1 0 -1
docker run -it --link beredissrv2:redis --rm redis redis-cli -h redis -p 6379 lpush myBeList2 BeL2First > /dev/null && \
printf "\nServer beredissrv2 - Key myBeList2 items:\n" && \
docker run -it --link beredissrv2:redis --rm redis redis-cli -h redis -p 6379 lrange myBeList2 0 -1
docker run -it --link feredissrv1:redis --rm redis redis-cli -h redis -p 6379 lpush myFeList1 FeL1First > /dev/null && \
printf "\nServer feredissrv1 - Key myFeList1 items:\n" && \
docker run -it --link feredissrv1:redis --rm redis redis-cli -h redis -p 6379 lrange myFeList1 0 -1
docker run -it --link feredissrv2:redis --rm redis redis-cli -h redis -p 6379 lpush myFeList2 FeL2First > /dev/null && \
printf "\nServer feredissrv2 - Key myFeList2 items:\n" && \
docker run -it --link feredissrv2:redis --rm redis redis-cli -h redis -p 6379 lrange myFeList2 0 -1
}
function startConsulBootstrapAgents()
{
printf "***** Starting consul bootsrap agents\n"
local agent=''
agent='be1srv1' && docker run -p 8411:8400 -p 8511:8500 -p 8611:8600/udp -p $(_dockerBridgeIp):53:8600/udp --name $agent -h $agent -d gliderlabs/consul-server -bootstrap-expect 3 -dc "backend" && _printContainerNameAndIp $agent
agent='fe1srv1' && docker run -p 8421:8400 -p 8521:8500 -p 8621:8600/udp --name $agent -h $agent -d gliderlabs/consul-server -bootstrap-expect 3 -dc "frontend" && _printContainerNameAndIp $agent
hostIp=$(_usedInterfaceIp)
printf "BE Web UI URL is http://$hostIp:8511/ui and FE Web UI URL is http://$hostIp:8521/ui\n"
}
function startConsulMonitor()
{
printf "***** Starting consul monitor\n"
# hostname -i
local ip=$1
# 8411 or 8421
local port=$2
consul monitor -log-level "info" -rpc-addr "$ip:$port"
}
function startConsulNonBootstrapAgents()
{
printf "***** Starting consul non bootsrap agents\n"
local agent=''
local beJoinIp="$(_containerIp be1srv1)"
agent='be2srv2' && docker run -d -p 8412:8400 -p 8512:8500 -p 8612:8600/udp --name $agent -h $agent gliderlabs/consul-server -dc "backend" -join $beJoinIp -join-wan $beJoinIp && _printContainerNameAndIp $agent
agent='be3srv3' && docker run -d -p 8413:8400 -p 8513:8500 -p 8613:8600/udp --name $agent -h $agent gliderlabs/consul-server -dc "backend" -join $beJoinIp -join-wan $beJoinIp && _printContainerNameAndIp $agent
agent='be4cli1' && docker run -d -p 8414:8400 -p 8514:8500 -p 8614:8600/udp --name $agent -h $agent gliderlabs/consul-agent -dc "backend" -join $beJoinIp && _printContainerNameAndIp $agent
agent='be5cli2' && docker run -d -p 8415:8400 -p 8515:8500 -p 8615:8600/udp --name $agent -h $agent gliderlabs/consul-agent -dc "backend" -join $beJoinIp && _printContainerNameAndIp $agent
local feJoinIp="$(_containerIp fe1srv1)"
agent='fe2srv2' && docker run -d -p 8422:8400 -p 8522:8500 -p 8622:8600/udp --name $agent -h $agent gliderlabs/consul-server -dc "frontend" -join $feJoinIp -join-wan $feJoinIp && _printContainerNameAndIp $agent
agent='fe3srv3' && docker run -d -p 8423:8400 -p 8523:8500 -p 8623:8600/udp --name $agent -h $agent gliderlabs/consul-server -dc "frontend" -join $feJoinIp -join-wan $feJoinIp && _printContainerNameAndIp $agent
agent='fe4cli1' && docker run -d -p 8424:8400 -p 8524:8500 -p 8624:8600/udp --name $agent -h $agent gliderlabs/consul-agent -dc "frontend" -join $feJoinIp && _printContainerNameAndIp $agent
agent='fe5cli2' && docker run -d -p 8425:8400 -p 8525:8500 -p 8625:8600/udp --name $agent -h $agent gliderlabs/consul-agent -dc "frontend" -join $feJoinIp && _printContainerNameAndIp $agent
}
function joinDatacenters()
{
printf "***** Joining data centers\n"
printf "Data centers before join (look also at Web UIs):\n$(curl --silent 'http://localhost:8515/v1/catalog/datacenters')\n"
local beJoinIp="$(_containerIp be1srv1)"
docker exec fe1srv1 /bin/consul join -wan $beJoinIp
sleep 5
printf "Data centers after join (look also at Web UIs):\n$(curl --silent 'http://localhost:8515/v1/catalog/datacenters')\n"
}
function showCatalogInfo()
{
printf "***** Showing catalog info\n"
printf "catalog/nodes\n"
curl --silent 'http://localhost:8515/v1/catalog/nodes' | jq
printf "catalog/nodes?dc=frontend&near=fe4cli1\n"
curl --silent 'http://localhost:8515/v1/catalog/nodes?dc=frontend&near=fe4cli1' | jq
printf "catalog/node/be1srv1\n"
curl --silent 'http://localhost:8515/v1/catalog/node/be1srv1' | jq
printf "catalog/services\n"
curl --silent 'http://localhost:8515/v1/catalog/services' | jq
printf "catalog/service/consul\n"
curl --silent 'http://localhost:8515/v1/catalog/service/consul' | jq
}
function showCoordinateInfo()
{
printf "***** Showing coordinate info\n"
printf "coordinate/datacenters\n"
curl --silent 'http://localhost:8515/v1/coordinate/datacenters' | jq
printf "coordinate/nodes\n"
curl --silent 'http://localhost:8515/v1/coordinate/nodes' | jq
}
function showAgentConfiguration()
{
printf "***** Showing local agent configuration\n"
# 8515
local port=$1
curl --silent "http://localhost:$port/v1/agent/self" | jq
}
function _showAgentServicesAndChecks()
{
local port=$1
printf "agent/services\n"
curl --silent "http://localhost:$port/v1/agent/services" | jq
printf "agent/checks\n"
curl --silent "http://localhost:$port/v1/agent/checks" | jq
}
function _removeAgentService()
{
# 8515
local port=$1
local serviceId=$2
curl --silent "http://localhost:$port/v1/agent/service/deregister/$serviceId"
printf "Removed service with ID $serviceId\n"
}
function _removeAgentCheck()
{
# 8515
local port=$1
local checkId=$2
curl --silent "http://localhost:$port/v1/agent/check/deregister/$checkId"
printf "Removed check with ID $checkId\n"
}
function addAgentServiceAndCheckSeparately()
{
printf "***** Adding agent service and check separately\n"
local beredissrv1Ip="$(_containerIp beredissrv1)"
curl --silent -X PUT --data '{ "ID": "berdssrv1", "Name": "be-redis", "Tags": [ "official", "nosql" ], "Address": "'$beredissrv1Ip'", "Port": 6379 }' 'http://localhost:8514/v1/agent/service/register'
printf "Added service { ID: \"berdssrv1\", Name: \"be-redis\" }\n"
_showAgentServicesAndChecks 8514
curl --silent -X PUT --data '{ "ServiceID": "berdssrv1", "ID": "beredissrv1UpAndRunning", "Name": "BE Redis 1 up and running", "TCP": "'$beredissrv1Ip':6379", "interval": "20s", "timeout": "2s" }' 'http://localhost:8514/v1/agent/check/register'
printf "Added check for service with ID berdssrv1 { ID: \"beredissrv1UpAndRunning\", Name: \"BE Redis 1 up and running\" }\n"
_showAgentServicesAndChecks 8514
}
function addAgentServiceAndCheckAtOnce()
{
printf "***** Adding agent service and check at once\n"
local beredissrv2Ip="$(_containerIp beredissrv2)"
curl --silent -X PUT --data '{ "ID": "berdssrv2", "Name": "be-redis", "Tags": [ "backup", "nosql" ], "Address": "$beredissrv2Ip", "Port": 6379, "Check": { "ID": "beredissrv2UpAndRunning", "Name": "BE Redis 2 up and running", "TCP": "'$beredissrv2Ip':6379", "interval": "15s", "timeout": "2s" } }' 'http://localhost:8515/v1/agent/service/register'
printf "Added service { ID: \"berdssrv2\", Name: \"be-redis\" } and check { ID: \"service:berdssrv2\", Name: \"Service 'be-redis' check\" }\n"
_showAgentServicesAndChecks 8515
_removeAgentService 8515 'berdssrv2'
_showAgentServicesAndChecks 8515
curl --silent -X PUT --data '{ "ID": "berdssrv2", "Name": "be-redis", "Tags": [ "backup", "nosql" ], "Address": "'$beredissrv2Ip'", "Port": 6379, "Check": { "ID": "beredissrv2UpAndRunning", "Name": "BE Redis 2 up and running", "TCP": "'$beredissrv2Ip':6379", "interval": "5s", "timeout": "2s" } }' 'http://localhost:8515/v1/agent/service/register'
printf "Readded service { ID: \"berdssrv2\", Name: \"be-redis\" } and check { ID: \"service:berdssrv2\", Name: \"Service 'be-redis' check\" }\n"
_showAgentServicesAndChecks 8515
_removeAgentCheck 8515 'service:berdssrv2'
_showAgentServicesAndChecks 8515
_removeAgentService 8515 'berdssrv2'
_showAgentServicesAndChecks 8515
curl --silent -X PUT --data '{ "ID": "berdssrv2", "Name": "be-redis", "Tags": [ "backup", "nosql" ], "Address": "'$beredissrv2Ip'", "Port": 6379, "Check": { "ID": "beredissrv2UpAndRunning", "Name": "BE Redis 2 up and running", "TCP": "'$beredissrv2Ip':6379", "interval": "15s", "timeout": "2s" } }' 'http://localhost:8515/v1/agent/service/register'
printf "Restored service { ID: \"berdssrv2\", Name: \"be-redis\" } and check { ID: \"service:berdssrv2\", Name: \"Service 'be-redis' check\" }\n"
}
function _showPassingAndCriticalChecks()
{
printf "health/state/passing\n"
curl --silent 'http://localhost:8511/v1/health/state/passing' | jq '.[] | select(.CheckID!="serfHealth")'
printf "health/state/critical\n"
curl --silent 'http://localhost:8511/v1/health/state/critical' | jq
}
function showNodeCheckServiceHealthInfo()
{
printf "***** Showing nodes, check and service health info\n"
printf "health/node/be5cli2\n"
curl --silent 'http://localhost:8511/v1/health/node/be5cli2' | jq '.[] | select(.CheckID!="serfHealth")'
printf "health/checks/be-redis (service name instead of ID)\n"
curl --silent 'http://localhost:8511/v1/health/checks/be-redis' | jq
printf "health/service/be-redis (service name instead of ID)\n"
curl --silent 'http://localhost:8511/v1/health/service/be-redis' | jq
}
function showCheckStateHealthInfo()
{
printf "***** Showing check state health info\n"
_showPassingAndCriticalChecks
printf "Stopping beredissrv2 and waiting before showing check status (look also at Web UIs)\n"
docker stop beredissrv2
sleep 15
_showPassingAndCriticalChecks
printf "Starting beredissrv2 and waiting before showing check status (look also at Web UIs)\n"
docker start beredissrv2
sleep 15
_showPassingAndCriticalChecks
}
function queryDns()
{
printf "***** Querying DNS\n"
printf "catalog/nodes\n"
curl --silent 'http://localhost:8515/v1/catalog/nodes' | jq
printf "catalog/services\n"
curl --silent 'http://localhost:8515/v1/catalog/services' | jq
printf "\n\n\n\n\nAll be-redis services\n"
dig @127.0.0.1 -p 8611 be-redis.service.consul
printf "\n\n\n\n\nFilter services by tag\n"
dig @127.0.0.1 -p 8611 official.be-redis.service.consul
printf "\n\n\n\n\nStopping beredissrv1\n"
docker stop beredissrv1 > /dev/null
sleep 20
printf "Detailed service info: DNS resolution is disabled\n"
dig @127.0.0.1 -p 8611 official.be-redis.service.consul SRV
printf "\n\n\n\n\nStarting beredissrv1\n"
docker start beredissrv1 > /dev/null
sleep 20
printf "Detailed service info: DNS resolution is enabled\n"
dig @127.0.0.1 -p 8611 official.be-redis.service.consul SRV
}
function manipulateKeyValueEntries()
{
printf "***** Manipulating key value entries\n"
printf "kv/?recurse\n"
curl --silent 'http://localhost:8515/v1/kv/?recurse'
printf "Putting kv/category/application/sampleKey with value ABC\n"
echo "$(curl --silent -X PUT --data 'ABC' 'http://localhost:8515/v1/kv/category/application/sampleKey')"
printf "Retrieving kv/category/application/sampleKey\n"
curl --silent 'http://localhost:8515/v1/kv/category/application/sampleKey' | jq
printf "\n\nDeleting kv/category/application/sampleKey\n"
echo "$(curl --silent -X DELETE 'http://localhost:8515/v1/kv/category/application/sampleKey')"
printf "Retrieving kv/category/application/sampleKey\n"
curl --silent 'http://localhost:8515/v1/kv/category/application/sampleKey' | jq
printf "\n\nPutting kv/category/application/retryInterval\n"
echo "$(curl --silent -X PUT --data '5000' 'http://localhost:8515/v1/kv/category/application/retryInterval')"
printf "Putting kv/category/application/dbHost\n"
echo "$(curl --silent -X PUT --data 'ìù@gò' 'http://localhost:8515/v1/kv/category/application/dbHost')"
printf "Deleting kv/category/application\n"
echo "$(curl --silent -X DELETE 'http://localhost:8515/v1/kv/category/application')"
printf "Retrieving all keys with decoded values (look also at Web UIs)\n"
encodedkv=$(curl --silent 'http://localhost:8515/v1/kv/?recurse') && \
decodedkv=$encodedkv && \
while read encoded; do decoded=$(printf "$encoded" | base64 -di); decodedkv=${decodedkv//$encoded/$decoded}; done < <(printf $encodedkv | jq '.[] | .Value' | sed 's@"@@'g) && \
printf $decodedkv | jq
}
function _showLeaderAndPeers()
{
printf "status/leader\n"
echo "$(curl --silent 'http://localhost:8515/v1/status/leader')"
printf "status/peers\n"
echo "$(curl --silent 'http://localhost:8515/v1/status/peers')"
}
function showStatusInfo()
{
printf "***** Showing status info\n"
_showLeaderAndPeers
printf "Sending SIGINT to be2srv2 (look at the monitor window)\n"
docker exec be2srv2 kill -INT 1
sleep 15
_showLeaderAndPeers
printf "Turning be2srv2 back on (look at the monitor window)\n"
docker start be2srv2
sleep 15
_showLeaderAndPeers
}