ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Docker consul的容器服务更新与发现

2021-10-20 19:32:06  阅读:352  来源: 互联网

标签:容器 0.0 consul 192.168 server nginx Docker root


Docker consul的容器服务更新与发现

目录

一、Consul简介

1. 服务注册与发现

服务注册与发现是微服务架构中不可或缺的重要组件。起初服务都是单节点的,不保障高可用性,也不考虑服务的压力承载,服务之间调用单纯的通过接口访问。直到后来出现了多个节点的分布式架构,起初的解决手段是在服务前端负载均衡,这样前端必须要知道所有后端服务的网络位置,并配置在配置文件中。这里就会有几个问题:
● 如果需要调用后端服务A-N,就需要配置N个服务的网络位置,配置很麻烦
● 后端服务的网络位置变化,都需要改变每个调用者的配置

既然有这些问题,那么服务注册与发现就是解决这些问题的。后端服务A-N可以把当前自己的网络位置注册到服务发现模块,服务发现就以K-V的方式记录下来,K一般是服务名,V就是IP:PORT。服务发现模块定时的进行健康检查,轮询查看这些后端服务能不能访问的了。前端在调用后端服务A-N的时候,就跑去服务发现模块问下它们的网络位置,然后再调用它们的服务。这样的方式就可以解决上面的问题了,前端完全不需要记录这些后端服务的网络位置,前端和后端完全解耦!

2. consul概述

consul是google开源的一个使用go语言开发的服务管理软件。支持多数据中心、分布式高可用的、服务发现和配置共享。采用Raft算法,用来保证服务的高可用。内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server 和 client。 每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。
安装consul是用于服务注册,也就是容器本身的一些信息注册到consul里面,其他程序可以通过consul获取注册的相关服务信息,这就是服务注册与发现。

3. consul的两种模式

在client模式下,所有注册到当前节点的服务会被转发到server节点,本身是不持久化这些信息。
在server模式下,功能和client模式相似,唯一不同的是,它会把所有的信息持久化到本地,这样遇到故障,信息是可以被保留的。
server-leader是所有server节点的老大,它和其它server节点不同的是,它需要负责同步注册的信息给其它的server节点,同时也要负责各个节点的健康监测。

4. consul提供的一些关键特性

● 服务注册与发现:consul通过DNS或者HTTP接口使服务注册和服务发现变的很容易,一些外部服务,例如saas提供的也可以一样注册。
● 健康检查:健康检测使consul可以快速的告警在集群中的操作。和服务发现的集成,可以防止服务转发到故障的服务上面。
● Key/Value存储:一个用来存储动态配置的系统。提供简单的HTTP接口,可以在任何地方操作。
● 多数据中心:无需复杂的配置,即可支持任意数量的区域。

二、consul部署

1. 服务器配置

服务器 IP地址 主机名 主要功能
consul服务器 192.168.122.10 consul 运行consul服务、nginx服务、consul-template守护进程
registrator服务器 192.168.122.11 registrator 运行registrator容器、运行nginx容器
systemctl disable --now firewalld.service
setenforce 0

2. consul服务器

2.1 建立consul服务

[root@consul ~]# mkdir /opt/consul
[root@consul ~]# cd /opt/consul
[root@consul consul]# rz -E
#传入安装包consul_0.9.2_linux_amd64.zip
rz waiting to receive.
[root@consul consul]# unzip consul_0.9.2_linux_amd64.zip 
[root@consul consul]# mv consul /usr/local/bin/

设置代理,在后台启动consul服务端

[root@consul consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.122.10 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 30191

-server: 以server身份启动。默认是client。
-bootstrap :用来控制一个server是否在bootstrap模式,在一个数据中心中只能有一个server处于bootstrap模式,当一个server处于 bootstrap模式时,可以自己选举为 server-leader。
-bootstrap-expect=2 :集群要求的最少server数量,当低于这个数量,集群即失效。
-ui :指定开启 UI 界面,这样可以通过 http://localhost:8500/ui 这样的地址访问 consul 自带的 web UI 界面。
-data-dir :指定数据存储目录。
-bind :指定用来在集群内部的通讯地址,集群内的所有节点到此地址都必须是可达的,默认是0.0.0.0。
-client :指定 consul 绑定在哪个 client 地址上,这个地址提供 HTTP、DNS、RPC 等服务,默认是 127.0.0.1。
-node :节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名。
-datacenter :指定数据中心名称,默认是dc1。

[root@consul consul]# netstat -natp | grep consul
tcp        0      0 192.168.122.10:8300     0.0.0.0:*               LISTEN      30191/consul        
tcp        0      0 192.168.122.10:8301     0.0.0.0:*               LISTEN      30191/consul        
tcp        0      0 192.168.122.10:8302     0.0.0.0:*               LISTEN      30191/consul        
tcp6       0      0 :::8500                 :::*                    LISTEN      30191/consul        
tcp6       0      0 :::8600                 :::*                    LISTEN      30191/consul     

启动consul后默认会监听5个端口:
8300:replication、leader farwarding的端口
8301:lan cossip的端口
8302:wan gossip的端口
8500:web ui界面的端口
8600:使用dns协议查看节点信息的端口

2.2 查看群集信息

查看members状态

[root@consul consul]# consul members
Node             Address              Status  Type    Build  Protocol  DC
consul-server01  192.168.122.10:8301  alive   server  0.9.2  2         dc1

查看群集状态

[root@consul consul]# consul operator raft list-peers
Node             ID                   Address              State   Voter  RaftProtocol
consul-server01  192.168.122.10:8300  192.168.122.10:8300  leader  true   2

2.3 通过http api获取群集信息

[root@consul consul]# curl 127.0.0.1:8500/v1/status/peers
["192.168.122.10:8300"][root@consul consul]# 
[root@consul consul]# curl 127.0.0.1:8500/v1/status/leader
"192.168.122.10:8300"[root@consul consul]# 
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[]}[root@consul consul]# 
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/nginx
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/nodes
[{"ID":"d96ffa01-9116-368b-1b88-407cf7efbda7","Node":"consul-server01","Address":"192.168.122.10","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.122.10","wan":"192.168.122.10"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}][root@consul consul]# 
curl 127.0.0.1:8500/v1/status/peers 			#查看集群server成员
curl 127.0.0.1:8500/v1/status/leader			#集群 server-leader
curl 127.0.0.1:8500/v1/catalog/services			#注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx			#查看 nginx 服务信息
curl 127.0.0.1:8500/v1/catalog/nodes			#集群节点详细信息

3. registrator服务器

容器服务自动加入nginx群集

3.1 安装Gliderlabs/Registrator

Gliderlabs/Registrator可检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心。目前支持Consul、Etcd和SkyDNS2。

[root@registrator ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> --ip=192.168.122.11 \
> consul://192.168.122.10:8500
[root@registrator ~]# docker images
REPOSITORY               TAG       IMAGE ID       CREATED       SIZE
gliderlabs/registrator   latest    3b59190c6c80   5 years ago   23.8MB
[root@registrator ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS     NAMES
70c4cab43710   gliderlabs/registrator:latest   "/bin/registrator --…"   3 minutes ago   Up 3 minutes             registrator

--net=host :把运行的docker容器设定为host网络模式。
-v /var/run/docker.sock:/tmp/docker.sock :把宿主机的Docker守护进程(Docker daemon)默认监听的Unix域套接字挂载到容器中。
--restart=always :设置在容器退出时总是重启容器。
--ip :刚才把network指定了host模式,所以我们指定ip为宿主机的ip。
consul :指定consul服务器的IP和端口。

3.2 测试服务发现功能是否正常

[root@registrator ~]# docker run -itd -p 81:80 --name test1 nginx
8b2fd482499911ddf46825b2995235aab6d14fdbefe0ef6d8d884b307dad2074
[root@registrator ~]# docker run -itd -p 82:80 --name test2 nginx
a7d1a09c7e6dff84787d19b3e323204af4cc50801e243ea8aa98c1b0923473c0
[root@registrator ~]# docker run -itd -p 83:80 --name test3 httpd
1f808f536a548432347800da73f27767e77a660d5e8d53b6269257d9de50db64
[root@registrator ~]# docker run -itd -p 84:80 --name test4 httpd
7dcf36adb446b0e0d5fc3fe55e4a4ac719e3b92c13aecdb09826cb1841b6d4e8
[root@registrator ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS                               NAMES
7dcf36adb446   httpd                           "httpd-foreground"       8 seconds ago    Up 8 seconds    0.0.0.0:84->80/tcp, :::84->80/tcp   test4
1f808f536a54   httpd                           "httpd-foreground"       15 seconds ago   Up 15 seconds   0.0.0.0:83->80/tcp, :::83->80/tcp   test3
a7d1a09c7e6d   nginx                           "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes    0.0.0.0:82->80/tcp, :::82->80/tcp   test2
8b2fd4824999   nginx                           "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes    0.0.0.0:81->80/tcp, :::81->80/tcp   test1
70c4cab43710   gliderlabs/registrator:latest   "/bin/registrator --…"   17 minutes ago   Up 17 minutes                                       registrator

3.3 验证http和nginx服务是否注册到consul

浏览器中,输入http://192.168.122.10:8500,在Web页面中“单击NODES”,然后单击“consul-server01”,会出现5个服务。


在consul服务器使用curl测试连接服务器

[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}[root@consul consul]# 

三、consul-template

1. consul-template概述

Consul-Template是基于Consul的自动替换配置文件的应用。Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新操作,重新加载 Nginx。

2. consul-template的作用

Consul-Template可以查询Consul中的服务目录、Key、Key-values 等。这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。

3. 准备template nginx模板文件

在consul服务器上操作

[root@consul ~]# vim /opt/consul/nginx.ctmpl

#定义nginx upstream一个简单模板
upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.Port}};
   {{end}}
}

#定义一个server,监听8000端口,反向代理到upstream
server {
    listen 8000;
    server_name localhost 192.168.122.10;
#修改日志路径
    access_log /var/log/nginx/test.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;
    }
}

4. 编译安装nginx

[root@consul ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@consul ~]# useradd -M -s /sbin/nologin nginx
[root@consul ~]# cd /opt
[root@consul opt]# rz -E
rz waiting to receive.
#传入安装包nginx-1.12.0.tar.gz
[root@consul opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@consul opt]# cd /opt/nginx-1.12.0/
[root@consul nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@consul nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

5. 配置并启动nginx

配置nginx

[root@consul nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf

······
http {
    include       mime.types;
    include       vhost/*.conf;
#添加虚拟主机目录
    default_type  application/octet-stream;
······

创建虚拟主机

[root@consul nginx-1.12.0]# mkdir /usr/local/nginx/conf/vhost

创建日志文件目录

[root@consul nginx-1.12.0]# mkdir /var/log/nginx

启动nginx

[root@consul nginx-1.12.0]# nginx
[root@consul nginx-1.12.0]# netstat -natp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      33272/nginx: master 

6. 编译安装、启动并配置template

[root@consul nginx-1.12.0]# cd /opt
[root@consul opt]# rz -E
#传入安装包consul-template_0.19.3_linux_amd64.zip
rz waiting to receive.
[root@consul opt]# unzip consul-template_0.19.3_linux_amd64.zip -d /opt/
[root@consul opt]# mv consul-template /usr/local/bin/

在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程

[root@consul opt]# consul-template --consul-addr 192.168.122.10:8500 \
> --template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
······

另外打开一个终端查看生成配置文件

[root@consul ~]# cd /usr/local/nginx/conf/vhost/
[root@consul vhost]# ls
test.conf
[root@consul vhost]# cat test.conf 
#定义nginx upstream一个简单模板
upstream http_backend {
  
   server 192.168.122.11:81;
   
   server 192.168.122.11:82;
   
}

#定义一个server,监听8000端口,反向代理到upstream
server {
    listen 8000;
    server_name localhost 192.168.122.10;
#修改日志路径
    access_log /var/log/nginx/test.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;
    }
}

7. 访问template-nginx

准备访问页面(registrator服务器)

[root@registrator ~]# docker exec -it test1 bash
root@8b2fd4824999:/# echo "this is test1 web" > /usr/share/nginx/html/index.html
root@8b2fd4824999:/# exit
exit
[root@registrator ~]# docker exec -it test2 bash
root@a7d1a09c7e6d:/# echo "this is test2 web" > /usr/share/nginx/html/index.html
root@a7d1a09c7e6d:/# exit
exit

浏览器访问http://192.168.122.10:8000/并不断刷新


rr轮询,权重为1。

8. 增加节点,测试配置更新

增加一个nginx容器节点

[root@registrator ~]# docker run -itd -p 85:80 --name test5 -h test5 nginx
3a75af012634265eb88fcebb5f691ea43d894facd1c6d7fd91534293851b4af5
[root@registrator ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                               NAMES
3a75af012634   nginx                           "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds   0.0.0.0:85->80/tcp, :::85->80/tcp   test5
7dcf36adb446   httpd                           "httpd-foreground"       2 hours ago     Up 2 hours     0.0.0.0:84->80/tcp, :::84->80/tcp   test4
1f808f536a54   httpd                           "httpd-foreground"       2 hours ago     Up 2 hours     0.0.0.0:83->80/tcp, :::83->80/tcp   test3
a7d1a09c7e6d   nginx                           "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:82->80/tcp, :::82->80/tcp   test2
8b2fd4824999   nginx                           "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:81->80/tcp, :::81->80/tcp   test1
70c4cab43710   gliderlabs/registrator:latest   "/bin/registrator --…"   2 hours ago     Up 2 hours                                         registrator

观察 template 服务,会从模板更新/usr/local/nginx/conf/vhost/test.conf 文件内容,并且重载 nginx 服务。

[root@consul vhost]# cat test.conf 
#定义nginx upstream一个简单模板
upstream http_backend {
  
   server 192.168.122.11:81;
   
   server 192.168.122.11:82;
   
   server 192.168.122.11:85;
   
}

#定义一个server,监听8000端口,反向代理到upstream
server {
    listen 8000;
    server_name localhost 192.168.122.10;
#修改日志路径
    access_log /var/log/nginx/test.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;
    }
}

四、consul多节点

添加一台已有docker环境的服务器192.168.122.12/24加入已有的群集中

[root@localhost ~]# consul agent \
> -server \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.122.12 \
> -client=0.0.0.0 \
> -node=consul-server02 \
> -enable-script-checks=true  \
> -datacenter=dc1  \
> -join 192.168.122.10 &> /var/log/consul.log &
[1] 3160

-enable-script-checks=true :设置检查服务为可用
-datacenter : 数据中心名称
-join :加入到已有的集群中

[root@localhost consul]# consul members
Node             Address              Status  Type    Build  Protocol  DC
consul-server01  192.168.122.10:8301  alive   server  0.9.2  2         dc1
consul-server02  192.168.122.12:8301  alive   server  0.9.2  2         dc1
[root@localhost consul]# consul operator raft list-peers
Node             ID                   Address              State     Voter  RaftProtocol
consul-server01  192.168.122.10:8300  192.168.122.10:8300  leader    true   2
consul-server02  192.168.122.12:8300  192.168.122.12:8300  follower  true   2

标签:容器,0.0,consul,192.168,server,nginx,Docker,root
来源: https://www.cnblogs.com/dingcong1201/p/15430308.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有