Test your Python deployment
Prerequisites
- Complete all the previous sections of this guide, starting with Use containers for Python development.
- Turn on Kubernetes in Docker Desktop.
Overview
Kubernetes is an open source platform that runs and orchestrates container workloads across one or more machines. You describe what you want to run, like which container images, how many replicas, and which network ports to expose, in YAML manifest files. Kubernetes reads the manifests and makes the cluster match that description.
In this section, you'll use the Kubernetes environment built into Docker
Desktop to deploy your application locally. You'll write two manifest files,
one for the PostgreSQL database and one for the FastAPI application, apply
them with kubectl, and verify the deployment by hitting your application
from a terminal.
Registry authentication
The Docker Hardened Images used in this guide are hosted on dhi.io. Docker
Desktop's Kubernetes shares credentials with Docker Desktop, so the docker login dhi.io
you completed earlier is all that's needed. No additional image pull secret is required.
NoteIf you're deploying to a Kubernetes cluster outside of Docker Desktop, you'll need to create an image pull secret and reference it in your pod specs. See Use a Docker Hardened Image for instructions.
Create a Kubernetes YAML file
Create the following two Kubernetes manifest files in your
python-docker-example directory. Before applying
docker-python-kubernetes.yaml, replace DOCKER_USERNAME/REPO_NAME with your
Docker username and the repository name that you created in Configure CI/CD for
your Python application.
In these Kubernetes YAML files, there are various objects, separated by the ---:
- A Deployment, describing a scalable group of identical pods. In this case,
you'll get just one replica, or copy of your pod. That pod, which is
described under
template, has just one container in it. The container is created from the image built by GitHub Actions in Configure CI/CD for your Python application. - A Service, which will define how the ports are mapped in the containers.
- A PersistentVolumeClaim, to define a storage that will be persistent through restarts for the database.
- A Secret, which stores the database password as a Kubernetes Secret resource.
- A NodePort service, which will route traffic from port 30001 on your host to port 8000 inside the pods it routes to, so you can reach your app from the network.
To learn more about Kubernetes objects, see the Kubernetes documentation.
NoteThe
NodePortservice is good for development and testing. For production, implement an ingress controller instead.
Deploy and check your application
In a terminal, navigate to
python-docker-exampleand deploy your database to Kubernetes.$ kubectl apply -f docker-postgres-kubernetes.yamlYou should see output that looks like the following, indicating your Kubernetes objects were created successfully.
deployment.apps/postgres created service/postgres created persistentvolumeclaim/postgres-pvc created secret/postgres-secret createdNow, deploy your Python application.
$ kubectl apply -f docker-python-kubernetes.yamlYou should see output that looks like the following, indicating your Kubernetes objects were created successfully.
deployment.apps/docker-python-demo created service/service-entrypoint createdMake sure everything worked by listing your deployments.
$ kubectl get deploymentsYour deployment should be listed as follows:
NAME READY UP-TO-DATE AVAILABLE AGE docker-python-demo 1/1 1 1 48s postgres 1/1 1 1 2m39sThis indicates all one of the pods you asked for in your YAML are up and running. Do the same check for your services.
$ kubectl get servicesYou should get output like the following.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 13h postgres ClusterIP 10.43.209.25 <none> 5432/TCP 3m10s service-entrypoint NodePort 10.43.67.120 <none> 8000:30001/TCP 79sIn addition to the default
kubernetesservice, you can see yourservice-entrypointservice, accepting traffic on port 30001/TCP and the internalClusterIPpostgreswith the port5432open to accept connections from your Python app.In a terminal, curl the root endpoint to verify the application is running.
$ curl http://localhost:30001/ Hello, Docker!Exercise the database by creating a hero with a POST request:
$ curl -X 'POST' \ 'http://localhost:30001/heroes/' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "id": 1, "name": "my hero", "secret_name": "austing", "age": 12 }'You should receive the following response:
{ "age": 12, "id": 1, "name": "my hero", "secret_name": "austing" }Then read it back with a GET request:
$ curl http://localhost:30001/heroes/You should receive an array containing the hero you just created. This confirms the application can read from and write to the PostgreSQL database running in the cluster.
Run the following commands to tear down your application.
$ kubectl delete -f docker-python-kubernetes.yaml $ kubectl delete -f docker-postgres-kubernetes.yaml
Summary
In this section, you learned how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine.
Related information: