html
master | node1 | node2 |
---|---|---|
192.168.1.1 | 192.168.1.2 |
#獲取tar包 [root@master ~]# wget https://releases.hashicorp.com/consul/1.8.4/consul_1.8.4_linux_amd64.zip #解包 [root@master ~]# unzip consul_1.5.1_linux_amd64.zip Archive: consul_1.5.1_linux_amd64.zip inflating: consul #移動到/usr/local/bin目錄是給用戶放置本身的可執行程序的地方,推薦放在這裏,不會被系統升級而覆蓋同名文件。 [root@master ~]# mv consul /usr/local/bin/ [root@master ~]# chmod +x /usr/local/bin/consul #啓動conusl服務,在後臺運行 [root@master ~]# nohup consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.1.1 -client=0.0.0.0 -node=master &
命令解釋:node
nohup:要運行後臺中的 nohup 命令 linux
-bootstrap:通常運行在master節點,自動選舉爲leader(領袖)nginx
-ui:開啓consul內部的web界面web
-data-dir:數據存儲的位置docker
-bind:制定服務的server IPbootstrap
-client:制定訪問的客戶端vim
-node:節點的名稱,主機名後端
開啓的端口:瀏覽器
8300 集羣的節點
8301 集羣內部的訪問
8302 誇數據中心的通訊
8500 web ui 界面
8600 使用dns協議查看節點信息的端口
1 //查看conusl信息 2 [root@master ~]# consul info 3 agent: 4 check_monitors = 0 5 check_ttls = 0 6 checks = 0 7 services = 0 8 build: 9 prerelease = 10 revision = 40cec984 11 version = 1.5.1 12 consul: 13 acl = disabled 14 bootstrap = true 15 known_datacenters = 1 16 leader = true 17 leader_addr = 192.168.1.1:8300 #consul節點IP:端口 18 server = true 19 raft: 20 applied_index = 60 21 commit_index = 60 22 fsm_pending = 0 23 last_contact = 0 24 last_log_index = 60 25 last_log_term = 2 26 last_snapshot_index = 0 27 last_snapshot_term = 0 28 latest_configuration = [{Suffrage:Voter ID:e2448f78-f220-e848-4316-128872e93ea1 Address:192.168.1.1:8300}] 29 latest_configuration_index = 1 30 num_peers = 0 31 protocol_version = 3 32 protocol_version_max = 3 33 protocol_version_min = 0 34 snapshot_version_max = 1 35 snapshot_version_min = 0 36 state = Leader 37 term = 2 38 runtime: 39 arch = amd64 40 cpu_count = 4 41 goroutines = 81 42 max_procs = 4 43 os = linux 44 version = go1.12.1 45 serf_lan: 46 coordinate_resets = 0 47 encrypted = false 48 event_queue = 1 49 event_time = 2 50 failed = 0 51 health_score = 0 52 intent_queue = 0 53 left = 0 54 member_time = 1 55 members = 1 56 query_queue = 0 57 query_time = 1 58 serf_wan: 59 coordinate_resets = 0 60 encrypted = false 61 event_queue = 0 62 event_time = 1 63 failed = 0 64 health_score = 0 65 intent_queue = 0 66 left = 0 67 member_time = 1 68 members = 1 69 query_queue = 0 70 query_time = 1
1 [root@master ~]# consul members 2 Node Address Status Type Build Protocol DC Segment 3 master 192.168.1.1:8301 alive server 1.5.1 2 dc1 <all>
這裏咱們採用容器的方式去運行consul服務。
#這裏名稱必定要注意這是個坑,爲啥寫2,其實着有含義的1表明單機、2表明羣集
#節點node1 [root@node1 ~]# docker run -d --name consu2 -p 8301:8301 -p 8301:8301/udp -p 8500:8500 -p 8600:8600 -p 8600:8600/udp --restart always progrium/consul:latest -join 192.168.1.1 -advertise 192.168.1.2 -client 0.0.0.0 --node=node1 #節點node2 [root@node2 ~]# docker run -d --name consu2 -p 8301:8301 -p 8301:8301/udp -p 8500:8500 -p 8600:8600 -p 8600:8600/udp --restart always progrium/consul:latest -join 192.168.1.1 -advertise 192.168.1.4 -client 0.0.0.0 --node=node2
#可能會遇到的問題,ipv4流量轉發未開啓,開啓便可!
[root@node1 ~]# docker run -d --name consul2 -p 8301:8301 -p 8301:8301/udp -p 8500:8500 -p 8600:8600 -p 8600:8600/udp --restart always progrium/consul:latest -join 192.168.1.1 -advertise 192.168.1.2 -client 0.0.0.0 --node=node1 WARNING: IPv4 forwarding is disabled. Networking will not work. bd6660df07110e0e524112f931a65d17a54eb29e73cbe47e017bf012e662e07e [root@node1 ~]# echo net.ipv4.ip_forward=1 > /etc/sysctl.conf [root@node1 ~]# systemctl restart network [root@node1 ~]# systemctl daemon-reload [root@node1 ~]# systemctl restart docker [root@node1 ~]# docker restart consul2 consul2
訪問地址:IP:8500
registrator是一個能自動發現docker container提供的服務,並在後端 服務註冊中心註冊服務或取消服務的工具,後端註冊中心支持conusl、 etcd、skydns二、zookeeper等。
#在node1節點執行 docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.2:8500 #在node2節點執行 docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.4:8500 #注意IP地址爲執行的宿主機IP
//依賴環境 # yum -y install gcc openssl openssl-devel zlib zlib-devel pcre pcre-devel # useradd -M -s /sbin/nologin nginx # tar -zxf nginx-1.14.0.tar.gz # ./configure --user=nginx --group=nginx \--with-http_stub_status_module --with-http_realip_module --with-pcre --with-http_ssl_module # make && make install # ln -s /usr/local/nginx/sbin/* /usr/local/bin/ # nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful # nginx ------------------------------------------------------------------------ #PS: 這裏nginx做爲反向代理,代理後端docker0二、docker03上nginx的 容器服務,因此咱們先去docker0二、docker03上部署一些服務,爲了方便等會看到負載的效果,因此,咱們運行完成容器以後,作一個主界面內容的區分。
nginx做爲反向代理,代理後端node一、node2上nginx 的 容器服務,因此咱們先去node一、node2上部署一些服務,爲 了方便等會看到負載的效果,因此,咱們運行完成容器以後,作一個主 界面內容的區分。
node1:web一、web2
node2:web三、web4
1 #建立nginx容器-p 隨意映射容器內的80端口到宿主機 2 [root@node1 ~]# docker run -itd --name web1 -p 80 nginx:latest 3 77d39af9c5c8bcaa1839b9002bd09ac093f1e0699cef5bd04515ae38b1ceace8 4 [root@node1 ~]# docker run -itd --name web2 -p 80 nginx:latest 5 98ad0a4a5244cbb812ee1c5d2cee4818edc360d6eaea25c70c88f07e88d04a68 6 #修改容器的nginx首頁 7 [root@node1 ~]# docker exec -it web1 sh 8 # echo web1 > /usr/share/nginx/html/index.html 9 # curl 127.0.0.1 10 web1 11 [root@node1 ~]# docker exec -it web2 sh 12 # echo web2 > /usr/share/nginx/html/index.html 13 # curl 127.0.0.1 14 web2
1 #建立容器 2 [root@node2 ~]# docker run -itd --name web3 -p 80 nginx:latest 3 29e0495b3a9c1db99f33374115d626fa37217ab67313e1e20845ccf2dd7ccb82 4 [root@node2 ~]# docker run -itd --name web4 -p 80 nginx:latest 5 2b2fcd9bd3742b9f806d1804a59a61c1f320bd616e66d5d216d6b2c85231fc20 6 #修改首頁 7 [root@node2 ~]# docker exec -it web3 sh 8 # echo web3 > /usr/share/nginx/html/index.html 9 # curl 127.0.0.1 10 web3 11 # 12 [root@node2 ~]# docker exec -it web4 sh 13 # echo web4 > /usr/share/nginx/html/index.html 14 # curl 127.0.0.1 15 web4
vim /usr/local/nginx/consul/nginx.ctmpl upstream http_backend { {{range service "nginx"}} server {{ .Address }}:{{ .Port }}; {{ end }} } server { listen 8000; server_name localhost; location / { proxy_pass http://http_backend; } }
1 [root@master ~]# vim /usr/local/nginx/conf/nginx.conf 2 116 include /usr/local/nginx/consul/*.conf; 3 [root@master ~]# nginx -t 4 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 5 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#重啓 nginx -s reload
Consul-Template簡介
Consul-Template是基於Consul的自動替換配置文件的應用。在Consul-Template沒出現以前,你們構建服務發現系統大多采用的是Zookeeper、Etcd+Confd這樣相似的系統。
Consul官方推出了本身的模板系統Consul-Template後,動態的配置系統能夠分化爲Etcd+Confd和Consul+Consul-Template兩大陣營。Consul-Template的定位和Confd差很少,Confd的後端能夠是Etcd或者Consul。
[root@master soft]# unzip consul-template_0.19.5_linux_amd64.zip Archive: consul-template_0.19.5_linux_amd64.zip inflating: consul-template [root@master soft]# mv consul-template /usr/local/bin/ [root@master soft]# chmod +x /usr/local/bin/consul-template [root@master soft]# nohup consul-template -consul-addr 192.168.1.1:8500 -template "/usr/local/nginx/consul/nginx.ctmpl:/usr/local/nginx/consul/vhost.conf:/usr/local/bin/nginx -s reload" & [2] 5729 [root@master soft]# nohup: 忽略輸入並把輸出追加到"nohup.out" [root@master soft]# cat /usr/local/nginx/consul/vhost.conf upstream http_backend { server 192.168.1.2:32768; server 192.168.1.2:32769; server 192.168.1.4:32768; server 192.168.1.4:32769; } server { listen 8000; server_name localhost; location / { proxy_pass http://http_backend; } }
固然,這時無論後端是新添加nginx的web容器,或是刪除,新生產的配置文件都會時時的更新,這時咱們在運行consul-template這條命令最後添加: /usr/local/bin/nginx -s reload 它的做用。
1.瀏覽器訪問consul ui ip:8500後報如下內容
Consul returned an error. You may have visited a URL that is loading an unknown resource, so you can try going back to the root or try re-submitting your ACL Token/SecretID by going back to ACLs. Try looking in our
#訪問,實現了負載均衡 [root@master soft]# curl localhost:8000 web1 [root@master soft]# curl localhost:8000 web2 [root@master soft]# curl localhost:8000 web3 [root@master soft]# curl localhost:8000 web4 [root@master soft]# curl localhost:8000 web1 [root@master soft]# curl localhost:8000 web2 [root@master soft]# curl localhost:8000 web3 [root@master soft]# curl localhost:8000 web4 #中止node1主機的容器,查看實時發現效果 [root@node1 sfot]# docker stop web1 ^[[Aweb1 [root@node1 sfot]# docker stop web2 web2 #在master訪問 [root@master soft]# curl localhost:8000 web3 [root@master soft]# curl localhost:8000 web4 [root@master soft]# curl localhost:8000 web3 [root@master soft]# curl localhost:8000 web4