Deploying Container Images to GKE using Kubectl
This article is a based on Kubernetes on GKE : Series Intro , which covers some basics of Google Kubernetes Engine (GKE) and how to setup a cluster.
Kubectl is a command line tool for controlling Kubernetes clusters, which allows you to run commands against Kubernetes clusters to deploy applications, inspect and manage cluster resources, or view logs. We’ll use the kubectl tool to deploy a container image to the Google Kubernetes Engine Cluster.
GKE uses Kubernetes objects to create and manage your cluster’s resources. Kubernetes provides the Deployment object for deploying stateless applications like web servers. Service objects define rules and load balancing for accessing your application from the internet.
To install Kubectl, follow steps described here.
After creating your cluster, you need to get authentication credentials to interact with the cluster, you can do with a command line that has both kubectl and gcloud installed or simply use the Google Cloud Shell.
- Login to Gcloud using:
gcloud auth login
This would open a browser window for you to login. You could also set a default project if you haven’t.
- To configure kubectl to manage your GKE cluster, execute:
gcloud container clusters get-credentials my-cluster
Where my-cluster is the name of the cluster you created earlier.
Creating a deployment
In this step, you would create deployments using the kubectl commands and not by writing YAML configurations.
To create a new deployment on your GKE cluster, run the following command:
kubectl create deployment hello-server — image=gcr.io/google-samples/hello-app:1.0
The command creates a Deployment named hello-server on your configured cluster. The Deployment’s Pod runs the hello-app container image. Inspect the running Pods by running:
kubectl get pods
Exposing the Deployment
You need to expose it to the internet so that users can access it. You can expose your application by creating a Service, which is a Kubernetes resource that exposes your application to external traffic.
To create a new service on your GKE cluster, run the following command:
kubectl expose deployment hello-server — type=LoadBalancer — port 8080
hello-server Service by running:
kubectl get service hello-server
It might take some minutes for an external IP address to be generated. Run the above command again if the EXTERNAL-IP column is in “pending” status.
To learn more about the Kubectl, check out Overview of kubectl.