1+ {#
2+ Copyright 2016 Google Inc. All rights reserved.
3+ Licensed under the Apache License, Version 2.0 (the "License");
4+ you may not use this file except in compliance with the License.
5+ You may obtain a copy of the License at
6+ http://www.apache.org/licenses/LICENSE-2.0
7+ Unless required by applicable law or agreed to in writing, software
8+ distributed under the License is distributed on an "AS IS" BASIS,
9+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+ See the License for the specific language governing permissions and
11+ limitations under the License.
12+ #}
13+
14+ {# [START all] #}
15+
16+ {# [START env] #}
17+ {% set NAME = "bookshelf-" + env ["deployment" ] %}
18+ {% set SERVICE = "bookshelf-" + env ["deployment" ] + "-frontend" %}
19+ {# [END env] #}
20+
21+ #
22+ # Instance group setup
23+ #
24+
25+ # First we have to create an instance template.
26+ # This template will be used by the instance group
27+ # to create new instances.
28+ resources:
29+ - name : {{ NAME }}
30+ type: compute.v1.instanceTemplate
31+ properties:
32+ properties:
33+ tags:
34+ items:
35+ - http-server
36+ disks:
37+ - boot: True
38+ type: PERSISTENT
39+ initializeParams:
40+ sourceImage: {{ properties['machine-image'] }}
41+ diskSizeGb: 10
42+ diskType: pd-ssd
43+ machineType: {{ properties['machine-type'] }}
44+ serviceAccounts:
45+ - email: default
46+ scopes: {{ properties['scopes'] }}
47+ metadata:
48+ items:
49+ - key: startup-script
50+ {# [START startup] #}
51+ value: |
52+ {{imports['startup-script'] |indent(14, true)}}
53+ {# [END startup] #}
54+ networkInterfaces:
55+ - network: global/networks/default
56+ accessConfigs:
57+ - type: ONE_TO_ONE_NAT
58+ name: External NAT
59+
60+ # Creates the managed instance group. This is responsible for creating
61+ # new instances using the instance template, as well as providing a named
62+ # port the backend service can target
63+ - name: {{ NAME }}-frontend-group
64+ type: compute.v1.instanceGroupManager
65+ properties:
66+ instanceTemplate: $(ref.{{ NAME }}.selfLink)
67+ baseInstanceName: frontend-group
68+ targetSize: 3
69+ zone: {{ properties['zone'] }}
70+ namedPorts:
71+ - name: http
72+ port: 8080
73+
74+
75+
76+ # Load Balancer Setup
77+ #
78+
79+ # A complete HTTP load balancer is structured as follows:
80+ #
81+ # 1) A global forwarding rule directs incoming requests to a target HTTP proxy.
82+ # 2) The target HTTP proxy checks each request against a URL map to determine the
83+ # appropriate backend service for the request.
84+ # 3) The backend service directs each request to an appropriate backend based on
85+ # serving capacity, zone, and instance health of its attached backends. The
86+ # health of each backend instance is verified using either a health check.
87+ #
88+ # We'll create these resources in reverse order:
89+ # service, health check, backend service, url map, proxy.
90+
91+ # Create a health check
92+ # The load balancer will use this check to keep track of which instances to send traffic to.
93+ # Note that health checks will not cause the load balancer to shutdown any instances.
94+ - name: {{ NAME }}-health-check
95+ type: compute.v1.httpHealthCheck
96+ properties:
97+ requestPath: /_ah/health
98+ port: 8080
99+
100+ # Create a backend service, associate it with the health check and instance group.
101+ # The backend service serves as a target for load balancing.
102+ - name: {{ SERVICE }}
103+ type: compute.v1.backendService
104+ properties:
105+ healthChecks:
106+ - $(ref.{{ NAME }}-health-check.selfLink)
107+ portName: http
108+ backends:
109+ {# [START reference] #}
110+ - group: $(ref.{{ NAME }}-frontend-group.instanceGroup)
111+ zone: {{ properties['zone'] }}
112+ {# [END reference] #}
113+
114+ # Create a URL map and web Proxy. The URL map will send all requests to the
115+ # backend service defined above.
116+ - name: {{ SERVICE }}-map
117+ type: compute.v1.urlMap
118+ properties:
119+ defaultService: $(ref.{{ SERVICE }}.selfLink)
120+
121+ # This is the actual proxy which uses the URL map to route traffic
122+ # to the backend service
123+ - name: {{ SERVICE }}-proxy
124+ type: compute.v1.targetHttpProxy
125+ properties:
126+ urlMap: $(ref.{{ SERVICE }}-map.selfLink)
127+
128+ # This is the global forwarding rule which creates an external IP to
129+ # target the http poxy
130+ - name: {{ SERVICE }}-http-rule
131+ type: compute.v1.globalForwardingRule
132+ properties:
133+ target: $(ref.{{ SERVICE }}-proxy.selfLink)
134+ portRange: 80
135+
136+ # Creates an autoscaler resource (note that when using the gcloud CLI,
137+ # autoscaling is set as a configuration of the managed instance group
138+ # but autoscaler is a resource so in deployment manager we explicitly
139+ # define it
140+ - name: {{ NAME }}-autoscaler
141+ type: compute.v1.autoscaler
142+ properties:
143+ zone: {{ properties['zone'] }}
144+ target: $(ref.{{ NAME }}-frontend-group.selfLink)
145+ autoscalingPolicy:
146+ {# [START properties] #}
147+ minNumReplicas: {{ properties['min-instances'] }}
148+ maxNumReplicas: {{ properties['max-instances'] }}
149+ loadBalancingUtilization:
150+ utilizationTarget: {{ properties['target-utilization'] }}
151+ {# [END properties] #}
152+
153+ # Firewall rule that allows traffic to GCE instances with the
154+ # http server tag we created
155+ - name: {{ NAME }}-allow-http
156+ type: compute.v1.firewall
157+ properties:
158+ allowed:
159+ - IPProtocol: tcp
160+ ports:
161+ - 8080
162+ sourceRanges:
163+ - 0.0.0.0/0
164+ targetTags:
165+ - http-server
166+ description: "Allow port 8080 access to http-server"
167+
168+ {# [END all] #}
0 commit comments