# 创建挂载路径
mkdir /data/nginx_keep/nginx/conf -p
mkdir /data/nginx_keep/keepalived/vim nginx.conf
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log notice;
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;include /etc/nginx/conf.d/*.conf;upstream clickhouse {server 192.168.88.171:8123 weight=1;server 192.168.88.172:8123 weight=1;#server 192.168.88.173:8123 weight=1;ip_hash;}server {listen 8124;server_name localhost;location / {proxy_pass http://clickhouse;}error_page 500 502 503 504 /50x.html;}
}
启动nginx
# 准备nginx配置文件# 主机1
docker run -d -p 8124:8124 --name nginx-master \
-v /data/nginx_keep/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx_keep/nginx/logs:/var/log/nginx \
nginx:latest# 主机2
docker run -d -p 8124:8124 --name nginx-backup \
-v /data/nginx_keep/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx_keep/nginx/logs:/var/log/nginx \
nginx:latest
keepalived配置文件
# master
! Configuration File for keepalivedglobal_defs {router_id Ck_Master
}
# 检测脚本
#vrrp_script chk_nginx {
# script "/etc/keepalived/nginx_check.sh"
# interval 2
# weight -20
#}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 133mcast_src_ip 192.168.88.171priority 100nopreemptadvert_int 1## 设置验证信息,两个节点必须一致authentication {auth_type PASSauth_pass 1111}## 将 track_script 块加入 instance 配置块
# track_script {
# chk_nginx
# }# 虚拟 IP 池, 两个节点设置必须一样virtual_ipaddress {192.168.88.100}
}
-------------------------------------------------------------------------
# backup
! Configuration File for keepalivedglobal_defs {router_id Ck_BACKUP
}
#vrrp_script chk_nginx {
# script "/etc/keepalived/nginx_check.sh"
# interval 2
# weight -20
#}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 133mcast_src_ip 192.168.88.172priority 90nopreemptadvert_int 1## 设置验证信息,两个节点必须一致authentication {auth_type PASSauth_pass 1111}## 将 track_script 块加入 instance 配置块
# track_script {
# chk_nginx
# }# 虚拟 IP 池, 两个节点设置必须一样virtual_ipaddress {192.168.88.100}
}
启动keepalived
# 主机1
docker run -it --name keepalived_master \
--net=host --cap-add NET_ADMIN \
-v /data/nginx_keep/keepalived/keepalived.conf:/etc/keepalived/keepalived.conf \
-d fxyfdf/keepalived:2.0.8# 主机2
docker run -it --name keepalived_backup \
--net=host --cap-add NET_ADMIN \
-v /data/nginx_keep/keepalived/keepalived.conf:/etc/keepalived/keepalived.conf \
-d fxyfdf/keepalived:2.0.8
查看vip
ip a |grep 192.168.88.100