使用背景:python
部署環境vim
系統:centos7.3centos
Master:192.168.0.100api
Slave:192.168.0.14bash
VIP:192.168.0.76
post
=================================================================測試
master和slave上部署python命令行環境centos7
yum -y install python python-pip pip install qcloudcli
安裝命令行自動補齊spa
complete -C '/usr/bin/qcloud_completer' qcloudcli命令行
自動補齊永久生效vi ~/.bash_profile 添加上述命令到最後一行
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH complete -C '/usr/bin/qcloud_completer' qcloudcli
=========================================================================================================================
安裝keepalived
yum -y install keepalived
進入到keepalived目錄並下載python-sdk
cd /etc/keepalived/ wget https://mc.qcloudimg.com/static/archive/b61ee1ce734e7437530304152c20ee14/qcloudapi-sdk-python-master.zip
unzip qcloudapi-sdk-python-master.zip
調用python-sdk申請VIP vim setup.py,注:master上執行便可 secretID和secretkey須要在雲AIP密鑰中申請,執行./setup.py
#!/usr/bin/python # -*- coding: utf-8 -*- from src.QcloudApi.qcloudapi import QcloudApi module = 'vpc' action = 'AssignPrivateIpAddresses' config = { 'Region': 'gz', 'secretId': '您的secretId', 'secretKey': '您的secretKey', 'method': 'post' } params = { 'vpcId': 'vpc-53nh7c3p', 'networkInterfaceId': 'eni-431tr0x5', 'secondaryPrivateIpAddressCount': '1' } try: service = QcloudApi(module, config) print service.generateUrl(action, params) print service.call(action, params) except Exception, e: print 'exception:', e
建立vip漂移腳本master:vi vip.py(backup只須要調換oldnetworkinterfaceID和newnetworkinterfaceID調換便可)
#!/usr/bin/python # -*- coding: utf-8 -*- from QcloudApi.qcloudapi import QcloudApi #SDK源碼直接使用方式使用 #from src.QcloudApi.qcloudapi import QcloudApi module = 'vpc' action = 'MigratePrivateIpAddress' config = { 'Region': 'gz', 'secretId': '####', 'secretKey': '####', 'method': 'post' } params = { 'vpcId': 'vpc-53nh7c3p', 'privateIpAddress': '192.168.0.76', 'oldNetworkInterfaceId': 'backup網卡ID', 'newNetworkInterfaceId': 'master網卡ID' } try: service = QcloudApi(module, config) print service.generateUrl(action, params) print service.call(action, params) except Exception, e: print 'exception:', e
配置keepalived.conf文件vi keepalived.conf (master)
! Configuration File for keepalived notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_sync_group G1 { group { VI_1 } notify_master "/etc/keepalived/vip.py" } vrrp_instance VI_1 { #注意主備參數選擇 state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } unicast_peer { 192.168.0.14 #對端設備的IP地址,例如:10.0.0.1 } virtual_ipaddress { 192.168.0.76 #第一步申請的 VIP } nopreempt garp_master_delay 1 garp_master_refresh 5 }
配置keepalived.conf文件vi keepalived.conf (backup)
! Configuration File for keepalived notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_sync_group G1 { group { VI_1 } notify_master "/etc/keepalived/vip.py" } 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_peer { ##keepalived單播模式 192.168.0.100 #對端設備的IP地址,例如:10.0.0.1 } virtual_ipaddress { 192.168.0.76 #第一步申請的 VIP } nopreempt garp_master_delay 1 garp_master_refresh 5
主從上啓動keepalived服務
systemctl start keepalived.service
測試:
關閉master上的keepalived服務,查看VIP是否自動漂移到backup上
測試正常