热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

keepalived实现lvs高可用

keeplaived实现lvs高可用名称ipnode1(lvs,keepalived)192.168.6.152node2(lvs,keepalived)192.168.6.153

keeplaived实现lvs高可用























名称ip
node1(lvs,keepalived)192.168.6.152
node2(lvs,keepalived)192.168.6.153
rs1192.168.6.135
rs2192.168.6.154

# 在Rs1,Rs2下载nginx写一个网页文件
[root@rs1 ~]# dnf -y install nginx
[root@rs1 ~]# echo 'rs1' > /usr/share/nginx/html/index.html
[root@rs1 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@rs2 ~]# dnf -y install nginx
[root@rs2 ~]# echo 'rs2' > /usr/share/nginx/html/index.html
[root@rs2 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

 

# 在192.168.6.152 lvs 调度器上配置VIP和DIP
[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls
ifcfg-ens160
[root@node1 network-scripts]# vim ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
DEVICE=ens160
OnBOOT=yes
IPADDR0=192.168.6.152
NETMASK=255.255.255.0
GATEWAY=192.168.6.2
DNS1=114.114.114.114
IPADDR1=192.168.6.250 # 加一个250的vip
NETMASK1=255.255.255.0 #添加一个子网掩码
[root@node1 network-scripts]# ifdown ens160;ifup ens160 # 虽然网卡起来了,但是走不了外网,应为有两个网关,我们可以配本地yum仓库
Connection 'ens160' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[root@node1 network-scripts]# ip a #
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d7:b0:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.6.152/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.6.250/24 brd 192.168.6.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
#配置本地yum仓库
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# ls
CentOS-Base.repo epel.repo epel-testing.repo
epel-modular.repo epel-testing-modular.repo
[root@node1 yum.repos.d]# mv * /opt/ # 把配好的阿里云的yum源移走
[root@node1 yum.repos.d]# ls
[root@node1 yum.repos.d]# mount /dev/cdrom /mnt/ # 挂载镜像
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@node1 yum.repos.d]# vi xx.repo # 配置本地源
[appstream]
name=appstream
baseurl=file:///mnt/AppStream
gpgcheck=0
enabled=1
[baseos]
name=baseos
baseurl=file:///mnt/BaseOS
gpgcheck=0
enabled=1
[root@node1 yum.repos.d]# yum clean all
36 files removed
[root@node1 yum.repos.d]# yum makecache

# 在192.168.6.153 lvs 调度器上配置VIP和DIP
[root@node2 ~]# cd /etc/sysconfig/network-scripts/
[root@node2 network-scripts]# vim ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
DEVICE=ens160
OnBOOT=yes
IPADDR0=192.168.6.153
NETMASK=255.255.255.0
GATEWAY=192.168.6.2
DNS1=114.114.114.114
IPADDR1=192.168.6.250
NETMASK1=255.255.255.0
[root@node2 network-scripts]# ifdown ens160;ifup ens160

[root@node2 network-scripts]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:42:d0:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.6.153/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.6.250/24 brd 192.168.6.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever


[root@node2 ~]# cd /etc/yum.repos.d/
[root@node2 yum.repos.d]# ls
CentOS-Base.repo epel.repo epel-testing.repo
epel-modular.repo epel-testing-modular.repo
[root@node2 yum.repos.d]# mv * /opt/
[root@node2 yum.repos.d]# ls

[root@node2 yum.repos.d]# mount /dev/cdrom /mnt/
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@node2 yum.repos.d]# vi xx.repo


[root@node2 yum.repos.d]# yum clean all
36 files removed
[root@node2 yum.repos.d]# yum makecache

[appstream]
name=appstream
baseurl=file:///mnt/AppStream
gpgcheck=0
enabled=1
[baseos]
name=baseos
baseurl=file:///mnt/BaseOS
gpgcheck=0
enabled=1

 

#在R1和R2上配置RIP
[root@rs1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
DEVICE=ens160
OnBOOT=yes
IPADDR=192.168.6.135
NETMASK=255.255.255.0
GATEWAY=192.168.6.2
DNS1=114.114.114.114
[root@rs1 ~]# ifdown ens160;ifup ens160
[root@rs2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
DEVICE=ens160
OnBOOT=yes
IPADDR=192.168.6.154
NETMASK=255.255.255.0
GATEWAY=192.168.6.2
DNS1=114.114.114.114
[root@rs2 ~]# ifdown ens160;ifup ens160

[root@rs1 ~]# vi /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.conf.all.arp_ignore = 1 #添加
net.ipv4.conf.all.arp_announce = 2
[root@rs1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@rs2 ~]# vi /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@rs2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

#在RS1和RS2上配置VIP
[root@rs1 ~]# yum -y install net-tools
[root@rs1 ~]# ifconfig lo:0 192.168.6.250/32 broadcast 192.168.6.250 up
[root@rs1 ~]# ifconfig lo:0 192.168.6.250/32 broadcast 192.168.6.250 up
[root@rs1 ~]# ip a
1: lo: 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
inet 192.168.6.250/0 brd 192.168.6.250 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:e2:21:19 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.135/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
[root@rs1 ~]# route add -host 192.168.6.180 dev lo
[root@rs1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.6.2 0.0.0.0 UG 100 0 0 ens160
192.168.6.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
192.168.6.180 0.0.0.0 255.255.255.255 UH 0 0 0 lo
[root@rs2 ~]# yum -y install net-tools
[root@rs2 ~]# ifconfig lo:0 192.168.6.250/32 broadcast 192.168.6.250 up
[root@rs2 ~]# ip a
1: lo: 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
inet 192.168.6.250/0 brd 192.168.6.250 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: mtu 1500 qdisc fq_codel state UP group defaultqlen 1000
link/ether 00:0c:29:bf:50:39 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.154/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
[root@rs2 ~]# route add -host 192.168.6.180 dev lo
[root@rs2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.6.2 0.0.0.0 UG 100 0 0 ens160
192.168.6.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
192.168.6.180 0.0.0.0 255.255.255.255 UH 0 0 0 lo

#node1 node2 添加规则并且保存
[root@node1 ~]# yum -y install ipvsadm
[root@node1 ~]# ipvsadm -A -t 192.168.6.250:80 -s rr
[root@node1 ~]# ipvsadm -a -t 192.168.6.250:80 -r 192.168.6.135:80 -g
[root@node1 ~]# ipvsadm -a -t 192.168.6.250:80 -r 192.168.6.154:80 -g
[root@node1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.6.250:80 rr
-> 192.168.6.135:80 Route 1 0 0
-> 192.168.6.154:80 Route 1 0 0
[root@node1 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@node1 ~]# systemctl enable ipvsadm
Created symlink /etc/systemd/system/multi-user.target.wants/ipvsadm.service → /usr/lib/systemd/system/ipvsadm.service.
[root@node1 ~]# echo "ipvsadm -R >/etc/rc.d/rc.local
[root@node2 yum.repos.d]# yum -y install ipvsadm
[root@node2 ~]# ipvsadm -A -t 192.168.6.250:80 -s rr
[root@node2 ~]# ipvsadm -a -t 192.168.6.250:80 -r 192.168.6.135:80 -g
[root@node2 ~]# ipvsadm -a -t 192.168.6.250:80 -r 192.168.6.154:80 -g
[root@node2 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.6.250:80 rr
-> 192.168.6.135:80 Route 1 0 0
-> 192.168.6.154:80 Route 1 0 0
[root@node2 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@node2 ~]# echo "ipvsadm -R >/etc/rc.d/rc.local

 

 


下载keepalived配置主keepalived

[root@node1 ~]# dnf -y install keepalived # 下载
[root@node1 ~]# cd /etc/keepalived/
[root@node1 keepalived]# ls
keepalived.conf
[root@node1 keepalived]# cp keepalived.conf{,.aa} #复制一个原本默认的配置文件修改名字备份一下
[root@node1 keepalived]# ls
keepalived.conf keepalived.conf.aa
[root@node1 keepalived]# > keepalived.conf #内容清空
[root@node1 keepalived]# vim keepalived.conf #编辑主配置文件
! Configuration File for keepalived
global_defs { # 全局配置
router_id lb01
}
vrrp_instance VI_1 { #定义实例
state MASTER # 定义keepalived节点的初始状态,可以为MASTER和BACKUP
interface ens160 # VRRP实施绑定的网卡接口,
virtual_router_id 51 # 虚拟路由的ID,同一集群要一致
priority 100 #定义优先级,按优先级来决定主备角色优先级越大越有限
advert_int 1 # 主备通讯时间间隔
authentication { #配置认证
auth_type PASS #认证方式此处为密码
auth_pass 023654 # 修改密码
}
virtual_ipaddress { #要使用的VIP地址
192.168.6.250 # 修改vip
}
}
virtual_server 192.168.6.250 80 { # 配置虚拟服务器
delay_loop 6 # 健康检查时间间隔
lb_algo rr # lvs调度算法
lb_kind DR #lvs模式
persistence_timeout 50 #持久化超时时间,单位是秒
protocol TCP #4层协议
real_server 192.168.6.152 80 { # 定义真实处理请求的服务器
weight 1 # 给服务器指定权重,默认为1
TCP_CHECK {
connect_port 80 #端口号为80
connect_timeout 3 # 连接超时时间
nb_get_retry 3 # 连接次数
delay_before_retry 3 # 在尝试之前延迟多少时间
}
}
real_server 192.168.6.153 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@node1 keepalived]# systemctl enable --now keepalived # 启动并开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@node1 keepalived]# systemctl status keepalived # 查看状态
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disa>
Active: active (running) since Thu 2022-09-01 23:57:20 CST; 10s ago
Process: 11618 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/>
Main PID: 11620 (keepalived)
Tasks: 3 (limit: 11202)
Memory: 2.1M
CGroup: /system.slice/keepalived.service
├─11620 /usr/sbin/keepalived -D
├─11621 /usr/sbin/keepalived -D
└─11622 /usr/sbin/keepalived -D

下载keepalived配置备keepalived

[root@node2 ~]# dnf -y install keepalived
[root@node2 ~]# cd /etc/keepalived/
[root@node2 keepalived]# ls
keepalived.conf
[root@node2 keepalived]# cp keepalived.conf{,.aa}
[root@node2 keepalived]# > keepalived.conf
[root@node2 keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 023654
}
virtual_ipaddress {
192.168.6.250
}
}
virtual_server 192.168.6.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.6.152 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.6.153 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@node2 keepalived]# systemctl enable --now keepalived #启动并设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@node2 keepalived]# systemctl status keepalived # 查看状态
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disa>
Active: active (running) since Fri 2022-09-02 00:02:04 CST; 12s ago
Process: 15241 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/>
Main PID: 15242 (keepalived)
Tasks: 3 (limit: 11202)
Memory: 2.4M
CGroup: /system.slice/keepalived.service
├─15242 /usr/sbin/keepalived -D
├─15243 /usr/sbin/keepalived -D
└─15244 /usr/sbin/keepalived -D

查看VIP在哪里

# 在MASTER上查看
[root@node1 keepalived]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d7:b0:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.6.152/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.6.250/32 scope global ens160 # vip在主节点上
valid_lft forever preferred_lft forever
inet 192.168.6.250/24 brd 192.168.6.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
#在SLAVE上查看
[root@node2 keepalived]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:42:d0:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.6.153/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.6.250/24 brd 192.168.6.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever

让keepalived监控lvs
keepalived通过脚本来监控lvs的状态
在node1上编写脚本

[root@node1 ~]# mkdir /scripts
[root@node1 ~]# cd /scripts/
[root@node1 scripts]# vim notify.sh
#!/bin/bash
case "$1" in
master)
ipvsadm -R ;;
backup)
ipvsadm -C
;;
*)
echo "Usage:$0 master|backup"
;;
esac

[root@node1 scripts]# chmod +x notify.sh
[root@node1 scripts]# ll
total 4
-rwxr-xr-x 1 root root 178 Sep 2 00:43 notify.sh

 

在node2上编写脚本

[root@node2 keepalived]# vim /etc/keepalived/keepalived.conf
[root@node2 keepalived]# mkdir /scripts
[root@node2 keepalived]# cd /scripts/
[root@node2 scripts]# vim notify.sh
#!/bin/bash
case "$1" in
master)
ipvsadm -R ;;
backup)
ipvsadm -C
;;
*)
echo "Usage:$0 master|backup"
;;
esac

[root@node2 scripts]# chmod +x notify.sh
[root@node2 scripts]# ll
total 4
-rwxr-xr-x 1 root root 178 Sep 2 00:47 notify.sh

 

配置keepalived加入监控脚本的配置

配置node1上的keepalived

加入脚本

 

[root@node1 scripts]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state BACKUP #不抢占此处都为备 ,如果设置抢占把两边的nopreempt都删除掉,此处改为MASTER为主
interface ens160
virtual_router_id 51
priority 100
nopreempt # 设置的不抢占,
advert_int 1
authentication {
auth_type PASS
auth_pass 023654
}
virtual_ipaddress {
192.168.6.250
}
notify_master "/scripts/notify.sh master 192.168.6.250"
notify_backup "/scripts/notify.sh backup 192.168.6.250"
}
virtual_server 192.168.6.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.6.152 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.6.153 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

[root@node1 scripts]# systemctl restart keepalived
[root@node1 scripts]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disa>
Active: active (running) since Fri 2022-09-02 00:57:07 CST; 9s ago
Process: 11678 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/>
Main PID: 11679 (keepalived)
Tasks: 3 (limit: 11202)
Memory: 3.0M
CGroup: /system.slice/keepalived.service
├─11679 /usr/sbin/keepalived -D
├─11680 /usr/sbin/keepalived -D
└─11681 /usr/sbin/keepalived -D

 

配置node2上的keepalived

加入脚本

[root@node2 scripts]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP # 不抢占模式必须都为备
interface ens160
virtual_router_id 51
priority 90
nopreempt # 不抢占
advert_int 1
authentication {
auth_type PASS
auth_pass 023654
}
virtual_ipaddress {
192.168.6.250
}
notify_master "/scripts/notify.sh master 192.168.6.250"
notify_backup "/scripts/notify.sh backup 192.168.6.250"
}
}
virtual_server 192.168.6.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.6.152 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.6.153 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

[root@node2 scripts]# systemctl restart keepalived
[root@node2 scripts]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disa>
Active: active (running) since Fri 2022-09-02 00:57:58 CST; 8s ago
Process: 15273 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/>
Main PID: 15274 (keepalived)
Tasks: 3 (limit: 11202)
Memory: 3.2M
CGroup: /system.slice/keepalived.service
├─15274 /usr/sbin/keepalived -D
├─15276 /usr/sbin/keepalived -D
└─15277 /usr/sbin/keepalived -D

 

验证,模拟node1出现故障,此处是不抢占模式

# 先查看状态node1,vip还在手里
[root@node1 scripts]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d7:b0:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.6.152/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.6.250/32 scope global ens160 # vip
valid_lft forever preferred_lft forever
inet 192.168.6.250/24 brd 192.168.6.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
#查看node2,没有vip
[root@node2 scripts]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:42:d0:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.6.153/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.6.250/24 brd 192.168.6.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
# 关掉node1
[root@node1 scripts]# init 0
#查看node2,在node1出现故障的时候抢占了vip成为主,继续让服务运行,从而达到主备故障切换
[root@node2 scripts]# ip a
1: lo: 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: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:42:d0:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.6.153/24 brd 192.168.6.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.6.250/32 scope global ens160 # 有vip了
valid_lft forever preferred_lft forever
inet 192.168.6.250/24 brd 192.168.6.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



推荐阅读
  • 在Linux系统中,网络配置是至关重要的任务之一。本文详细解析了Firewalld和Netfilter机制,并探讨了iptables的应用。通过使用`ip addr show`命令来查看网卡IP地址(需要安装`iproute`包),当网卡未分配IP地址或处于关闭状态时,可以通过`ip link set`命令进行配置和激活。此外,文章还介绍了如何利用Firewalld和iptables实现网络流量控制和安全策略管理,为系统管理员提供了实用的操作指南。 ... [详细]
  • WCF类型共享的最佳实践
    在使用WCF服务时,经常会遇到同一个实体类型在不同服务中被生成为不同版本的问题。本文将介绍几种有效的类型共享方法,以解决这一常见问题。 ... [详细]
  • C#实现文件的压缩与解压
    2019独角兽企业重金招聘Python工程师标准一、准备工作1、下载ICSharpCode.SharpZipLib.dll文件2、项目中引用这个dll二、文件压缩与解压共用类 ... [详细]
  • Hadoop的文件操作位于包org.apache.hadoop.fs里面,能够进行新建、删除、修改等操作。比较重要的几个类:(1)Configurati ... [详细]
  • JUC(三):深入解析AQS
    本文详细介绍了Java并发工具包中的核心类AQS(AbstractQueuedSynchronizer),包括其基本概念、数据结构、源码分析及核心方法的实现。 ... [详细]
  • 用阿里云的免费 SSL 证书让网站从 HTTP 换成 HTTPS
    HTTP协议是不加密传输数据的,也就是用户跟你的网站之间传递数据有可能在途中被截获,破解传递的真实内容,所以使用不加密的HTTP的网站是不 ... [详细]
  • 零拷贝技术是提高I/O性能的重要手段,常用于Java NIO、Netty、Kafka等框架中。本文将详细解析零拷贝技术的原理及其应用。 ... [详细]
  • 在分析Android的Audio系统时,我们对mpAudioPolicy->get_input进行了详细探讨,发现其背后涉及的机制相当复杂。本文将详细介绍这一过程及其背后的实现细节。 ... [详细]
  • 网站访问全流程解析
    本文详细介绍了从用户在浏览器中输入一个域名(如www.yy.com)到页面完全展示的整个过程,包括DNS解析、TCP连接、请求响应等多个步骤。 ... [详细]
  • 开机自启动的几种方式
    0x01快速自启动目录快速启动目录自启动方式源于Windows中的一个目录,这个目录一般叫启动或者Startup。位于该目录下的PE文件会在开机后进行自启动 ... [详细]
  • 本文详细介绍了 InfluxDB、collectd 和 Grafana 的安装与配置流程。首先,按照启动顺序依次安装并配置 InfluxDB、collectd 和 Grafana。InfluxDB 作为时序数据库,用于存储时间序列数据;collectd 负责数据的采集与传输;Grafana 则用于数据的可视化展示。文中提供了 collectd 的官方文档链接,便于用户参考和进一步了解其配置选项。通过本指南,读者可以轻松搭建一个高效的数据监控系统。 ... [详细]
  • CentOS 7 中 iptables 过滤表实例与 NAT 表应用详解
    在 CentOS 7 系统中,iptables 的过滤表和 NAT 表具有重要的应用价值。本文通过具体实例详细介绍了如何配置 iptables 的过滤表,包括编写脚本文件 `/usr/local/sbin/iptables.sh`,并使用 `iptables -F` 清空现有规则。此外,还深入探讨了 NAT 表的配置方法,帮助读者更好地理解和应用这些网络防火墙技术。 ... [详细]
  • 在CentOS 7环境中安装配置Redis及使用Redis Desktop Manager连接时的注意事项与技巧
    在 CentOS 7 环境中安装和配置 Redis 时,需要注意一些关键步骤和最佳实践。本文详细介绍了从安装 Redis 到配置其基本参数的全过程,并提供了使用 Redis Desktop Manager 连接 Redis 服务器的技巧和注意事项。此外,还探讨了如何优化性能和确保数据安全,帮助用户在生产环境中高效地管理和使用 Redis。 ... [详细]
  • 本文是Java并发编程系列的开篇之作,将详细解析Java 1.5及以上版本中提供的并发工具。文章假设读者已经具备同步和易失性关键字的基本知识,重点介绍信号量机制的内部工作原理及其在实际开发中的应用。 ... [详细]
  • Python 程序转换为 EXE 文件:详细解析 .py 脚本打包成独立可执行文件的方法与技巧
    在开发了几个简单的爬虫 Python 程序后,我决定将其封装成独立的可执行文件以便于分发和使用。为了实现这一目标,首先需要解决的是如何将 Python 脚本转换为 EXE 文件。在这个过程中,我选择了 Qt 作为 GUI 框架,因为之前对此并不熟悉,希望通过这个项目进一步学习和掌握 Qt 的基本用法。本文将详细介绍从 .py 脚本到 EXE 文件的整个过程,包括所需工具、具体步骤以及常见问题的解决方案。 ... [详细]
author-avatar
心窝子难受呢
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有