forked from GoogleCloudPlatform/getting-started-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·123 lines (103 loc) · 3.69 KB
/
deploy.sh
File metadata and controls
executable file
·123 lines (103 loc) · 3.69 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
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#! /bin/bash
set -ex
ZONE=us-central1-f
GROUP=frontend-group
TEMPLATE=$GROUP-tmpl
MACHINE_TYPE=f1-micro
STARTUP_SCRIPT=startup-script.sh
SCOPES="userinfo-email,\
logging-write,\
storage-full,\
datastore,\
https://www.googleapis.com/auth/pubsub,\
https://www.googleapis.com/auth/projecthosting"
TAGS=http-server
MIN_INSTANCES=1
MAX_INSTANCES=10
TARGET_UTILIZATION=0.6
SERVICE=frontend-web-service
#
# Instance group setup
#
# First we have to create an instance template.
# This template will be used by the instance group
# to create new instances.
gcloud compute instance-templates create $TEMPLATE \
--machine-type $MACHINE_TYPE \
--scopes $SCOPES \
--metadata-from-file startup-script=$STARTUP_SCRIPT \
--tags $TAGS
# Create the managed instance group.
gcloud compute instance-groups managed \
create $GROUP \
--base-instance-name $GROUP \
--size $MIN_INSTANCES \
--template $TEMPLATE \
--zone $ZONE
gcloud compute instance-groups managed set-named-ports \
$GROUP \
--named-port http:8080 \
--zone $ZONE
#
# 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.
gcloud compute http-health-checks create ah-health-check \
--request-path /_ah/health
# Create a backend service, associate it with the health check and instance group.
# The backend service serves as a target for load balancing.
gcloud compute backend-services create $SERVICE \
--http-health-check ah-health-check
gcloud compute backend-services add-backend $SERVICE \
--group $GROUP \
--zone $ZONE
# Create a URL map and web Proxy. The URL map will send all requests to the
# backend service defined above.
gcloud compute url-maps create $SERVICE-map \
--default-service $SERVICE
gcloud compute target-http-proxies create $SERVICE-proxy \
--url-map $SERVICE-map
# Create a global forwarding rule to send all traffic to our proxy
gcloud compute forwarding-rules create $SERVICE-http-rule \
--global \
--target-http-proxy $SERVICE-proxy \
--port-range 80
#
# Autoscaler configuration
#
gcloud compute instance-groups managed set-autoscaling \
$GROUP \
--max-num-replicas $MAX_INSTANCES \
--target-load-balancing-utilization $TARGET_UTILIZATION \
--zone $ZONE
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"