docker-compose 运行 nginx -> haproxy -> zuul 集群的高可用
blank
blank
发布于 2019-08-29 / 954 阅读 / 0 评论 / 0 点赞

docker-compose 运行 nginx -> haproxy -> zuul 集群的高可用

当前系统环境

  • 192.168.10.51 nginx haproxy 运行服务器
  • 192.168.10.61:5555 zuul服务器
  • 192.168.10.62:5555 zuul服务器
  • 192.168.10.63:5555 zuul服务器

配置 docker-compose.yml

# 创建 haproxy nginx 数据配置目录
mkdir -p /docker/haproxy
# 创建 docker-compose.yml 文件
cat > /docker/haproxy/docker-compose.yml <<EOF
version: '3.1'

services:

  web:
    image: nginx
    container_name: nginx_haproxy
    restart: always
    ports:
      - 80:80
    environment:
      TZ : "Asia/Shanghai"
    volumes:
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d:/etc/nginx/conf.d/
      - ./nginx/log:/var/log/nginx/

  haproxy:
    image: haproxy
    container_name: haproxy
    restart: always
    ports:
        - 5554:5554
        - 9999:9999
    environment:
      TZ : "Asia/Shanghai"
    volumes:
      - ./haproxy/conf/:/usr/local/etc/haproxy
EOF

配置 haproxy.cfg

# 创建 haproxy 配置文件目录
mkdir -p /docker/haproxy/haproxy/conf
# 创建 haproxy 配置文件
cat > /docker/haproxy/haproxy/conf/haproxy.cfg <<EOF
global
    #日志输出配置,所有日志都记录在本机,通过local0输出
    #log /dev/log local0 notice
    log localhost local1 notice
    #改变当前的工作目录
    #chroot      /var/lib/haproxy
    #当前进程PID
    #pidfile     /var/run/haproxy.pid
    #最大连接数
    maxconn     5000
    #user        haproxy
    #group       haproxy
    #以守护进程方式运行haproxy #debug #quiet
    daemon

    # turn on stats unix socket
    #stats socket /var/lib/haproxy/stats

defaults
        #应用全局的日志配置
        log global
        #默认的模式mode{tcp|http|health}
        #tcp是4层,http是7层,health只返回OK
        mode http
        #日志类别
        option httplog
        option httpclose
    	option forwardfor
    	option originalto
        #不记录空日志
        option dontlognull
        #3次失败则认为服务不可用
        retries 3
        #连接超时
        timeout connect         10s
        #客户端超时
        timeout client          50s
        #服务端超时
        timeout server          50s
        #最大连接数
        maxconn 2000


# 监听 5554 端口 后端代理 zuul 负载均衡    
listen zuul
    bind *:5554
    #配置http模式
    mode http
    #加权轮询
    balance roundrobin
    #Zuul集群节点配置,其中node61~node63为zuul集群节点ip地址
    # weight - 调节服务器的负重
    # check - 允许对该服务器进行健康检查
    # inter - 设置连续的两次健康检查之间的时间,单位为毫秒(ms),默认值 2000(ms)
    # rise - 指定多少次连续成功的健康检查后,可认定该服务器处于可操作状态,默认值 2
    # fall - 指定多少次不成功的健康检查后,认为服务器为当掉状态,默认值 3
    # maxconn - 指定可被发送到该服务器的最大并发连接数
    server node61  192.168.10.61:5555  check inter 5s rise 2 fall 3
    server node62  192.168.10.62:5555  check inter 5s rise 2 fall 3
    server node63  192.168.10.63:5555  check inter 5s rise 2 fall 3

# haproxy 状态监控
listen stats 
  bind 0.0.0.0:9999
  mode http
  stats enable
  stats hide-version
  # haproxy 监控地址为 http://192.168.10.51:9999/
  stats uri /
  # haproxy监控 访问账号密码为 admin:123465
  stats auth admin:123465
EOF

配置nginx.conf zuul.conf

# 创建 nginx 默认配置文件目录
mkdir -p /docker/nginx/conf
# 创建 nginx 自定义配置文件目录
mkdir -p /docker/nginx/conf.d
# 创建 nginx 日志目录
mkdir -p /docker/nginx/log

# 创建 nginx 默认配置文件 nginx.conf
cat > /docker/haproxy/nginx/conf/nginx.conf <<EOF
user  nginx;
worker_processes  auto;

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;

    include /etc/nginx/conf.d/*.conf;
}
EOF

# 创建 nginx 的 zuul 代理配置文件 zuul.conf
cat > /docker/haproxy/nginx/conf.d/zuul.conf <<EOF
    #haproxy
    server {
	# 监听 80 端口
        listen       80;
        listen       [::]:80;
	# nginx 所在服务器ip 或域名
        server_name  192.168.10.51;


        charset utf-8;
	# 指定日志位置
        access_log  /var/log/nginx/zuul.access.log  main;

        location / {

             proxy_redirect off;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

             #请求代理到 haproxy 5554 服务端口
             proxy_pass http://192.168.10.51:5554;
        }
    }
EOF

启动服务

cd /docker/haproxy
# 以后台运行方式启动 haproxy 和 nginx 
docker-compose up -d

向 192.168.10.51:80 发送3次请求测试

nginx-haproxy-zuul-req-1

nginx-haproxy-zuul-req-2

nginx-haproxy-zuul-req-3

可以看到响应是由不同服务器返回的 说明配置成功

访问 192.168.10.51:9999 输入 admin 123465 登陆 haproxy 监控

haproxy-zuul


评论