【云原生Docker系列第十二篇】Docker consul的容器服务更新与发现(清晨和夜晚都请用力去生活)(二)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 【云原生Docker系列第十二篇】Docker consul的容器服务更新与发现(清晨和夜晚都请用力去生活)(二)

三、consul-template


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


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



安装template在consul服务器


#上传并解压
[root@localhost opt]# unzip consul-template_0.19.3_linux_amd64.zip 
Archive:  consul-template_0.19.3_linux_amd64.zip
  inflating: consul-template   
#解压完就是一个可执行文件
[root@localhost opt]# mv consul-template /usr/local/bin/


3.1 准备 template nginx 模板文件

//在consul服务器上操作
vim /opt/consul/nginx.ctmpl
#定义nginx upstream一个简单模板
upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.Port}};
   {{end}}
}
#定义一个server,监听8000端口,反向代理到upstream
server {
    listen 8080;
    server_name www.stevelu.com;    
    access_log /var/log/nginx/stevelu.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;
    }
}


3.2 yum安装nginx

#导入nginx.repo源文件到yum.repo.d下
#下载nginx服务
yum install -y nginx
systemctl start nginx
netstat -natp |grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      58472/nginx: master


3.3 配置并启动 template

#在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程。
consul-template --consul-addr 192.168.109.137:8500 \
--template "/opt/consul/nginx.ctmpl:/etc/nginx/conf.d/stevelu-consul.conf:/usr/sbin/nginx -s reload" \
--log-level=info
//另外打开一个终端查看生成配置文件
cd /etc/nginx/conf.d/
cat stevelu-consul.conf
upstream http_backend {
   server 192.168.109.133:83;
   server 192.168.109.133:84;
}
server {
    listen 8080;
    server_name www.stevelu.com;
    access_log /var/log/nginx/stevelu.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-template --consul-addr 192.168.80.15:8500 \


#指定consul的ip和端口号;8500既是UI界面端口也是管理服务端口


–template “/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload” \


#/opt/consul/nginx.ctmpl:模板文件路径


#/usr/local/nginx/conf/vhost/kgc.conf:生成的配置文件的路径


#/usr/local/nginx/sbin/nginx -s reload:重载


–log-level=info


#日志的级别




3.5 测试

#再次在registrator开两个容器
docker run -itd -p:85:80 --name test-05 -h test05 nginx
docker run -itd -p:86:80 --name test-06 -h test06 nginx
#consul端再次查看文件,两个容器自动添加进来了
cat stevelu-consul.conf 
upstream http_backend {
   server 192.168.109.133:83;
   server 192.168.109.133:84;
   server 192.168.109.133:85;
   server 192.168.109.133:86;
}



#在nginx容器中的index.html文件中写入不同的数据,测试负载
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                               NAMES
be29e10a1d42   nginx                           "/docker-entrypoint.…"   8 minutes ago   Up 7 minutes   0.0.0.0:86->80/tcp, :::86->80/tcp   test-06
14e16a17f6d2   nginx                           "/docker-entrypoint.…"   8 minutes ago   Up 8 minutes   0.0.0.0:85->80/tcp, :::85->80/tcp   test-05
41919685ef10   httpd                           "httpd-foreground"       7 hours ago     Up 7 hours     0.0.0.0:89->80/tcp, :::89->80/tcp   test-04
f29239f4b1c3   httpd                           "httpd-foreground"       7 hours ago     Up 7 hours     0.0.0.0:88->80/tcp, :::88->80/tcp   test-03
1e039dadebfb   nginx                           "/docker-entrypoint.…"   7 hours ago     Up 7 hours     0.0.0.0:84->80/tcp, :::84->80/tcp   test-02
f3475a9f9055   nginx                           "/docker-entrypoint.…"   7 hours ago     Up 7 hours     0.0.0.0:83->80/tcp, :::83->80/tcp   test-01
0d54ff80e159   gliderlabs/registrator:latest   "/bin/registrator --…"   7 hours ago     Up 7 hours                                         registrator
[root@localhost ~]# docker exec -it test-01 bash
root@test01:/# cd /usr/share/nginx/html/
root@test01:/usr/share/nginx/html# echo "this is test-01 web" > index.html 
root@test01:/usr/share/nginx/html# exit
exit
[root@localhost ~]# docker exec -it test-02 bash
root@test02:/# cd /usr/share/nginx/html/
root@test02:/usr/share/nginx/html# echo "this is test-02 web" > index.html 
root@test02:/usr/share/nginx/html# exit
exit
[root@localhost ~]# docker exec -it test-05 bash
root@test05:/# cd /usr/share/nginx/html/
root@test05:/usr/share/nginx/html# echo "this is test-05 web" > index.html
root@test05:/usr/share/nginx/html# exit
exit
[root@localhost ~]# docker exec -it test-06 bash
root@test06:/# cd /usr/share/nginx/html/
root@test06:/usr/share/nginx/html# echo "this is test-06 web" > index.html
root@test06:/usr/share/nginx/html# exit
exit





四、consul多节点


//添加一台已有docker环境的服务器192.168.109.134/24加入已有的群集中
consul agent \
-server \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.109.134 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true  \
-datacenter=dc1  \
-join 192.168.109.137 &> /var/log/consul.log &
------------------------------------------------------------------------
-enable-script-checks=true :设置检查服务为可用
-datacenter : 数据中心名称
-join :加入到已有的集群中
------------------------------------------------------------------------
相关实践学习
使用ACS算力快速搭建生成式会话应用
阿里云容器计算服务 ACS(Container Compute Service)以Kubernetes为使用界面,采用Serverless形态提供弹性的算力资源,使您轻松高效运行容器应用。本文将指导您如何通过ACS控制台及ACS集群证书在ACS集群中快速部署并公开一个容器化生成式AI会话应用,并监控应用的运行情况。
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
2月前
|
存储 监控 测试技术
如何将现有的应用程序迁移到Docker容器中?
如何将现有的应用程序迁移到Docker容器中?
255 57
|
2月前
|
存储 监控 Java
如何对迁移到Docker容器中的应用进行性能优化?
如何对迁移到Docker容器中的应用进行性能优化?
249 59
|
2月前
|
缓存 Java Docker
如何对应用代码进行优化以提高在Docker容器中的性能?
如何对应用代码进行优化以提高在Docker容器中的性能?
204 1
|
1月前
|
安全 持续交付 Docker
Docker:重塑现代软件交付的容器引擎
Docker:重塑现代软件交付的容器引擎
|
1月前
|
存储 持续交付 Docker
Docker:轻量级容器技术重塑应用交付
Docker:轻量级容器技术重塑应用交付
|
1月前
|
Kubernetes Cloud Native 持续交付
Docker:轻量级容器化技术解析
Docker:轻量级容器化技术解析
|
1月前
|
运维 测试技术 Docker
Docker:轻量级容器化技术革命
Docker:轻量级容器化技术革命
|
1月前
|
存储 持续交付 Docker
Docker:颠覆传统开发的轻量级容器革命
Docker:颠覆传统开发的轻量级容器革命
|
2月前
|
Docker 容器
熟悉Docker容器管理命令:start、stop与restart详细使用指南
掌握这些Docker容器管理命令对于维护应用程序的正常运行至关重要。在实际操作中,应注意容器配置、关联资源以及日志等信息,确保各项操作都能够顺畅并且安全地执行。
279 0

相关产品

  • 容器计算服务
  • 容器服务Kubernetes版