Skip to content

Instantly share code, notes, and snippets.

@simbo1905 simbo1905/run
Created Nov 29, 2018

Embed
What would you like to do?
OKD openshift randomise which database portal host to use for compose.com high availability
#!/bin/sh -e
if [[ ! -z ${DB_HOST_LIST} ]]; then
echo "Selecting DB_HOST from ${DB_HOST_LIST}"
SELECT_LIST_SEPERATOR=","
SELECT_ARRAY=( ${DB_HOST_LIST//$SELECT_LIST_SEPERATOR/ } )
export DB_HOST="${SELECT_ARRAY[$RANDOM % ${#SELECT_ARRAY[@]}]}"
echo "DB_HOST=${DB_HOST}"
fi
source ${STI_SCRIPTS_PATH}/run
@simbo1905

This comment has been minimized.

Copy link
Owner Author

simbo1905 commented Nov 29, 2018

At compose.com they give you two entrypoints to your high availability database cluster such as:

aws-eu-west-1-portal.2.dblayer.com:18686
aws-eu-west-1-portal.0.dblayer.com:18686

if you hard configure your DB_HOST to one or the other hostname then you have a single point of failure at the portal entry point. You may then need to manually reconfigure to use the surviving portal (aka HAProxy fronting your db as a TCP proxy).

with this snippet executable at .s2i/bin/run you can set DB_HOST_LIST as a comma-separated list like:

docker run -u 100000 -p 8080:8080 --env-file=./.env.staging -e DB_HOST_LIST="aws-eu-west-1-portal.0.dblayer.com,aws-eu-west-1-portal.2.dblayer.com" uniqkey-backend-app

and it will log:

DB_HOST=aws-eu-west-1-portal.0.dblayer.com

or:

DB_HOST=aws-eu-west-1-portal.2.dblayer.com

selected at random.

Note that if the DB_HOST is still being set as an env var when you login on the terminal you see what is set for the whole environment not how it is has been overridden in the script that launches php. The randomly selected value can be confirmed by looking at the /proc/1/environ (the environment of the process that docker launched):

bash-4.2$ strings /proc/1/environ | grep DB_HOST
DB_HOST=aws-eu-west-1-portal.0.dblayer.com
DB_HOST_LIST=aws-eu-west-1-portal.0.dblayer.com,aws-eu-west-1-portal.2.dblayer.com

This means that when we set this new code and the new env var DB_HOST_LIST we should delete the old DB_HOST that is ignored else it will cause confusion by being visible in the terminal and web console of openshift.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.