Varnish pointing to a Amazon ELB (elastic load balance) does not work.
Error:
Backend host “XXXXXXXXXXX-123456789.us-east-1.elb.amazonaws.com”: resolves to multiple IPv4 addresses.
Only one address is allowed.

I wrote this script to have a way to auto update the vcl once a new
instance comes up or down.
it requires that the .vcl has an include to backend.vcl
Other pepole did it in different ways
http://blog.cloudreach.co.uk/2013/01/varnish-and-autoscaling-love-story.html
This script is just a part of the solution, the tasks should be:
1. get new servername and IP (auto scale) can use AWS API cmds to do that, also via bash and reload varnish UPDATED HERE
2. update vcl (setvcl script) UPDATED HERE
1.Get new servername and IP and Reload varnish script

#!/bin/bash
# Script to monitor if the backend servers have changed
# if it does change the varnish vcl and reload it
#
# Requirements:
# 1)  ec2-describe-instances cmd working
# 2)  varnich vcl has include backend.vcl
# 3)  setvcl script that takes   args and updates vcl
#
# By Felipe Ferreira - June 2013
FILE="/etc/varnish/infograficos.vcl"
NOW=$(date +"%Y%m%d_%H%_M_%S")
DATA=$(date +"%Y-%m-%d %H:%M:%S")
##### EDIT HERE ##############
# ID common to all instances on the backend
AMI_ID="ami-b9943dd0"
# Varnish vcl
FILE="/etc/varnish/infograficos.vcl"
#any given name (has to end with 00)
SERVER="amzweb00"
#max 6, add more if needed
NOMES=`ec2-describe-instances |grep $AMI_ID |awk '{print $4}' | xargs -n 1 echo "${SERVER}" | sed -e '0,/00/s//01/' -e '1,/00/s//02/' -e '1,/00/s//03/' -e '0,/00/s//04/' -e '1,/00/s//05/' -e '0,/00/s//06/'`
#echo $NOMES
/root/bin/setvcl $NOMES > /tmp/backend.vcl
DIF=`diff /tmp/backend.vcl /etc/varnish/backend.vcl`
#echo $DIF
if [ -n $DIF ]; then
 echo "${DATA} - Backend sem mudancas"
else
 echo -e "\n Bakcend changed, updating VCL and Reloading VARNISH\n\n"
 mv -f /tmp/backend.vcl /etc/varnish/backend.vcl
 [ -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
fi

2.Update VCL
Here is the script to update the vcl

#!/bin/bash
#script to setup the vcl
if [ $# -lt 2 ]
then
        echo "Usage : $0 SERVER IP    "
        exit 1
fi
PORT='"'8008'"'
LINE=';.port = "8008";  .probe = {.url = "/index.html";.interval = 5s;.timeout = 1 s;.window = 5;.threshold = 3;  }}'
while [ "$1" != "" -a "$2" != "" ]
do
 SERVER=$1
 IP="$2"
 IP='"'$IP'"'
# echo -e "\n${I}\nSetting SERVER=${SERVER} and IP=${IP} \n "
 LINEB="$LINEB \n backend ${SERVER}{  .host = ${IP} $LINE"
 LINEF="$LINEF \n { .backend = ${SERVER};  } "
 shift
 shift
done
echo -e $LINEB
echo -e "\ndirector lbweb1 round-robin {\n"
echo -e $LINEF
echo -e "}"
Tags: , , , , ,

1 thought on “varnish dynamic backend

  1. After the editing of the vcl file, you can reload the vcl with the command “varnish_reload_vcl”, that switches the configuration using vcl.load, vcl.use and vcl.list in the same way you did with your script.

Leave a Reply

Your email address will not be published. Required fields are marked *