lb01、lb02服务器
规划:
HOSTNAME IP 说明
lb01 192.168.238.5 Keepalived主服务器(Nginx主负载均衡器)
lb02 192.168.238.6 Keepalived备服务器(Nginx辅负载均衡器)
web01 192.168.238.7 web01服务器
web02 192.168.238.8 web02服务器
#部署web01
[root@web01 conf.d]# cat /etc/nginx/conf.d/05_www.conf
server {
listen 80;
server_name www.yunwei.com;
root /data/www;
location / {
index index.html;
}
}
[root@web01 conf.d]# mkdir -p /data/www
[root@web01 conf.d]# echo web01 >/data/www/index.html
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# curl -H "host:www.yunwei.com" 192.168.238.7
web01
#部署web01
[root@web01 conf.d]# cat /etc/nginx/conf.d/05_www.conf
server {
listen 80;
server_name www.yunwei.com;
root /data/www;
location / {
index index.html;
}
}
[root@web01 conf.d]# mkdir -p /data/www
[root@web01 conf.d]# echo web02 >/data/www/index.html
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# curl -H "host:www.yunwei.com" 192.168.238.8
web02
#部署lb01、lb02
[root@lb01 conf.d]# cat /etc/nginx/conf.d/05_www.conf
upstream www {
server 172.16.1.7 weight=1;
server 172.16.1.8 weight=1;
}
server {
listen 80;
server_name www.yunwei.com;
location / {
proxy_pass http://www;
proxy_set_header Host $http_host;
}
}
nginx -t
systemctl restart nginx
#测试
[root@lb01 conf.d]# curl -H "host:www.yunwei.com" 192.168.238.5
web01
[root@lb01 conf.d]# curl -H "host:www.yunwei.com" 192.168.238.5
web02
[root@lb01 conf.d]# curl -H "host:www.yunwei.com" 192.168.238.6
web01
[root@lb01 conf.d]# curl -H "host:www.yunwei.com" 192.168.238.6
web02
#安装keepalived
#lb01、lb02分别安装keepalived
yum install keepalived -y
systemctl start keepalived #先不启动,修改过配置文件后在启动
#Keepalived配置文件说明
TOP HIERACHY
GLOBAL CONFIGURATION
VRRPD CONFIGURATION
LVS CONFIGURATION(管理LVS的,nginx此处删掉)
#Master配置说明。
[root@lb01 keepalived]# cat keepalived.conf
global_defs {
router_id lb01 #<==id为lb01,不同的keepalived.conf此ID要唯一。
}
vrrp_instance VI_1 { #<==实例名字为VI_1,相同实例的备节点名字要和这个相同。
state MASTER #<==状态为MASTER,备节点状态需要为BACKUP。
interface eth0 #<==通信接口为eth0,此参数备节点设置和主节点相同。
virtual_router_id 51 #<==实例ID为51,keepalived.conf里唯一。
priority 150 #<==优先级为150,备节点的优先级必须比此数字低。
advert_int 1 #<==通信检查间隔时间1秒。
authentication {
auth_type PASS #<==PASS认证类型,此参数备节点设置和主节点相同。
auth_pass 1111 #<==密码是1111,此参数备节点设置和主节点相同。
}
virtual_ipaddress {
192.168.238.3/24 dev eth0 label eth0:3 #<==虚拟IP,即VIP,子网掩码为24位,绑定接口为eth0,别名为eth0:3,此参数备节点设置和主节点相同。
}
} #提示:此处设置的虚拟IP为192.168.238.3,即网站域名绑定的IP。
配置 lb(单实例)
#配置 lb01 keepalved.conf
[root@lb01 ~]# cp /etc/keepalived/keepalived.conf{,.ori}
[root@lb01 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.3/24 dev eth0 label eth0:3
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|grep '192.168.238.3'
#配置 lb02 keepalved.conf
[root@lb02 ~]# cp /etc/keepalived/keepalived.conf{,.ori}
[root@lb02 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.3/24 dev eth0 label eth0:3
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|grep '192.168.238.3'
停止一端服务器。看另一端IP接管情况
配置 lb(多实例)
#配置 lb01 keepalved.conf
[root@lb01 conf.d]$cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.3/24 dev eth0 label eth0:3
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.4/24 dev eth0 label eth0:4
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|egrep '192.168.238.3|192.168.238.4'
#配置 lb02 keepalved.conf
[root@lb02 conf.d]$cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.3/24 dev eth0 label eth0:3
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.4/24 dev eth0 label eth0:4
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|egrep '192.168.238.3|192.168.238.4'
说明:单实例和多实例场景对比
单实例hosts
lb01:
192.168.238.3 www.yunwei.com blog.yunwei.com
lb02:热备 浪费机器
多实例hosts
两边跑服务;
lb01:
192.168.238.3 www.yunwei.com
lb02:
192.168.238.4 blog.yunwei.com
某一台宕机,总流量能被撑住即可。
LB配置脚本配置如下脚本【参考】
[root@lb01 scripts]# cat /server/scripts/chk_nginx_proxy.sh
#!/bin/sh
if [ `netstat -lntup|grep nginx|wc -l` -ne 1 ];then
systemctl stop keepalived
fi
[root@lb01 scripts]# chmod +x chk_nginx_proxy.sh
[root@lb01 scripts]# /server/scripts/chk_nginx_proxy.sh ##要可以执行
测试:
[root@lb01 scripts]# pkill nginx
[root@lb01 scripts]# sh chk_nginx_proxy.sh
[root@lb01 scripts]# ps -ef|egrep "nginx|keep"
[root@lb01 scripts]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_script chk_nginx_proxy { #<==定义vrrp脚本,检测HTTP端口。
script "/server/scripts/chk_nginx_proxy.sh" #<==执行脚本,当nginx服务有问题,就停掉keepalived服务。
interval 2 #<==间隔2秒。
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 53
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.3/24 dev eth0 label eth0:3
}
track_script {
chk_nginx_proxy #<==触发检查。
}
}
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.238.4/24 dev eth0 label eth0:4
}
}
[root@lb02 scripts]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_script chk_nginx_proxy { #<==定义vrrp脚本,检测HTTP端口。
script "/server/scripts/chk_nginx_proxy.sh" #<==执行脚本,当nginx服务有问题,就停掉keepalived服务。
interval 2 #<==间隔2秒。
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 53
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.3/24 dev eth0 label eth0:3
}
track_script {
chk_nginx_proxy #<==触发检查。
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.238.4/24 dev eth0 label eth0:4
}
}