Nginx+keepalived realizes load balancing and high availability apache as the server
Agent introduction
Configuration | IP address | Function |
---|---|---|
nginx,keepalived | 192.168.108.10 | nginx reverse proxy apache application, keepalived high availability (master) |
nginx,keepalived | 192.168.108.20 | nginx reverse proxy Baidu address, keepalived high availability (slave) |
none | 192.168.108.100 | Virtual IP address (VIP) |
apache | 192.168.108.30 | application |
apache | 192.168.108.40 | application |
Installation configuration
#安装keepalived配置
yum -y install keepalived
#使用yum安装nginx配置
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
#安装apache
yum -y install httpd
Preliminary preparation
systemctl stop firewalld
setenforce 0
[root@nginx1 ~]#cat /usr/share/nginx/html/index.html
<h1>Welcome to nginx! 10</h1>
<h2>192.168.108.10</h2>
[root@nginx2 ~]#cat /usr/share/nginx/html/index.html
<h1>Welcome to nginx! 20</h1>
<h2>192.168.108.20</h2>
[root@apache1 ~]# cat /var/www/html/index.html
<h1>This is my first apache application </h1>
[root@apache2 ~]# cat /var/www/html/index.html
<h1>This is my second apache application</h1>
nginx main configuration file
https://blog.csdn.net/zxd1435513775/article/details/102508463
Nginx load balancing
nginx load balancing configuration
Among them, the server_name field is used to match the host name of the user accessing resources. The proxy_pass field also indicates transferred access.
After location hits
If it is root, the request URL will be ip/域名+port
replaced with the directory specified by root to access the resource.
If it is alias, the request URL will be ip/域名+port+匹配到的路径
replaced with the directory specified by alias to access the resource.
vim /etc/nginx/nginx.conf
#http模块中
upstream apacheserver{
server 192.168.108.30;
server 192.168.108.40;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://apacheserver;
root html;
index index.html index.htm;
}
Supplement: configure domain name
#将上面配置文件中的server_name修改为 www.123.com,不需要修改hosts文件
[root@nginx1 ~]# curl www.123.com
<h1>This is my first apache application </h1>
<h2>192.168.108.30</h2>
[root@nginx1 ~]# curl www.123.com
<h1>This is my first apache application </h1>
<h2>192.168.108.30</h2>
[root@nginx1 ~]# curl www.123.com
<h1>This is my second apache application </h1>
<h2>192.168.108.40</h2>
keepalived high availability
Nginx configuration high availability (Nginx+keepalived) - wenxuehai - Blog Park (cnblogs.com)
main configuration file
master keepalived master configuration
【master】
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
# keepalived服务宕机异常出现的时候,发送通知邮件 可以是多个
[email protected] # 收件人邮箱1
[email protected] # 收件人邮箱2
[email protected] # 收件人邮箱3
}
notification_email_from [email protected] #邮件发件人
smtp_ server 192.168.108.10 #主服务器的ip地址。邮件服务器地址
smtp_connect_timeout 30 # 超时时间
router_id LVS_DEVEL # 机器标识 局域网内唯一即可。 LVS_DEVEL这字段在/etc/hosts文件中看;通过它访问到主机
}
vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh" #检测脚本存放的路径
interval 2 # 检测脚本执行的间隔,即检测脚本每隔2s会自动执行一次
weight 2 #权重,如果这个脚本检测为真,服务器权重+2
}
vrrp_instance VI_1 {
state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备。备份服务器上需将MASTER 改为BACKUP
interface ens33 # 通信端口 通过ip addr可以看到,根据自己的机器配置
virtual_router_id 51 # vrrp实例id keepalived集群的实例id必须一致,即主、备机的virtual_router_id必须相同
priority 100 #优先级,数值越大,获取处理请求的优先级越高。主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1 #心跳间隔,默认为1s。keepalived多机器集群 通过心跳检测当前服务器是否还正常工作,如果发送心跳没反应,备份服务器就会立刻接管;
authentication {
# 服务器之间通信密码
auth type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
auth pass 1111
}
virtual_ipaddress {
# 自定义虚拟IP。自定义的虚拟ip得根据真实ip设置。比如真实ip是192.168.91.138,那么虚拟ip可以设置为192.168.91.139~255,前面三个数得一致
192.168.108.100 # 定义虚拟ip(VIP),可多设,每行一个
}
}
master nginx monitoring script
【master 脚本】
vim /usr/local/sbin/check_ng.sh
#! /bin/bash
#检测nginx是否启动了
A=`ps -C nginx -no-header | wc - 1`
if [ $A -eq 0];then #如果nginx没有启动就启动nginx
/usr/local/nginx/sbin/nginx #通过Nginx的启动脚本来重启nginx
sleep 2
if [`ps -C nginx --no-header| wc -1` -eq 0 ];then #如果nginx重启失败,则下面就会停掉keepalived服务,进行VIP转移
killall keepalived
fi
fi
configuration file
slave keepalived master configuration file
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_ server 192.168.108.20 #备份服务器的ip地址
smtp_connect_timeout 30
router_id LVS_DEVEL # LVS_DEVEL这字段在/etc/hosts文件中看;通过它访问到主机
}
vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh" #检测脚本
interval 2 # (检测脚本执行的间隔)2s
weight 2 #权重,如果这个脚本检测为真,服务器权重+2
}
vrrp_instance VI_1 {
state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备。备份服务器上需将MASTER 改为BACKUP
interface ens33 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
virtual_router_id 51 # 虚拟路由编号,主、备机的virtual_router_id必须相同
priority 90 #优先级,数值越大,获取处理请求的优先级越高。主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数),每隔1s发送一次心跳
authentication {
# 校验方式, 类型是密码,密码1111
auth type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
auth pass 1111
}
virtual_ipaddress {
# 虛拟ip
192.168.108.100 # 定义虚拟ip(VIP),可多设,每行一个
}
}
slave nginx monitoring script
vim /usr/local/src/nginx_check.sh
#! /bin/bash
#检测nginx是否启动了
A=`ps -C nginx -no-header | wc - 1`
if [ $A -eq 0];then #如果nginx没有启动就启动nginx
/usr/local/nginx/sbin/nginx #通过Nginx的启动脚本来重启nginx
sleep 2
if [`ps -C nginx --no-header| wc -1` -eq 0 ];then #如果nginx重启失败,则下面就会停掉keepalived服务,进行VIP转移
killall keepalived
fi
fi
Service start
systemctl restart keepalived
systemctl restart nginx
Verify configuration
Master machine IP address ip a
Virtual IP address 192.168.108.100
Access using virtual IP 192.168.108.100
Close main service
Close keepalived and nginx on the master machine, and switch the virtual IP address to the slave machine
systemctl stop keepalived
systemctl stop nginx
**Access virtual IP address**192.168.108.100