hitjethva and Easha Abid
Okteto is a development platform used to streamline the development process for Kubernetes applications. It allows developers to build and test their applications directly in Kubernetes clusters without setting up complex local development environments. Okteto enables live updates to applications running in Kubernetes clusters, allowing developers to see their code changes reflected in real time without needing to rebuild or redeploy their applications.
In this tutorial, you will create a Python application and deploy it on Kubernetes using Okteto.
Before you begin this tutorial, you’ll need the following:
kubectl
installed and configured to communicate with your cluster.A
record with the name *
and Load Balancer IP.First, you need to ensure that Python is installed on your Ubuntu system. You can check this by opening a terminal and typing:
If Python is installed, this command will display the version of Python. If not, you can install it using the following command:
Next, create a directory to store your application code and other configurations.
Next, navigate to your application directory and create a virtual environment to isolate your project dependencies.
Next, activate the virtual environment using the following command.
Next, create a new Python file for your application. For example, create a simple app.py
file:
Add the following code:
Next, install the Flask web framework.
Now, run your Python application using the following command.
You will see the following output.
Now that the Flask application is running locally, and you can verify it using curl
:
You should receive the following response from the Flask application.
When you’re done working on your project, you can deactivate the virtual environment by running:
At this point, you have created a Python application and tested it locally.
Dockerizing a Python application involves creating a Docker image that contains the Python environment and dependencies required to run the application.
First, create a file named Dockerfile
in the root directory of your Python application.
Add the following content:
In this Dockerfile:
FROM python:3.8-slim
: Specifies the base image to use. You’re using an official Python image with Python 3.8 installed.WORKDIR /app
: Sets the working directory within the container to /app
.ADD . /app
: Copies the current directory’s contents into the container’s /app
directory.RUN pip install flask
: Installs Flask framework.EXPOSE 5000
: Exposes port 5000
to allow incoming connections.CMD ["python3", "app.py"]
: Specifies the command to run when the container starts. In this case, it runs app.py
.Now, run the following command to build the image based on the instructions in your Dockerfile.
After building the Docker image, you can run your application in a container using the built image:
This command runs a container from the my-app
image and maps the container port to a host port 5000
.
You can verify your running app container using the docker ps
command.
The output will be:
Open a web browser or use curl to access the application using the URL http://your-server-ip:5000. You should see the message Hello, This is a simple Python App!
indicating that your Python application is running inside the Docker container.
Till now, you have successfully dockerized your Python application.
First, you need to have a DockerHub account. If you don’t have one, you can create it on the DockerHub website.
Use the docker login
command to log in to Docker Hub. You’ll be prompted to enter your Docker Hub username and password.
The output of this command will be:
Before pushing the image to Docker Hub, you also need to tag it with your Docker Hub username and the desired repository name.
You can now push your Docker image to Docker Hub using the docker push
command.
After pushing the image, you can verify that it’s available on Docker Hub by searching for it using the Docker Hub CLI.
Your Python app Docker image is now available on Docker Hub and can be pulled by others or deployed to various environments.
Now, you will need to create a Kubernetes manifests file using Okteto to define the deployment, service, and ingress resources for an application called my-app
.
Add the following configuration to the file.
The above file deploys an application named my-app
using Okteto, exposes it internally via a ClusterIP service on port 5000
, and sets up an Ingress resource to route HTTP traffic to the application. Okteto-specific annotations are used to enable certain features provided by Okteto, such as automatic hostname generation.
First, you need to add the Okteto Helm repository to your Helm client.
Once you’ve added the Okteto Helm repository, you should update your Helm repositories to ensure you have the latest information about available charts:
Next, create a config.yaml
for Okteto.
Add your Okteto license key, subdomain, and other configuration as shown below:
Note:
Replace the license key with your valid Okteto license key and okteto.example.com
with your domain name.
Now, install the latest version of Okteto using the config.yaml
configuration file.
Once the Okteto is installed, you will get the following output.
Note:
Remember the Personal Access Tokens 88f8cc11
from the above output. You will need it to authenticate Okteto.
Wait for some time then fetch the IP address that DigitalOcean has dynamically allocated to the NGINX Ingress you’ve just installed and configured as a part of Okteto:
You will see the following output.
You need to take the EXTERNAL-IP
address and add it to your DNS for the domain you have chosen to use. This is done by creating an A
record with the name *
.
Now, open your web browser and access your Okteto instance at https://okteto.okteto.example.com/login#token=88f8cc11.
The Okteto CLI is an open-source command line tool that allows you to develop your applications directly on Kubernetes.
You can install the Okteto CLI on Linux and macOS using curl
. Open your terminal and run the following command:
Once the installation is complete, you can verify that the Okteto CLI is installed correctly by running the following command:
This command will display the version of the Okteto CLI installed on your system.
Next, you will need to use your Personal Access Tokens to authenticate with the Okteto CLI.
Its output will be:
Next, run the following command to verify that your Okteto CLI is configured.
This gives the following output:
To start developing the Python application, you will need to create an Okteto manifest file and define the configuration for your development environment.
Let’s create an okteto.yaml
file for a simple Python application.
Add the following configuration.
In the above file,
deploy
: This section defines the deployment configuration. When you run okteto up
or okteto deploy
, Okteto will execute the kubectl apply -f k8s.yaml
command to deploy the Kubernetes resources defined in the k8s.yaml
file. This allows you to specify your deployment configuration separately from your development environment configuration.command: bash
: Specifies the command to run when the development environment starts.environment
: Specifies environment variables to set in the development environment. In this case, it sets FLASK_ENV
to development.sync
: Specifies folders to synchronize between your local machine and the development environment. It syncs the current directory (.)
with /app
inside the development environment.reverse
: Specifies port forwarding rules to expose ports from the development environment to your local machine. In this case, it forwards port 9000
from the development environment to port 9000
on your local machine.volumes
: Specifies additional volumes to mount into the development environment. In this case, it mounts the /root/.cache/pip
directory, which might be used to cache pip packages, into the development environment.Now, deploy your Python application to the Kubernetes cluster using the following command.
After successful deployment, you will see the following output.
Next, go back to your web browser and refresh the Okteto dashboard. You will see your deployed application:
You can also access your Python application using the URL https://my-app-okteto-admin.okteto.example.com.
In this section, you will use the okteto up
command to deploy a Python application directly on Kubernetes. This command will synchronize your local code with the development environment. You can modify the code using your preferred IDE or text editor on your local machine, and the changes will be automatically synchronized with the development environment in Kubernetes.
Let’s start the development environment using Okteto:
This command will create a development environment based on the configurations specified in the okteto.yaml
file.
Next, run your Python application:
You will see the following output.
Now, edit your app.py
application file:
Modify the following line:
Save and close the file. Okteto will automatically detect the code changes and apply them instantly to Kubernetes.
Go back to your web browser and reload the page for your Python application. You will see your modified application on the following screen.
In this tutorial, you have created a basic Python application and deployed it on the Kubernetes cluster using Okteto. The Okteto’s ability to synchronize local code changes with the development environment in real-time allows for rapid development iterations and immediate feedback on code changes. Overall, Okteto empowers developers to focus on building high-quality Python applications without worrying about the complexities of Kubernetes infrastructure.
The author selected FreeBSD Foundation to receive a donation as part of the Write for DOnations program.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!