@@ -37,6 +37,10 @@ Google Kubernetes Engine.
37
37
1 . Set up a project on [ Google Cloud Console] ( http://cloud.google.com/console )
38
38
if you don't have one, and enable billing.
39
39
40
+ Optional: Set the ` PROJECT_ID ` environment variable in your shell, to the
41
+ ID of the project you created. Some commands below will use this variable
42
+ substitution. (Or you can just substitue the value yourself.)
43
+
40
44
2 . Install the [ Google Cloud SDK] ( https://cloud.google.com/sdk/ ) on your
41
45
workstation if you don't have it. This includes:
42
46
@@ -54,12 +58,13 @@ Google Kubernetes Engine.
54
58
55
59
3 . Configure gcloud for your project.
56
60
57
- 1 . Set the default project:
61
+ 1 . Set the default project. (Substitute your project ID.)
58
62
59
- gcloud config set project *<your-project-id>*
63
+ gcloud config set project ${PROJECT_ID}
60
64
61
65
2 . Set a default zone (i.e. data center location). I think "us-central1-c"
62
- is a good one.
66
+ is generally a good one for much of the US, but you may choose one
67
+ closer to your location.
63
68
64
69
gcloud config set compute/zone us-central1-c
65
70
@@ -75,27 +80,30 @@ Google Kubernetes Engine.
75
80
76
81
### Building
77
82
78
- We will use Google's [ Container Builder ] ( https://cloud.google.com/container-builder / )
83
+ We will use Google's [ Cloud Build ] ( https://cloud.google.com/cloud-build / )
79
84
service to build the Tanx application in a Docker image. Notice that there is
80
85
a Dockerfile provided. It uses Distillery to produce an OTP release, and
81
86
installs it in a Docker image based on bitwalker's Alpine-Erlang image. We
82
- will just hand this to Container Builder to build an image and upload it to
87
+ will just hand this to Google Cloud Build to build an image and upload it to
83
88
your project's container registry.
84
89
85
- To perform the first build:
90
+ To perform the first build.
86
91
87
- gcloud container builds submit --tag=gcr.io/${PROJECT_ID}/tanx:v1 .
92
+ gcloud builds submit --config deploy/cloudbuild.yml .
88
93
89
- Make sure you substitute your project ID. The period at the end is required; it
90
- is the root directory for the application you are building.
94
+ The period at the end is required; it is the root directory for the application
95
+ you are building.
91
96
92
- This will build your image in the cloud, and upload it to the tag you provided.
97
+ This will build your image in the cloud, and upload it to your project's image
98
+ registry.
93
99
94
100
### Local builds (optional)
95
101
96
- You may also build an image locally using, for example,
97
- ` docker build -t tanx . ` . Note that Docker 17.05 or later is required because
98
- the Dockerfile is a multistage script.
102
+ You may also build an image locally using, for example:
103
+
104
+ docker build -f deploy/Dockerfile -t tanx .
105
+
106
+ Note that Docker 17.05 or later is required.
99
107
100
108
You may run a local build using:
101
109
@@ -106,18 +114,18 @@ container.
106
114
107
115
### Create a cluster
108
116
109
- Now we'll set up container engine to host an online tanx server.
117
+ Now we'll set up Google Kubernetes Engine to host an online tanx server.
110
118
111
119
1 . Choose a cluster name. For the rest of these instructions, I'll assume that
112
120
name is "tanx-cluster-1".
113
121
114
122
2 . Create the cluster.
115
123
116
- gcloud container clusters create tanx-cluster-1 --machine-type=n1-highcpu-2 -- num-nodes=1
124
+ gcloud container clusters create tanx-cluster-1 --num-nodes=2
117
125
118
- You can of course replace the cluster name with a name of your choosing.
119
- You can use a different machine type as well, although for now I recommend
120
- highcpu types since the application seems CPU bound for the moment .
126
+ You can of course replace the cluster name with a name of your choosing, as
127
+ well as change the number of nodes in the cluster and the machine type.
128
+ For now, the defaults will get you started .
121
129
122
130
3 . Configure gcloud to use your cluster as default so you don't have to
123
131
specify it every time for the remaining gcloud commands.
@@ -126,59 +134,70 @@ Now we'll set up container engine to host an online tanx server.
126
134
127
135
Replace the name if you named your cluster differently.
128
136
129
- Check the cloud console at http://cloud.google.com/console under container
130
- engine to see that your cluster is running. Note that once the cluster is
131
- running, you will be charged for the VM usage.
137
+ 4 . Check the cloud console at http://console.cloud.google.com/kubernetes to
138
+ make sure your cluster is running. Note that once the cluster is running,
139
+ you will be charged for the VM usage.
140
+
141
+ 5 . The app will need to access the kubernetes API so it can configure the OTP
142
+ cluster. To set up that access, first give yourself the ability to edit
143
+ your cluster's role bindings.
144
+
145
+ kubectl create clusterrolebinding my-admin-binding \
146
+ --clusterrole cluster-admin \
147
+ --user $(gcloud config get-value account)
148
+
149
+ 6 . Now give the necessary access to the cluster default service account. The
150
+ necessary role and binding objects are provided in a config file:
151
+
152
+ kubectl create -f deploy/cluster-roles.yml
132
153
133
154
### Deploy to the cluster
134
155
135
- A production deployment comprises two parts: a running phoenix container, and a
136
- front-end load balancer (which doesn't do much load balancing per se, but
137
- provides a public IP address.)
156
+ A production deployment comprises two parts: a cluster of running containers
157
+ managed by a Kubernetes _ deployment_ , and a front-end load balancer.
138
158
139
- We'll assume that you built the image to ` gcr.io/${PROJECT_ID}/tanx:v1 ` and
140
- you've created the Kubernetes cluster as described above.
159
+ We'll assume that you've built the image and created the Kubernetes cluster as
160
+ described above.
141
161
142
- Next we'll create a deployment
162
+ First we'll create the deployment and load balancer:
143
163
144
- kubectl run tanx-1 --image=gcr.io/${PROJECT_ID}/ tanx:v1 --port 8080
164
+ kubectl create -f deploy/ tanx-deployment.yml
145
165
146
- This will run your image on a Kubernetes pod. You may view the running pods
147
- using:
166
+ This will create the Kubernetes resources and start up containers in pods. The
167
+ initial deployment, however, just runs nginx in a container (rather than your
168
+ app). So next you'll need to set the image:
148
169
149
- kubectl get pods
170
+ kubectl set image deployment/tanx tanx=gcr.io/${PROJECT_ID}/tanx:latest
150
171
151
- Now, we need to "expose" the application using a load balancer.
172
+ You may view the running pods using:
152
173
153
- kubectl expose deployment tanx-1 --type=LoadBalancer --port 80 --target-port 8080
174
+ kubectl get pods
154
175
155
- This creates a "service" resource pointing at your running tanx pod. After
156
- creating the service, run
176
+ And the load balancer using:
157
177
158
178
kubectl get service
159
179
160
- to view the service. Initially, the "external IP" will be pending while
161
- container engine works to procure an IP address for you. If you rerun the
162
- ` kubectl get service ` command, eventually the IP address will appear. You can
163
- then point your browser at that URL to view the running application.
180
+ Initially, the "external IP" for the service will be pending while Kubernetes
181
+ Engine works to procure an IP address for you. If you rerun the ` get service `
182
+ command, eventually the IP address will appear. You can then point your browser
183
+ at that URL to view the running application.
164
184
165
185
### Updating the app
166
186
167
187
To update the tanx app to reflect changes you have made, rebuild with a new
168
188
version tag. For example, if your original build image was tagged
169
189
` gcr.io/${PROJECT_ID}/tanx:v1 ` , you might do a new build as:
170
190
171
- gcloud container builds submit --tag=gcr.io/${PROJECT_ID}/tanx:v2 .
191
+ gcloud builds submit --config deploy/cloudbuild.yml \
192
+ --substitutions _BUILD_ID=v2 .
172
193
173
194
Now the new image ` gcr.io/${PROJECT_ID}/tanx:v2 ` will be available in your
174
195
project's container registry. You may deploy it by setting the pod's image
175
196
to the new image:
176
197
177
- kubectl set image deployment/tanx-1 tanx-1 =gcr.io/${PROJECT_ID}/tanx:v2
198
+ kubectl set image deployment/tanx tanx=gcr.io/${PROJECT_ID}/tanx:v2
178
199
179
- This generally performs a "rolling" update for zero downtime deploys. However,
180
- since our service has only one instance, it simply stops and starts that single
181
- instance.
200
+ This performs a "rolling" update for zero downtime deploys.
182
201
183
202
### Cleanup and tearing down a deployment
184
203
@@ -187,7 +206,7 @@ following.
187
206
188
207
1 . Delete the service
189
208
190
- kubectl delete service tanx-1
209
+ kubectl delete service tanx
191
210
192
211
2 . Wait for the load balancer to go away. This may take a few minutes. You
193
212
may watch the output of the following command to see when this is complete.
0 commit comments