DEV Community

Cover image for Deploying an ASP.NET Core App on Google Kubernetes Engine
Sahan
Sahan

Posted on • Originally published at sahansera.dev on

Deploying an ASP.NET Core App on Google Kubernetes Engine

I recently started playing around with the Google Cloud Platform. Since ASP.NET Core now runs on virtually any major OS, I wanted to give it a go with GCP’s Google Kubernetes Engine offering to see whether we can use it to host a .NET Core application.

Note: This tutorial is focused on using the gcloud CLI rather than Cloud Tools for Visual Studio to provide more flexibility and to understand what happens under the hood.

So here are the steps we are going to follow:

  1. Create/Clone a sample ASP.NET Core app
  2. Build the docker image
  3. Push the containerized app to Google Container Registry (GCR)
  4. Deploy the app to Google Kubernetes Engine (GKE)

First things first, what are the prerequisites?

1. Create/Clone a sample ASP.NET Core app

You can grab the ASP.NET Sample app from my Github Repo:

ASP.NET Core Food App

This app has nothing to do with food though 😁 Let’s just say I was hungry when creating it.

You can clone the above repo into your local machine and navigate to the FoodApp folder and publish the app to app/ folder from a command line:

cd FoodAppCore/FoodApp
dotnet restore
dotnet publish -o app
Enter fullscreen mode Exit fullscreen mode

2. Build the docker image

Don’t forget to replace PROJ_ID with your GCP project’s ID

Let’s build the docker image:

docker build -t gcr.io/PROJ_ID/food-app .
Enter fullscreen mode Exit fullscreen mode

If you want to test out your docker build locally, try it by doing so:

docker run --rm -p 8080:8080 gcr.io/PROJ_ID/food-app:latest
Enter fullscreen mode Exit fullscreen mode

3. Push the containerized app to Google Container Registry (GCR)

Let’s push our image to GCR:

docker push gcr.io/PROJ_ID/food-app
Enter fullscreen mode Exit fullscreen mode

If you haven’t configured gcloud CLI to authenticate to GCR you need to run the following command (you need to this only once):

gcloud auth configure-docker
Enter fullscreen mode Exit fullscreen mode

4. Deploy the app to Google Kubernetes Engine (GKE)

Next, we need to create a cluster in GKE to deploy our app to:

gcloud container clusters create foodapp-cluster --num-nodes=3
Enter fullscreen mode Exit fullscreen mode

Finally, let’s deploy it to GKE:

kubectl create deployment food-app --image=gcr.io/PROJ_ID/food-app:latest
Enter fullscreen mode Exit fullscreen mode

If the above command gives you an error, don’t forget to install kubectl for gcloud :

gcloud components install kubectl
Enter fullscreen mode Exit fullscreen mode

Wait, now the app is deployed, how can our clients access it? This final command will expose our app through a load balancer binding port 8080 to 80

kubectl expose deployment food-app --type="LoadBalancer" --port=80 --target-port=8080
Enter fullscreen mode Exit fullscreen mode

Well, that’s it! you have your app running in GKE! If you are wondering how to access it, run a kubectl get service and grab its external IP and paste it in a browser.

GKE Cluster

References

  1. https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app
  2. https://cloud.google.com/kubernetes-engine/docs/troubleshooting

Top comments (0)