Skip to content

Commit 755f64c

Browse files
author
金山云SDK
authored
Merge pull request KscSDK#91 from KscSDK/trunk
Trunk
2 parents a78a884 + 2a13159 commit 755f64c

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

examples/keepalived.conf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
! Configuration File for keepalived
2+
3+
global_defs {
4+
notification_email {
5+
acassen@firewall.loc
6+
failover@firewall.loc
7+
sysadmin@firewall.loc
8+
}
9+
notification_email_from Alexandre.Cassen@firewall.loc
10+
smtp_server 192.168.200.1
11+
smtp_connect_timeout 30
12+
router_id LVS_DEVEL
13+
vrrp_skip_check_adv_addr
14+
vrrp_garp_interval 0
15+
vrrp_gna_interval 0
16+
}
17+
18+
vrrp_instance VI_1 {
19+
#注意主备参数选择
20+
state BACKUP #无常主模式初始角色,均填写 BACKUP
21+
#state BACKUP #备
22+
interface eth0 #改成本机网卡名 例如 eth0
23+
virtual_router_id 51
24+
nopreempt #非抢占模式
25+
# preempt_delay 10
26+
priority 50 #无常主模式大小相同
27+
advert_int 1
28+
authentication {
29+
auth_type PASS
30+
auth_pass 1111
31+
}
32+
unicast_src_ip 172.18.0.13 # 修改本机内网 IP
33+
unicast_peer {
34+
172.18.0.16 #修改为对端设备的 IP 地址
35+
}
36+
virtual_ipaddress {
37+
172.18.0.253 #修改为内网 VIP
38+
}
39+
notify_master "/etc/keepalived/notify_action.sh MASTER"
40+
notify_backup "/etc/keepalived/notify_action.sh BACKUP"
41+
notify_fault "/etc/keepalived/notify_action.sh FAULT"
42+
notify_stop "/etc/keepalived/notify_action.sh STOP"
43+
garp_master_delay 1
44+
garp_master_refresh 5
45+
track_interface {
46+
eth0 #改成本机网卡名 例如 eth0
47+
}
48+
track_script {
49+
checkhaproxy
50+
}
51+
}

examples/nexthop.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#/etc/keepalived/nexthop.py
4+
5+
import os
6+
import time
7+
import json
8+
import sys
9+
from kscore.session import get_session
10+
11+
##################需修改部分Begin####################
12+
region='cn-beijing-6' #region code
13+
vpcId = '1858a08a-6cc9-4278-8d0c-d536f441fe8e' #vpcId
14+
ks_access_key_id = '您的ak'
15+
ks_secret_access_key = '您的sk'
16+
vip = "172.18.0.253" #改成您的本机内网 VIP
17+
DestinationCidrBlock = '172.18.0.253/32' #修改为VIP
18+
thisInstanceId = '1cf963ff-7847-4859-8462-5405f0facc1d' #当前主机的Id
19+
thatInstanceId = 'b141da5f-8e3e-44c0-ac0f-a0feccba78c7' #迁移前所在主机Id
20+
thisInstanceIp = "172.18.0.13" #当前机器IP
21+
##################需修改部分End######################
22+
23+
log = open('/var/log/keepalived.log', 'a+')
24+
state_file = open('/var/keepalived/state', 'r')
25+
26+
def get_now_time():
27+
return time.strftime('[%Y-%m-%d %H:%M:%S]',time.localtime(time.time())) + '[pid' + str(os.getpid()) + ']'
28+
29+
def log_write(message=''):
30+
log.write(get_now_time() + " " + str(message) + "\n")
31+
32+
33+
def findRoute():
34+
for route in vpcClient.describe_routes()['RouteSet']:
35+
if route['DestinationCidrBlock'] == DestinationCidrBlock:
36+
log_write('an existing route found')
37+
return route['RouteId']
38+
log_write('route not found')
39+
def migrateVip():
40+
param={'VpcId':vpcId,
41+
'DestinationCidrBlock':DestinationCidrBlock,
42+
'RouteType':'Host',
43+
'InstanceId':thisInstanceId}
44+
log_write("migrating vip to another host.")
45+
time.sleep(0.5)
46+
r = findRoute()
47+
if r:
48+
print vpcClient.delete_route(RouteId=r)
49+
log_write(" now change the nexthop of vip to this host." + thisInstanceIp)
50+
if vpcClient.create_route(**param):
51+
log_write('migrating vip success')
52+
53+
def print_help():
54+
log_write(
55+
'''
56+
./nexthop.py migrate
57+
migrate your vip
58+
''')
59+
60+
if __name__ == '__main__':
61+
s = get_session()
62+
s.set_credentials(ks_access_key_id,ks_secret_access_key)
63+
vpcClient = s.create_client("vpc", region, use_ssl=True)
64+
if len(sys.argv) == 1:
65+
log_write("nexthop.py: parameter num is 0")
66+
print_help()
67+
elif sys.argv[1] == 'migrate':
68+
migrateVip()
69+
log_write()
70+
else:
71+
log_write("nexthop.py: misMatched parameter")
72+
print_help()
73+

examples/notify_action.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
#/etc/keepalived/notify_action.sh
3+
log_file=/var/log/keepalived.log
4+
log_write()
5+
{
6+
echo "[`date '+%Y-%m-%d %T'`] $1" >> $log_file
7+
}
8+
9+
[ ! -d /var/keepalived/ ] && mkdir -p /var/keepalived/
10+
11+
case "$1" in
12+
"MASTER" )
13+
echo -n "$1" > /var/keepalived/state
14+
log_write " notify_master"
15+
echo -n "0" > /var/keepalived/vip_check_failed_count
16+
python /etc/keepalived/nexthop.py migrate >> $log_file 2>&1 &
17+
;;
18+
19+
"BACKUP" )
20+
echo -n "$1" > /var/keepalived/state
21+
log_write " notify_backup"
22+
;;
23+
24+
"FAULT" )
25+
echo -n "$1" > /var/keepalived/state
26+
log_write " notify_fault"
27+
;;
28+
29+
"STOP" )
30+
echo -n "$1" > /var/keepalived/state
31+
log_write " notify_stop"
32+
;;
33+
*)
34+
log_write "notify_action.sh: STATE ERROR!!!"
35+
;;
36+
esac

0 commit comments

Comments
 (0)