├── node_1 ├── Dockerfile ├── docker-compose.yml ├── keepalived.conf └── nginx.conf ├── node_2 ├── Dockerfile ├── docker-compose.yml ├── keepalived.conf └── nginx.conf └── README.md /node_1/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM osixia/keepalived 2 | MAINTAINER wurang 3 | 4 | ADD ./keepalived.conf /container/service/keepalived/assets/keepalived.conf -------------------------------------------------------------------------------- /node_2/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM osixia/keepalived 2 | MAINTAINER wurang 3 | 4 | ADD ./keepalived.conf /container/service/keepalived/assets/keepalived.conf -------------------------------------------------------------------------------- /node_1/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | keepalived: 5 | build: ./ 6 | depends_on: 7 | - nginx 8 | network_mode: "host" 9 | cap_drop: 10 | - NET_ADMIN 11 | privileged: true 12 | restart: on-failure:3 13 | nginx: 14 | image: nginx 15 | ports: 16 | - "80:80" 17 | volumes: 18 | - "/etc/cluster/nginx.conf:/etc/nginx/nginx.conf" 19 | restart: on-failure:3 20 | -------------------------------------------------------------------------------- /node_2/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | keepalived: 5 | build: ./ 6 | depends_on: 7 | - nginx 8 | network_mode: "host" 9 | cap_drop: 10 | - NET_ADMIN 11 | privileged: true 12 | restart: on-failure:3 13 | nginx: 14 | image: nginx 15 | ports: 16 | - "80:80" 17 | volumes: 18 | - "/etc/cluster/nginx.conf:/etc/nginx/nginx.conf" 19 | restart: on-failure:3 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker-Keepalived-Nginx 2 | 利用docker快速构建Nginx+Keepalived双主集群实现负载均衡 3 | 4 | **目录结构:** 5 | 6 | - node_1 7 | - docker-compose.yml 8 | - Dockerfile 9 | - keepalived.conf 10 | - nginx.conf 11 | - node_2 同上 12 | 13 | 分别用于部署在两台不同的服务器上 14 | 15 | **使用简介:** 16 | 17 | - nginx.conf 18 | 19 | 配置nginx以实现负载均衡,node_1和node_2一般情况下相同 20 | 21 | - keepalived.conf 22 | 23 | 默认使用双BACKUP,nopreempt非抢占模式,根据需要修改 24 | 25 | - interface 26 | - auth_pass 27 | - unicast_peer 28 | - virtual_ipaddress 29 | 30 | - docker-compose 31 | 32 | 修改宿主机和nginx容器的端口映射 33 | 34 | 拷贝修改后的node_1和node_2到宿主机,进入目录执行 35 | 36 | ```shell 37 | docker-compose up 38 | ``` 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /node_2/keepalived.conf: -------------------------------------------------------------------------------- 1 | global_defs { 2 | 3 | } 4 | 5 | vrrp_instance VI_1 { 6 | state BACKUP 7 | interface ens33 8 | virtual_router_id 51 9 | priority 99 10 | nopreempt 11 | authentication { 12 | auth_type PASS 13 | auth_pass 1111 14 | } 15 | unicast_peer { 16 | 192.168.21.130 17 | } 18 | virtual_ipaddress { 19 | 192.168.21.51 20 | } 21 | notify "/container/service/keepalived/assets/notify.sh" 22 | } 23 | 24 | vrrp_instance VI_2 { 25 | state BACKUP 26 | interface ens33 27 | virtual_router_id 52 28 | priority 150 29 | nopreempt 30 | authentication { 31 | auth_type PASS 32 | auth_pass 1111 33 | } 34 | unicast_peer { 35 | 192.168.21.130 36 | } 37 | virtual_ipaddress { 38 | 192.168.21.52 39 | } 40 | notify "/container/service/keepalived/assets/notify.sh" 41 | } -------------------------------------------------------------------------------- /node_1/keepalived.conf: -------------------------------------------------------------------------------- 1 | global_defs { 2 | 3 | } 4 | 5 | vrrp_instance VI_1 { 6 | state BACKUP 7 | interface ens33 8 | virtual_router_id 51 9 | priority 150 10 | nopreempt 11 | authentication { 12 | auth_type PASS 13 | auth_pass 1111 14 | } 15 | unicast_peer { 16 | 192.168.21.131 17 | } 18 | virtual_ipaddress { 19 | 192.168.21.51 20 | } 21 | notify "/container/service/keepalived/assets/notify.sh" 22 | } 23 | 24 | vrrp_instance VI_2 { 25 | state BACKUP 26 | interface ens33 27 | virtual_router_id 52 28 | priority 99 29 | nopreempt 30 | authentication { 31 | auth_type PASS 32 | auth_pass 1111 33 | } 34 | unicast_peer { 35 | 192.168.21.131 36 | } 37 | virtual_ipaddress { 38 | 192.168.21.52 39 | } 40 | notify "/container/service/keepalived/assets/notify.sh" 41 | } 42 | -------------------------------------------------------------------------------- /node_1/nginx.conf: -------------------------------------------------------------------------------- 1 | user nginx; 2 | worker_processes 1; 3 | 4 | error_log /var/log/nginx/error.log warn; 5 | pid /var/run/nginx.pid; 6 | 7 | 8 | events { 9 | worker_connections 1024; 10 | } 11 | 12 | 13 | http { 14 | include /etc/nginx/mime.types; 15 | default_type application/octet-stream; 16 | 17 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 18 | '$status $body_bytes_sent "$http_referer" ' 19 | '"$http_user_agent" "$http_x_forwarded_for"'; 20 | 21 | access_log /var/log/nginx/access.log main; 22 | 23 | sendfile on; 24 | #tcp_nopush on; 25 | 26 | keepalive_timeout 65; 27 | 28 | gzip on; 29 | 30 | upstream api { 31 | server 192.168.21.132:8080 max_fails=1; 32 | server 192.168.21.133:8080 max_fails=1; 33 | } 34 | 35 | server 36 | { 37 | listen 80; 38 | location / { 39 | proxy_next_upstream error timeout http_500 http_502 http_504; 40 | proxy_read_timeout 10s; 41 | proxy_pass http://api; 42 | proxy_set_header Host $host; 43 | proxy_set_header X-Real-IP $remote_addr; 44 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /node_2/nginx.conf: -------------------------------------------------------------------------------- 1 | user nginx; 2 | worker_processes 1; 3 | 4 | error_log /var/log/nginx/error.log warn; 5 | pid /var/run/nginx.pid; 6 | 7 | 8 | events { 9 | worker_connections 1024; 10 | } 11 | 12 | 13 | http { 14 | include /etc/nginx/mime.types; 15 | default_type application/octet-stream; 16 | 17 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 18 | '$status $body_bytes_sent "$http_referer" ' 19 | '"$http_user_agent" "$http_x_forwarded_for"'; 20 | 21 | access_log /var/log/nginx/access.log main; 22 | 23 | sendfile on; 24 | #tcp_nopush on; 25 | 26 | keepalive_timeout 65; 27 | 28 | gzip on; 29 | 30 | upstream api { 31 | server 192.168.21.132:8080 max_fails=1; 32 | server 192.168.21.133:8080 max_fails=1; 33 | } 34 | 35 | server 36 | { 37 | listen 80; 38 | location / { 39 | proxy_next_upstream error timeout http_500 http_502 http_504; 40 | proxy_read_timeout 10s; 41 | proxy_pass http://api; 42 | proxy_set_header Host $host; 43 | proxy_set_header X-Real-IP $remote_addr; 44 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 45 | } 46 | } 47 | } 48 | --------------------------------------------------------------------------------