Deploying Go App with GCP, CircleCI, and Spinnaker

Introducing a deployment of the Go app using Google Cloud Platform, CircleCI, and Spinnaker.

Deploying Go App with GCP, CircleCI, and Spinnaker

Prerequisites

Example repo

Deployment Overview

Set up Go app

go run main.go
Hello, Go App!
FROM golang:1.13.3-alpineENV GO111MODULE=onWORKDIR /go/src/golang-deploy-gcp
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["golang-deploy-gcp"]
docker build -t golang-deploy-gcp ./
docker run -it --rm --name golang-deploy-gcp -p 8081:8080 golang-deploy-gcp
http://localhost:8081

Set up Cloud SDK

curl https://sdk.cloud.google.com | zsh
gcloud init
$ gcloud config list[core]
account = xxx@gmail.com
disable_usage_reporting = True
Your active configuration is: [default]
$ gcloud auth list

Credentialed Accounts
ACTIVE ACCOUNT
* xxx@gmail.com
To set the active account, run:
$ gcloud config set account `ACCOUNT`

Create a project

gcloud projects create golang-deploy-gcp

Enable a billing account

$ gcloud beta billing accounts listACCOUNT_ID            NAME              OPEN  MASTER_ACCOUNT_ID
xxxxx-xxxxx-xxxxx xxxx True
gcloud beta billing projects link golang-deploy-gcp --billing-account {ACCOUNT_ID}
billingAccountName: billingAccounts/xxxxx-xxxxx-xxxxx
billingEnabled: true
name: projects/golang-deploy-gcp/billingInfo
projectId: golang-deploy-gcp
$ gcloud compute regions listNAME                     CPUS  DISKS_GB  ADDRESSES  RESERVED_ADDRESSES  STATUS  TURNDOWN_DATE
asia-east1 0/8 0/2048 0/8 0/1 UP
asia-east2 0/8 0/2048 0/8 0/1 UP
...

Push the image to GCR with CircleCI

gcloud iam service-accounts create circleci --display-name circleci
gcloud iam service-accounts list
gcloud projects add-iam-policy-binding golang-deploy-gcp --member serviceAccount:circleci@golang-deploy-gcp.iam.gserviceaccount.com --role roles/storage.admin
$ gcloud projects get-iam-policy golang-deploy-gcp...- members:
- serviceAccount:circleci@golang-deploy-gcp.iam.gserviceaccount.com
role: roles/storage.admin
...
gcloud iam  service-accounts keys create --iam-account circleci@golang-deploy-gcp.iam.gserviceaccount.com ./key.json
Environment Variables
Add an Environment Variables
GOOGLE_PROJECT_ID
GOOGLE_COMPUTE_ZONE
gcloud compute zones list
gcloud services enable containerregistry.googleapis.com
{hostname}/{project id}/{image name}:{tag}
CircleCI Result
gcloud container images list

Spinnaker

Install Halyard on GCE

gcloud iam service-accounts create halyard \
--project=golang-deploy-gcp \
--display-name halyard
gcloud projects add-iam-policy-binding golang-deploy-gcp \
--role roles/iam.serviceAccountKeyAdmin \
--member serviceAccount:halyard@golang-deploy-gcp.iam.gserviceaccount.com
gcloud projects add-iam-policy-binding golang-deploy-gcp \
--role roles/container.admin \
--member serviceAccount:halyard@golang-deploy-gcp.iam.gserviceaccount.com
gcloud compute instances create halyard \--project=golang-deploy-gcp \--zone=asia-east1-b \--scopes=cloud-platform \--service-account=halyard@golang-deploy-gcp.iam.gserviceaccount.com \--image-project=ubuntu-os-cloud \--image-family=ubuntu-1404-lts \--machine-type=n1-standard-1
gcloud compute ssh halyard
// at GCE instance$ curl -O https://raw.githubusercontent.com/spinnaker/halyard/master/install/debian/InstallHalyard.sh$ sudo bash InstallHalyard.sh$ hal -v
$ . ~/.bashrc
// at GCE instanceLATEST=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)curl -LO https://storage.googleapis.com/kubernetes-release/release/$LATEST/bin/linux/amd64/kubectlchmod +x kubectlsudo mv kubectl /usr/local/bin/kubectl

Create a Kubernetes cluster

// at local machinegcloud services enable container.googleapis.com
// at local machinegcloud container clusters create golang-deploy-gcp-cluster --machine-type=n1-standard-1 --num-nodes=1
// at local machinegcloud container clusters update golang-deploy-gcp-cluster --enable-legacy-authorization

Set up credentials

// at GCE instancegcloud container clusters get-credentials golang-deploy-gcp-cluster --zone=asia-east1-b
Google Cloud Resource Manager API
// at local marchinegcloud iam service-accounts create spinnaker \--project=golang-deploy-gcp \--display-name spinnaker
// at local marchinegcloud projects add-iam-policy-binding golang-deploy-gcp \--role roles/storage.admin \--member serviceAccount:spinnaker@golang-deploy-gcp.iam.gserviceaccount.com
// at local marchinegcloud projects add-iam-policy-binding golang-deploy-gcp \--member serviceAccount:spinnaker@golang-deploy-gcp.iam.gserviceaccount.com \--role roles/browser
// at GCE instancegcloud iam service-accounts keys create ./spinnaker.key.json \--iam-account spinnaker@golang-deploy-gcp.iam.gserviceaccount.com

Set Spinnaker configuration

// at GCE instance$ hal config provider docker-registry enable$ hal config provider docker-registry account add my-gcr-account \--address gcr.io \--password-file ~/spinnaker.key.json \--username _json_key
// at GCE instance$ hal config provider kubernetes enable$ hal config provider kubernetes account add my-k8s-account \--docker-registries my-gcr-account \--context $(kubectl config current-context)
// at GCE instance$ hal config storage gcs edit \--project $(gcloud info --format='value(config.project)') \--json-path ~/spinnaker.key.json$ hal config storage edit --type gcs

Deploy Spinnaker

// at GCE instance$ hal config deploy edit \--account-name my-k8s-account \--type distributed
// at GCE instance$ hal config version edit --version $(hal version latest -q)
// at local machinegcloud container clusters resize golang-deploy-gcp-cluster --num-nodes=4
// at GCE instancehal deploy apply
// at GCE instancehal deploy connect
gcloud compute ssh halyard -- -L 9000:localhost:9000 -L 8084:localhost:8084
Spinnaker

Deploy Go App with Spinnaker

Create an application

Create an application
New Application

Create a load balancer

Create Load Balancer
Create New Load Balancer
Load Balancers

Create a pipeline

Configure a new pipeline
Create New pipeline
Pipeline
Add Trigger
Configure Trigger
Add stage
Add stage form
Deploy stage
Add server group
Configure Deployment Cluster
After add

Deploy application

Pipeline progressing
Deploy succeed
LOAD BALANCERS

Conclusion

Web Developer, TypeScript, React, React Native, Vue.js, Go, Swift, and Ruby on Rails https://manatoworks.me/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store