.NET, Kubernetes, and Minikube - Part 2, A Load Balanced Web API Deployment

Download full source code.

This is part 2 of a short series of posts on using .NET with Kubernetes and Minikube. In the previous post, I showed how to build and deploy a simple Web API application, running as a single instance.

In this post, I will show you how to deploy the same application, but this time there will be 3 instances of the application, with a load balancer routing traffic to each of the instances.

Steps 1 to 5

Go to the previous post and follow them.

6. Creating the deployment

Create a new file called webapiinminikube-deployment.yaml with the following content -

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapiinminikube-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapi-application
  template:
    metadata:
      labels:
        app: webapi-application
    spec:
      containers:
      - name: webapiinminikube
        image: webapiinminikube:1.0
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 60
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 10 
          periodSeconds: 10
          failureThreshold: 3

Run the following command to create the deployment -

kubectl apply -f webapiinminikube-deployment.yaml

This will create a deployment with 3 instances of the application.

You can check that the deployment is running using the following command -

kubectl get deployment

You should see something like this -

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
webapiinminikube-deployment   3/3     3            3           12s

7. Create the service to expose the deployment

Create a new file called webapiinminikube-service.yaml with the following content -

apiVersion: v1
kind: Service
metadata:
  name: web-service
  labels: 
    svc: web-service
spec:
  selector: 
    app: webapi-application
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080

Run the following to create the service -

kubectl apply -f webapiinminikube-service.yaml 

Check the status of the service by running -

kubectl get service -o wide

You will see output that looks like -

NAME          TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE    SELECTOR
kubernetes    ClusterIP      10.96.0.1        <none>        443/TCP          6d1h   <none>
web-service   LoadBalancer   10.106.237.216   <pending>     8080:31614/TCP   38s    app=webapi-application

Some things to note here for the web-service -

  • it is a LoadBalancer
  • the EXTERNAL-IP is pending
  • it is setup on port 8080 which is what we wanted
  • the service will look for the deployment that has a label of app=webapi-application

8. Trying it out (it’s not going to work!)

Open a terminal.

Run the following -

curl http://localhost:8080

As I mentioned it will not work -

curl: (7) Failed to connect to localhost port 8080 after 2217 ms: Couldn't connect to server

9. Start the Minikube tunnel

In a terminal run the following and leave the terminal open -

minikube tunnel

Now check the service again -

kubectl get service

Now you will see that the EXTERNAL-IP is no longer pending but has an IP address -

NAME          TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes    ClusterIP      10.96.0.1        <none>        443/TCP          6d1h
web-service   LoadBalancer   10.106.237.216   127.0.0.1     8080:31614/TCP   9m36s

10. Trying it out again

It’s important to use curl here, and not your web browser.

again…

It is important to use curl here, not your web browser.

Back in a terminal run the curl command a few times -

curl http://localhost:8080

You should see the output from the application, but it will vary as different pods are hit by the service.

Any request from your browser will include a Connection: keep-alive header and repeatedly hit the same pod. If you want to use Fiddler, Postman, etc, make sure the header includes Connection: close to see the load balancing occur.

11. Cleaning up

In the terminal with the tunnel running, press Ctrl+C to stop it. In the terminal with the dashboard running, press Ctrl+C to stop it.

Clean up the deployment and service by running the following commands -

kubectl delete service web-service
kubectl delete deployment webapiinminikube-deployment

Stop Minikube by running -

minikube stop

There you go, you have now deployed a load-balanced Web API application in Kubernetes using Minikube.

Download full source code.

comments powered by Disqus

Related