当前位置: 首页> 科技> 名企 > 肇庆做网站_网页游戏开服表最全_百度排名查询_营销图片素材

肇庆做网站_网页游戏开服表最全_百度排名查询_营销图片素材

时间:2025/7/18 6:28:01来源:https://blog.csdn.net/Karoku/article/details/144346106 浏览次数:0次
肇庆做网站_网页游戏开服表最全_百度排名查询_营销图片素材

文章目录

  • Docker + consul的容器服务更新与发现
    • Consul-Template
    • Consul-Template部署
      • 一、准备 Nginx 模板文件
      • 二、编译安装 Nginx
      • 三、配置 Nginx
      • 四、配置并启动 Consul-Template
      • 五、测试 Nginx 反向代理
      • 六、增加 Nginx 容器节点并测试服务发现
      • 七、Consul 多节点配置

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

Consul-Template

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

Consul-Template 可以查询 Consul 中的服务目录、Key、Key-values 等,适合动态创建配置文件,如 Apache/Nginx Proxy Balancers、Haproxy Backends 等。

Consul-Template部署

一、准备 Nginx 模板文件

  1. 在 Consul 服务器上,创建 Nginx 模板文件 /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.80.15;access_log /var/log/nginx/xy101.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;}
}

二、编译安装 Nginx

  1. 安装依赖:
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
  1. 创建 Nginx 用户:
useradd -M -s /sbin/nologin nginx
  1. 解压、编译、安装 Nginx:
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

三、配置 Nginx

vim /usr/local/nginx/conf/nginx.conf
# 添加虚拟主机目录
http {include       mime.types;include  vhost/*.conf;       default_type  application/octet-stream;
}# 创建虚拟主机目录和日志文件目录
mkdir /usr/local/nginx/conf/vhost
mkdir /var/log/nginx# 启动 Nginx
nginx

四、配置并启动 Consul-Template

  1. 解压 Consul-Template 并移动到系统路径:
unzip consul-template_0.19.3_linux_amd64.zip -d /opt/
cd /opt/
mv consul-template /usr/local/bin/
  1. 启动 Consul-Template 服务:
    在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程
consul-template --consul-addr 192.168.80.15:8500 \
--template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/xy101.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
  1. 在另一个终端查看生成的配置文件:
upstream http_backend {server 192.168.80.10:83;server 192.168.80.10:84;
}server {listen 8000;server_name localhost 192.168.80.15;access_log /var/log/nginx/xy101.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. 启动 Docker 容器并设置测试页面:
docker run -itd -p 83:80 --name test-01 -h test01 nginx
docker run -itd -p 84:80 --name test-02 -h test02 nginxdocker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED       STATUS          PORTS                NAMES
9f0dc08956f4   httpd                           "httpd-foreground"       1 hours ago   Up 1 hours      0.0.0.0:89->80/tcp   test-04
a0bde07299da   httpd                           "httpd-foreground"       1 hours ago   Up 1 hours      0.0.0.0:88->80/tcp   test-03
4f74d2c38844   nginx                           "/docker-entrypoint.…"   1 hours ago   Up 1 hours      0.0.0.0:84->80/tcp   test-02
b73106db285b   nginx                           "/docker-entrypoint.…"   1 hours ago   Up 1 hours      0.0.0.0:83->80/tcp   test-01
409331c16824   gliderlabs/registrator:latest   "/bin/registrator -i…"   1 hours ago   Up 1 hours                        registratordocker exec -it 4f74d2c38844 bash
echo "this is test1 web" > /usr/share/nginx/html/index.htmldocker exec -it b73106db285b bash
echo "this is test2 web" > /usr/share/nginx/html/index.html
  1. 访问 http://192.168.80.15:8000/ 并不断刷新,观察轮询效果。

六、增加 Nginx 容器节点并测试服务发现

  1. 增加一个新的 Nginx 容器节点:
docker run -itd -p 85:80 --name test-05 -h test05 nginx
  1. 查看配置文件内容:
    观察 template 服务,会从模板更新/usr/local/nginx/conf/vhost/xy101.conf 文件内容,并且重载 nginx 服务。
cat /usr/local/nginx/conf/vhost/xy101.conf
upstream http_backend {server 192.168.80.10:83;server 192.168.80.10:84;server 192.168.80.10:85;
}
  1. 查看三台 Nginx 容器日志,确认请求正常轮询到各个节点。
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05

七、Consul 多节点配置

  1. 在另一台已有 Docker 环境的服务器上加入192.168.80.12 Consul 集群:
consul agent \
-server \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.80.12 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true  \
-datacenter=dc1  \
-join 192.168.80.15 &> /var/log/consul.log &
-enable-script-checks=true :设置检查服务为可用
-datacenter : 数据中心名称
-join :加入到已有的集群中
  1. 检查集群状态:
consul members
Node             Address             Status  Type    Build  Protocol  DC
consul-server01  192.168.80.15:8301  alive   server  0.9.2  2         dc1
consul-server02  192.168.80.12:8301  alive   server  0.9.2  2         dc1consul operator raft list-peers
Node             ID                  Address             State     Voter  RaftProtocol
consul-server01  192.168.80.15:8300  192.168.80.15:8300  leader    true   2
consul-server02  192.168.80.12:8300  192.168.80.12:8300  follower  true   2
  1. 设置 agent 离开集群并关闭 agent:
consul leave
关键字:肇庆做网站_网页游戏开服表最全_百度排名查询_营销图片素材

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: