A simple project to quickstart GKE
-
Ensure you have GCP account.
-
Enable the Cloud Build and Google Kubernetes Engine APIs.
-
Google Cloud SDK (
gcloud
command) is installed and initialized. You can install and initialize it by following the official installation guide. -
kubectl
command is installed. You can install it by usinggcloud
command:gcloud components install kubectl
This app is containerized with Dockerfile
.
Build your container image running the following command:
gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/helloworld-gke .
Your image is build on Google Cloud by using Cloud Build, and stored in Container Registry.
A GKE cluster is a managed set of Compute Engine virtual machines that operate as a single GKE cluster.
You can create the cluster with the following command:
# Replace `COMPUTE_ZONE` with the Google Cloud zone where you want to host your cluster, such as `us-west1-a`
gcloud container clusters create helloworld-gke \
--num-nodes 1 \
--zone COMPUTE_ZONE
You can verify that you have access to the cluster with the following command. The command lists the nodes in your container cluster which are up and running and indicates that you have access to the cluster.
kubectl get nodes
To deploy your app to the GKE cluster you created, you need two Kubernetes objects.
- A Deployment to define your app.
- A Service to define how to access your app.
Run the following command. Before running the command, you need to replace $GCLOUD_PROJECT in the deployment.yml
file with your project ID.
kubectl apply -f deployment.yaml
- Create the Hello World Service:
kubectl apply -f service.yaml
- Get the external IP address of the Service:
kubectl get services
It can take up to 60 seconds to allocate the IP address. The external IP address is listed under the column EXTERNAL-IP for the hello Service.
You have now deployed all the resources needed to run the Hello World app on GKE.
Use the external IP address from the previous step to load the app in your web browser, and see your running app:
http://EXTERNAL_IP
Or, you can make a curl call to the external IP address of the Service:
curl EXTERNAL_IP
The output displays the following:
Hello World!
Congrats! 🎉 Your app is running on GKE!