-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdocker
executable file
·125 lines (87 loc) · 4.81 KB
/
docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
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}')
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}')
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 "* Export Data from MySQL01 to MySQL02"
mysqldump -uroot -proot -h $MYSQL01_IP --single-transaction --all-databases \
--flush-privileges | mysql -uroot -proot -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}')
mysql -uroot -proot -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}')
mysql -uroot -proot -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;"
echo "* Create database 'wordpress' on MySQL01"
mysql -uroot -proot -h $MYSQL01_IP -e "create database wordpress;"
echo "* Load up Site SQL data"
mysql -uroot -proot -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
echo
echo "Create MySQL Load Balancer"
echo "--------------------------"
echo "* Create HAProxy-MySQL"
mysql -uroot -proot -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}')
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
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}')
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}')
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"
echo "Environment Created!"
echo "--------------------"
echo
echo "Browse to http://$HAPROXY_WEB_IP to access your wordpress site"
echo
echo Variables available fo you :-
echo
echo MYSQL01_IP : $MYSQL01_IP
echo MYSQL02_IP : $MYSQL02_IP
echo HAPROXY_MYSQL_IP : $HAPROXY_MYSQL_IP
echo WORDPRESS1_IP : $WORDPRESS1_IP
echo WORDPRESS2_IP : $WORDPRESS2_IP
echo HAPROXY_WEB_IP : $HAPROXY_WEB_IP
echo
echo you may want to run the following :-
#echo "sudo su -c 'echo \"$HAPROXY_WEB_IP wordpress\" >> /etc/hosts'"
echo "sudo sed -i 's/^.*wordpress.*$/$HAPROXY_WEB_IP wordpress/' /etc/hosts"