Deploy a "hello-world" docker image to Google Compute Engine (gce).
Other Services
- PaaS
- CaaS
- IaaS
Table of Contents
- OVERVIEW
- PREREQUISITES
- SOFTWARE STACK
- RUN
- STEP 1 - TEST
- STEP 2 - BUILD (DOCKER IMAGE VIA DOCKERFILE)
- STEP 3 - PUSH (TO DOCKERHUB)
- STEP 4 - DEPLOY (TO GCE)
- CHECK SERVICES ARE RUNNING
- CONTINUOUS INTEGRATION & DEPLOYMENT
Documentation and Reference
- jeffs-gce-image-ubuntu-2204 is where I build the gce image
- The hello-go-deploy-gce docker image on dockerhub
- My gce cheat sheet for more detailed information and illustrations
- This repos github webpage built with concourse
Every 2 seconds this App will print,
INFO[0000] Let's Start this!
Hello everyone, count is: 1
Hello everyone, count is: 2
Hello everyone, count is: 3
etc...
You will need the following go packages,
go get -u -v github.com/sirupsen/logrus
go get -u -v github.com/cweill/gotests/...
This repo contains the packer gce image build scripts,
git clone [email protected]:JeffDeCola/my-packer-image-builds.git
- DEVELOPMENT
- OPERATIONS
- concourse/fly (optional)
- docker
- packer
- SERVICES
To run.sh,
cd hello-go-deploy-gce-code
go run main.go
To create-binary.sh,
cd hello-go-deploy-gce-code/bin
go build -o hello-go ../main.go
./hello-go
This binary will not be used during a docker build since it creates it's own.
To create unit _test
files,
cd hello-go-deploy-gce-code
gotests -w -all main.go
To run unit-tests.sh,
go test -cover ./... | tee test/test_coverage.txt
cat test/test_coverage.txt
This docker image is built in two stages.
In stage 1, rather than copy a binary into a docker image (because
that can cause issues), the Dockerfile will build the binary in the
docker image.
In stage 2, the Dockerfile will copy this binary
and place it into a smaller docker image based
on alpine
, which is around 13MB.
To build.sh with a Dockerfile,
cd hello-go-deploy-gce-code/build
docker build -f Dockerfile -t jeffdecola/hello-go-deploy-gce .
You can check and test this docker image,
docker images jeffdecola/hello-go-deploy-gce
docker run --name hello-go-deploy-gce -dit jeffdecola/hello-go-deploy-gce
docker exec -i -t hello-go-deploy-gce /bin/bash
docker logs hello-go-deploy-gce
docker rm -f hello-go-deploy-gce
You must be logged in to DockerHub,
docker login
To push.sh,
docker push jeffdecola/hello-go-deploy-gce
Check the hello-go-deploy-gce docker image at DockerHub.
There are three steps to deploy on gce,
- STEP 4.1 - Build a gce image
- STEP 4.2 - Create an instance template (HW resources)
- STEP 4.3 - Create an instance group (Launch VM in region)
For this example, I will add two running services,
- The dockerhub image runs at boot hello-go-deploy-gce
- A binary /home/jeff/hello-go executable runs at boot
To keep things simple, the files are located in my my-packer-image-builds repo.
You will need to set the following environment variables (I added mine in ~/.bashrc),
export GCP_JEFFS_SERVICE_ACCOUNT_PATH=[path to your google platform .json file]
export GCP_JEFFS_PROJECT_ID=[your project id]
To validate your packer template file template.pkr.hcl,
cd my-packer-image-builds/google-compute-engine-images/jeffs-gce-image-ubuntu-2204
packer validate \
-var "image_name=hello-go-deploy-gce" \
-var "account_file=$GCP_JEFFS_SERVICE_ACCOUNT_PATH" \
-var "project_id=$GCP_JEFFS_PROJECT_ID" \
template.pkr.hcl
To build-image.sh on gce,
cd my-packer-image-builds/google-compute-engine-images/jeffs-gce-image-ubuntu-2204
packer build \
-var "image_name=hello-go-deploy-gce" \
-var "account_file=$GCP_JEFFS_SERVICE_ACCOUNT_PATH" \
-var "project_id=$GCP_JEFFS_PROJECT_ID" \
template.pkr.hcl
Check that the image was created at gce,
gcloud config set project $GCP_JEFFS_PROJECT_ID
gcloud compute images list --no-standard-images
The instance template contains the HW resources the instance group needs to create the VM instance.
cd my-packer-image-builds/google-compute-engine-images/jeffs-gce-image-ubuntu-2204
sh create-instance-template.sh "jeffs-hello-go-deploy-gce-image" "hello-go-deploy-gce"
Check the instance template was created,
gcloud compute instance-templates list
The instance group controls the show. It launches and scales your VM instances as needed.
cd my-packer-image-builds/google-compute-engine-images/jeffs-gce-image-ubuntu-2204
sh create-instance-group.sh "jeffs-hello-go-deploy-gce-instance-template" "hello-go-deploy-gce"
Check that the instance group and VM instance were created,
gcloud compute instance-groups list
gcloud compute instances list
To ssh into your gce VM, I placed my public keys in gce metadata ssh keys, which automatically places them in the authorized_keys files on my VM,
ssh -i ~/.ssh/google_compute_engine jeff@<IP>
Check the docker service is running,
docker ps
docker logs -f --tail 10 -f hello-go-deploy-gce
Check that your hello-go.service is running,
# Remember, it kicks off /home/jeff/hello-go
systemctl list-unit-files | grep hello.go
sudo systemctl status hello-go
journalctl -f
sudo systemctl stop hello-go
cat /lib/systemd/system/hello-go.service
sudo -s
Last, if you have multiple VMS, and since you put the
same ssh keys in /home/jeff/.ssh
when you built the image with packer,
your VMs can talk to each other using gce's internal DNS.
ssh <USERNAME>@<HOSTNAME>.us-west1-a.c.<PROJECT>.internal
Refer to ci-README.md on how I automated the above steps using concourse.