使用背景:
- 通过调用python SDK在腾讯云手动部署keepalived高可用
部署环境
系统:centos7.3
Master:192.168.0.100
Slave:192.168.0.14
VIP:192.168.0.76
=================================================================
master和slave上部署python命令行环境
yum -y install python python-pip pip install qcloudcli
安装命令行自动补齐
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上
测试正常