Deploying Python and Java Applications to Kubernetes With Korifi
Tutorial covering the installation and configuration of Cloud Foundry Korifi to push a Java and Python application to Kubernetes with a single command.
Join the DZone community and get the full member experience.Join For Free
Open-source Cloud Foundry Korifi is designed to provide developers with an efficient approach to delivering and managing cloud-native applications on Kubernetes with automated networking, security, availability, and more. With Korifi, the simplicity of the cf push command is now available on Kubernetes.
In this tutorial, I will walk you through the installation of Korifi on kind using a locally deployed container registry. The installation process happens in two steps:
- Installation of prerequisites
- Installation of Korifi and dependencies
Then, we will deploy two applications developed in two very different programming languages: Java and Python. This tutorial has been tested on Ubuntu Server 22.04.2 LTS.
Let's dive in!
There are several prerequisites needed to install Korifi. There is a high chance that Kubernetes users will already have most of them installed.
Here is the list of prerequisites:
- Cf8 cli
To save time, I wrote a Bash script that installs the correct version of prerequisites for you. You can download it and run it by running the two commands below.
git clone https://github.com/sylvainkalache/korifi-prerequisites-installation cd korifi-prerequisites-installation && ./install-korifi-prerequisites.sh
The Korifi development team maintains an installation script to install Korifi on a kind cluster. It installs the required dependencies and a local container registry. This method is especially recommended if you are trying Korifi for the first time.
git clone https://github.com/cloudfoundry/korifi cd korifi/scripts && ./deploy-on-kind.sh korifi-cluster
The install script does the following:
- Creates a kind cluster with the correct port mappings for Korifi
- Deploys a local Docker registry using the twuni helm chart
- Creates an admin user for Cloud Foundry
- Installs cert-manager to create and manage internal certificates within the cluster
- Installs kpack, which is used to build runnable applications from source code using Cloud Native Buildpacks
- Installs contour, which is the ingress controller for Korifi
- Installs the service binding runtime, which is an implementation of the service binding spec
- Installs the metrics server
- Installs Korifi
Similar to installing prerequisites, you can always do this manually by following the installation instructions.
Setting up Your Korifi Instance
Before deploying our application to Kubernetes, we must sign into our Cloud Foundry instance. This will set up a tenant, known as a target, to which our apps can be deployed.
Authenticate with the Cloud FoundryAPI:
cf api https://localhost --skip-ssl-validation cf auth cf-admin
cf create-org tutorial-org cf create-space -o tutorial-org tutorial-space
Target the Org and Space you created.
cf target -o tutorial-org -s tutorial-space
Everything is ready; let’s deploy two applications to Kubernetes.
Single-Command Deployment to Kubernetes
Deploying a Java Application
For the sake of the tutorial, I am using a sample Java app, but you feel free to try it out on your own.
git clone https://github.com/sylvainkalache/sample-web-apps cd sample-web-apps/java
Once you are inside your application repository, run the following command. Note that the first run of this command will take a while as it needs to install language dependencies from the requirements.txt and create a runnable container image. But all subsequent updates will be much faster:
cf push my-java-app
That’s it! The application has been deployed to Kubernetes. To check the application status, you can simply use the following command:
cf app my-java-app
Which will return an output similar to this:
Showing health and status for app
my-java-app in org tutorial-org / space tutorial-space as cf-admin...
Showing health and status for app my-java-app in org tutorial-org / space tutorial-space as cf-admin... name: my-java-app requested state: started routes: my-java-app.apps-127-0-0-1.nip.io last uploaded: Tue 25 Jul 19:14:34 UTC 2023 stack: io.buildpacks.stacks.jammy buildpacks: type: web sidecars: instances: 1/1 memory usage: 1024M state since cpu memory disk logging details #0 running 2023-07-25T20:46:32Z 0.1% 16.1M of 1G 0 of 1G 0/s of 0/s type: executable-jar sidecars: instances: 0/0 memory usage: 1024M There are no running instances of this process. type: task sidecars: instances: 0/0 memory usage: 1024M There are no running instances of this process.
Within this helpful information, we can see the app URL of our app and the fact that it is properly running.
You can double-check that the application is properly responding using curl:
curl -I --insecure https://my-java-app.apps-127-0-0-1.nip.io/
And you should get an HTTP 200 back.
HTTP/2 200 date: Tue, 25 Jul 2023 20:47:07 GMT x-envoy-upstream-service-time: 134 vary: Accept-Encoding server: envoy
Deploying a Python Application
Next, we will deploy a simple Python Flask application. While we could deploy a Java application directly, there is an additional step for a Python one. Indeed, we need to provide a Buildpack that Korifi can use for Python applications – a more detailed explanation is available in the documentation.
Korifi uses Buildpacks to transform your application source code into images that are eventually pushed to Kubernetes. The Paketo open-source project provides base production-ready Buildpacks for the most popular languages and frameworks. In this example, I will use the Python Paketo Buildpacks as the base Buildpacks.
Let’s start by adding the Buildpacks source to our ClusterStore by running the following command:
kubectl edit clusterstore cf-default-buildpacks -n tutorial-space
Then add the line
- image: gcr.io/paketo-buildpacks/python, your file should look like this:
spec: sources: - image: gcr.io/paketo-buildpacks/java - image: gcr.io/paketo-buildpacks/nodejs - image: gcr.io/paketo-buildpacks/ruby - image: gcr.io/paketo-buildpacks/procfile - image: gcr.io/paketo-buildpacks/go - image: gcr.io/paketo-buildpacks/python
Then we need to specify when to use these Buildbacks by editing our ClusterBuilder. Execute the following command:
kubectl edit clusterbuilder cf-kpack-cluster-builder -n tutorial-space
Add the line
- id: paketo-buildpacks/python at the top of the spec order list. your file should look like this:
spec: order: - group: - id: paketo-buildpacks/python - group: - id: paketo-buildpacks/java - group: - id: paketo-buildpacks/go - group: - id: paketo-buildpacks/nodejs - group: - id: paketo-buildpacks/ruby - group: - id: paketo-buildpacks/procfile
Now you can either bring your own Python app or use this sample one by running the following commands:
git clone https://github.com/sylvainkalache/sample-web-apps cd sample-web-apps/python
And deploy it:
cf push my-python-app
curl to make sure the app is responding:
curl --insecure https://my-python-app.apps-127-0-0-1.nip.io/dzone
Curl should return the following output:
Hello world! Python version: 3.10.12
As you can see, Korifi makes deploying applications to Kubernetes very easy. While Java and Python are two very different stacks, the shipping experience remains the same. Korifi supports many other languages like Go, Ruby, PHP, Node.js, and more.
Opinions expressed by DZone contributors are their own.