经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Docker » 查看文章
docker09-实现服务发现(docker+consul+ registrator)
来源:cnblogs  作者:林奋斗的成长之路  时间:2021/3/8 11:30:46  对本文有异议

实现环境

masternode1node2
192.168.1.1 192.168.1.2 192.168.1.4
  1. #获取tar包
  2. [root@master ~]# wget https://releases.hashicorp.com/consul/1.8.4/consul_1.8.4_linux_amd64.zip
  3. #解包
  4. [root@master ~]# unzip consul_1.5.1_linux_amd64.zip
  5. Archive: consul_1.5.1_linux_amd64.zip
  6. inflating: consul
  7. #移动到/usr/local/bin目录是给用户放置自己的可执行程序的地方,推荐放在这里,不会被系统升级而覆盖同名文件。
  8. [root@master ~]# mv consul /usr/local/bin/
  9. [root@master ~]# chmod +x /usr/local/bin/consul
  10. #启动conusl服务,在后台运行
  11. [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 &

命令解释:

nohup:要运行后台中的 nohup 命令 

-bootstrap:一般运行在master节点,自动选举为leader(领袖)

-ui:开启consul内部的web界面

-data-dir:数据存储的位置

-bind:制定服务的server IP

-client:制定访问的客户端

-node:节点的名称,主机名

 

开启的端口:

8300 集群的节点

8301 集群内部的访问

8302 夸数据中心的通信

8500 web ui 界面

8600 使用dns协议查看节点信息的端口

  1. 1 //查看conusl信息
  2. 2 [root@master ~]# consul info
  3. 3 agent:
  4. 4 check_monitors = 0
  5. 5 check_ttls = 0
  6. 6 checks = 0
  7. 7 services = 0
  8. 8 build:
  9. 9 prerelease =
  10. 10 revision = 40cec984
  11. 11 version = 1.5.1
  12. 12 consul:
  13. 13 acl = disabled
  14. 14 bootstrap = true
  15. 15 known_datacenters = 1
  16. 16 leader = true
  17. 17 leader_addr = 192.168.1.1:8300 #consul节点IP:端口
  18. 18 server = true
  19. 19 raft:
  20. 20 applied_index = 60
  21. 21 commit_index = 60
  22. 22 fsm_pending = 0
  23. 23 last_contact = 0
  24. 24 last_log_index = 60
  25. 25 last_log_term = 2
  26. 26 last_snapshot_index = 0
  27. 27 last_snapshot_term = 0
  28. 28 latest_configuration = [{Suffrage:Voter ID:e2448f78-f220-e848-4316-128872e93ea1 Address:192.168.1.1:8300}]
  29. 29 latest_configuration_index = 1
  30. 30 num_peers = 0
  31. 31 protocol_version = 3
  32. 32 protocol_version_max = 3
  33. 33 protocol_version_min = 0
  34. 34 snapshot_version_max = 1
  35. 35 snapshot_version_min = 0
  36. 36 state = Leader
  37. 37 term = 2
  38. 38 runtime:
  39. 39 arch = amd64
  40. 40 cpu_count = 4
  41. 41 goroutines = 81
  42. 42 max_procs = 4
  43. 43 os = linux
  44. 44 version = go1.12.1
  45. 45 serf_lan:
  46. 46 coordinate_resets = 0
  47. 47 encrypted = false
  48. 48 event_queue = 1
  49. 49 event_time = 2
  50. 50 failed = 0
  51. 51 health_score = 0
  52. 52 intent_queue = 0
  53. 53 left = 0
  54. 54 member_time = 1
  55. 55 members = 1
  56. 56 query_queue = 0
  57. 57 query_time = 1
  58. 58 serf_wan:
  59. 59 coordinate_resets = 0
  60. 60 encrypted = false
  61. 61 event_queue = 0
  62. 62 event_time = 1
  63. 63 failed = 0
  64. 64 health_score = 0
  65. 65 intent_queue = 0
  66. 66 left = 0
  67. 67 member_time = 1
  68. 68 members = 1
  69. 69 query_queue = 0
  70. 70 query_time = 1
//查看conusl信息
  1. 1 [root@master ~]# consul members
  2. 2 Node Address Status Type Build Protocol DC Segment
  3. 3 master 192.168.1.1:8301 alive server 1.5.1 2 dc1 <all>
#群集节点的信息

将node1、node2加入到conusl集群

这里我们采用容器的方式去运行consul服务。

  1. #这里名称一定要注意这是个坑,为啥写2,其实着有含义的1代表单机、2代表群集
    #节点node1
  2. [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
  3. #节点node2
  4. [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
  1. #可能会遇到的问题,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
  2. WARNING: IPv4 forwarding is disabled. Networking will not work.
  3. bd6660df07110e0e524112f931a65d17a54eb29e73cbe47e017bf012e662e07e
  4. [root@node1 ~]# echo net.ipv4.ip_forward=1 > /etc/sysctl.conf
  5. [root@node1 ~]# systemctl restart network
  6. [root@node1 ~]# systemctl daemon-reload
  7. [root@node1 ~]# systemctl restart docker
  8. [root@node1 ~]# docker restart consul2
  9. consul2

 访问地址:IP:8500

 

 

 

docker02、docker03上部署registrator服务

registrator是一个能自动发现docker container提供的服务,并在后端 服务注册中心注册服务或取消服务的工具,后端注册中心支持conusl、 etcd、skydns2、zookeeper等。

  1. #在node1节点执行
  2. docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.2:8500
  3. #在node2节点执行
  4. docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.4:8500
  5. #注意IP地址为执行的宿主机IP

master部署一个nginx服务  

  1. //依赖环境
  2. # yum -y install gcc openssl openssl-devel zlib zlib-devel pcre pcre-devel
  3. # useradd -M -s /sbin/nologin nginx
  4. # tar -zxf nginx-1.14.0.tar.gz
  5. # ./configure --user=nginx --group=nginx \--with-http_stub_status_module --with-http_realip_module --with-pcre --with-http_ssl_module
  6. # make && make install
  7. # ln -s /usr/local/nginx/sbin/* /usr/local/bin/
  8. # nginx -t
  9. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  10. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  11. # nginx
  12. ------------------------------------------------------------------------
  13. #PS: 这里nginx作为反向代理,代理后端docker02、docker03上nginx的 容器服务,所以我们先去docker02、docker03上部署一些服务,为了方便等会看到负载的效果,所以,我们运行完成容器之后,做一个主界面内容的区分。
源码编译nginx

nginx作为反向代理,代理后端node1、node2上nginx 的 容器服务,所以我们先去node1、node2上部署一些服务,为 了方便等会看到负载的效果,所以,我们运行完成容器之后,做一个主 界面内容的区分。

node1:web1、web2

node2:web3、web4

  1. 1 #创建nginx容器-p 随意映射容器内的80端口到宿主机
  2. 2 [root@node1 ~]# docker run -itd --name web1 -p 80 nginx:latest
  3. 3 77d39af9c5c8bcaa1839b9002bd09ac093f1e0699cef5bd04515ae38b1ceace8
  4. 4 [root@node1 ~]# docker run -itd --name web2 -p 80 nginx:latest
  5. 5 98ad0a4a5244cbb812ee1c5d2cee4818edc360d6eaea25c70c88f07e88d04a68
  6. 6 #修改容器的nginx首页
  7. 7 [root@node1 ~]# docker exec -it web1 sh
  8. 8 # echo web1 > /usr/share/nginx/html/index.html
  9. 9 # curl 127.0.0.1
  10. 10 web1
  11. 11 [root@node1 ~]# docker exec -it web2 sh
  12. 12 # echo web2 > /usr/share/nginx/html/index.html
  13. 13 # curl 127.0.0.1
  14. 14 web2
node1创建容器并修改首页
  1. 1 #创建容器
  2. 2 [root@node2 ~]# docker run -itd --name web3 -p 80 nginx:latest
  3. 3 29e0495b3a9c1db99f33374115d626fa37217ab67313e1e20845ccf2dd7ccb82
  4. 4 [root@node2 ~]# docker run -itd --name web4 -p 80 nginx:latest
  5. 5 2b2fcd9bd3742b9f806d1804a59a61c1f320bd616e66d5d216d6b2c85231fc20
  6. 6 #修改首页
  7. 7 [root@node2 ~]# docker exec -it web3 sh
  8. 8 # echo web3 > /usr/share/nginx/html/index.html
  9. 9 # curl 127.0.0.1
  10. 10 web3
  11. 11 #
  12. 12 [root@node2 ~]# docker exec -it web4 sh
  13. 13 # echo web4 > /usr/share/nginx/html/index.html
  14. 14 # curl 127.0.0.1
  15. 15 web4
node2创建容器并修改首页

 

 更改nginx服务的配置文件

  1. vim /usr/local/nginx/consul/nginx.ctmpl
  2. upstream http_backend {
  3. {{range service "nginx"}}
  4. server {{ .Address }}:{{ .Port }};
  5. {{ end }}
  6. }
  7. server {
  8. listen 8000;
  9. server_name localhost;
  10. location / {
  11. proxy_pass http://http_backend;
  12. }
  13. }
更改nginx配置

  1. 1 [root@master ~]# vim /usr/local/nginx/conf/nginx.conf
  2. 2 116 include /usr/local/nginx/consul/*.conf;
  3. 3 [root@master ~]# nginx -t
  4. 4 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  5. 5 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
添加支持

 

 #重启  nginx -s reload

安装consul-template命令

Consul-Template简介

Consul-Template是基于Consul的自动替换配置文件的应用。在Consul-Template没出现之前,大家构建服务发现系统大多采用的是Zookeeper、Etcd+Confd这样类似的系统。

Consul官方推出了自己的模板系统Consul-Template后,动态的配置系统可以分化为Etcd+Confd和Consul+Consul-Template两大阵营。Consul-Template的定位和Confd差不多,Confd的后端可以是Etcd或者Consul。

  1. [root@master soft]# unzip consul-template_0.19.5_linux_amd64.zip
  2. Archive: consul-template_0.19.5_linux_amd64.zip
  3. inflating: consul-template
  4. [root@master soft]# mv consul-template /usr/local/bin/
  5. [root@master soft]# chmod +x /usr/local/bin/consul-template
  6. [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" &
  7. [2] 5729
  8. [root@master soft]# nohup: 忽略输入并把输出追加到"nohup.out"
  9. [root@master soft]# cat /usr/local/nginx/consul/vhost.conf
  10. upstream http_backend {
  11. server 192.168.1.2:32768;
  12. server 192.168.1.2:32769;
  13. server 192.168.1.4:32768;
  14. server 192.168.1.4:32769;
  15. }
  16. server {
  17. listen 8000;
  18. server_name localhost;
  19. location / {
  20. proxy_pass http://http_backend;
  21. }
  22. }
#解压consul-template并设置

当然,这时不管后端是新添加nginx的web容器,或是删除,新生产的配置文件都会时时的更新,这时我们在运行consul-template这条命令最后添加: /usr/local/bin/nginx -s reload 它的作用。 

consul常见错误:

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 documentation

解决办法:更换高版本浏览器,谷歌火狐等。

  1. #访问,实现了负载均衡
  2. [root@master soft]# curl localhost:8000
  3. web1
  4. [root@master soft]# curl localhost:8000
  5. web2
  6. [root@master soft]# curl localhost:8000
  7. web3
  8. [root@master soft]# curl localhost:8000
  9. web4
  10. [root@master soft]# curl localhost:8000
  11. web1
  12. [root@master soft]# curl localhost:8000
  13. web2
  14. [root@master soft]# curl localhost:8000
  15. web3
  16. [root@master soft]# curl localhost:8000
  17. web4
  18. #停止node1主机的容器,查看实时发现效果
  19. [root@node1 sfot]# docker stop web1
  20. ^[[Aweb1
  21. [root@node1 sfot]# docker stop web2
  22. web2
  23. #在master访问
  24. [root@master soft]# curl localhost:8000
  25. web3
  26. [root@master soft]# curl localhost:8000
  27. web4
  28. [root@master soft]# curl localhost:8000
  29. web3
  30. [root@master soft]# curl localhost:8000
  31. web4
#测试

 

 

原文链接:http://www.cnblogs.com/lin-strive/p/14454758.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号