Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes with respect to current docker syntax for IP detection #2

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
95 changes: 59 additions & 36 deletions docker
Original file line number Diff line number Diff line change
@@ -1,115 +1,138 @@
#!/bin/bash
#~/kklepper/docker-wordpress$ nano docker

# to get rid of MySQL warnings "Warning: Using a password on the command line interface can be insecure."

echo
echo "Create credentials"

credentialsFile=~/.docker-wordpress-credentials.cnf
echo "[client]" > $credentialsFile
echo "user=root" >> $credentialsFile
echo "password=root" >> $credentialsFile


echo
echo "Create MySQL Tier"
echo "-----------------"
echo "* Create MySQL01"

MYSQL01=$(docker run -d paulczar/mysql mysqld_safe --server-id=1 --log-bin=mysql-bin --log-slave-updates=1 --auto_increment_increment=2 --auto_increment_offset=1)
MYSQL01_IP=$(docker inspect $MYSQL01 | grep IPAd | awk -F'"' '{print $4}')
MYSQL01=$(docker run -d --name m1 paulczar/mysql mysqld_safe --server-id=1 --log-bin=mysql-bin --log-slave-updates=1 --auto_increment_increment=2 --auto_increment_offset=1)
MYSQL01_IP=$(docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' $MYSQL01)
# new docker syntax

echo "* Create MySQL02"

MYSQL02=$(docker run -d paulczar/mysql mysqld_safe --server-id=2 --log-bin=mysql-bin --log-slave-updates=1 --auto_increment_increment=2 --auto_increment_offset=2)
MYSQL02_IP=$(docker inspect $MYSQL02 | grep IPAd | awk -F'"' '{print $4}')
MYSQL02=$(docker run -d --name m2 paulczar/mysql mysqld_safe --server-id=2 --log-bin=mysql-bin --log-slave-updates=1 --auto_increment_increment=2 --auto_increment_offset=2)
MYSQL02_IP=$(docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' $MYSQL02)
# new docker syntax

echo "* Sleep for two seconds for servers to come online..."
sleep 2

echo "* Creat replication user"

mysql -uroot -proot -h $MYSQL01_IP -AN -e 'GRANT REPLICATION SLAVE ON *.* TO "replication"@"%" IDENTIFIED BY "password";'
mysql -uroot -proot -h $MYSQL01_IP -AN -e 'flush privileges;'
echo "* Create replication user"

mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -AN -e 'GRANT REPLICATION SLAVE ON *.* TO "replication"@"%" IDENTIFIED BY "password";'
mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -AN -e 'flush privileges;'

echo "* Export Data from MySQL01 to MySQL02"

mysqldump -uroot -proot -h $MYSQL01_IP --single-transaction --all-databases \
--flush-privileges | mysql -uroot -proot -h $MYSQL02_IP
mysqldump --defaults-extra-file=$credentialsFile -h $MYSQL01_IP --single-transaction --all-databases \
--flush-privileges | mysql --defaults-extra-file=$credentialsFile -h $MYSQL02_IP

echo "* Set MySQL01 as master on MySQL02"

MYSQL01_Position=$(mysql -uroot -proot -h $MYSQL01_IP -e "show master status \G" | grep Position | awk '{print $2}')
MYSQL01_File=$(mysql -uroot -proot -h $MYSQL01_IP -e "show master status \G" | grep File | awk '{print $2}')
MYSQL01_Position=$(mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -e "show master status \G" | grep Position | awk '{print $2}')
MYSQL01_File=$(mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -e "show master status \G" | grep File | awk '{print $2}')

mysql -uroot -proot -h $MYSQL02_IP -AN -e "CHANGE MASTER TO master_host='$MYSQL01_IP', master_port=3306, \
mysql --defaults-extra-file=$credentialsFile -h $MYSQL02_IP -AN -e "CHANGE MASTER TO master_host='$MYSQL01_IP', master_port=3306, \
master_user='replication', master_password='password', master_log_file='$MYSQL01_File', \
master_log_pos=$MYSQL01_Position;"

echo "* Set MySQL02 as master on MySQL01"

MYSQL02_Position=$(mysql -uroot -proot -h $MYSQL02_IP -e "show master status \G" | grep Position | awk '{print $2}')
MYSQL02_File=$(mysql -uroot -proot -h $MYSQL02_IP -e "show master status \G" | grep File | awk '{print $2}')
MYSQL02_Position=$(mysql --defaults-extra-file=$credentialsFile -h $MYSQL02_IP -e "show master status \G" | grep Position | awk '{print $2}')
MYSQL02_File=$(mysql --defaults-extra-file=$credentialsFile -h $MYSQL02_IP -e "show master status \G" | grep File | awk '{print $2}')

mysql -uroot -proot -h $MYSQL01_IP -AN -e "CHANGE MASTER TO master_host='$MYSQL02_IP', master_port=3306, \
mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -AN -e "CHANGE MASTER TO master_host='$MYSQL02_IP', master_port=3306, \
master_user='replication', master_password='password', master_log_file='$MYSQL02_File', \
master_log_pos=$MYSQL02_Position;"

echo "* Start Slave on both Servers"
mysql -uroot -proot -h $MYSQL01_IP -AN -e "start slave;"
mysql -uroot -proot -h $MYSQL02_IP -AN -e "start slave;"
mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -AN -e "start slave;"
mysql --defaults-extra-file=$credentialsFile -h $MYSQL02_IP -AN -e "start slave;"

echo "* Create database 'wordpress' on MySQL01"

mysql -uroot -proot -h $MYSQL01_IP -e "create database wordpress;"
mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -e "create database wordpress;"

echo "* Load up Site SQL data"

mysql -uroot -proot -h $MYSQL01_IP wordpress < wordpress.sql
mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP wordpress < wordpress.sql

echo "* Sleep 2 seconds, then check that database 'wordpress' exists on MySQL02"

sleep 2
mysql -uroot -proot -h $MYSQL02_IP -e "show databases; \G" | grep wordpress
mysql --defaults-extra-file=$credentialsFile -h $MYSQL02_IP -e "show databases; \G" | grep wordpress

echo
echo "Create MySQL Load Balancer"
echo "--------------------------"

echo "* Create HAProxy-MySQL"

mysql -uroot -proot -h $MYSQL01_IP -AN -e "GRANT USAGE ON *.* TO 'haproxy'@'%';"
mysql --defaults-extra-file=$credentialsFile -h $MYSQL01_IP -AN -e "GRANT USAGE ON *.* TO 'haproxy'@'%';"

HAPROXY_MYSQL=$(docker run -d paulczar/haproxy-mysql /haproxy/start "$MYSQL01_IP:3306,$MYSQL02_IP:3306")
HAPROXY_MYSQL_IP=$(docker inspect $HAPROXY_MYSQL | grep IPAd | awk -F'"' '{print $4}')
HAPROXY_MYSQL_IP=$(docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' $HAPROXY_MYSQL)
# new docker syntax

echo "* Check our haproxy works"
echo " (should show alternating server_id)"

mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql -uroot -proot -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql --defaults-extra-file=$credentialsFile -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql --defaults-extra-file=$credentialsFile -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql --defaults-extra-file=$credentialsFile -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id
mysql --defaults-extra-file=$credentialsFile -h $HAPROXY_MYSQL_IP -e 'show variables like "server_id"' | grep server_id

echo
echo "Create Wordpress Web Servers"
echo "------------------------"

echo "* Create WordPress01"
WORDPRESS1=$(docker run -d paulczar/apache2-wordpress /wordpress/start $HAPROXY_MYSQL_IP)
WORDPRESS1_IP=$(docker inspect $WORDPRESS1 | grep IPAd | awk -F'"' '{print $4}')
WORDPRESS1_IP=$(docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' $WORDPRESS1)
# new docker syntax

echo "* Create WordPress02"
WORDPRESS2=$(docker run -d paulczar/apache2-wordpress /wordpress/start $HAPROXY_MYSQL_IP)
WORDPRESS2_IP=$(docker inspect $WORDPRESS2 | grep IPAd | awk -F'"' '{print $4}')
WORDPRESS2_IP=$(docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' $WORDPRESS2)
# new docker syntax

echo
echo "Create Web Load Balancer"
echo "--------------------------"

echo "* Create HAProxy-Web"
HAPROXY_WEB=$(docker run -d paulczar/haproxy-web /haproxy/start "$WORDPRESS1_IP:80,$WORDPRESS2_IP:80")
HAPROXY_WEB_IP=$(docker inspect $HAPROXY_WEB | grep IPAd | awk -F'"' '{print $4}')

echo "* Check it works"

curl -s http://$HAPROXY_WEB_IP/phpinfo.php | grep "PHP API"
HAPROXY_WEB=$(docker run -p 80:80 -d paulczar/haproxy-web /haproxy/start "$WORDPRESS1_IP:80,$WORDPRESS2_IP:80")
HAPROXY_WEB_IP=$(docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' $HAPROXY_WEB)
# new docker syntax

echo "* Check it works ------------ (should show alternating container id) ----------"
curl -s http://$HAPROXY_WEB_IP/phpinfo.php | grep "HOSTNAME" | awk '{print $4}' | awk -F\> '{printf "container ID (HOSTNAME): %s\n", $2}'
curl -s http://$HAPROXY_WEB_IP/phpinfo.php | grep "HOSTNAME" | awk '{print $4}' | awk -F\> '{printf "container ID (HOSTNAME): %s\n", $2}'
curl -s http://$HAPROXY_WEB_IP/phpinfo.php | grep "HOSTNAME" | awk '{print $4}' | awk -F\> '{printf "container ID (HOSTNAME): %s\n", $2}'
curl -s http://$HAPROXY_WEB_IP/phpinfo.php | grep "HOSTNAME" | awk '{print $4}' | awk -F\> '{printf "container ID (HOSTNAME): %s\n", $2}'
#curl -s http://$HAPROXY_WEB_IP/phpinfo.php | grep "PHP API"

echo "--------------------"
echo "Environment Created!"
echo "--------------------"
echo
echo "Browse to http://$HAPROXY_WEB_IP to access your wordpress site"
echo "Browse to http://$HAPROXY_WEB_IP"
echo "--- or to your public IP to access your wordpress site"
echo "--- may look bad due to incorrect link to wordpress css file: "
echo "--- http://wordpress/wp-content/themes/twentythirteen/style.css?ver=2013-07-18"
echo
echo Variables available fo you :-
echo
Expand Down