部门将网站建设的需求,万网网站空间服务范围及费用,h5开发用什么工具,网页编程代码VIP常用于负载均衡的高可用#xff0c;使用VIP可以给多个主机绑定一个IP#xff0c;这样#xff0c;当某个负载应用挂了之后#xff0c;可以自动切到另一个负载。
我这里是在k8s环境中做的测试#xff0c;集群中有6个节点#xff0c;我给140和141两个节点配置VIP。 1. 安…VIP常用于负载均衡的高可用使用VIP可以给多个主机绑定一个IP这样当某个负载应用挂了之后可以自动切到另一个负载。
我这里是在k8s环境中做的测试集群中有6个节点我给140和141两个节点配置VIP。 1. 安装keepalived
1.1 Ubuntu / Debian:
使用 apt 包管理器安装 Keepalived
sudo apt update
sudo apt install keepalived
1.2 CentOS / RHEL:
使用 yum 包管理器安装 Keepalived
sudo yum install keepalived等待安装完成。
2. 配置keepalived
2.1 准备一个未被使用的ip作为VIP
可以联系网络管理员获取或者自行获取我是通过ping 一个一个试的ping不通的就是未被使用的。
我这里准备的IP是10.10.101.254
keepalived配置文件
global_defs {router_id node140 # 唯一的节点标识不同节点需要配置成不一样的
}vrrp_script check_health {script /etc/keepalived/check_nginx.sh # 替换为你的健康检查脚本interval 10weight -2 # 注意这里必须为负数
}vrrp_instance VI_1 {state MASTER # MASTER 或 BACKUPinterface ens192 # 根据你的网络接口名修改virtual_router_id 51priority 100 # 在 MASTER 节点上设置较高的优先级advert_int 1authentication {auth_type PASSauth_pass your_authentication_password}virtual_ipaddress {10.10.101.254 # 你的 VIP 地址}track_script {check_health}
}
/etc/keepalived/check_nginx.sh
#!/bin/bash# Check Nginx health
if curl -s -o /dev/null -w %{http_code} http://10.10.101.140:10254/healthz | grep -q 200; thenecho Nginx is healthyexit 0 # Nginx is healthy, return success
elseecho Nginx is not healthyexit 1 # Nginx is not healthy, return failure
fi
重要参数详解
参数一global_defs.router_id
唯一的节点标识不同节点需要配置成不一样的。
参数二vrrp_instance VI_1.interface
网络接口名称即VIP所负载的节点的IP的网络接口名称也叫网卡名称。
我这里给140节点配置的VIP所以我需要确定10.10.101.140这个IP的网卡名称。
ip addr show | grep inet 10.10.101.140 ens192就是10.10.101.140的网卡名称。
参数三vrrp_instance VI_1.virtual_ipaddress
VIP即上面准备的VIP。
参数四vrrp_instance VI_1.state
MASTER 或 BACKUP
参数五vrrp_instance VI_1.priority
节点权重。
除了这五个参数需要修改其他参数都不需要改。
3. 启动keepalived
sudo systemctl start keepalived # 启动服务
sudo systemctl stop keepalived # 停止服务
sudo systemctl restart keepalived # 重启服务4. 扩展知识