rs配置:
[root@rs1 httpd]# vim conf.d/vhosts.conf#配置三个httpd虚拟主机
<VirtualHost 192.168.1.11:80>
ServerName 192.168.1.11
DocumentRoot "/data/web/vhost1"
<Directory "/data/web/vhost1">
options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.12:80>
ServerName 192.168.1.12
DocumentRoot "/data/web/vhost2"
<Directory "/data/web/vhost2">
options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.13:80>
ServerName 192.168.1.13
DocumentRoot "/data/web/vhost3"
<Directory "/data/web/vhost3">
options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@rs1 httpd]# mkdir -pv /data/web/vhost{1,2,3} #创建网页资源目录
mkdir: 已创建目录 "/data/web"
mkdir: 已创建目录 "/data/web/vhost1"
mkdir: 已创建目录 "/data/web/vhost2"
mkdir: 已创建目录 "/data/web/vhost3"
[root@rs1 httpd]# echo "<h1>Vhost1</h1>" > /data/web/vhost1/index.html
[root@rs1 httpd]# echo "<h1>Vhost2</h1>" > /data/web/vhost2/index.html
[root@rs1 httpd]# echo "<h1>Vhost3</h1>" > /data/web/vhost3/index.html
节点1主机设置:
[root@keepalive21 ~]# cd /etc/keepalived/
[root@keepalive21 keepalived]# vim notify.sh#编辑邮件脚本
#!/bin/bash
#
contact='root@localhost'
notify() {
local mailsubject="$(hostname) to be $1, vip floating"
local mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
systemctl start nginx.service
notify master
;;
backup)
systemctl start nginx.service
notify backup
;;
fault)
systemctl stop nginx.service
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
[root@keepalive21 ~]# yum -y install nginx #安装nginx
[root@keepalive21 ~]# vim /etc/nginx/nginx.conf#配置nginx
.....
upstream websrvs { #后端服务器组
server 192.168.1.11:80;
server 192.168.1.12:80;
server 192.168.1.13:80;
}
# Load modular configuration files from the /etc/nginx/conf.d directory.
.....
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://websrvs;
}
[root@keepalive21 ~]# vim /etc/keepalived/keepalived.conf#配置keepalived
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id keepalive130
vrrp_mcast_group4 224.1.101.33
}
vrrp_script chk_down{
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -10
fall 1
rise 1
}
vrrp_script chk_ngx {
script "killall -0 nginx && exit 0 || exit 1"
weight -10
interval 2
fall 3
rise 3
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 33
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass a1b2c3d4
}
virtual_ipaddress {
192.168.1.99/24 dev ens33 label ens33:0
}
track_script {
chk_dow
chk_ngx
}
track_interface {
ens33
ens37
}
notify_master “/etc/keepalived/notify.sh master”
notify_backup “/etc/keepalived/notify.sh backup”
notify_fault “/etc/keepalived/notify.sh fault“
}
vrrp_instance VI_2{
state BACKUP
interface ens33
virtual_router_id 34
priority 96
advert_int 1
authentication {
auth_type PASS
auth_pass a6b7c8d9
}
virtual_ipaddress {
192.168.1.99/24 dev ens33 label ens33:1
}
track_script {
chk_dow
chk_ngx
}
track_interface {
ens33
ens37
}
notify_master “/etc/keepalived/notify.sh master”
notify_backup “/etc/keepalived/notify.sh backup”
notify_fault “/etc/keepalived/notify.sh fault“
}
[root@keepalive21 ~]# systemctl start nginx
[root@keepalive21 ~]# systemctl start keepalived
节点2主机设置:
[root@keepalive22 ~]# cd /etc/keepalived/
[root@keepalive22 keepalived]# vim notify.sh#编辑邮件脚本
#!/bin/bash
#
contact='root@localhost'
notify() {
local mailsubject="$(hostname) to be $1, vip floating"
local mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
systemctl start nginx.service
notify master
;;
backup)
systemctl start nginx.service
notify backup
;;
fault)
systemctl stop nginx.service
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
[root@keepalive22 ~]# yum -y install nginx
[root@keepalive22 ~]# vim /etc/nginx/nginx.conf
.....
upstream websrvs {
server 192.168.1.11:80;
server 192.168.1.12:80;
server 192.168.1.13:80;
}
# Load modular configuration files from the /etc/nginx/conf.d directory.
.....
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://websrvs;
}
[root@keepalive22 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id keepalive130
vrrp_mcast_group4 224.1.101.33
}
vrrp_script chk_down{
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -10
fall 1
rise 1
}
vrrp_script chk_ngx {
script "killall -0 nginx && exit 0 || exit 1"
weight -10
interval 2
fall 3
rise 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 33
priority 96
advert_int 1
authentication {
auth_type PASS
auth_pass a1b2c3d4
}
virtual_ipaddress {
192.168.1.99/24 dev ens33 label ens33:0
}
track_script {
chk_dow
chk_ngx
}
track_interface {
ens33
ens37
}
notify_master “/etc/keepalived/notify.sh master”
notify_backup “/etc/keepalived/notify.sh backup”
notify_fault “/etc/keepalived/notify.sh fault“
}
vrrp_instance VI_2{
state MASTER
interface ens33
virtual_router_id 34
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass a6b7c8d9
}
virtual_ipaddress {
192.168.1.99/24 dev ens33 label ens33:1
}
track_script {
chk_dow
chk_ngx
}
track_interface {
ens33
ens37
}
notify_master “/etc/keepalived/notify.sh master”
notify_backup “/etc/keepalived/notify.sh backup”
notify_fault “/etc/keepalived/notify.sh fault“
}
[root@keepalive22 ~]# systemctl start nginx
[root@keepalive22 ~]# systemctl start keepalived
客户端测试:
访问节点1:
[root@vs ~]# curl http://172.16.1.21
<h1>Vhost1</h1>
[root@vs ~]# curl http://172.16.1.21
<h1>Vhost2</h1>
[root@vs ~]# curl http://172.16.1.21
<h1>Vhost3</h1>
访问节点2
[root@vs ~]# curl http://172.16.1.22
<h1>Vhost1</h1>
[root@vs ~]# curl http://172.16.1.22
<h1>Vhost2</h1>
[root@vs ~]# curl http://172.16.1.22
<h1>Vhost3</h1>