虚拟机环境如下:
Node1 | 192.168.1.110 | Centos8 |
Node2 | 192.168.1.111 | Centos8 |
1.在两台虚拟机上安装Nginx服务;
yum install –y nginx
2.为两个节点自定义index.html;
Echo “<h1>This is Nginx Web Server from Node1</h1>” | tee /usr/share/nginx/html/index.html
或
Echo “<h1>This is Nginx Web Server from Node2</h1>” | tee /usr/share/nginx/html/index.html
3.关闭防火墙;
systemctl stop firewalld
systemctl disable firewalld
4.启动nginx服务;
systemctl start nginx
5.使用浏览器或者curl命令查看两个节点的nginx服务部署是否成功;
6.在两台虚拟机上安装Keepalived服务;
yum install –y keepalived / dnf install –y keepalived
7.在此选用虚拟机1作为主节点,2作为从节点。将两台主机名修改成Node1和Node2;
hostnamectl set-hostname Node1/Node2
8.在Node1上备份配置文件;
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf-org
9.编辑配置文件;
vi /etc/keepalived/keepalived.conf
内容如下:
global_defs {
router_id nginx
}
vrrp_script check_nginx {
script "/bin/check_nginx.sh"
interval 2
weight 50
}
vrrp_instance VI_01 {
state MASTER
interface ens33
virtual_router_id 151
priority 110
virtual_ipaddress {
192.168.1.150/24 \\这个设置的是自己网段的虚拟IP
}
track_script {
check_nginx
}
authentication {
auth_type AH
auth_pass secret
}
}
10.创建脚本检查Nginx服务是否正在运行;
vi /bin/check_nginx.sh
内容如下
#!/bin/sh
if [ -z "`pidof nginx`" ]; then
exit 1
fi
11.设置执行权限;
chmod 755 /bin/check_nginx.sh
12.使用scp命令把keepalived.conf和check_nginx.sh文件从Node1复制到Node2;
scp /etc/keepalived/keepalived.conf root@192.168.1.111:/etc/keepalived/
scp /bin/check_nginx.sh root@192.168.1.111:/bin/
13.检查Node2上的keepalived主配置文件;
vi /etc/keepalived/keepalived.conf
内容如下:
global_defs {
router_id nginx
}
vrrp_script check_nginx {
script "/bin/check_nginx.sh"
interval 2
weight 50
}
vrrp_instance VI_01 {
state BACKUP \\修改
interface ens33
virtual_router_id 151
priority 100 \\修改
virtual_ipaddress {
192.168.1.150/24 \\这个设置的是自己网段的虚拟IP,别直接复制
}
track_script {
check_nginx
}
authentication {
auth_type AH
auth_pass secret
}
}
14.使用【systemctl】命令管理服务;
systemctl start keepalived
systemctl enable keepalived
systemctl restart nginx
15.在Node1上可以看到ens33有两个ip地址,一个自定义的一个是150.
使用浏览器输入192.168.1.150能看到设置的文本内容。
16.在Node1上把nginx服务停了,再刷新一次;