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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
我的博客已迁移到xdoujiang.com请去那边和我交流
keepalived使用纯C语言写成。软件围绕中心的I
/O
多路复用器设计提供实时的网络。
它的设计重点是在各个元素之间实现模块化,为了保证稳定性和健壮性,守护进程被分成三个独立的进程。
整体设计基于一个简单的父进程,父进程负责fork并监控子进程。两个子进程,一个负责VRRP框架,
另一个负责健康检查。每个子进程有自己的I
/O
调度多路复用器,这样可以优化VRRP调度,因为VRRP
调度比健康检查更敏感。另一方面,这个分开的设计使得健康检查机制对外部函数库的使用最小化,
最小化自身的动作并且让主循环空闲来避免自身引起的故障。父进程的监控框架叫做watchdog,
它的设计是:每个子进程打开一个UNIX套接字等待请求,当守护进程启动后,
父进程连接这些UNIX套接字并周期性(5s)地发送hello包到子进程。如果父进程无法发送hello包到远程连接的UNIX套机字,
它会简单地重启子进程。watchdog的设计有两个好处,首先hello包从父进程发送到远程连接的子进程是通过I
/O
多路复用器调度,
这样它就能够检测子进程调度框架中的死循环。第二个好处是可以使用sysV信号检测子进程是否死掉。
测试当serverA机器重启后,虚IP漂移到serverB充当master角色,就是说serverA重启后不抢占。
一、基础环境
1、角色、ip、版本、内核
masterA 10.1.10.117 3.2.0-4-amd64 7.8 keepaliaved
serverB 10.1.10.185 3.2.0-4-amd64 7.8 keepaliaved
二、安装keepalived
1、apt方式安装keepalived服务(serverA serverB)
apt-get -y
install
keepalived --force-
yes
2、修改master
/slave
配置(serverA serverB)
1)master 创建keepalived.conf并添加以下内容
cat
/etc/keepalived/keepalived
.conf
global_defs
{
router_id aaa
}
vrrp_instance VI_1
{
state BACKUP
interface eth0
virtual_router_id 90
nopreempt
priority 100
advert_int 1
virtual_ipaddress
{
10.1.10.240
/24
}
}
2)slave 创建keepalived.conf并添加以下内容
cat
/etc/keepalived/keepalived
.conf
global_defs
{
router_id bbb
}
vrrp_instance VI_1
{
state BACKUP
interface eth0
virtual_router_id 90
priority 99
advert_int 1
virtual_ipaddress
{
10.1.10.240
/24
}
}
PS:配置说明
state
#MASTER是主服务器,BACKUP是备服务器。
virtual_ipaddress
#设置虚拟IP地址,也叫漂移IP地址。
priority
#值越大优先级越高,主的值在设置上大于备的值
nopreempt
#不抢占(仅配置在state BACKUP时有效 并且这个机器的优先级必须比另一台高)
virtual_router_id
#取值范围是1-255(must be between 1 & 255)
advert_int
#MASTER和BACKUP之间同步检查的时间间隔(s)
3、自定义日志存放地方(不定义的话默认是在
/var/log/messages
)(serverA serverB)
1)添加以下内容
echo
"local3.* /var/log/keepalived"
>>
/etc/rsyslog
.conf
2)重启下rsyslog服务
/etc/init
.d
/rsyslog
restart
[ ok ] Stopping enhanced syslogd: rsyslogd.
[ ok ] Starting enhanced syslogd: rsyslogd.
4、启动服务(serverA serverB)
keepalived -d -D -S 3 -f
/etc/keepalived/keepalived
.conf
PS:启动参数说明
--use-
file
, -f keepalived.conf_file Use the specified configuration
file
.
--dump-conf, -d Dump the configuration data.
--log-detail, -D Detailed log messages (the default with the rc script provided).
--log-facility, -S 0-7 Set syslog facility to LOG_LOCAL[0-7] (default=LOG_DAEMON)
5、查看进程(serverA serverB)
ps
aux |
grep
keepalived |
grep
-
v
grep
root 2944 0.0 0.3 21984 796 ? Ss 16:44 0:00 keepalived -d -D -S 3 -f
/etc/keepalived/keepalived
.conf
root 2945 0.0 0.9 30520 2160 ? S 16:44 0:00 keepalived -d -D -S 3 -f
/etc/keepalived/keepalived
.conf
root 2946 0.0 0.6 30396 1468 ? S 16:44 0:00 keepalived -d -D -S 3 -f
/etc/keepalived/keepalived
.conf
6、查看相关日志文件(serverA serverB)
ll
/var/log/keepalived
-rw-r----- 1 root adm 4215 Sep 22 16:44
/var/log/keepalived
7、查看启动日志(serverA serverB)
1)
cat
/var/log/keepalived
Sep 22 16:44:12 debian Keepalived: Starting Keepalived v1.2.2 (12
/18
,2011)
Sep 22 16:44:12 debian Keepalived: Remove a zombie pid
file
/var/run/keepalived
.pid
Sep 22 16:44:12 debian Keepalived: Remove a zombie pid
file
/var/run/vrrp
.pid
Sep 22 16:44:12 debian Keepalived: Remove a zombie pid
file
/var/run/checkers
.pid
Sep 22 16:44:12 debian Keepalived: Starting Healthcheck child process, pid=2945
Sep 22 16:44:12 debian Keepalived: Starting VRRP child process, pid=2946
Sep 22 16:44:12 debian Keepalived_vrrp: Netlink reflector reports IP 10.1.10.117 added
Sep 22 16:44:12 debian Keepalived_vrrp: Netlink reflector reports IP 10.1.10.240 added
Sep 22 16:44:12 debian Keepalived_vrrp: Netlink reflector reports IP fe80::20c:29ff:fec8:87a1 added
Sep 22 16:44:12 debian Keepalived_vrrp: Registering Kernel netlink reflector
Sep 22 16:44:12 debian Keepalived_vrrp: Registering Kernel netlink
command
channel
Sep 22 16:44:12 debian Keepalived_vrrp: Registering gratutious ARP shared channel
Sep 22 16:44:12 debian Keepalived_healthcheckers: Initializing ipvs 2.6
Sep 22 16:44:12 debian Keepalived_vrrp: Initializing ipvs 2.6
Sep 22 16:44:12 debian Keepalived_vrrp: Opening
file
'/etc/keepalived/keepalived.conf'
.
Sep 22 16:44:12 debian Keepalived_healthcheckers: Netlink reflector reports IP 10.1.10.117 added
Sep 22 16:44:12 debian Keepalived_healthcheckers: Netlink reflector reports IP 10.1.10.240 added
Sep 22 16:44:12 debian Keepalived_healthcheckers: Netlink reflector reports IP fe80::20c:29ff:fec8:87a1 added
Sep 22 16:44:12 debian Keepalived_healthcheckers: Registering Kernel netlink reflector
Sep 22 16:44:12 debian Keepalived_healthcheckers: Registering Kernel netlink
command
channel
Sep 22 16:44:12 debian Keepalived_healthcheckers: Opening
file
'/etc/keepalived/keepalived.conf'
.
Sep 22 16:44:12 debian Keepalived_vrrp: VRRP is trying to assign invalid VIP {. skipping VIP...
Sep 22 16:44:12 debian Keepalived_vrrp: Configuration is using : 60186 Bytes
Sep 22 16:44:12 debian Keepalived_vrrp: ------< Global definitions >------
Sep 22 16:44:12 debian Keepalived_vrrp: Router ID = aaa
Sep 22 16:44:12 debian Keepalived_vrrp: Smtp server connection timeout = 30
Sep 22 16:44:12 debian Keepalived_vrrp: Email notification from = root@debian
Sep 22 16:44:12 debian Keepalived_vrrp: ------< VRRP Topology >------
Sep 22 16:44:12 debian Keepalived_vrrp: VRRP Instance = VI_1
Sep 22 16:44:12 debian Keepalived_vrrp: Want State = MASTER
Sep 22 16:44:12 debian Keepalived_vrrp: Runing on device = eth0
Sep 22 16:44:12 debian Keepalived_vrrp: Virtual Router ID = 90
Sep 22 16:44:12 debian Keepalived_vrrp: Priority = 100
Sep 22 16:44:12 debian Keepalived_vrrp: Advert interval = 1sec
Sep 22 16:44:12 debian Keepalived_vrrp: Preempt disabled
Sep 22 16:44:12 debian Keepalived_vrrp: Virtual IP = 1
Sep 22 16:44:12 debian Keepalived_vrrp: 10.1.10.240
/24
dev eth0 scope global
Sep 22 16:44:12 debian Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 22 16:44:12 debian Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Sep 22 16:44:12 debian Keepalived_healthcheckers: Configuration is using : 5509 Bytes
Sep 22 16:44:12 debian Keepalived_healthcheckers: ------< Global definitions >------
Sep 22 16:44:12 debian Keepalived_healthcheckers: Router ID = aaa
Sep 22 16:44:12 debian Keepalived_healthcheckers: Smtp server connection timeout = 30
Sep 22 16:44:12 debian Keepalived_healthcheckers: Email notification from = root@debian
Sep 22 16:44:12 debian Keepalived_healthcheckers: ------< SSL definitions >------
Sep 22 16:44:12 debian Keepalived_healthcheckers: Using autogen SSL context
Sep 22 16:44:12 debian Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector...
Sep 22 16:44:13 debian Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 22 16:44:14 debian Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 22 16:44:14 debian Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 22 16:44:14 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0
for
10.1.10.240
Sep 22 16:44:19 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0
for
10.1.10.240
2)
cat
/var/log/keepalived
Sep 22 16:51:31 debian Keepalived: Starting Keepalived v1.2.2 (12
/18
,2011)
Sep 22 16:51:31 debian Keepalived: Starting Healthcheck child process, pid=2414
Sep 22 16:51:31 debian Keepalived: Starting VRRP child process, pid=2415
Sep 22 16:51:31 debian Keepalived_vrrp: Netlink reflector reports IP 10.1.10.185 added
Sep 22 16:51:31 debian Keepalived_vrrp: Netlink reflector reports IP fe80::20c:29ff:fe1c:c991 added
Sep 22 16:51:31 debian Keepalived_vrrp: Registering Kernel netlink reflector
Sep 22 16:51:31 debian Keepalived_healthcheckers: Initializing ipvs 2.6
Sep 22 16:51:31 debian Keepalived_vrrp: Registering Kernel netlink
command
channel
Sep 22 16:51:31 debian Keepalived_vrrp: Registering gratutious ARP shared channel
Sep 22 16:51:31 debian Keepalived_vrrp: Initializing ipvs 2.6
Sep 22 16:51:31 debian Keepalived_healthcheckers: IPVS: Cant initialize ipvs: Protocol not available
Sep 22 16:51:31 debian Keepalived_healthcheckers: Netlink reflector reports IP 10.1.10.185 added
Sep 22 16:51:31 debian Keepalived_healthcheckers: Netlink reflector reports IP fe80::20c:29ff:fe1c:c991 added
Sep 22 16:51:31 debian Keepalived_healthcheckers: Registering Kernel netlink reflector
Sep 22 16:51:31 debian Keepalived_healthcheckers: Registering Kernel netlink
command
channel
Sep 22 16:51:31 debian Keepalived_healthcheckers: Opening
file
/etc/keepalived/keepalived
.conf.
Sep 22 16:51:31 debian Keepalived_healthcheckers: Configuration is using : 5355 Bytes
Sep 22 16:51:31 debian Keepalived_vrrp: Opening
file
/etc/keepalived/keepalived
.conf.
Sep 22 16:51:31 debian Keepalived_vrrp: VRRP is trying to assign invalid VIP {. skipping VIP...
Sep 22 16:51:31 debian Keepalived_vrrp: Configuration is using : 60164 Bytes
Sep 22 16:51:31 debian Keepalived_vrrp: ------< Global definitions >------
Sep 22 16:51:31 debian Keepalived_vrrp: Router ID = bbb
Sep 22 16:51:31 debian Keepalived_vrrp: Smtp server connection timeout = 30
Sep 22 16:51:31 debian Keepalived_vrrp: Email notification from = root@debian
Sep 22 16:51:31 debian Keepalived_vrrp: ------< VRRP Topology >------
Sep 22 16:51:31 debian Keepalived_vrrp: VRRP Instance = VI_1
Sep 22 16:51:31 debian Keepalived_vrrp: Want State = BACKUP
Sep 22 16:51:31 debian Keepalived_vrrp: Runing on device = eth0
Sep 22 16:51:31 debian Keepalived_vrrp: Virtual Router ID = 90
Sep 22 16:51:31 debian Keepalived_vrrp: Priority = 99
Sep 22 16:51:31 debian Keepalived_vrrp: Advert interval = 1sec
Sep 22 16:51:31 debian Keepalived_vrrp: Virtual IP = 1
Sep 22 16:51:31 debian Keepalived_vrrp: 10.1.10.240
/24
dev eth0 scope global
Sep 22 16:51:31 debian Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 22 16:51:31 debian Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Sep 22 16:51:31 debian Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Sep 22 16:51:31 debian Keepalived_healthcheckers: ------< Global definitions >------
Sep 22 16:51:31 debian Keepalived_healthcheckers: Router ID = bbb
Sep 22 16:51:31 debian Keepalived_healthcheckers: Smtp server connection timeout = 30
Sep 22 16:51:31 debian Keepalived_healthcheckers: Email notification from = root@debian
Sep 22 16:51:31 debian Keepalived_healthcheckers: ------< SSL definitions >------
Sep 22 16:51:31 debian Keepalived_healthcheckers: Using autogen SSL context
Sep 22 16:51:31 debian Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector...
8、使用ip命令查看(serverA serverB)
1)使用ip a命令查看
ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:c8:87:a1 brd ff:ff:ff:ff:ff:ff
inet 10.1.10.117
/24
brd 10.1.10.255 scope global eth0
inet 10.1.10.240
/24
scope global secondary eth0
inet6 fe80::20c:29ff:fec8:87a1
/64
scope link
valid_lft forever preferred_lft forever
2)使用ip a命令查看
ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:1c:c9:91 brd ff:ff:ff:ff:ff:ff
inet 10.1.10.185
/24
brd 10.1.10.255 scope global eth0
inet6 fe80::20c:29ff:fe1c:c991
/64
scope link
valid_lft forever preferred_lft forever
9、删除开机默认启动(serverA serverB)
update-rc.d -f keepalived remove
update-rc.d: using dependency based boot sequencing
10、添加开机启动(serverA serverB)
echo
"/usr/sbin/keepalived -d -D -S 3 -f /etc/keepalived/keepalived.conf"
>>
/etc/rc
.
local
三、测试
1、直接重启masterA机器
2、在slave上查看IP地址
ip a
lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:8c:61:6e brd ff:ff:ff:ff:ff:ff
inet 10.1.10.250
/24
brd 10.1.10.255 scope global eth0
inet 10.1.10.240
/24
scope global secondary eth0
inet6 fe80::20c:29ff:fe8c:616e
/64
scope link
valid_lft forever preferred_lft forever
3、在slave上查看日志
cat
/var/log/keepalived
Sep 22 16:55:00 debian Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 22 16:55:01 debian Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 22 16:55:01 debian Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 22 16:55:01 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0
for
10.1.10.240
Sep 22 16:55:01 debian Keepalived_healthcheckers: Netlink reflector reports IP 10.1.10.240 added
Sep 22 16:55:06 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0
for
10.1.10.240
四、参考文章
http:
//www
.keepalived.org/
|
本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1654949,如需转载请自行联系原作者