阿里雲ECS使用高可用ip


一、介紹

阿里雲不支持使用keepalived等服務器的vip使用,所以只能使用阿里雲的ENI(彈性網卡)來實現

彈性網卡(Elastic Network Interface,簡稱ENI)是一種可以附加到專有網絡VPC類型ECS實例上的虛擬網卡。通過彈性網卡,您可以在任何阿里雲地域下實現高可用集群搭建、低成本故障轉移和精細化的網絡管理。

官方文檔:https://help.aliyun.com/document_detail/58496.html?spm=a2c4g.11186623.6.824.771060e0Ke0wH1

 

二、創建彈性網卡

 

 三、使用彈性網卡

 

 

 

 綁定完幾秒鍾后內網就能通了

 

四、控制ENI的demo(python3)

1、python3 SDK 安裝

pip3 install aliyun-python-sdk-core
pip3 install aliyun-python-sdk-ecs

  

2、訪問控制權限

管理阿里雲ECS權限

 

3、控制ENI的demo

 

#!/usr/bin/env python3
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.AttachNetworkInterfaceRequest import AttachNetworkInterfaceRequest
from aliyunsdkecs.request.v20140526.DetachNetworkInterfaceRequest import DetachNetworkInterfaceRequest
import sys
from time import sleep

AccessKeyId = 'AccessKeyId'  
AccessKeySecret = 'AccessKeySecret'
Endpoint = 'cn-qingdao'
remove_ecs_id = sys.argv[1]  #傳遞移除網卡的ecs_id
ENI_ID = 'xxxx'  #彈性輔助網卡ENI的id
ECS_ID_LIST = ['xxxx','xxxxx']   #ENI網卡綁定的ecs集群列表
try:
    ECS_ID_LIST.remove(remove_ecs_id)
except Exception  as e:
    print(e)
    exit(1)

add_ecs_id = ECS_ID_LIST[0]   #選一個可以用的ecs的id,做為網卡綁定的服務器


def remove_vip(remove_ecs_id,ENI_ID):
    '''解綁函數'''
    client = AcsClient(AccessKeyId,AccessKeySecret,Endpoint) #建議不要使用變量,直接輸入字符串
    request = DetachNetworkInterfaceRequest()
    request.set_accept_format('json')
    request.set_InstanceId(remove_ecs_id)
    request.set_NetworkInterfaceId(ENI_ID)
    try:
        response = client.do_action_with_exception(request)
    except Exception  as e:
        print(e)
        exit(2)
    print(str(response, encoding='utf-8'))
    return True
    

def add_vip(add_ecs_id,ENI_ID):
    '''綁定函數'''
    client = AcsClient(AccessKeyId,AccessKeySecret,Endpoint) #建議不要使用變量,直接輸入字符串
    request = AttachNetworkInterfaceRequest()
    request.set_accept_format('json')
    request.set_NetworkInterfaceId(ENI_ID)
    request.set_InstanceId(add_ecs_id)
    try:
        response = client.do_action_with_exception(request)
    except Exception  as e:
        print(e)
        exit(3)
    print(str(response, encoding='utf-8'))
    
if __name__  == '__main__':
    ret = remove_vip(remove_ecs_id,ENI_ID)
    if ret:
        add_vip(add_ecs_id,ENI_ID)

  

 五、結合keepalived的單播模式使用

1、keepalived試例配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc  #發送郵箱
   }
   notification_email_from Alexandre.Cassen@firewall.loc #郵箱地址
   smtp_server 127.0.0.1   #郵件服務器地址
   smtp_connect_timeout 30
   router_id LVS_DEVEL-01   #主機名,每個節點不同即可
}

vrrp_instance VI_1 {
    state MASTER   #節點主從,在另一個節點上為BACKUP
    interface eth0  #IP地址漂移到的網卡
    virtual_router_id 51  #多個節點必須相同
    priority 100   #優先級,備用節點的值必須低於主節點的值
    advert_int 1   #通告間隔1秒
    authentication { 
        auth_type PASS   #預共享密鑰認證
        auth_pass 1111   #密鑰
    }

unicast_src_ip 172.31.0.204  #本機地址
    unicast_peer {
        172.31.0.205    #目標地址
    }


    virtual_ipaddress {
        192.168.1.100/24  dev eth0  #VIP地址
    }
}
主端配置
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL-02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

unicast_src_ip 172.31.0.205
    unicast_peer {
        172.31.0.204
    }


    virtual_ipaddress {
        192.168.1.100/24 dev eth0 
    }
}
從配置

 

2、檢查腳本(未測試過的腳本,可以根據思路自己修改或測試)

結合了rsync使用

#!/bin/bash

source /etc/init.d/functions

vip=192.168.1.100  #虛擬vip
local_ip=172.31.0.204  #本機ip
rsync_module_ip=172.31.0.205  #rsync目標的ip
ecs_id_file=/tmp/ecs_id.txt  #保存彈性網卡所在的ecs的id名稱(ecsID)

declare -A arr
arr["172.31.0.204"]="ecs的id"    #內網ip作為key,ecs的id作為value
arr["172.31.0.205"]="ecs的id"

if [ `ip a s eth0|egrep -o ${vip}|wc -l` -eq 1 ];then  #判斷是否擁有vip
	 if [ `cat ${ecs_id_file}|wc -l` -eq 1 ];then #判斷文件是否有內容
         if `cat ${ecs_id_file}` != ${arr[${local_ip}]};then #判斷ecs_id_file文件中記錄的ecsID和本機的ecsID是否相同
			 python3  ENI.py ${arr[${rsync_module_ip}]}  #執行腳本(python控制ENI的腳本)
			 echo ${arr[${local_ip}]} > ${ecs_id_file}  
			 rsync -avz ${ecs_id_file} module_user@${rsync_module_ip}::aliyun_eni_module  --password-file=/etc/rsync.password
	     else
		     rsync -avz ${ecs_id_file} module_user@${rsync_module_ip}::aliyun_eni_module  --password-file=/etc/rsync.password
		    
	     fi
	 else
	 #    unset arr[${local_ip}]  #刪除arr中的本機的變量內容
		 python3  ENI.py ${arr[${rsync_module_ip}]}  #執行腳本(python控制ENI的腳本)
		 if [ $? -eq 0 ];then   #判斷執行是否失敗
			 #腳本執行成功,覆蓋文件內容
			 echo ${arr[${local_ip}]} > ${ecs_id_file}   
			 #rsync覆蓋給另一台服務器的ecs_id_file文件(建議使用rsync的demon)
			 rsync -avz ${ecs_id_file} module_user@${rsync_module_ip}::aliyun_eni_module  --password-file=/etc/rsync.password
		 else
		     #執行失敗了操作
			 action "python3 scripts carried out Fail ${rsync_module_ip}" /bin/false
			 
		 
		 fi
	 fi
fi

  

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM