consul服务器 192.168.247.132 运行consul服务、nginx服务、consul-template守护进程
registrator服务器 192.168.247.131 运行registrator容器、运行nginx容器
(1)准备Consul 服务
[root@node02 ~]# systemctl stop firewalld.service
[root@node02 ~]# setenforce 0
setenforce: SELinux is disabled
[root@node02 ~]# mkdir /opt/consul
[root@node02 ~]# cd /opt/consul
[root@node02 consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@node02 consul]# mv consul /usr/local/bin/
(2)设置代理,在后台启动 consul 服务端
[root@node02 consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.247.132 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 1939
名称 | 作用 |
---|---|
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 |
(3)查看端口号
[root@node02 consul]# netstat -natp | grep consul
tcp 0 0 192.168.247.132:8300 0.0.0.0:* LISTEN 1939/consul
tcp 0 0 192.168.247.132:8301 0.0.0.0:* LISTEN 1939/consul
tcp 0 0 192.168.247.132:8302 0.0.0.0:* LISTEN 1939/consul
tcp 0 0 192.168.247.132:52015 192.168.247.132:8300 ESTABLISHED 1939/consul
tcp 0 0 192.168.247.132:8300 192.168.247.132:52015 ESTABLISHED 1939/consul
tcp6 0 0 :::8500 :::* LISTEN 1939/consul
tcp6 0 0 :::8600 :::* LISTEN 1939/consul
端口号 | 解释 |
---|---|
8300 | replication、leader farwarding的端口 |
8301 | lan cossip的端口 |
8302 | wan gossip的端口 |
8500 | web ui界面的端口 |
8600 | 使用dns协议查看节点信息的端口 |
(1)查看members状态
[root@node02 consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.247.132:8301 alive server 0.9.2 2 dc1
(2)查看集群状态
[root@node02 consul]# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.247.132:8300 192.168.247.132:8300 leader true 2
[root@node02 consul]# consul info | grep leader
leader = true
leader_addr = 192.168.247.132:8300
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 #集群节点详细信息
(1)安装 Gliderlabs/Registrator
[root@lion ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> --ip=192.168.247.131 \
> consul://192.168.247.132:8500
560c5ce43950da1e8fb86c1059d355042b33c176b69a7a2631e2013d23910811
字段 | 作用 |
---|---|
–net=host | 把运行的docker容器设定为host网络模式 |
-v /var/run/docker.sock:/tmp/docker.sock | 把宿主机的Docker守护进程(Docker daemon)默认监听的Unix域套接字挂载到容器中 |
–restart=always | 设置在容器退出时总是重启容器 |
–ip | 刚才把network指定了host模式,所以我们指定ip为宿主机的ip |
[root@lion ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
2295f309fff88f72b5c6b19b2e688739ae8e7ab60ad97fb8a332138699e17861
[root@lion ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
55757a713bbdbe575ddf40b93f72e87a1b5bc4eea1d2909d8d980f669a4d323a
[root@lion ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a2abf6c4d29d: Already exists
dcc4698797c8: Pull complete
41c22baa66ec: Pull complete
67283bbdd4a0: Pull complete
d982c879c57e: Pull complete
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
641ed2409f2e1168084894dc4dc5a1df8b0f6e37c986fb2abc24683a3d846a92
[root@lion ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
675e7af3911689326e20b4d22e5cfd24802f509b8a9d9fab287e21c9a6aa27ce
#-h:设置容器主机名
出现以下报错使用:systemctl restart docker
docker: Error response from daemon: driver failed programming external connectivity on endpoint test-02 (e5164ff8e230c5f3e3c52457132e5945ce8e106b18b586538853adeed47c29ce): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 84 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
[root@node02 consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"harbor-log":[],"httpd":[],"nginx":[]}[
(1)定义nginx upstream一个简单模板(在consul服务器上操作)
vim /opt/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
(2)定义一个server,监听8000端口,反向代理到upstream
server {
listen 8000;
server_name localhost 192.168.247.132;
access_log /var/log/nginx/group.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;
}
}
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin 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/
(1)修改配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
include mime.types;
include vhost/*.conf; #添加虚拟主机目录
default_type application/octet-stream;
......
(2)创建虚拟主机目录
[root@node02 nginx-1.24.0]# mkdir /usr/local/nginx/conf/vhost
(3)创建日志文件目录
[root@node02 nginx-1.24.0]# mkdir /var/log/nginx
(4)启动nginx
[root@node02 nginx-1.24.0]# nginx
[root@node02 nginx-1.24.0]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2023-07-26 17:56:19 CST; 50min ago
Process: 1170 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 1183 (nginx)
CGroup: /system.slice/nginx.service
├─1183 nginx: master process /usr/local/nginx/sbin/nginx
└─1189 nginx: worker process
7月 26 17:56:19 node02 systemd[1]: Starting nginx...
7月 26 17:56:19 node02 systemd[1]: Started nginx.
(1)配置template
[root@node02 opt]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@node02 opt]# cd /opt/
[root@node02 opt]# mv consul-template /usr/local/bin/
(2)在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程
[root@node02 opt]# consul-template --consul-addr 192.168.247.132:8500 \
> --template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/group.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
2023/07/26 11:26:49.018918 [INFO] consul-template v0.19.3 (ebf2d3d)
(3)另外打开一个终端查看生成配置文件
[root@node02 sbin]# cd /usr/local/nginx/conf/vhost
[root@node02 vhost]# ls
group.conf
[root@node02 vhost]# cat group.conf
upstream http_backend {
server 192.168.247.131:83;
server 192.168.247.131:84;
}
server {
listen 8000;
server_name localhost 192.168.247.132;
access_log /var/log/nginx/group.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;
}
}
docker 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 registrator
docker exec -it 55757a713bbd bash
echo "this is test1 web" > /usr/share/nginx/html/index.html
docker exec -it 2295f309fff8bash
echo "this is test2 web" > /usr/share/nginx/html/index.html
(1)增加一个 nginx 容器节点,测试服务发现及配置更新功能
docker run -itd -p:85:80 --name test-05 -h test05 nginx
(2)查看/usr/local/nginx/conf/vhost/group.conf 文件内容
cat /usr/local/nginx/conf/vhost/group.conf
upstream http_backend {
server 192.168.247.131:83;
server 192.168.247.131:84;
server 192.168.247.131:85;
}
(3)查看三台 nginx 容器日志,请求正常轮询到各个容器节点上
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05
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.247.132 &> /var/log/consul.log &
-enable-script-checks=true :设置检查服务为可用
-datacenter : 数据中心名称
-join :加入到已有的集群中
consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.247.132:8301 alive server 0.9.2 2 dc1
consul-server02 192.168.80.12:8301 alive server 0.9.2 2 dc1
consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.247.132:8300 192.168.247.132:8300 leader true 2
consul-server02 192.168.80.12:8300 192.168.80.12:8300 follower true 2