AWS -- EC2 ELB -- shell script to change ip in backend varnish incase the ELB ips keeep changing

in AWS the ELP ( load balancer ) ip is dynamic .. so we need to change this in the varnish config file  . this script does the trick .

------------------------------------------------------------
#!/bin/bash


#check if varnish is reunning 
#if running reload if not restart 
varnishstatus=$(/usr/bin/pgrep varnishd |wc -l )

if [ $varnishstatus -gt 0  ]
then
vstatus='reload'
else 
vstatus='restart'
fi
#check IP in hosts set in vairrable 

temdigcount=/tmp/temdigcount 
tmpbackendconfig=/tmp/tmpbackendconfig


backendconfig='/etc/varnish/health.backend.vcl'
LBS_aws='/etc/varnish/LBS_aws'
touch $LBS_aws


defaultvcl=''


OIFS=$IFS 
IFS=$'\n' 
array=($(dig +short $LB2|sort))
IFS=$OIFS




echo ${array[@]} > $temdigcount

DIFF1=`diff $temdigcount $LBS_aws|wc -l `

echo $DIFF1

if [ $DIFF1 -eq 0 ]
then
echo -e "no diff in ip \n"
exit 
fi 


set_count=0

cat /dev/null > $tmpbackendconfig


for i in ${array[@]}
do
let set_count++
 SERVERIP=$i
cat <<EOF >> $tmpbackendconfig
backend health$set_count {
  .host = "$SERVERIP";
  .port = "http";
  .first_byte_timeout = 300s;
  .probe = {
         .request = "GET / HTTP/1.1" "Host: yoursite.com" "Connection: close";
         .interval = 10s;
         .timeout = 10s;
         .window = 5;
         .threshold = 3;
    }
  }
EOF
done




echo -e "\n Bakcend changed, updating VCL and Reloading VARNISH\n\n"
 /bin/cp -f $tmpbackendconfig  $backendconfig
/etc/init.d/varnish reload
 [ -f $FILE ] && \
 cp ${FILE} $FILE_${NOW} && \
 varnishadm -T localhost:1234 vcl.load vcl_${NOW} $FILE && \
 varnishadm -T localhost:1234 vcl.use  vcl_${NOW}  && \
 varnishadm -T localhost:1234 vcl.list |tail -n 3





No comments:

Other Articles

Enter your email address: