Test your Python deployment



In this section, you'll learn how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine. This allows you to test and debug your workloads on Kubernetes locally before deploying.

Create a Kubernetes YAML file

In your python-docker-dev directory, create a file named docker-python-kubernetes.yaml. Open the file in an IDE or text editor and add the following contents. Replace DOCKER_USERNAME/REPO_NAME with your Docker username and the name of the repository that you created in Configure CI/CD for your Python application.

apiVersion: apps/v1
kind: Deployment
  name: docker-python-demo
  namespace: default
  replicas: 1
      service: flask
        service: flask
       - name: flask-service
         imagePullPolicy: Always
          - name: POSTGRES_PASSWORD
            value: mysecretpassword
apiVersion: v1
kind: Service
  name: service-entrypoint
  namespace: default
  type: NodePort
    service: flask
  - port: 8001
    targetPort: 8001
    nodePort: 30001

In this Kubernetes YAML file, there are two 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 NodePort service, which will route traffic from port 30001 on your host to port 8001 inside the pods it routes to, allowing you to reach your app from the network.

To learn more about Kubernetes objects, see the Kubernetes documentation.

Deploy and check your application

  1. In a terminal, navigate to python-docker-dev and deploy your application to Kubernetes.

    $ kubectl apply -f docker-python-kubernetes.yaml

    You should see output that looks like the following, indicating your Kubernetes objects were created successfully.

    deployment.apps/docker-python-demo created
    service/service-entrypoint created
  2. Make sure everything worked by listing your deployments.

    $ kubectl get deployments

    Your deployment should be listed as follows:

    NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
    docker-python-demo   1/1     1            1           15s

    This 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 services

    You should get output like the following.

    NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
    kubernetes           ClusterIP       <none>        443/TCP          23h
    service-entrypoint   NodePort   <none>        8001:30001/TCP   75s

    In addition to the default kubernetes service, you can see your service-entrypoint service, accepting traffic on port 30001/TCP.

  3. In a terminal, curl the service. Note that a database was not deployed in this example.

    $ curl http://localhost:30001/
    Hello, Docker!!!
  4. Run the following command to tear down your application.

    $ kubectl delete -f docker-python-kubernetes.yaml


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: