赞
踩
docker network create net1
docker pull webdevops/php-apache:7.4
# 创建第一个php节点,并作端口与目录映射 docker run -it -d --name a1 \ -p 8081:80 \ -v ~/apache/app1:/app \ --net=net1 \ webdevops/php-apache:7.4 # 创建第二个节点 docker run -it -d --name a2 \ -p 8082:80 \ -v ~/apache/app2:/app \ --net=net1 \ webdevops/php-apache:7.4 # 创建第三个 docker run -it -d --name a3 \ -p 8083:80 \ -v ~/apache/app3:/app \ --net=net1 \ webdevops/php-apache:7.4
docker pull nginx
docker run --name n1 \
-p 6101:80 \
-v ~/nginx/conf.d:/etc/nginx/conf.d \
-v ~/nginx/nginx.conf:/etc/nginx/nginx.conf \
--net=net1 \
--privileged \
-d nginx
# 宿主机目录 vim ~/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream webs { # 刚才所创建的三个节点 对应的 宿主机IP:端口 server 192.168.5.144:8081; server 192.168.5.144:8082; server 192.168.5.144:8083; } include /etc/nginx/conf.d/*.conf; }
主要是 upstream webs { }这块, 后边的webs名字可以自己定义, 但是要与后边的配置保持一致
# 进入宿主机目录 vim ~/nginx/conf.d/default.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; proxy_pass http://webs; proxy_set_header Host $host; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
主要是 location / { }这块 , proxy_pass http://webs; 名字webs与前面的配置保持一致
# 查看防火墙开放端口
firewall-cmd --permanent --list-ports
# 添加开放端口
firewall-cmd --permanent --add-port=6101-6102/tcp
firewall-cmd --permanent --add-port=8081-8083/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8081-8083/tcp
# 重新加载
firewall-cmd --reload
# 暂停a1 节点
docker pause a1
# 开启
docker unpause a1
# 创建第二个Nginx节点
docker run --name n2 \
-p 6102:80 \
-v ~/nginx/conf.d:/etc/nginx/conf.d \
-v ~/nginx/nginx.conf:/etc/nginx/nginx.conf \
--net=net1 \
--privileged \
-d nginx
# 进入n1 容器, n2容器 docker exec -it n1 bash docker exec -it n2 bash # 更新 apt-get update # 安装 keepalived apt-get install keepalived # 安装 vim apt-get install vim # 修改配置文件 vim /etc/keepalived/keepalived.conf vrrp_instance VI_1 { ## 指定Keepalived的身份(MASTER主服务器, BACKUP备服务器)主服务器要抢占IP, 备用服务器不会抢占IP, 如果把所有Keepalived节点都设置成MASTER,这些节点启动之后都会去争抢IP,只有一个节点抢到,其他节点身份降为BACKUP state MASTER ## 网卡设备 eth0是Docker虚拟机的网卡,该网卡在局域网看不到,把虚拟IP写到Docker网卡内,宿主机是可以访问该网卡,局域网其他电脑不能访问,所以需要在宿主机上把eth0网卡内的虚拟IP映射到局域网上的某个虚拟IP上 interface eth0 ## 虚拟路由标识, MASTER和BACKUP的虚拟路由标识必须一致,标识可以是0~255 virtual_router_id 55 ## 权重 MASTER权重要高于BACKUP,数字越大优先级越高,优先抢到虚拟IP priority 100 ## MASTER与BACKUP节点间同步检测的时间间隔,单位为秒,主备之间必须一致 advert_int 1 ## 心跳检测需要登录到某一节点,主从服务器验证方式,主备必须使用相同的密码才能正常通信 authentication { auth_type PASS auth_pass 123456 } ## 虚拟IP地址,可以设置多个虚拟IP地址,每行一个,只能在Docker内部可见 virtual_ipaddress { 172.17.0.201 } }
## 在容器内启动
service keepalived start
## 在宿主机中查看Keepalived是否配置成功
ping 172.17.0.201
## 在宿主机安装Keepalived yum -y install keepalived ## 配置文件 vim /etc/keepalived/keepalived.conf ## 定义局域网内的虚拟IP vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 75 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.5.155 } } ## 为虚拟IP设置转发端口,应用程序的请求是发送到局域网的虚拟IP,局域网的虚拟IP将请求转发到Docker的虚拟IP ## 宿主机IP: virtual_server 192.168.5.155 8080{ ## 心跳检测间隔 3秒 delay_loop 3 ## 轮询转发 lb_algo rr ## NAT模式 lb_kind NAT persistence_timeout 50 protocol TCP ## 要转发到的Docker虚拟IP:对应的Nginx容器 real_server 172.17.0.201 80{ ## 权重 weight 1 } } ## 在宿主机上启动Keepalived service keepalived start ## 测试 ping 192.168.5.155
# 暂停 Nginx节点 n1
docker pause n1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。