-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
170 lines (142 loc) · 5.16 KB
/
deploy.sh
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
#!/bin/bash
# The MIT License (MIT)
# Copyright (c) 2016 ScoreCI
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -ex
ZONE=us-central1-f
GROUP=scoreci-github-worker-group
TEMPLATE=$GROUP-tmpl
MACHINE_TYPE=f1-micro
IMAGE_FAMILY=debian-8
IMAGE_PROJECT=debian-cloud
STARTUP_SCRIPT=startup-script.sh
SCOPES="cloud-platform"
TAGS=http-server
MIN_INSTANCES=1
MAX_INSTANCES=1
TARGET_UTILIZATION=0.6
SERVICE=scoreci-github-worker
#
# Instance group setup
#
# First we have to create an instance template.
# This template will be used by the instance group
# to create new instances.
echo "Create Instance Template."
# [START create_template]
gcloud compute instance-templates create $TEMPLATE \
--image-family $IMAGE_FAMILY \
--image-project $IMAGE_PROJECT \
--machine-type $MACHINE_TYPE \
--scopes $SCOPES \
--metadata-from-file startup-script=$STARTUP_SCRIPT \
--tags $TAGS
# [END create_template]
# Create the managed instance group.
echo "Create Managed Instance Group."
# [START create_group]
gcloud compute instance-groups managed \
create $GROUP \
--base-instance-name $GROUP \
--size $MIN_INSTANCES \
--template $TEMPLATE \
--zone $ZONE
# [END create_group]
# [START create_named_port]
echo "Create Named Port."
gcloud compute instance-groups managed set-named-ports \
$GROUP \
--named-ports http:8080 \
--zone $ZONE
# [END create_named_port]
#
# Load Balancer Setup
#
# A complete HTTP load balancer is structured as follows:
#
# 1) A global forwarding rule directs incoming requests to a target HTTP proxy.
# 2) The target HTTP proxy checks each request against a URL map to determine the
# appropriate backend service for the request.
# 3) The backend service directs each request to an appropriate backend based on
# serving capacity, zone, and instance health of its attached backends. The
# health of each backend instance is verified using either a health check.
#
# We'll create these resources in reverse order:
# service, health check, backend service, url map, proxy.
# Create a health check
# The load balancer will use this check to keep track of which instances to send traffic to.
# Note that health checks will not cause the load balancer to shutdown any instances.
# [START create_health_check]
echo "Create Health Check."
gcloud compute http-health-checks create ah-health-check \
--request-path /_ah/health \
--port 8080
# [END create_health_check]
# Create a backend service, associate it with the health check and instance group.
# The backend service serves as a target for load balancing.
# [START create_backend_service]
echo "Create Backend Service."
gcloud compute backend-services create $SERVICE \
--http-health-checks ah-health-check \
--port 8080
# [END create_backend-service]
# [START add_backend_service]
echo "Add Backend Service."
gcloud compute backend-services add-backend $SERVICE \
--instance-group $GROUP \
--instance-group-zone $ZONE
# [END add_backend_service]
# Create a URL map and web Proxy. The URL map will send all requests to the
# backend service defined above.
# [START create_url_map]
echo "Create URL Map."
gcloud compute url-maps create $SERVICE-map \
--default-service $SERVICE
# [END create_url_map]
# [START create_http_proxy]
echo "Create HTTP Proxy."
gcloud compute target-http-proxies create $SERVICE-proxy \
--url-map $SERVICE-map
# [END create_http_proxy]
# Create a global forwarding rule to send all traffic to our proxy
# [START create_forwarding_rule]
echo "Create Forwarding Rule."
gcloud compute forwarding-rules create $SERVICE-http-rule \
--global \
--target-http-proxy $SERVICE-proxy \
--ports 80
# [END create_forwarding_rule]
#
# Autoscaler configuration
#
# [START set_autoscaling]
echo "Set autoscaling."
gcloud compute instance-groups managed set-autoscaling \
$GROUP \
--max-num-replicas $MAX_INSTANCES \
--target-load-balancing-utilization $TARGET_UTILIZATION \
--zone $ZONE
# [END set_autoscaling]
# [START create_firewall]
echo "Create Firewall Rules."
gcloud compute firewall-rules create default-allow-http-8080 \
--allow tcp:8080 \
--source-ranges 0.0.0.0/0 \
--target-tags http-server \
--description "Allow port 8080 access to http-server"
# [END create_firewall]
echo "Done."