Authored by chunhua.zhang

add

@@ -38,24 +38,23 @@ yoho.switch @@ -38,24 +38,23 @@ yoho.switch
38 >2.可用区之间的网络出现故障。例如az1 <--> az2 网络不通; 切换方案:流量切换到主可用区 38 >2.可用区之间的网络出现故障。例如az1 <--> az2 网络不通; 切换方案:流量切换到主可用区
39 39
40 40
41 -#### 1.1 流量切换到主可用区(AZ1) 41 +#### 1.1 流量切换到主可用区(AZ1)
42 42
43 -副可用区出现故障的场景,细分如下2个场景:  
44 -  
45 - - 副可用区所在的外部负载均衡器正常: `LB Switch To AZ1` (AZ2流量从负载均衡器上切换到AZ1) 43 +>1)AZ2公网流量切换到AZ1
  44 +>
  45 +>2)AZ1公网、内网(*.yoho.yohoops.org)流量切换到AZ1
46 46
47 - - 副可用区所在的外部负载均衡器异常: `ASK Switch To AZ1` (问询服务器上将流量切换到AZ1)  
48 47
  48 +#### 1.2 流量切换到副可用区(AZ2)
49 49
50 -###### LB Switch to AZ1  
51 50
52 ->1)AZ2 外部负载均衡器(`lb-ivt5h95t`, `123.206.2.50`)上 `*.yoho.cn` 切换到AZ1 51 +>1)AZ1公网流量、内网流量切换到AZ2
53 > 52 >
54 ->2)AZ1 外部负载均衡器(`lb-09g87u8f`, `123.206.1.98`) 切换到AZ1 53 +>2)AZ2公网流量切换到AZ2
55 54
56 55
57 56
58 -###### ASK Switch To AZ1 57 +#### 1.3 切换问询服务器流量到AZ1
59 58
60 > `curl -H "Host: dnsresolver.yohoops.org" "http://10.66.4.23/switchCloud?cloudType=1"` 59 > `curl -H "Host: dnsresolver.yohoops.org" "http://10.66.4.23/switchCloud?cloudType=1"`
61 > 60 >
@@ -46,7 +46,7 @@ class QcloudApi: @@ -46,7 +46,7 @@ class QcloudApi:
46 sign = self.signature(params, req_url) 46 sign = self.signature(params, req_url)
47 params['Signature'] = sign 47 params['Signature'] = sign
48 response_dict = requests.post("https://" + req_url, data=params).json() 48 response_dict = requests.post("https://" + req_url, data=params).json()
49 - print("Query qcloud api success. params:\n %s \n" % (params)) 49 + print("call qcloud api success. params:\n %s \n" % (params))
50 print json.dumps(response_dict, indent=4, sort_keys=True) 50 print json.dumps(response_dict, indent=4, sort_keys=True)
51 return response_dict 51 return response_dict
52 52
@@ -20,22 +20,27 @@ from ansible.inventory.manager import InventoryManager @@ -20,22 +20,27 @@ from ansible.inventory.manager import InventoryManager
20 20
21 demo: 21 demo:
22 22
23 - lbswitch = LBSwitch(secretId="AKID6dwpxxxxxqPv5GK", secretKey="ACJxxxxBsQaW",  
24 - az1_lb_id="lb-e14oxiq3", az2_lb_id="lb-e14oxiq3", az3_lb_id="lb-e14oxiq3")  
25 - lbswitch.all_to_az1()  
26 -  
27 23
  24 +lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW",
  25 + az1_lb_id="lb-dyh6eucn", az2_lb_id="lb-capto5b5", az3_lb_id="lb-capto5b5", az1_innerlb_id="lb-c26tgo5r")
  26 +
  27 +# 全部流量切换到AZ1
  28 +ret = lbswitch.all_to_az1()
  29 +
  30 +# 全部流量切换到AZ2
  31 +ret = lbswitch.all_to_az2()
28 32
29 """ 33 """
30 34
31 35
32 class LBSwitch: 36 class LBSwitch:
33 - def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id): 37 + def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id, az1_innerlb_id):
34 self.SecretId = secretId 38 self.SecretId = secretId
35 self.SecretKey = secretKey 39 self.SecretKey = secretKey
36 self.az1_lb_id = az1_lb_id 40 self.az1_lb_id = az1_lb_id
37 self.az2_lb_id = az2_lb_id 41 self.az2_lb_id = az2_lb_id
38 self.az3_lb_id = az3_lb_id 42 self.az3_lb_id = az3_lb_id
  43 + self.az1_innerlb_id = az1_innerlb_id
39 44
40 def all_to_az1(self): 45 def all_to_az1(self):
41 """ 46 """
@@ -45,19 +50,21 @@ class LBSwitch: @@ -45,19 +50,21 @@ class LBSwitch:
45 az1_nginxs = self.get_inventory_ifo("az1", 'java-nginx') 50 az1_nginxs = self.get_inventory_ifo("az1", 'java-nginx')
46 print "get all java nginx: %s for az: %s" % (az1_nginxs, "az1") 51 print "get all java nginx: %s for az: %s" % (az1_nginxs, "az1")
47 52
48 - ip_weight_dict = {} 53 + az1_nginx_weight = {}
49 for az1_nginx in az1_nginxs: 54 for az1_nginx in az1_nginxs:
50 - ip_weight_dict[az1_nginx] = 10 55 + az1_nginx_weight[az1_nginx] = 10
51 56
52 # az2 公网流量切换到az1 57 # az2 公网流量切换到az1
53 - ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict) 58 + ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=az1_nginx_weight)
54 print("switch all incoming request from az2 to az1 result: %s" % ret_az2) 59 print("switch all incoming request from az2 to az1 result: %s" % ret_az2)
55 60
56 - # az1 公网流量切换到az1  
57 - ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=ip_weight_dict)  
58 - print("switch all incoming request from az1 to az1 result: %s" % ret_az1) 61 + # az1 公网流量、内部流量切换到az1
  62 + ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=az1_nginx_weight)
  63 + ret_az1_inner = self.modify_alb_weight(lb_id=self.az1_innerlb_id, domain="*.yoho.yohoops.org",
  64 + ip_weight_dict=az1_nginx_weight)
  65 + print("switch all incoming request from az1 to az1 result: %s inner result: %s" % (ret_az1, ret_az1_inner))
59 66
60 - return ret_az1 and ret_az2 67 + return ret_az1 and ret_az1_inner and ret_az2
61 68
62 69
63 70
@@ -67,17 +74,20 @@ class LBSwitch: @@ -67,17 +74,20 @@ class LBSwitch:
67 :return: 74 :return:
68 """ 75 """
69 az2_nginx = self.get_inventory_ifo("az2", 'java-nginx') 76 az2_nginx = self.get_inventory_ifo("az2", 'java-nginx')
70 - ip_weight_dict = {} 77 + az2_nginx_weight = {}
71 for nginx in az2_nginx: 78 for nginx in az2_nginx:
72 - ip_weight_dict[nginx] = 10 79 + az2_nginx_weight[nginx] = 10
73 80
74 - ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict) 81 + # az2 公网流量切换到az2
  82 + ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=az2_nginx_weight)
75 print("switch all incoming request from az2 to az2 result: %s" % ret_az2) 83 print("switch all incoming request from az2 to az2 result: %s" % ret_az2)
76 84
77 - ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=ip_weight_dict)  
78 - print("switch all incoming request from az1 to az2 result: %s" % ret_az1) 85 + # az1 公网流量、内部流量切换到az2
  86 + ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=az2_nginx_weight)
  87 + ret_az1_inner = self.modify_alb_weight(lb_id=self.az1_innerlb_id, domain="*.yoho.yohoops.org", ip_weight_dict=az2_nginx_weight)
  88 + print("switch all incoming request from az1 to az2 result: %s inner lb:%s" % (ret_az1, ret_az1_inner))
79 89
80 - return ret_az1 & ret_az2 90 + return ret_az1 and ret_az2 and ret_az1_inner
81 91
82 @staticmethod 92 @staticmethod
83 def get_inventory_ifo(az, group): 93 def get_inventory_ifo(az, group):
@@ -179,11 +189,13 @@ class LBSwitch: @@ -179,11 +189,13 @@ class LBSwitch:
179 return return_code.count(0) == len(return_code) 189 return return_code.count(0) == len(return_code)
180 190
181 191
  192 +
182 lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW", 193 lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW",
183 - az1_lb_id="lb-e14oxiq3", az2_lb_id="lb-e14oxiq3", az3_lb_id="lb-e14oxiq3") 194 + az1_lb_id="lb-dyh6eucn", az2_lb_id="lb-capto5b5", az3_lb_id="lb-capto5b5", az1_innerlb_id="lb-c26tgo5r")
184 195
185 #ret = lbswitch.modify_clb_weight(lb_id="lb-e14oxiq3", ip_weight_dict={"10.66.104.15": 10, "10.66.104.13":20}) 196 #ret = lbswitch.modify_clb_weight(lb_id="lb-e14oxiq3", ip_weight_dict={"10.66.104.15": 10, "10.66.104.13":20})
186 -ret = lbswitch.modify_alb_weight(lb_id="lb-fodpm611", domain="*.yoho.cn", ip_weight_dict={"10.66.0.2": 10, "10.66.0.3": 10})  
187 - 197 +#ret = lbswitch.modify_alb_weight(lb_id="lb-fodpm611", domain="*.yoho.cn", ip_weight_dict={"10.66.0.2": 10, "10.66.0.3": 10})
  198 +#ret = lbswitch.all_to_az1()
  199 +ret = lbswitch.all_to_az2()
188 print ret 200 print ret
189 201