Skaffold to GKE Deployment

abhishek
3 min readJul 11, 2021

Recently I heard the term Skaffold and I started digging out what this Skaffold actually means. And came to know, it's a command-line utility to continuously build and deploy your application to the KuberNative cluster, no worry whether it's a local or remote cluster. It is as simple as building an image from Dockerfile. 😉

Skaffold to GCR to GKE

Let’s intro keep short and starts the real requirements & implementation.

Requirements for this short journey

  1. Your app, I choose HelloWorld node app
  2. Local environment setup includes: Docker, Skaffold, gcloud configured
  3. GCP APIs enabled for Container Registry, Kubernetes Engine, & also you’ve got the right privilege to deploy the changes.

[ You can use this link for the HelloWorld node app ]

Steps For the deployment

This Skaffold is going to change your deployment method, you just need to care about your real application development and Skaffold will rest manage all.

At this point I assume, you’ve your environment properly setup. Docker installed, gcloud setup, you’ve created a GKE cluster and it’s now in your context.

view cluster

Since under this helloworld app, we don’t have any Kubernetes manifest files [deployment, service…], we will create them first. 😟 Need to type deployment yaml configurations??

No worry, here we can use kubectl to generate a deployment file for us.

kubectl create deployment helloworld --image=gcr.io/MYPROJECTID/helloworld --dry-run=client --output=yaml

Soon you’ve your deployment file in your terminal, copy and paste in the file and modify accordingly like changing tags. & here is my ‘deployment.yaml’ ‘service.yaml’ looks like this ;

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: helloworld
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: gcr.io/MYPROJECTID/helloworld:latest
name: helloworld
resources: {}
status: {}

Replace project id, with yours.

apiVersion: v1
kind: Service
metadata:
name: helloworld-svc
spec:
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer

Now the last step is the Skaffold configuration. Make sure, you’re in your app directory and then run “skaffold init”. Skaffold scans the folder and comes with a YAML configuration where all minimal configuration already loaded, like Dockerfile location, Kubernetes manifests, image name.
At this point, this configuration is enough to deploy our app to the GKE cluster.

apiVersion: skaffold/v2beta17
kind: Config
metadata:
name: cicd
build:
artifacts:
- image: gcr.io/MYPROJECTID/helloworld
docker:
dockerfile: Dockerfile
deploy:
kubectl:
manifests:
- k8-manifests/deployments.yaml
- k8-manifests/service.yaml

At this point, we can also set our GCR location as skaffold default-repo by using this command

skaffold config set default-repo gcr.io/MYPROJECTID/helloworld

That’s it, we’re done with our configuration. And let’s start deploying.

Skaffold has some commands which are needed to know

For more: https://skaffold.dev/docs/references/cli/

We’ll be using the “skaffold run” to deploy once, for continuous build and deploy we can use “skaffold dev”.

After running “skaffold run”, it will start all processes like creating docker image, tagging, pushing to GCR, and deploying to GKE by using kubectl.

Go to container registry and verify the image there, and then GKE, there will workload will helloworld, access it using endpoints IP in the service.

That’s it. HelloWorld is on your screen now 🔆. Now try with “skaffold dev” and change some code in your app. And see automated deployment.

Source: https://skaffold.dev/

--

--

abhishek

Finished college, and then started in DevOps. Not a Omniscient 😉