#はじめに
IBM Cloud: VPC Custom Routesを利用したAZまたぎのHA環境の構成例(VPC内部からのアクセス)という記事を書いたが、この経路変更を障害時に手動で行うわけにはいかないので、自動実行できるように切り替えのスクリプトを書いてみた。
#スクリプトサンプル
ibmcloud_apikey
というファイルにAPI Keyを記載しておく。
vpcrouteswitch.sh
#!/bin/sh
#Usage example: ./vpcrouteswitch.sh 10.1.0.7
#Environment
export LANG=C
VPC_ID=r022-85621b2c-a349-4ae6-87e3-767ddeac7298
ROUTING_TABLE_ID=r022-904f9ad6-86b7-42fc-959a-c17a80c60577
ZONE1=jp-tok-1
ZONE2=jp-tok-2
ZONE3=jp-tok-3
DELETE_NEXT_HOP1=10.0.0.7
DELETE_NEXT_HOP2=10.1.0.7
NEXT_HOP=$1
DESTINATION_CIDR=192.168.10.10/32
#Variables Check
if [ $# -ne 1 ]; then
echo "Sorry, we had a problem there!"
exit 1
fi
#Login
ibmcloud login -a cloud.ibm.com --apikey @ibmcloud_apikey -r jp-tok
#LIST EXISTING ROUTE
ROUTE_IDS=$(ibmcloud is vpc-routing-table-routes ${VPC_ID} ${ROUTING_TABLE_ID} | grep -e ${DELETE_NEXT_HOP1} -e ${DELETE_NEXT_HOP2} | awk '{print $1}')
echo ${ROUTE_IDS}
#DELETE ROUTES
ibmcloud is vpc-routing-table-route-delete ${VPC_ID} ${ROUTING_TABLE_ID} $(echo ${ROUTE_IDS}) -f
#CREATE NEW ROUTES
ibmcloud is vpc-routing-table-route-create ${VPC_ID} ${ROUTING_TABLE_ID} --zone ${ZONE1} --destination ${DESTINATION_CIDR} --next-hop ${NEXT_HOP} --action deliver
ibmcloud is vpc-routing-table-route-create ${VPC_ID} ${ROUTING_TABLE_ID} --zone ${ZONE2} --destination ${DESTINATION_CIDR} --next-hop ${NEXT_HOP} --action deliver
ibmcloud is vpc-routing-table-route-create ${VPC_ID} ${ROUTING_TABLE_ID} --zone ${ZONE3} --destination ${DESTINATION_CIDR} --next-hop ${NEXT_HOP} --action deliver
実行例
10.1.0.7に切り替えたい時。
# ./vpcrouteswitch.sh 10.1.0.7