Docker部署Nginx+Keepalived

# 创建挂载路径
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 keepalived

global_defs {
  router_id Ck_Master
}
# 检测脚本
#vrrp_script chk_nginx {
#       script "/etc/keepalived/nginx_check.sh"
#       interval 2
#       weight -20
#}

vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 133
        mcast_src_ip 192.168.88.171
        priority 100
        nopreempt
        advert_int 1
        ## 设置验证信息,两个节点必须一致
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        ## 将 track_script 块加入 instance 配置块
#       track_script {
#               chk_nginx
#       }
        # 虚拟 IP 池, 两个节点设置必须一样
        virtual_ipaddress {
                192.168.88.100
        }
}
-------------------------------------------------------------------------
# backup
! Configuration File for keepalived

global_defs {
  router_id Ck_BACKUP
}
#vrrp_script chk_nginx {
#       script "/etc/keepalived/nginx_check.sh"
#       interval 2
#       weight -20
#}

vrrp_instance VI_1 {
        state BACKUP
        interface ens33
        virtual_router_id 133
        mcast_src_ip 192.168.88.172
        priority 90
        nopreempt
        advert_int 1
        ## 设置验证信息,两个节点必须一致
        authentication {
                auth_type PASS
                auth_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

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值