欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Consul初体验

程序员文章站 2022-03-09 11:15:48
Preface Today I'm gonna implement a consul in my environment to discover service of MySQL database and check whether the master-slave is working norma ......
 
preface
 
    today i'm gonna implement a consul in my environment to discover service of mysql database and check whether the master-slave is working normally.
 
introduce
 
    consul is a tool like zookeeper which can discover services we've registered on it.it's just a simple binary command which can run with simple configuration file(json format).
 
official website:
 
framework
 
hostname ip port os version mysql version mysql role consul version consul role
zlm1 192.168.56.100 3306 centos 7.0 5.7.21 master consul v1.2.2 server
zlm2 192.168.56.101 3306 centos 7.0 5.7.21 slave consul v1.2.2 client
 
procedure
 
download and install consul.
 1 [root@zlm1 10:12:06 /vagrant]
 2 #wget https://releases.hashicorp.com/consul/1.2.2/consul_1.2.2_linux_amd64.zip
 3 --2018-08-16 10:02:13--  https://releases.hashicorp.com/consul/1.2.2/consul_1.2.2_linux_amd64.zip
 4 resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.229.183, 2a04:4e42:36::439
 5 connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.229.183|:443... connected.
 6 http request sent, awaiting response... 200 ok
 7 length: 18371676 (18m) [application/zip]
 8 saving to: ‘consul_1.2.2_linux_amd64.zip’
 9 
10 100%[===========================================================================================================>] 18,371,676  38.8kb/s   in 6m 12s 
11 
12 2018-08-16 10:08:29 (48.3 kb/s) - ‘consul_1.2.2_linux_amd64.zip’ saved [18371676/18371676]
13 
14 [root@zlm1 10:14:12 /vagrant/consul_1.2.2_linux_amd64]
15 #ls -l
16 total 82431
17 -rwxrwxrwx 1 vagrant vagrant 84408501 jul 30 18:04 consul //there's only a binary command in the unziped directory of zip file.
18 
19 [root@zlm1 10:26:18 /vagrant/consul_1.2.2_linux_amd64]
20 #mkdir /etc/consul.d
21 
22 [root@zlm1 10:26:36 /vagrant/consul_1.2.2_linux_amd64]
23 #mkdir /data/consul
24 
25 [root@zlm1 10:27:44 /vagrant/consul_1.2.2_linux_amd64]
26 #cp consul ~
27 
28 [root@zlm1 10:29:58 ~]
29 #cd /usr/local/bin
30 
31 [root@zlm1 10:30:02 /usr/local/bin]
32 #ls -l|grep consul
33 lrwxrwxrwx 1 root root      12 aug 16 10:29 consul -> /root/consul
34 
35 [root@zlm1 10:32:08 /usr/local/bin]
36 #consul --help
37 usage: consul [--version] [--help] <command> [<args>]
38 
39 available commands are:
40     agent          runs a consul agent
41     catalog        interact with the catalog
42     connect        interact with consul connect
43     event          fire a new event
44     exec           executes a command on consul nodes
45     force-leave    forces a member of the cluster to enter the "left" state
46     info           provides debugging information for operators.
47     intention      interact with connect service intentions
48     join           tell consul agent to join cluster
49     keygen         generates a new encryption key
50     keyring        manages gossip layer encryption keys
51     kv             interact with the key-value store
52     leave          gracefully leaves the consul cluster and shuts down
53     lock           execute a command holding a lock
54     maint          controls node or service maintenance mode
55     members        lists the members of a consul cluster
56     monitor        stream logs from a consul agent
57     operator       provides cluster-level tools for consul operators
58     reload         triggers the agent to reload configuration files
59     rtt            estimates network round trip time between nodes
60     snapshot       saves, restores and inspects snapshots of consul server state
61     validate       validate config files/directories
62     version        prints the consul version
63     watch          watch for changes in consul

 

start agent of consul by "dev" mode.
 1 [root@zlm1 10:40:43 /usr/local/bin]
 2 #consul agent -dev
 3 ==> starting consul agent...
 4 ==> consul agent running!
 5            version: 'v1.2.2'
 6            node id: '7c839914-8a47-ab36-8920-1a9da54fc6c3'
 7          node name: 'zlm1'
 8         datacenter: 'dc1' (segment: '<all>')
 9             server: true (bootstrap: false)
10        client addr: [127.0.0.1] (http: 8500, https: -1, dns: 8600)
11       cluster addr: 127.0.0.1 (lan: 8301, wan: 8302)
12            encrypt: gossip: false, tls-outgoing: false, tls-incoming: false
13 
14 ==> log data will now stream in as it occurs:
15 
16     2018/08/16 10:47:41 [debug] agent: using random id "7c839914-8a47-ab36-8920-1a9da54fc6c3" as node id
17     2018/08/16 10:47:41 [info] raft: initial configuration (index=1): [{suffrage:voter id:7c839914-8a47-ab36-8920-1a9da54fc6c3 address:127.0.0.1:8300}]
18     2018/08/16 10:47:41 [info] serf: eventmemberjoin: zlm1.dc1 127.0.0.1
19     2018/08/16 10:47:41 [info] serf: eventmemberjoin: zlm1 127.0.0.1
20     2018/08/16 10:47:41 [info] raft: node at 127.0.0.1:8300 [follower] entering follower state (leader: "")
21     2018/08/16 10:47:41 [info] consul: adding lan server zlm1 (addr: tcp/127.0.0.1:8300) (dc: dc1)
22     2018/08/16 10:47:41 [info] consul: handled member-join event for server "zlm1.dc1" in area "wan"
23     2018/08/16 10:47:41 [debug] agent/proxy: managed connect proxy manager started
24     2018/08/16 10:47:41 [warn] agent/proxy: running as root, will not start managed proxies
25     2018/08/16 10:47:41 [info] agent: started dns server 127.0.0.1:8600 (tcp)
26     2018/08/16 10:47:41 [info] agent: started dns server 127.0.0.1:8600 (udp)
27     2018/08/16 10:47:41 [info] agent: started http server on 127.0.0.1:8500 (tcp)
28     2018/08/16 10:47:41 [info] agent: started state syncer
29     2018/08/16 10:47:41 [warn] raft: heartbeat timeout from "" reached, starting election
30     2018/08/16 10:47:41 [info] raft: node at 127.0.0.1:8300 [candidate] entering candidate state in term 2
31     2018/08/16 10:47:41 [debug] raft: votes needed: 1
32     2018/08/16 10:47:41 [debug] raft: vote granted from 7c839914-8a47-ab36-8920-1a9da54fc6c3 in term 2. tally: 1
33     2018/08/16 10:47:41 [info] raft: election won. tally: 1
34     2018/08/16 10:47:41 [info] raft: node at 127.0.0.1:8300 [leader] entering leader state
35     2018/08/16 10:47:41 [info] consul: cluster leadership acquired
36     2018/08/16 10:47:41 [info] consul: new leader elected: zlm1
37     2018/08/16 10:47:41 [info] connect: initialized ca with provider "consul"
38     2018/08/16 10:47:41 [debug] consul: skipping self join check for "zlm1" since the cluster is too small
39     2018/08/16 10:47:41 [info] consul: member 'zlm1' joined, marking health alive
40     2018/08/16 10:47:41 [debug] agent: skipping remote check "serfhealth" since it is managed automatically
41     2018/08/16 10:47:41 [info] agent: synced node info
42     2018/08/16 10:47:44 [debug] agent: skipping remote check "serfhealth" since it is managed automatically
43     2018/08/16 10:47:44 [debug] agent: node info in sync
44     2018/08/16 10:47:44 [debug] agent: node info in sync
45     2018/08/16 10:48:41 [debug] consul: skipping self join check for "zlm1" since the cluster is too small
46     2018/08/16 10:49:13 [debug] agent: skipping remote check "serfhealth" since it is managed automatically
47     2018/08/16 10:49:13 [debug] agent: node info in sync
48     2018/08/16 10:49:41 [debug] manager: rebalanced 1 servers, next active server is zlm1.dc1 (addr: tcp/127.0.0.1:8300) (dc: dc1)
49     2018/08/16 10:49:41 [debug] consul: skipping self join check for "zlm1" since the cluster is too small
50     2018/08/16 10:50:41 [debug] consul: skipping self join check for "zlm1" since the cluster is too small
51     2018/08/16 10:51:06 [debug] agent: skipping remote check "serfhealth" since it is managed automatically
52     2018/08/16 10:51:06 [debug] agent: node info in sync
53     
54 //now the consul cluser has only one node "zlm1".
55 
56 [root@zlm1 11:11:17 ~]
57 #consul members
58 node  address         status  type    build  protocol  dc   segment
59 zlm1  127.0.0.1:8301  alive   server  1.2.2  2         dc1  <all>
60 
61 //type "ctrl+c" can exit consul gracefully.
62 ^c    2018/08/16 11:24:50 [info] agent: caught signal:  interrupt
63     2018/08/16 11:24:50 [info] agent: graceful shutdown disabled. exiting
64     2018/08/16 11:24:50 [info] agent: requesting shutdown
65     2018/08/16 11:24:50 [warn] agent: dev mode disabled persistence, killing all proxies since we can't recover them
66     2018/08/16 11:24:50 [debug] agent/proxy: stopping managed connect proxy manager
67     2018/08/16 11:24:50 [info] consul: shutting down server
68     2018/08/16 11:24:50 [warn] serf: shutdown without a leave
69     2018/08/16 11:24:50 [warn] serf: shutdown without a leave
70     2018/08/16 11:24:50 [info] manager: shutting down
71     2018/08/16 11:24:50 [info] agent: consul server down
72     2018/08/16 11:24:50 [info] agent: shutdown complete
73     2018/08/16 11:24:50 [info] agent: stopping dns server 127.0.0.1:8600 (tcp)
74     2018/08/16 11:24:50 [info] agent: stopping dns server 127.0.0.1:8600 (udp)
75     2018/08/16 11:24:50 [info] agent: stopping http server 127.0.0.1:8500 (tcp)
76     2018/08/16 11:24:50 [info] agent: waiting for endpoints to shut down
77     2018/08/16 11:24:50 [info] agent: endpoints down
78     2018/08/16 11:24:50 [info] agent: exit code: 1
79 
80 [root@zlm1 11:24:50 /usr/local/bin]
81 #ps aux|grep consul
82 root      4245  0.0  0.0 112640   960 pts/0    r+   11:24   0:00 grep --color=auto consul
83 
84 [root@zlm1 11:24:59 /usr/local/bin]
85 #consul members
86 error retrieving members: get http://127.0.0.1:8500/v1/agent/members?segment=_all: dial tcp 127.0.0.1:8500: connect: connection refused

 

prepare the server configuration file and start it again.
 1 [root@zlm1 11:38:35 /etc/consul.d]
 2 #cat server.json 
 3 {
 4     "data_dir":"/data/consul",
 5     "datacenter":"dc1",
 6     "log_level":"info",
 7     "server":true,
 8     "bootstrap_expect":1,
 9     "bind_addr":"192.168.56.100",
10     "client_addr":"192.168.56.100",
11     "ports":{
12     },
13     "ui":true,
14     "retry_join":[],
15     "retry_interval":"3s",
16     "raft_protocol":3,
17     "rejoin_after_leave":true
18 }
19 
20 [root@zlm1 11:38:39 /etc/consul.d]
21 #consul agent --config-dir=/etc/consul.d/ > /data/consul/consul.log 2>&1 &
22 [1] 4320
23 
24 [root@zlm1 11:39:02 /etc/consul.d]
25 #ps aux|grep consul
26 root      4320  1.3  2.0  54388 21252 pts/0    sl   11:39   0:00 consul agent --config-dir=/etc/consul.d/
27 root      4333  0.0  0.0 112640   960 pts/0    r+   11:39   0:00 grep --color=auto consul
28 
29 [root@zlm1 11:39:08 /etc/consul.d]
30 #tail -f /data/consul/consul.log 
31     2018/08/16 11:39:02 [info] agent: started http server on 192.168.56.100:8500 (tcp)
32     2018/08/16 11:39:02 [info] agent: started state syncer
33     2018/08/16 11:39:07 [warn] raft: heartbeat timeout from "" reached, starting election
34     2018/08/16 11:39:07 [info] raft: node at 192.168.56.100:8300 [candidate] entering candidate state in term 2
35     2018/08/16 11:39:07 [info] raft: election won. tally: 1
36     2018/08/16 11:39:07 [info] raft: node at 192.168.56.100:8300 [leader] entering leader state
37     2018/08/16 11:39:07 [info] consul: cluster leadership acquired
38     2018/08/16 11:39:07 [info] consul: new leader elected: zlm1
39     2018/08/16 11:39:07 [info] consul: member 'zlm1' joined, marking health alive
40     2018/08/16 11:39:07 [info] agent: synced node info
41     
42 [root@zlm1 11:51:38 ~]
43 #consul members
44 error retrieving members: get http://127.0.0.1:8500/v1/agent/members?segment=_all: dial tcp 127.0.0.1:8500: connect: connection refused
45 
46 [root@zlm1 11:51:44 ~]
47 #consul members --http-addr=192.168.56.100:8500
48 node  address              status  type    build  protocol  dc   segment
49 zlm1  192.168.56.100:8301  alive   server  1.2.2  2         dc1  <all>

 

add a client on zlm2.
 1 [root@zlm1 13:33:57 /etc/consul.d]
 2 #scp /root/consul zlm2:~
 3 consul                                                                                                             100%   80mb  40.3mb/s   00:02
 4 
 5 [root@zlm2 13:35:33 ~]
 6 #mkdir /etc/consul.d
 7 
 8 [root@zlm2 13:36:37 ~]
 9 #mkdir /data/consul
10 
11 [root@zlm2 13:37:11 ~]
12 #cd /etc/consul.d/
13 
14 [root@zlm2 13:37:46 /etc/consul.d]
15 #ls -l
16 total 4
17 -rw-r--r-- 1 root root 241 aug 16 13:35 client.json
18 
19 [root@zlm2 13:37:47 /etc/consul.d]
20 #cat client.json 
21 {
22     "data_dir": "/data/consul",
23     "enable_script_checks": true,
24     "bind_addr": "192.168.56.101",
25     "retry_join": ["192.168.56.100"],
26     "retry_interval": "30s",
27     "rejoin_after_leave": true,
28     "start_join": ["192.168.56.100"]
29 }
30 
31 [root@zlm2 13:43:42 /etc/consul.d]
32 #consul agent -client 192.168.56.101 -bind 192.168.56.101 --config-dir=/etc/consul.d
33 ==> starting consul agent...
34 ==> joining cluster...
35     join completed. synced with 1 initial agents
36 ==> consul agent running!
37            version: 'v1.2.2'
38            node id: 'a69eae21-4e31-7edf-1f1a-3ec285a8fb3b'
39          node name: 'zlm2'
40         datacenter: 'dc1' (segment: '')
41             server: false (bootstrap: false)
42        client addr: [192.168.56.101] (http: 8500, https: -1, dns: 8600)
43       cluster addr: 192.168.56.101 (lan: 8301, wan: 8302)
44            encrypt: gossip: false, tls-outgoing: false, tls-incoming: false
45 
46 ==> log data will now stream in as it occurs:
47 
48     2018/08/16 13:43:48 [info] serf: eventmemberjoin: zlm2 192.168.56.101
49     2018/08/16 13:43:48 [info] agent: started dns server 192.168.56.101:8600 (udp)
50     2018/08/16 13:43:48 [warn] agent/proxy: running as root, will not start managed proxies
51     2018/08/16 13:43:48 [info] agent: started dns server 192.168.56.101:8600 (tcp)
52     2018/08/16 13:43:48 [info] agent: started http server on 192.168.56.101:8500 (tcp)
53     2018/08/16 13:43:48 [info] agent: (lan) joining: [192.168.56.100]
54     2018/08/16 13:43:48 [info] agent: retry join lan is supported for: aliyun aws azure digitalocean gce os packet scaleway softlayer triton vsphere
55     2018/08/16 13:43:48 [info] agent: joining lan cluster...
56     2018/08/16 13:43:48 [info] agent: (lan) joining: [192.168.56.100]
57     2018/08/16 13:43:48 [info] serf: eventmemberjoin: zlm1 192.168.56.100
58     2018/08/16 13:43:48 [info] agent: (lan) joined: 1 err: <nil>
59     2018/08/16 13:43:48 [info] agent: started state syncer
60     2018/08/16 13:43:48 [info] consul: adding server zlm1 (addr: tcp/192.168.56.100:8300) (dc: dc1)
61     2018/08/16 13:43:48 [info] agent: (lan) joined: 1 err: <nil>
62     2018/08/16 13:43:48 [info] agent: join lan completed. synced with 1 initial agents
63     2018/08/16 13:43:48 [info] agent: synced node info
64     
65 [root@zlm2 13:45:36 /etc/consul.d]
66 #consul members --http-addr=192.168.56.100:8500
67 node  address              status  type    build  protocol  dc   segment
68 zlm1  192.168.56.100:8301  alive   server  1.2.2  2         dc1  <all>
69 zlm2  192.168.56.101:8301  alive   client  1.2.2  2         dc1  <default>

 

add two services on node zlm2.
 1 [root@zlm2 13:58:37 /etc/consul.d]
 2 #cat service_master_check.json
 3 {
 4     "service":
 5        {
 6             "name": "w_db3306",
 7             "tags":  [
 8                           "master satus check"
 9                         ],
10             "address": "192.168.56.100",
11             "port": 3306,
12             "check":    
13                             {
14                                 "args": [
15                                              "/data/consul/script/checkmaster.py",
16                                              "3306"
17                                             ],
18                                 "interval": "15s"
19                             }
20         }
21 }
22 
23 [root@zlm2 13:59:42 /etc/consul.d]
24 #cat service_slave_check.json
25 {
26     "service":
27         {
28             "name": "r_db3306",
29             "tags": [
30                          "slave satus check"
31                         ],
32             "address": "192.168.56.101",
33             "port": 3306,
34             "check":    
35                             {
36                                 "args": [
37                                              "/data/consul/script/checkslave.py",
38                                              "3306"
39                                             ],
40                                 "interval": "15s"
41                             }
42         }
43 }
44 
45 [root@zlm2 14:02:53 /etc/consul.d]
46 #ls -l
47 total 12
48 -rw-r--r-- 1 root root 241 aug 16 13:35 client.json
49 -rw-r--r-- 1 root root 593 aug 16 13:56 service_master_check.json
50 -rw-r--r-- 1 root root 590 aug 16 13:57 servi_slave_check.json

 

check checkmaster.py script.
 1 [root@zlm2 14:05:13 /data/consul/script]
 2 #cat checkmaster.py 
 3 #!/usr/bin/python
 4 import sys
 5 import os
 6 import pymysql
 7 
 8 port = int(sys.argv[1])
 9 var={}
10 conn = pymysql.connect(host='192.168.56.100',port=port,user='zlm',passwd='zlmzlm')
11 #cur = conn.cursor(pymysql.cursor.dictcursor)
12 cur = conn.cursor()
13 
14 cur.execute("show global variables like \"%read_only%\"")
15 rows = cur.fetchall()
16 
17 for r in rows:
18     var[r[0]]=r[1]
19 if var['read_only']=='off' and var['super_read_only']=='off':
20         print "mysql %d master instance." % port
21 else:
22         print "this is read only instance."
23         sys.exit(2)
24 
25 sys.exit(0)
26 cur.close()
27 conn.close()

 

check checkslave.py script.
 1 [root@zlm2 14:07:11 /data/consul/script]
 2 #cat checkslave.py 
 3 #!/usr/bin/python
 4 import sys
 5 import os
 6 import pymysql
 7 
 8 port = int(sys.argv[1])
 9 var={}
10 conn = pymysql.connect(host='192.168.56.101',port=port,user='zlm',passwd='zlmzlm')
11 cur = conn.cursor
12 cur.execute("show global variables like \"%read_only%\"")
13 rows = cur.fetchall()
14 
15 for r in rows:
16     var[r[0]]=r[1]
17 if var['read_only']=='off' and var['super_read_only']=='off':
18         print "mysql %d master instance." % port
19         sys.exit(2)
20 else:
21         print "mysql %d is read only instance." % port
22 
23 cur = conn.cursor(pymysql.cursors.dictcursor)
24 cur.execute("show slave status")
25 slave_status = cur.fetchone()
26 
27 if len(slave_status)<30:
28         print "slave replication setup error.";
29         sys.exit(2)
30 
31 if slave_status['slave_io_running'] !='yes' or slave_status['slave_sql_running'] !='yes':
32         print "replication error: replication from host=%s, port=%s, io_thread=%s, sql_thread=%s, error info %s %s" % (slave_status['master_host'],slave_status['master_port'],slave_status['slave_io_running'],slave_status['slave_sql_running'],slave_status['last_io_error'],slave_status['last_sql_error'])
33         sys.exit(1)
34 print slave_status
35 
36 sys.exit(0)
37 cur.close()
38 conn.close()

 

restartt consul(or reload).
 1 [root@zlm2 14:12:37 /etc/consul.d]
 2 #consul members --http-addr=192.168.56.100:8500
 3 node  address              status  type    build  protocol  dc   segment
 4 zlm1  192.168.56.100:8301  alive   server  1.2.2  2         dc1  <all>
 5 zlm2  192.168.56.101:8301  alive   client  1.2.2  2         dc1  <default>
 6 
 7 [root@zlm2 14:13:27 /etc/consul.d]
 8 #consul leave --http-addr=192.168.56.101:8500
 9 graceful leave complete
10 
11 [root@zlm2 14:13:41 /etc/consul.d]
12 #consul members --http-addr=192.168.56.100:8500
13 node  address              status  type    build  protocol  dc   segment
14 zlm1  192.168.56.100:8301  alive   server  1.2.2  2         dc1  <all>
15 zlm2  192.168.56.101:8301  left    client  1.2.2  2         dc1  <default>
16 
17 [root@zlm2 14:18:44 /etc/consul.d]
18 #consul agent --config-dir=/etc/consul.d/ -client 192.168.56.101 > /data/consul/consul.log 2>&1 &
19 [1] 5013
20 
21 [root@zlm2 14:19:59 /etc/consul.d]
22 #!ps
23 ps aux|grep consul
24 root      5013  2.3  2.0  56440 21132 pts/2    sl   14:19   0:00 consul agent --config-dir=/etc/consul.d/ -client 192.168.56.101
25 root      5026  0.0  0.0 112640   960 pts/2    r+   14:19   0:00 grep --color=auto consul
26 
27 [root@zlm2 14:20:49 /etc/consul.d]
28 #consul reload -http-addr=192.168.56.101:8500
29 configuration reload triggered

 

well,now we can login the dashbord(gui) of consul to check mysql working status.
 
Consul初体验Consul初体验Consul初体验
 Consul初体验
 
Consul初体验