Centos8部署Keepalived+VRRP

虚拟机环境如下:

Node1 192.168.1.110 Centos8
Node2 192.168.1.111     Centos8

1.在两台虚拟机上安装Nginx服务;

yum install –y nginx

2.为两个节点自定义index.html;

Echo “

This is Nginx Web Server from Node1

” | tee /usr/share/nginx/html/index.html 或 Echo “

This is Nginx Web Server from Node2

” | 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作为从节点。将两台主机名修改成Node1Node2;

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.confcheck_nginx.sh文件从Node1复制到Node2;

scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/

scp /bin/check_nginx.sh [email protected]:/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能看到设置的文本内容。

Centos8部署Keepalived+VRRP_第1张图片

16.Node1上把nginx服务停了,再刷新一次;

Centos8部署Keepalived+VRRP_第2张图片

你可能感兴趣的:(Centos8服务部署,linux,运维,服务器,centos8)