-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhaproxy.cloudscript
More file actions
225 lines (171 loc) · 7.22 KB
/
haproxy.cloudscript
File metadata and controls
225 lines (171 loc) · 7.22 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Install latest HAProxy on CS05-SSD (1 x $.008/hr) and latest Apache on CS1-SSD (2x $.015/hr) all running Linux Ubuntu Server 14.04 LTS 64-bit
cloudscript haproxy_multi_stack
version = _LATEST
result_template = haproxy_result_template
globals
haproxy_hostname = 'haproxy'
haproxy_image_type = 'Ubuntu Server 14.04 LTS'
haproxy_instance_type = 'CS05-SSD' # 0.5GB RAM, 1 vCore, 25GB SSD, 10Gbps
apache_hostname_first = 'apache1'
apache_hostname_second = 'apache2'
apache_image_type = 'Ubuntu Server 14.04 LTS'
apache_instance_type = 'CS1-SSD' # 1GB RAM, 1 vCore, 25GB SSD, 10Gbps
server_pass = lib::random_password()
console_pass = lib::random_password()
default_slice_user = 'haproxy'
thread haproxy_setup
tasks = [haproxy_server_setup]
task haproxy_server_setup
#-------------------------------
# create haproxy keys
#-------------------------------
# create haproxy server root password key
/key/password haproxy_server_password_key read_or_create
key_group = _SERVER
password = server_pass
# create haproxy server console key
/key/password haproxy_server_console_key read_or_create
key_group = _CONSOLE
password = console_pass
# create storage slice keys
/key/token default_slice_key read_or_create
username = default_slice_user
#-------------------------------
# create slice and container
#-------------------------------
# create slice to store script in cloudstorage
/storage/slice default_slice read_or_create
keys = [default_slice_key]
# create slice container to store script in cloudstorage
/storage/container haproxy_container => [default_slice] read_or_create
slice = default_slice
#-------------------------
# create apache bootstrap
# script and recipe
#-------------------------
# place script data in cloudstorage
/storage/object apache_bootstrap_object => [default_slice, haproxy_container] read_or_create
container_name = 'haproxy_container'
file_name = 'bootstrap_apache.sh'
slice = default_slice
content_data = apache_bootstrap_data
# associate the cloudstorage object with the haproxy script
/orchestration/script apache_bootstrap_script => [default_slice, haproxy_container, apache_bootstrap_object] read_or_create
data_uri = 'cloudstorage://default_slice/haproxy_container/bootstrap_apache.sh'
script_type = _SHELL
encoding = _STORAGE
# create the recipe and associate the script
/orchestration/recipe apache_bootstrap_recipe read_or_create
scripts = [apache_bootstrap_script]
#-----------------------
# create apache servers
#-----------------------
/server/cloud apache1_server read_or_create
hostname = '{{ apache_hostname_first }}'
image = '{{ apache_image_type }}'
service_type = '{{ apache_instance_type }}'
keys = [haproxy_server_password_key, haproxy_server_console_key]
recipes = [apache_bootstrap_recipe]
/server/cloud apache2_server read_or_create
hostname = '{{ apache_hostname_second }}'
image = '{{ apache_image_type }}'
service_type = '{{ apache_instance_type }}'
keys = [haproxy_server_password_key, haproxy_server_console_key]
recipes = [apache_bootstrap_recipe]
#--------------------------
# create haproxy bootstrap
# script and recipe
#--------------------------
# place script data in cloudstorage
/storage/object haproxy_bootstrap_object => [default_slice, haproxy_container, apache1_server, apache2_server] read_or_create
container_name = 'haproxy_container'
file_name = 'bootstrap_haproxy.sh'
slice = default_slice
content_data = haproxy_bootstrap_data
# associate the cloudstorage object with the haproxy script
/orchestration/script haproxy_bootstrap_script => [default_slice, haproxy_container, haproxy_bootstrap_object] read_or_create
data_uri = 'cloudstorage://default_slice/haproxy_container/bootstrap_haproxy.sh'
script_type = _SHELL
encoding = _STORAGE
# create the recipe and associate the script
/orchestration/recipe haproxy_bootstrap_recipe read_or_create
scripts = [haproxy_bootstrap_script]
#-----------------------
# create haproxy server
#-----------------------
/server/cloud haproxy_server read_or_create
hostname = '{{ haproxy_hostname }}'
image = '{{ haproxy_image_type }}'
service_type = '{{ haproxy_instance_type }}'
keys = [haproxy_server_password_key, haproxy_server_console_key]
recipes = [haproxy_bootstrap_recipe]
#-------------------------
# Apache
#-------------------------
text_template apache_bootstrap_data
#!/bin/sh
# get latest package list
apt-get update
# install apache2
apt-get install -y apache2
echo "Server `hostname` is responding which was created at `date`" > /var/www/html/index.html
_eof
#-------------------------
# HAProxy
#-------------------------
text_template haproxy_bootstrap_data
#!/bin/sh
# get latest package list
apt-get update
# install haproxy
apt-get install -y haproxy
cat <<EOF>/etc/default/haproxy
ENABLED=1
EOF
# build haproxy.cfg
cat <<EOF>/etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local1 notice
maxconn 4096
daemon
defaults
log global
balance roundrobin
mode http
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http
bind 0.0.0.0:80
mode http
default_backend server_pool
backend server_pool
stats enable
stats uri /stats
EOF
# use private interfaces for free traffic between proxy and web servers
echo " server server1 {{ apache1_server.ipaddress_private }}:80 check inter 2000 fall 3" >> /etc/haproxy/haproxy.cfg
echo " server server2 {{ apache2_server.ipaddress_private }}:80 check inter 2000 fall 3" >> /etc/haproxy/haproxy.cfg
# start haproxy
service haproxy start
_eof
text_template haproxy_result_template
Your haproxy loadbalanced site is located at:
http://{{ haproxy_server.ipaddress_public }}/
You may SSH to your servers and login with the following credentials:
haproxy: {{ haproxy_server.ipaddress_public }}
username: root
password: {{ haproxy_server_password_key.password }}
apache1: {{ apache1_server.ipaddress_public }}
username: root
password: {{ haproxy_server_password_key.password }}
apache2: {{ apache2_server.ipaddress_public }}
username: root
password: {{ haproxy_server_password_key.password }}
Your apache web servers are located at:
http://{{ apache1_server.ipaddress_public }}/
http://{{ apache2_server.ipaddress_public }}/
_eof