Deploying Docker Images To Google Cloud Using Kubernetes Engine
This post assumes that you already have a working docker image, that you can build and run on your local machine, and obviously a Google Cloud account with all of the necessary APIs activated, and the Google Cloud SDK installed on your machine. If all of that is in order, you are good to go!
The first thing I do is ensure that I have an up-to-date GitHub repo on my code, which I usually keep in the following format:
-- <APP CODE>
I set up a new repository in Github, and then make sure the repository is up to date using
git add . and
git commit -m "Some Message" before doing a final
git push origin master
Now, let’s get on and build our Docker image and push it up to Google Cloud’s container registry. Start by issuing:
docker build -t myimage .
Feel free to test in on your local machine with:
docker run -d --name mycontainer -p 80:80 myimage
And kill / remove the process once you have finished using a combination of
docker kill pid, and
docker rm pid.
Before pushing it to Google Cloud, we must quickly tag our image with the necessary information that Google Cloud needs. Make sure you replace
<PROJECT NAME> with the correct project ID in the Google Cloud dashboard. Issue:
docker tag myimage gcr.io/<PROJECT NAME>/myimage:v1
We can now push it to Google Cloud’s Container Registry using the
gcloud command installed by the Google Cloud SDK. The registry is similar to Docker Cloud, but ties in nicely with Google’s console and the Kubernetes Engine. Push it with:
gcloud docker -- push gcr.io/<PROJECT NAME>/myimage:v1
Now if you head to your Google Cloud Console, and over to the Container Registry, you should see your new Docker image there. Good work!
We now need to get our image served in Kubernetes Engine. Kubernetes Engine is Google’s hosted version of Kubernetes, which enables you to create a cluster of “nodes” to serve your containers among. To get started, create a cluster by following the prompts in the Kubernetes Engine dashboard. Then head to “workloads” and hit the “Deploy” button.
Make sure you click the “Select existing Google Container Registry image” button, and then choose your repository and tag (which will initially be v1). Click ‘Done’, give it a useful name, and then hit ‘Deploy’.
In the panel on the right, you will see the option to ‘Expose’ your image to the internet using a load balancer. Go ahead and do this. You will then be provided with an internet-facing endpoint which you can test your deployment on. Congratulations, you have just deployed a docker image to Google Cloud using Kubernetes Engine!