From Docker to Kubernetes: A Journey with Cyclops UI

mdabidhussain

Md Abid Hussain

Posted on August 1, 2024

From Docker to Kubernetes: A Journey with Cyclops UI

From "localhost" to cloud's vast expanse,
Your app's journey, a cosmic trance.
With Docker's might and K8s's art,
Cyclops UI gives it a flying start.


Table of content:


Intorduction

This guide offers a comprehensive overview of the process involved in transitioning a Python application from a local development environment to a production-ready Kubernetes cluster. By leveraging Docker for containerization and Cyclops UI for simplified cluster management


Prerequisites

What are we going to use

  • A simple rag project built using langchain, google-gemini and streamlit.
  • Docker: For containerizing the application and its dependencies.
  • minikube: To simulate a Kubernetes cluster for local development and testing.
  • kubectl: To interact with the Kubernetes cluster and manage deployed applications.
  • Cyclops UI: To simplify Kubernetes cluster management and application deployment.
  • Gemini Api key

Ensure you have Docker, Minikube, and Kubectl installed on your system before proceeding with this guide.

Project repo: Github


Pizza shop analogy of docker, kubernetes and cyclopsui

  • Docker: The Pizza Box 🍕

Think of Docker as a perfect pizza box. It neatly packages your delicious pizza (application) with all its flavorful toppings (dependencies) to ensure a consistent taste (performance) no matter where you enjoy it.

  • Kubernetes: The Pizza Delivery Fleet 🚚

Kubernetes is like a fleet of speedy delivery cars. It manages and coordinates the delivery of your tasty pizzas (containers) to hungry customers (servers). It can even automatically add more delivery cars when the orders pile up!

  • Cyclops UI: The Delivery Manager 🧑‍💻

Cyclops UI is the savvy delivery manager overseeing the entire pizza operation. It helps you manage your fleet of delivery cars (Kubernetes cluster), track pizza orders (container status), and make adjustments to keep customers happy.


Dockerization of the RAG App

This section describes the process of containerizing your RAG application using Docker.

Required Libraries:

The application utilizes the following libraries:

  • langchain
  • langchain_community
  • langchain-google-genai
  • streamlit
  • python-dotenv
  • pymupdf
  • faiss-cpu

Default Port:

By default, the Streamlit application runs on port 8501.

Dockerfile:

Here's the provided Dockerfile with some enhancements:

# Base image with required Python version
FROM python:3.12-slim-bookworm

# Working directory for the application
WORKDIR /app

# Copy requirements file
COPY requirements.txt requirements.txt

# Install dependencies based on requirements
RUN pip install -r requirements.txt

# Copy all application files
COPY . .

# Expose Streamlit port (optional, depending on deployment scenario)
EXPOSE 8501

# Run the Streamlit application
CMD ["streamlit", "run", "main.py"]
Enter fullscreen mode Exit fullscreen mode

Dockerfile Breakdown:

The provided Dockerfile defines the steps to create a containerized version of your application. Here's a breakdown of each line:

  1. Base Image: FROM python:3.12-slim-bookworm - This line specifies the base image for the container. We use python:3.12-slim-bookworm because it provides a lightweight Python 3.12 environment.

  2. Working Directory: WORKDIR /app - This sets the working directory within the container to /app. This directory will hold all your application files.

  3. Copy Requirements: COPY requirements.txt requirements.txt - This copies the requirements.txt file, which lists all the Python libraries your application needs, into the container's /app directory.

  4. Install Dependencies: RUN pip install -r requirements.txt - This line installs all the required libraries listed in requirements.txt using pip.

  5. Copy Application Files: COPY . . - This copies all files and directories from your current directory (where the Dockerfile resides) into the container's /app directory. This includes your Python script (main.py), models, and any other application files.

  6. Expose Port (Optional): EXPOSE 8501 (Optional) - This line exposes port 8501, which is the default port used by the Streamlit application. This allows you to access the application from outside the container if needed.

  7. Run Streamlit: CMD ["streamlit", "run", "main.py"] - This sets the default command for the container. It instructs the container to run streamlit run main.py when it starts, launching your Streamlit application.

Building the Docker Image:

To create the Docker image for your RAG application, run the following command in your terminal (replace your-rag with your desired image name):

docker build -t your-rag .
Enter fullscreen mode Exit fullscreen mode

Running the app

  • Locally:
streamlit run main.py
Enter fullscreen mode Exit fullscreen mode
  • Using Docker:
docker run -e GOOGLE_API_KEY=your_gemini_api_key -p 8501:8501 your-rag
Enter fullscreen mode Exit fullscreen mode

Now your app is running:

RAG app ui

Upload and chat with document.


Deploying to Kubernetes with Minikube and Cyclops UI

Installing Cyclops UI and running it locally
Make sure minikube and kubectl are installed and running. Installation guide for cyclops can be found at Cyclops Docs.

  • To install Cyclops in your cluster, run commands below:
kubectl apply -f https://raw.githubusercontent.com/cyclops-ui/cyclops/v0.8.2/install/cyclops-install.yaml && kubectl apply -f https://raw.githubusercontent.com/cyclops-ui/cyclops/v0.8.2/install/demo-templates.yaml
Enter fullscreen mode Exit fullscreen mode
  • To run cyclops, run command below:
kubectl port-forward svc/cyclops-ui 3000:3000 -n cyclops
Enter fullscreen mode Exit fullscreen mode

Cyclops UI is now running at http://localhost:3000

Cyclops UI running at localhost

Steps to deploy app
The docker image of app is already published at docker hub hub.docker.com/mdabidhussain/your-rag. You can also use this image or create one and publish to docker hub by following documentation. Docker Workshop. It will be great if you try to deploy your own dockerize application

  • Click on button Add module and new page will open Module section
  • In module select drop down select app-template Screen after selecting app-template
  • Add name for module
  • In the general options under Define Module section. Fill the name of image you want to use (mdabidhussain/your-rag in this case) and the tag of that image you want to use (latest in this case). Add environment variables if needed (in this case we need GOOGLE_API_KEY) Configuration for app
  • Under Scaling options add the necessary scaling configuration. For this app I have gone with default. Scaling config
  • Under Network option expose the port on which your app run (8501 in this case for streamlit app). Network configuration
  • Finally click on Save button. Module created Module status will updated to green if you have properly configure the app deployment.

Now all that is left is to expose your-rag application outside the cluster:

kubectl port-forward svc/your-rag 8501:8501
Enter fullscreen mode Exit fullscreen mode

which follow syntax like

kubectl port-forward svc/<module_name> <port_to_exposed>:<port_to_map>
Enter fullscreen mode Exit fullscreen mode

Now your application is running on localhost:8501
RAG app ui

Upload document and chat.

💖 💪 🙅 🚩
mdabidhussain
Md Abid Hussain

Posted on August 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related