[root@haproxy1 keepalived]# vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { #定义收件人邮箱 root@localhost } notification_email_from root@localhost #定义发件人邮箱 smtp_server 127.0.0.1 #定义邮件服务器地址 smtp_connect_timeout 30 #定有邮件服务器连接超时时长为30秒 router_id LVS_DEVEL #运行keepalive的机器的标识 } vrrp_instance VI_1 { #定义VRRP实例,实例名自定义 state MASTER #指定当前节点的角色,master为主,backup为从 interface eth1 #直接HA监测的接口 virtual_router_id 51 #虚拟路由标识,在同一VRRP实例中,主备服务器ID必须一样 priority 100 #定义节点优先级,数字越大越优先,主服务器优先级高于从服务器 advert_int 1 #设置主备之间永不检查时间间隔,单位为秒 authentication { 设置主从之间验证类型和密码 auth_type PASS auth_pass a23c7f32dfb519d6a5dc67a4b2ff8f5e } virtual_ipaddress { 192.168.80.200 #定义虚拟ip地址 } } vrrp_instance VI_2 { state BACKUP interface eth1 virtual_router_id 52 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 56f7663077966379d4106e8ee30eb1a5 } virtual_ipaddress { 192.168.80.201 } } View Code 将此配置文件同步到另一个haproxy2.daixiang.com上:
1
[root@haproxy1 keepalived]# scp keepalived.conf haproxy2:/etc/keepalived/
在haproxy2.daixiang.com上修改keepalived.conf配置文件:
[root@haproxy2 keepalived]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass a23c7f32dfb519d6a5dc67a4b2ff8f5e } virtual_ipaddress { 192.168.80.200 } } vrrp_instance VI_2 { state MASTER interface eth1 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 56f7663077966379d4106e8ee30eb1a5 } virtual_ipaddress { 192.168.80.201 } }
重启keepalived:
1 2
[root@haproxy1 keepalived]# service keepalived restart [root@haproxy1 keepalived]# ssh haproxy2 'service keepalived restart'
配置haproxy: 先将原始配置文件进行备份:
1 2 3 4
[root@haproxy1 ~]# cd /etc/haproxy/ [root@haproxy1 haproxy]# cp haproxy.cfg{,.bak} ##节点2也进行同样的操作
编辑其配置文件修改其内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
[root@haproxy1 haproxy]# vim haproxy.cfg #--------------------------------------------------------------------- # Example configuration for a possible web application. See the # full configuration options online. # # # #--------------------------------------------------------------------- #--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global #定义全局配置段 # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log #