consul 是一个支持多数据中心分布式高可用的 服务发现 和 配置共享 的服务软件,由 hashicorp 公司用 go 语言开发, 基于 mozilla public license 2.0 的协议进行开源。 consul 支持 健康检查,并允许 http 、grpc 和 dns 协议调用 api 存储键值对.
命令行超级好用的虚拟机管理软件 vgrant 也是 hashicorp 公司开发的产品.
一致性协议采用 raft 算法,用来保证服务的高可用. 使用 gossip 协议管理成员和广播消息, 并且支持 acl 访问控制.
consul 使用场景
docker 实例的注册与配置共享
coreos 实例的注册与配置共享
saas 应用的配置共享、服务发现和健康检查。
vitess 集群
与 confd 服务集成,动态生成 nginx 和 haproxy 配置文件
-consul部署-
服务器:192.168.37.100 docker-ce、consul、consul-template
服务器:192.168.37.107 docker-ce、registrator
template模板(更新配置文件)
registrator(自动发现+注册到consul-server端)
后端每更新出一个容器,会向registrator进行注册,控制consul完成更新操作,consul会触发consultemplate模板进行热更新(reload)
核心机制: consul :自动发现、自动更新,为容器提供服务(添加、删除、生命周期)———consul服务器
创建consul目录
?
1
2
3
[root@localhost ~]# hostnamectl set-hostname consul
[root@localhost ~]# su
[root@consul ~]# mkdir /root/consul
将consul压缩包压缩并放到/use/bin下
?
1
2
3
4
5
6
7
8
9
[root@consul ~]# cd consul/
[root@consul ~/consul]# ls
consul_0.9.2_linux_amd64.zip
[root@consul ~/consul]# unzip consul_0.9.2_linux_amd64.zip
archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@consul ~/consul]# ls
consul consul_0.9.2_linux_amd64.zip
[root@consul ~/consul]# mv consul /usr/bin/
配置consul的模式
onsul agent \
-server \ server模式
-bootstrap 前端框架
-ui \ 可被访问的web界面
-data-dir=/var/lib/consul-data \
-bind=192.168.37.100 \
-client=0.0.0.0 \ registrator(应用服务器-docker-tomcat/apache)
-node=consul-server01 &> /var/ log / consul.log &
?
1
2
3
4
5
6
7
8
9
10
11
12
[root@consul ~/consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.37.100 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 24144
[root@consul ~/consul]# consul members
node address status type build protocol dc
consul-server01 192.168.37.100:8301 alive server 0.9.2 2 dc1
此时我们看群集信息
?
1
2
3
[root@consul ~/consul]# consul members
node address status type build protocol dc
consul-server01 192.168.37.100:8301 alive server 0.9.2 2 dc1
查看详细信息,过滤出leader
?
1
2
3
[root@consul ~/consul]# consul info | grep leader
leader = true
leader_addr = 192.168.37.100:8300
查看consul界面。使用8500端口,8300是通信端口
容器服务自动加入consul集群
安装gliderlabs/registrator gliderlabs/registrator
可检查容器运行状态自动注册,还可注册docker容器的服务信息到服务配置中心(consul 8300 —–》8500展示)。目前支持consul、etcd和skydns2。
在192.168.37.107执行以下操作
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@server ~]# docker run -d \
> –name=registrator \
> –net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> –restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.37.107 \
> consul://192.168.37.100:8500
###下面的是加载
unable to find image gliderlabs/registrator:latest locally
latest: pulling from gliderlabs/registrator
image docker.io/gliderlabs/registrator:latest uses outdated schema1 manifest format. please upgrade to a sc hema2 image for better future compatibility. more information at https://docs.docker.com/registry/spec/depr ecated-schema-v1/
c87f684ee1c2: pull complete
a0559c0b3676: pull complete
a28552c49839: pull complete
digest: sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10
status: downloaded newer image for gliderlabs/registrator:latest
2536df1b1fbb77dcbf5d2e09295f222496ecc63f00a49020ce6976decd52a439
查看容器
?
1
2
3
[root@server ~]# docker ps -a
container id image command created status ports names
2536df1b1fbb gliderlabs/registrator:latest “/bin/registrator -i…” 51 seconds ago up 50 seconds registrator
测试服务发现功能是否正常
安装两个nginx和2个apache
?
1
2
3
4
docker run -itd -p:83:80 –name test-01 -h test01 nginx
docker run -itd -p:84:80 –name test-02 -h testo2 nginx
docker run -itd -p:88:80 –name test-03 -h test03 httpd
docker run -itd -p:89:80 –name test-04 -h test04 httpd
此时去网页查看
安装consul-template
consul-template 是一个守护进程,用于实时查询consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行shell命令执行更新操作,重新加载nginx。consul-template可以查询consul中的服务目录、key、 key-values 等。
这种强大的抽象功能和查询语言模板可以使consul-template 特别适合动态的创建配置文件。例如:创建 apache/nginx (nginx upstream) proxy balancers、haproxy backends
准备template nginx模板文件
————————————————–在consul上操作—————————————————-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@consul ~/consul]# vim nginx.ctmp1
upstream http_backend {
{{range service “nginx”}}
server {{.address}}:{{.port}};
{{end}}
}
server {
listen 83;
server_name localhost 192.168.37.100;
access_log /var/log/nginx/xiao.cn-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;
}
安装nginx
?
1
2
3
4
5
6
7
8
####安装所需软件包
[root@server ~]# yum install gcc pcre-devel zlib-devel -y
###解压nginx软件包
[root@server ~]# tar zxf nginx-1.12.0.tar.gz
###执行
[root@server /opt/nginx-1.12.0]# ./configure –prefix=/usr/local/nginx
##编译
[root@server /opt/nginx-1.12.0]# make && make install
修改配置文件,让其识别.conf文件结尾的文件
创建vhost目录
?
1
[root@server /usr/local/nginx/conf]# mkdir vhost
创建nginx日志目录
?
1
[root@server /usr/local/nginx/conf/vhost]# mkdir /var/log/nginx.log
启动nginx
?
1
2
[root@server /usr/local/sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@server /usr/local/sbin]# nginx
查看是否启动
?
1
2
[root@server /usr/local/sbin]# netstat -natp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* listen 54739/nginx: master
配置并启动template
?
1
2
3
4
5
6
[root@consul ~/consul]# unzip consul-template_0.19.3_linux_amd64.zip
archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@consul ~/consul]# ls
consul_0.9.2_linux_amd64.zip consul-template consul-template_0.19.3_linux_amd64.zip nginx.ctmp1
[root@consul ~/consul]# mv consul-template /usr/bin
##关联nginx虚拟目录中的子配置文件操作
?
1
2
3
[root@consul ~/consul]# consul-template -consul-addr 192.168.37.100:8500 \
> -template “/root/consul/nginx.ctmp1:/usr/local/nginx/conf/vhost/xiao.conf:/usr/local/nginx/sbin/nginx -s reload” \
> –log-level=info
查看生成配置文件
是都可以访问。
到此这篇关于docker容器部署consul的详细步骤的文章就介绍到这了,更多相关docker consul 部署内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_48404074/article/details/122782564
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。