keekalived+nginx 高可用

  1. 高可用环境准备

  2. 后端服务器主配置文件

[192.168.2.7-root@web01~]#cat /etc/nginx/nginx.conf

user www;

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;

include /etc/nginx/conf.d/www.conf;

include /etc/nginx/conf.d/bbs.conf;

}

 

  1. 后端服务器站点目录文件

[192.168.2.7-root@web01~]#cat /etc/nginx/conf.d/bbs.conf

server {

    listen 80;

    index index.html;

    server_name bbs.etiantian.org;

location / {

    root html/bbs;

    index index.html;

}    

}

 

 

 

[192.168.2.7-root@web01~]#cat /etc/nginx/conf.d/www.conf

server {

    listen 80;

    index index.html;

    server_name www.etiantian.org;

location / {

    root html/www;

    index index.html;

}    

}

 

  1. 后端服务器网页文件

[192.168.2.7-root@web01~]#cat /etc/nginx/html/www/bingbing.html

www web01

[192.168.2.7-root@web01~]#cat /etc/nginx/html/bbs/bingbing.html

bbs web01

 

  1. 2台lb 前端配置

  2. nginx负载均衡配置

[root@lb01 ~]# cat /etc/nginx/nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

gzip on;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

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;

upstream spool {

server 192.168.2.7:80 weight=1 max_conns=2999 max_fails=1 fail_timeout=3s;

server 192.168.2.8:80 weight=1 max_conns=4000 max_fails=2 fail_timeout=3s;

}

server {

listen 192.168.2.3:80;

server_name www.eitantian.org;

location / {

proxy_pass http://spool;

     include proxy.conf;

}

}

server {

listen 192.168.2.4:80;

server_name bbs.etiantian.org;

location / {

proxy_pass http://spool;

     include proxy.conf;

}

}

}

 

  1. keepailved配置双主配置

    1. lb01 keepalived配置

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf

global_defs {

router_id lb01

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.2.3/24 dev eth0 label eth0:3

}

}

 

vrrp_script chk_nginx_proxy {

script "/server/scripts/chk_nginx_proxy.sh"

interval 2

weight 2

}

vrrp_instance VI_2 {

state BACKUP

interface eth0

virtual_router_id 52

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.2.4/24 dev eth0 label eth0:4

}

track_script {

chk_nginx_proxy

}

}

  1. lb 02 keepalived 配置

[root@lb02 scripts]# cat /etc/keepalived/keepalived.conf

global_defs {

router_id lb02

}

 

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.2.3/24 dev eth0 label eth0:3

}

}

vrrp_script chk_nginx_proxy {

script "/server/scripts/chk_nginx_proxy.sh"

interval 2

weight 2

}

 

vrrp_instance VI_2 {

state MASTER

interface eth0

virtual_router_id 52

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.2.4/24 dev eth0 label eth0:4

}

track_script {

chk_nginx_proxy

}

}

  1. 让内核识别网卡ip

[root@lb01 ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf

[root@lb01 ~]# sysctl -p

  1. 前端解析

[root@lb01 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.2.3 www.eitantian.org

192.168.2.4 bbs.eitantian.org

 

windows本地解析

192.168.2.3 www.etiantian.org

192.168.2.4 bbs.etiantian.org

 

测试:因为是高可用

[root@nfs ~]# curl -H Host:www.etiantian.org 192.168.2.3/bingbing.html

www web01

[root@nfs ~]# curl -H Host:www.etiantian.org 192.168.2.3/bingbing.html

www web01

[root@nfs ~]# curl -H Host:bbs.etiantian.org 192.168.2.4/bingbing.html

bbs web01

[root@nfs ~]# curl -H Host:bbs.etiantian.org 192.168.2.4/bingbing.html

bbs web01

原文地址:https://www.cnblogs.com/john5yang/p/10394855.html