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