Linux操作文档——Docker+Consul+registrator 实现服务发现


docker01docker02docker03nginx
192.168.1.10192.168.1.20192.168.1.30192.168.1.100

一、配置consul服务

1、启动consul

[root@docker01 ~]# wget https://releases.hashicorp.com/consul/1.8.4/consul_1.8.4_linux_amd64.zip
[root@docker01 ~]# unzip consul_1.8.4_linux_amd64.zip 
[root@docker01 ~]# mv consul /usr/local/bin/
[root@docker01 ~]# chmod +x /usr/local/bin/consul 
[root@docker01 ~]# nohup consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.1.10 -client=0.0.0.0 -node=master &
参数说明
-bootstrap加入这个选项时,一般都在server单节点的时候用,自选举为leader
-ui开启内部的web页面
-data-dirkey/volume数据存储位置
-bind指定开启服务的IP
-client指定访问的客户端
-node指定集群内通信使用的名称。默认是主机名
开启的端口
8300集群节点
8301集群内部的访问
8302跨数据中心的通信
8500web ui 界面
8600使用dns协议查看节点信息的端口

2、查看consul的信息

[root@docker01 ~]# consul info

3、查看consul集群内成员的信息

[root@docker01 ~]# consul members
Node    Address            Status  Type    Build  Protocol  DC   Segment
master  192.168.1.10:8301  alive   server  1.8.4  2         dc1  <all>

二、节点加入consul集群

1、docker02加入consul集群

[root@docker02 ~]# docker run -d --name consul -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.10 -advertise 192.168.1.20 -client 0.0.0.0 -node=node01
[root@docker02 ~]# docker run -itd -p 80 --name web01 --restart always nginx
[root@docker02 ~]# docker exec -it web01 /bin/bash
root@ab1768940dc9:/# echo web01 > /usr/share/nginx/html/index.html
[root@docker02 ~]# docker run -itd -p 80 --name web02 --restart always nginx
[root@docker02 ~]# docker exec -it web02 /bin/bash
root@fb1d82b7be2d:/# echo web02 > /usr/share/nginx/html/index.html

2、docker03加入consul集群

[root@docker03 ~]# docker run -d --name consul -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.10 -advertise 192.168.1.30 -client 0.0.0.0 -node=node02
[root@docker03 ~]# docker run -itd -p 80 --name web03 --restart always nginx
[root@docker03 ~]# docker exec -it web03 /bin/bash
root@19d6b3d053f4:/# echo web03 > /usr/share/nginx/html/index.html
[root@docker03 ~]# docker run -itd -p 80 --name web03 --restart always nginx
[root@docker03 ~]# docker exec -it web04 /bin/bash
root@5feef0ae1920:/# echo web04 > /usr/share/nginx/html/index.html

在这里插入图片描述

三、部署registrator服务

[root@docker02 ~]# docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.20:8500
[root@docker03 ~]# docker run -d --name registrator -v /var/run/docker.sock:/tmp/docker.sock --restart always gliderlabs/registrator consul://192.168.1.30:8500

四、部署一个nginx服务

1、安装nginx

[root@nginx ~]# useradd -M -s /sbin/nologin nginx
[root@nginx ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@nginx ~]# tar zxf nginx-1.18.0.tar.gz 
[root@nginx ~]# cd nginx-1.18.0/
[root@nginx nginx-1.18.0]# ./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-pcre --with-http_ssl_module && make && make install
[root@nginx nginx-1.18.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@nginx nginx-1.18.0]# nginx

2、更改nginx服务的配置文件

[root@nginx ~]# mkdir /usr/local/nginx/consul
[root@nginx ~]# cd /usr/local/nginx/consul/
[root@nginx consul]# vim nginx.ctmpl
upstream http_backend {
        {{range service "nginx"}}
        server {{ .Address }}:{{ .Port }};
        {{ end }}
}

server {
        listen 8000;
        server_name localhost;
        location / {
        proxy_pass http://http_backend;
        }
}
[root@nginx consul]# vim /usr/local/nginx/conf/nginx.conf
.......
include /usr/local/nginx/consul/*.conf;      //配置文件最后
[root@nginx consul]# 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
[root@nginx consul]# nginx -s reload

五、安装consul-template

[root@nginx ~]# wget https://releases.hashicorp.com/consul-template/0.25.1/consul-template_0.25.1_linux_amd64.zip
[root@nginx ~]# unzip consul-template_0.25.1_linux_amd64.zip 
Archive:  consul-template_0.25.1_linux_amd64.zip
  inflating: consul-template         
[root@nginx ~]# mv consul-template /usr/local/bin/
[root@nginx ~]# chmod +x /usr/local/bin/consul-template 
[root@nginx ~]# nohup consul-template -consul-addr 192.168.1.10:8500 -template "/usr/local/nginx/consul/nginx.ctmpl:/usr/local/nginx/consul/vhost.conf:/usr/local/sbin/nginx -s reload" &
[root@nginx ~]# cat /usr/local/nginx/consul/vhost.conf 
upstream http_backend {
        
        server 192.168.1.20:32768;
        
        server 192.168.1.20:32769;
        
        server 192.168.1.30:32770;
        
        server 192.168.1.30:32769;
        
}

server {
        listen 8000;
        server_name localhost;
        location / {
        proxy_pass http://http_backend;
        }
}
[root@docker01 ~]# curl 192.168.1.100:8000
web01
[root@docker01 ~]# curl 192.168.1.100:8000
web02
[root@docker01 ~]# curl 192.168.1.100:8000
web03
[root@docker01 ~]# curl 192.168.1.100:8000
web04

在这里插入图片描述

posted @ 2020-09-26 13:18  高中僧  阅读(30)  评论(0)    收藏  举报