" name="sm-site-verification"/>
侧边栏壁纸
博主头像
PySuper博主等级

千里之行,始于足下

  • 累计撰写 203 篇文章
  • 累计创建 14 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Nginx + keepalived 实现Load Balance

PySuper
2020-07-03 / 0 评论 / 1 点赞 / 19 阅读 / 3990 字
温馨提示:
所有牛逼的人都有一段苦逼的岁月。 但是你只要像SB一样去坚持,终将牛逼!!! ✊✊✊

Nginx

创建容器:# docker run -itd --name nginx_for_ha -p 8000:80 nginx:latest

配置:vi 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 flask_pool 
    {
        server 192.168.11.98:5000 weight=4 max_fails=2 fail_timeout=30s;
        server 192.168.11.57:5000 weight=4 max_fails=2 fail_timeout=30s;
    }
    server {
        listen    80;
        server_name    localhost;
        location / {
            proxy_pass http://flask_pool;    #转向flask处理
        }
    }
    include /etc/nginx/conf.d/*.conf;
}

# docker restart nginx

keepalived

192.168.11.25 nginx负载均衡 + keepalived(master)
# docker-compose.yml for keepalived
  version: '3'
  services:
  keepalived:
  image: keepalived:x86
  volumes:
  - /var/run/docker.sock:/var/run/docker.sock
  - /home/keepalived/check_ng.sh:/container/service/keepalived/assets/check_ng.sh
  environment:
  - KEEPALIVED_INTERFACE=eno1 # 25宿主机网卡信息  命令是:ip route |awk '$2=="via" {print $5}' |head -1
  - KEEPALIVED_STATE=BACKUP  # 表示该节点是keepalived和备节点
  - KEEPALIVED_PRIORITY=90   #
  - KEEPALIVED_VIRTUAL_IPS=192.168.11.58 #  虚拟ip
  - KEEPALIVED_UNICAST_PEERS=192.168.11.200 #  取keepalived主节点的宿主机ip
  - KEEPALIVED_ROUTER_ID=25 # 主备节点通信标志,要一致
  privileged: true
  restart: always
  container_name: keepalived
  network_mode: host
192.168.11.200 nginx负载均衡 + keepalived(backup)

# docker-compose.yml for keepalived
  version: '3'
  services:
  keepalived:
  image: keepalived:x86
  volumes:
  - /var/run/docker.sock:/var/run/docker.sock
  - /home/keepalived/check_ng.sh:/container/service/keepalived/assets/check_ng.sh
  environment:
  - KEEPALIVED_INTERFACE=eno1 # 25宿主机网卡信息  命令是:ip route |awk '$2=="via" {print $5}' |head -1
  - KEEPALIVED_STATE=BACKUP  # 表示该节点是keepalived和备节点
  - KEEPALIVED_PRIORITY=90   #
  - KEEPALIVED_VIRTUAL_IPS=192.168.11.58 #  虚拟ip
  - KEEPALIVED_UNICAST_PEERS=192.168.11.200 #  取keepalived主节点的宿主机ip
  - KEEPALIVED_ROUTER_ID=25
  privileged: true
  restart: always
  container_name: keepalived
  network_mode: host

docker-compose up -d # docker-compose所在目录

MetalLB和Nginx+keepalived


1

评论区