目录
3. 访问http://192.168.119.20:8500
Consul的应用场景包括服务发现、服务隔离、服务配置
- '创建consul目录'
- # mkdir /root/consul
- # cd consul
- rz consul_0.9.2_linux_amd64.zip
-
- 'unzip进行解压'
- # unzip consul_0.9.2_linux_amd64.zip
-
- '将解压出来的可执行文件移动到路径环境变量下,便于系统识别'
- # mv consul /usr/bin
- # consul agent \ # 进行初始化
- -server \
- -bootstrap \
- -ui \ # ui界面
- -data-dir=/var/lib/consul-data \ # 数据存储的位置
- -bind=192.168.119.20 \ # 绑定地址
- -client=0.0.0.0 \ # 监听所有地址
- -node=consul-server01 &> /var/log/consul.log &
-
- # consul members # 插卡群集成员
- # consul info | grep leader # 查看leader信息,当前只有一个成员
查看集群server成员 | curl 127.0.0.1:8500/v1/status/peers |
集群Raf leader | curl 127.0.0.1:8500/v1/status/leader |
注册的所有服务 | curl 127.0.0.1:8500/v1/catalog/services |
查看nginx服务信息 | curl 127.0.0.1:8500/v1/catalog/nginx |
集群节点详细信息 | curl 127.0.0.1:8500/v1/catalog/nodes |
- 容器服务自动加入nginx集群
- 1、安装Gliderlabs/Registrator Gliderlabs/Registrator
- 可检查容器运行状态自动注册,还可注销docker容器的服务 到服务配置中心
- 目前支持Consul、Etcd和SkyDNS2
-
- 在192.168.184.12节点上,执行以下操作
-
- # docker run -d \
- --name=registrator \
- --net=host \
- -v /var/run/docker.sock:/tmp/docker.sock \
- --restart=always \
- gliderlabs/registrator:latest \
- -ip=192.168.119.30 \
- consul://192.168.119.20:8500
- '运行两个nginx容器,两个apache容器,以测试服务发现功能'
- # systemctl restart docker
- # docker run -itd -p:81:80 --name test-01 -h test01 nginx
- # docker run -itd -p:82:80 --name test-02 -h test02 nginx
- # docker run -itd -p:83:80 --name test-03 -h test03 httpd
- # docker run -itd -p:84:80 --name test-04 -h test04 httpd
#此处会有5个服务
Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件,更新完成以后,可以查询Consul中的服务目录,Key、Key-values等
- # cd consul/
-
- # vim nginx.ctmpl
-
- upstream http_backend {
- {{range service "nginx"}}
- server {{.Address}}:{{.Port}};
- {{end}}
- }
-
- server {
- listen 100;
- server_name localhost 192.168.119.20;
- access_log /var/log/nginx/lic.com-access.log;
- index index.html index.php;
- location / {
- proxy_set_header HOST $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header Client-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://http_backend;
- }
- }
- # yum -y install gcc pcre-devel zlib-devel
- rz nginx-1.12.0.tar.gz
- # tar zxvf nginx-1.12.0.tar.gz -C /opt
- # cd /opt/nginx-1.12.10
-
- # ./configure --prefix=/usr/local/nginx
-
- # make && make install
- # vim /usr/local/nginx/conf/nginx.conf
- //19行添加 include vhost/*.conf;
- # cd /usr/local/nginx/conf/
- # mkdir vhost
- # mkdir /var/log/nginx
-
- # /usr/local/nginx/sbin/nginx
-
- # cd /opt
- rz consul-template_0.19.3_linux_amd64.zip
-
- # unzip consul-template_0.19.3_linux_amd64.zip
- # mv consul-template /usr/bin
- # consul-template -consul-addr 192.168.109.11:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/lic.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
再开一个20的终端
192.168.119.20
# docker run -itd -p:85:80 --name test-05 -h test05 nginx
192.168.119.20
- http://192.168.119.20:100/
-
- # docker logs -f test-01
- # docker logs -f test-02
- # docker logs -f test-05