keekalived+nginx 高可用

Posted john5的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keekalived+nginx 高可用相关的知识,希望对你有一定的参考价值。

  1. 高可用环境准备

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

[[email protected]~]#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. 后端服务器站点目录文件

[[email protected]~]#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;

}????

}

?

?

?

[[email protected]~]#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. 后端服务器网页文件

[[email protected]~]#cat /etc/nginx/html/www/bingbing.html

www web01

[[email protected]~]#cat /etc/nginx/html/bbs/bingbing.html

bbs web01

?

  1. 2台lb 前端配置

  2. nginx负载均衡配置

[[email protected] ~]# 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配置

[[email protected] ~]# cat /etc/keepalived/keepalived.conf

[[email protected] ~]# 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 配置

[[email protected] 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

[[email protected] ~]# echo ‘net.ipv4.ip_nonlocal_bind = 1‘ >>/etc/sysctl.conf

[[email protected] ~]# sysctl -p

  1. 前端解析

[[email protected] ~]# 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

?

测试:因为是高可用

[[email protected] ~]# curl -H Host:www.etiantian.org 192.168.2.3/bingbing.html

www web01

[[email protected] ~]# curl -H Host:www.etiantian.org 192.168.2.3/bingbing.html

www web01

[[email protected] ~]# curl -H Host:bbs.etiantian.org 192.168.2.4/bingbing.html

bbs web01

[[email protected] ~]# curl -H Host:bbs.etiantian.org 192.168.2.4/bingbing.html

bbs web01

以上是关于keekalived+nginx 高可用的主要内容,如果未能解决你的问题,请参考以下文章

Nginx高可用

Nginx keepalived 高可用集群

nginx高可用

nginx内置高可用配置与第三方高可用模块nginx_ustream_check_mudule配置

Nginx高可用

Nginx高可用