docker consul
1. Overview of Consul
1. Introduction to Consul
Consul is a service discovery and registration tool, which has the characteristics of distributed and extended performance.
2. Consul features
(1) Support health check and allow storage of value pairs
(2) Based on Golong language, strong portability
(3) Support ACL access control (cooperate seamlessly with lightweight containers such as docker)
3. Docker consul container service update and discovery
Interpretation: the establishment of consul service
(1) each node that provides services must deploy and allow consul agent
(2) consul agent has two operating modes (server, client)
(3) server and cilent are just the distinction between consul clusters , Has nothing to do with the application services built on the cluster
Second, department
1. Server deployment (192.168.177.33)
(Need to install docker-ce, compose, consul, consul-template)
mkdir /root/consul
cd consul
rz consul_0.9.2_linux_amd64.zip
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.177.33 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
consul members
consul info | grep leader
2. Client deployment (192.168.177.8)
容器服务自动加入nginx集群
安装Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销docker容器的服务 到服务配置中心
目前支持Consul、Etcd和SkyDNS2
在192.168.177.8节点上,执行以下操作
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.177.8 \
consul://192.168.177.33:8500
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
Open the browser and visit http://192.168.177.33:8500
3. Configure the template module to update automatically
server(192.168.177.33)
cd consul/
vim nginx.ctmpl
upstream http_backend {
{
{range service "nginx"}}
server {
{.Address}}:{
{.Port}};
{
{end}}
}
server {
listen 100;
server_name localhost 192.168.177.33;
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;
}
}
====>:wq
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.0
./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.177.33:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/lic.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
Open another terminal
192.168.177.8
docker run -itd -p:85:80 --name test-05 -h test05 nginx
4. Test access to the proxy server
http://192.168.177.33:100/
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05