环境准备
server1 192.168.200.111:nginx + keepalived master
server2 192.168.200.112:nginx + keepalived backup
server3 192.168.200.113:httpd(apache)
server4 192.168.200.115:httpd (apache)
虚拟ip(VIP):192.168.200.254,对外提供服务的ip,也可称作浮动ip
apache做应用服务器
两台nginx做负载均衡,配置一样
#keepalive_timeout 0;
keepalive_timeout 65;
upstream httpd_server {
server 192.168.200.113 weight=1; // 此处为两个apache 服务器的地址
server 192.168.200.115 weight=1;
}
#gzip on;
keepalive_timeout 65;
upstream httpd_server {
server 192.168.200.113 weight=1; // 此处为两个apache 服务器的地址
server 192.168.200.115 weight=1;
}
#gzip on;
server {
listen 80;
server_name localhost;
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://httpd_server; // nginx做负载均衡
proxy_set_header Host $http_host;
}
root html;
index index.html index.htm;
proxy_pass http://httpd_server; // nginx做负载均衡
proxy_set_header Host $http_host;
}
#error_page 404 /404.html;
配置好后 启动nginx
keepalived实现nginx高可用(HA)
keepalived主要起到两个作用:实现VIP到本地ip的映射; 以及检测nginx状态。
master上的keepalived.conf内容如下:
! Configuration File for keepalived
global_defs {
router_id 192.168.200.111 //不重要,可有可无,也可以不改
}
vrrp_script check_nginx { //引用脚本文件
global_defs {
router_id 192.168.200.111 //不重要,可有可无,也可以不改
}
vrrp_script check_nginx { //引用脚本文件
script "/root/nginx_check.sh" //脚本文件所在位置,绝对路径
interval 2 //检测脚本执行的间隔,单位是秒
weight -20 // 减少权重 (减少后比主要少)
}
vrrp_instance VI_1 {
state MASTER //指定keepalived的角色,MASTER为主,BACKUP为备
interface ens32 //指定当前网卡
virtual_router_id 51 //虚拟路由编号,主从相同
priority 100 //优先级,数值越大优先级越大
advert_int 1 //检查间隔 ,默认为1秒
authentication {
auth_type PASS 主从一致
auth_pass bhz 主从一致
}
}
vrrp_instance VI_1 {
state MASTER //指定keepalived的角色,MASTER为主,BACKUP为备
interface ens32 //指定当前网卡
virtual_router_id 51 //虚拟路由编号,主从相同
priority 100 //优先级,数值越大优先级越大
advert_int 1 //检查间隔 ,默认为1秒
authentication {
auth_type PASS 主从一致
auth_pass bhz 主从一致
}
virtual_ipaddress { // 定义虚拟ip (VIP),可多设,每行一个
192.168.200.213
}
}
track_script {
check_nginx // 调用检测脚本
}
check_nginx // 调用检测脚本
}
主从服务器keepalived设置基本一样,不同地方:优先级数值 和 角色分别是MASTER和BACKUP.
nginx检测/root/nginx_check.sh脚本内容如下
#!/bin/bash
count="$(ps -C nginx --no-header| wc -l)"
if [ $count -eq 0 ]; then
nginx 重启nginx
sleep 2
if [ 'ps -c nginx --no-header' | wc -l -eq 0 ]; then //nginx重启失败
systemctl stop keepalived
fi
fi
count="$(ps -C nginx --no-header| wc -l)"
if [ $count -eq 0 ]; then
nginx 重启nginx
sleep 2
if [ 'ps -c nginx --no-header' | wc -l -eq 0 ]; then //nginx重启失败
systemctl stop keepalived
fi
fi
脚本加权限
chmod +x /root/nginx_check.sh
启动keepalived
systemctl restart keepalived
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:62:c0:d3 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.111/24 brd 192.168.200.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet 192.168.200.213/32 scope global ens32 //可以看到多了一个虚拟网卡
valid_lft forever preferred_lft forever
inet6 fe80::f277:e538:4157:4d2b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:62:c0:d3 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.111/24 brd 192.168.200.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet 192.168.200.213/32 scope global ens32 //可以看到多了一个虚拟网卡
valid_lft forever preferred_lft forever
inet6 fe80::f277:e538:4157:4d2b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
apache服务器1创建测试页 echo "222222" > /var/www/html/index.html
apache服务器2创建测试也 ccho "1111111" > /var/www/html/index.html
打开网页测试