In Part 1 of this tutorial, we deployed a simple Flask app using docker. In this part, we'll deploy that same app using Kubernetes.

Create Kubernetes configuration

For the Kubernetes deployment, we'll need to create a deployment for managing pods/containers.

We create the deployment using a YAML file.

trains-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: trains
spec:
  selector:
    matchLabels:
      app: trains
  replicas: 2
  template:
    metadata:
      labels:
        app: trains
    spec:
      containers:
      - name: trains
        image: slemasne/trains:latest
        resources:
          limits:
            memory: "100Mi"
          requests:
            memory: "50Mi"
        ports:
        - containerPort: 5000

Check that the pods are running

We can see that two pods are running using the following command:

$ kubectl get pods

NAME                      READY   STATUS    RESTARTS   AGE
trains-758d4f498c-kbs9k   1/1     Running   0          6m19s
trains-758d4f498c-kfbpq   1/1     Running   1          6m19s

Run port forward

Next we run a port-forward command so we can connect to the cluster from our local machine.

kubectl port-forward deployment/trains 5000:5000

We check the pod is online by running a curl command.

$ curl -I http://127.0.0.1:5000/

This command returns a status code of 200:

HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 21
Server: Werkzeug/1.0.1 Python/3.7.9
Date: Wed, 14 Sep 2022 18:01:56 GMT