一、KeepAlived 单主和双主配置
-
主配置文件:/etc/keepalived/keepalived.conf主程序文件:/usr/sbin/keepalivedUnit File:/usr/lib/systemd/system/keepalived.serviceUnit File的环境配置文件:/etc/sysconfig/keepalived
-
配置文件组成部分TOP HIERACHYGLOBAL CONFIGURATIONGlobal definitionsStatic routes/addressesVRRPD CONFIGURATIONVRRP synchronization group(s):vrrp同步组VRRP instance(s):即一个vrrp虚拟路由器LVS CONFIGURATIONVirtual server group(s)Virtual server(s):ipvs集群的 vs 和 rs
-
配置语法vrrp_instance <STGING> {….}专用参数:state MASTER|BACKUP:当前节点在此虚拟路由器上的初始状态;只能有一个是 MASTER,余下的都应该为BACKUPinterface IFACE_NAME:绑定为当前虚拟路由器使用的物理接口virtual_router_id VRID:当前虚拟路由器惟一标识,范围是0-255priority 100:当前物理节点在此虚拟路由器中的优先级,范围1-254advert_int 1:vrrp通告的时间间隔,默认1sauthentication { #认证机制auth_type AH|PASSauth_pass 仅前8位有效}virtual_ipaddress { #虚拟IP<IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPE> label <LABEL>192.168.200.17/24 dev eth1192.168.200.18/24 dev eth2 label eth2:1}track_interface { #配置监控网络接口,一旦出现故障,则转为FAULT状态实现地址转移eth0eth1…}
-
nopreempt:定义工作模式为非抢占模式preempt_delay 300:抢占式模式,节点上线后触发新选举操作的延迟时长,默认模式定义通知脚本:notify_master <STRING>|<QUOTED-STRING>: 当前节点成为主节点时触发的脚本 notify_backup <STRING>|<QUOTED-STRING>: 当前节点转为备节点时触发的脚本notify_fault <STRING>|<QUOTED-STRING>: 当前节点转为“失败”状态时触发的脚本notify <STRING>|<QUOTED-STRING>: 通用格式的通知触发机制,一个脚本可完成以上三种状态的转换时的通知
-
单主配置文件示例! Configuration File for keepalivedglobal_defs {notification_email {root@localhost}notification_email_from keepalived@localhostsmtp_server 127.0.0.1 # 即本机,配不配都无所谓smtp_connect_timeout 30router_id node1 #主机名,在另一结点为node2=============vrrp_mcast_group4 224.0.100.100 # 多播 IPV4,找个没人用的}vrrp_instance VI_1 {state MASTER # 在另一个结点上为BACKUP=============interface eth0virtual_router_id 6 #多个节点必须相同priority 100 #在另一个结点上为90===================advert_int 1 #通告间隔1sauthentication {auth_type PASS #预共享密钥认证auth_pass 571f97b2}virtual_ipaddress {172.18.100.66/16}}然后在另一台上配置上类似的配置文件,====标注的为需要修改的
-
双主配置示例! Configuration File for keepalivedglobal_defs {notification_email {root@localhost}notification_email_from keepalived@localhostsmtp_server 127.0.0.1smtp_connect_timeout 30router_id node1 # ===================vrrp_mcast_group4 224.0.100.100}vrrp_instance VI_1 {state MASTER #===================interface eth0virtual_router_id 6priority 100 #===================advert_int 1authentication {auth_type PASSauth_pass 571f97b2}virtual_ipaddress {172.16.0.10/16}}vrrp_instance VI_2 {state BACKUP #===================interface eth0virtual_router_id 8priority 80 # ===================advert_int 1authentication {auth_type PASSauth_pass 578f07b2}virtual_ipaddress {172.16.0.11/16}}然后在另一台上配置上类似的配置文件,====标注的为需要修改的
-
示例通知脚本#!/bin/bashcontact=’root@localhost’notify() {mailsubject=”$(hostname) to be $1, vip floating”mailbody=”$(date +’%F %T’): vrrp transition, $(hostname) changed to be $1″echo “$mailbody” | mail -s “$mailsubject” $contact}case $1 inmaster)notify master;;backup)notify backup;;fault)notify fault;;*)echo “Usage: $(basename $0) {master|backup|fault}”exit 1;;esac脚本的调用方法:notify_master “/etc/keepalived/notify.sh master”notify_backup “/etc/keepalived/notify.sh backup”notify_fault “/etc/keepalived/notify.sh fault”
二、keepalived + lvs 实现配置
-
先在两台 RS服务器 上执行 RS 的脚本
-
常用参数delay_loop <INT>:检查后端服务器的时间间隔lb_algo rr|wrr|lc|wlc|lblc|sh|dh:定义调度方法lb_kind NAT|DR|TUN:集群的类型persistence_timeout <INT>:持久连接时长protocol TCP:服务协议,仅支持TCPsorry_server <IPADDR> <PORT>:所有RS故障时,备用服务器地址real_server <IPADDR> <PORT> {weight <INT> #RS权重notify_up <STRING> | <QUOTED-STRING> #RS 上线通知脚本notify_down <STRING> | <QUOTED-STRING> #RS 下线通知脚本HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK { … }:# 定义当前主机的健康状态检测方法}HTTP_GET|SSL_GET:应用层检测HTTP_GET|SSL_GET {url {path <URL_PATH> :定义要监控的URLstatus_code <INT>:判断上述检测机制为健康状态的响应码digest <STRING>:判断为健康状态的响应的内容的校验码}connect_timeout <INTEGER>:连接请求的超时时长nb_get_retry <INT>:重试次数delay_before_retry <INT>:重试之前的延迟时长connect_ip <IP ADDRESS>:向当前RS哪个IP地址发起健康状态检测请求connect_port <PORT>:向当前RS的哪个PORT发起健康状态检测请求bindto <IP ADDRESS>:发出健康状态检测请求时使用的源地址bind_port <PORT> :发出健康状态检测请求时使用的源端口}TCP_CHECK {connect_ip <IP ADDRESS>:向当前RS的哪个IP地址发起健康状态检测请求connect_port <PORT>:向当前RS的哪个PORT发起健康状态检测请求bindto <IP ADDRESS>:发出健康状态检测请求时使用的源地址bind_port <PORT>:发出健康状态检测请求时使用的源端口connect_timeout <INTEGER>:连接请求的超时时长}
-
两台 VS 服务器上配置示例! Configuration File for keepalivedglobal_defs {notification_email {root@localhost}notification_email_from keepalived@localhostsmtp_server 127.0.0.1smtp_connect_timeout 30router_id node1 # ===================vrrp_mcast_group4 224.0.100.100}vrrp_instance VI_1 {state MASTER #===================interface eth0virtual_router_id 6priority 100 #===================advert_int 1authentication {auth_type PASSauth_pass 571f97b2}virtual_ipaddress {172.16.0.10/16}# notify_master “/etc/keepalived/notify.sh master”# notify_backup “/etc/keepalived/notify.sh backup”# notify_fault “/etc/keepalived/notify.sh fault”}virtual_server 172.16.0.10 80 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPsorry_server 172.16.0.10 80 # 记得把本机上的先配置好即可real_server 192.168.8.27 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.8.6 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 3}}}然后在另一台上配置上类似的配置文件,====标注的为需要修改的
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/101048