Skip to content

Plugging Apps Together

In this guide we will show you how to connect two apps together in Kraftcloud; we will use, as example, a web server written in Python as frontend and a MariaDB instance as backend, as shown in this diagram:

Service groups in KraftCloud

If you haven’t come across what a service group is yet, you can have a glance at this guide, but essentially in KraftCloud a service group is the mechanism we use to expose apps to the Internet. So in this guide we will attach the web server instance to the service group so that it’s externally accessible, and then connect that instance to the MariaDB instance via a private connection/IP.

Setting it Up

To start, let’s create the MariaDB instance (you can get more details about this in the MariaDB guide):

Terminal window
git clone https://github.com/kraftcloud/examples
cd examples/mariadb
kraft cloud deploy --name mariadb -M 1024 .

Note that we don’t specify the -p flag, since this instance will be private, i.e., it does not need to connect to the Internet and so no service group is needed for it. We use the --name flag to tell KraftCloud to set the instance’s name to mariadb, but more importantly, to use it as its private DNS name so we can put that into our web server later. The resulting output of the deploy command should be similar to:

Terminal window
[] Deployed successfully!
────────── name: mariadb
────────── uuid: 269019de-f7dc-4077-9568-012ad594ca87
───────── state: running
─────────── url: wispy-moon-dpg6d54i.fra0.kraft.host
───────── image: mariadb@sha256:6abb4f2ba4501068a84885d7b8b127adaf3d83c25fd43e79d5a142f6d8703c93
───── boot time: 162.65 ms
──────── memory: 1024 MiB
service group: wispy-moon-dpg6d54i
── private fqdn: mariadb.internal
──── private ip: 172.16.6.5
────────── args: /usr/sbin/mariadbd --user=root --log-bin

Note the private fqdn field’s value of mariadb.internal which will go into our Python server code.

Next we need to create the Python server to connect to the DB. We’ll use Flask for this:

Terminal window
git clone https://github.com/kraftcloud/examples
cd examples/http-python3.12-flask3.0

Replace the contents of server.py and requirements.txt with:

from flask import Flask, jsonify
import mysql.connector
app = Flask(__name__)
db_config = {
'host': 'mariadb.internal',
'user': 'root',
'password': 'unikraft',
'database': 'mysql'
}
def get_data_from_database():
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor()
# Replace this query with your actual SQL query
query = "SELECT count(*) FROM user;"
cursor.execute(query)
data = cursor.fetchall()
return data
except Exception as e:
print(f"Error: {e}")
return None
finally:
if connection.is_connected():
cursor.close()
connection.close()
@app.route('/')
def get_data():
data = get_data_from_database()
if data is not None:
return jsonify({'data': data})
else:
return jsonify({'error': 'Failed to retrieve data from the database'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)

With this in place, let’s start our Flask web server and create a service group for it via the -p flag:

Terminal window
kraft cloud deploy -M 512 -p 443:8080 .

You should see output similar to:

Terminal window
[] Deployed successfully!
────────── name: http-python312-flask30-hb8p8
────────── uuid: eea65094-33fe-423f-b820-0bdaa875a494
───────── state: running
─────────── url: https://quiet-river-w2muxbwe.fra0.kraft.host
───────── image: http-python312-flask30@sha256:49bba8cbd207391c6a8b785b7b6eeb1ff2f959618aa7f80b30dc5c252996e120
───── boot time: 267.91 ms
──────── memory: 512 MiB
service group: quiet-river-w2muxbwe
── private fqdn: http-python312-flask30-hb8p8.internal
──── private ip: 172.16.6.7
────────── args: /usr/bin/python3 /app/server.py

So now we have our Flask instance attached to a service group named quiet-river-w2muxbwe and with public URL https://quiet-river-w2muxbwe.fra0.kraft.host.

Testing it

Simply curl the URL:

Terminal window
curl https://quiet-river-w2muxbwe.fra0.kraft.host

You should get results from the MariaDB database, i.e.,:

Terminal window
{"data":[[6]]}

This is the result from the SELECT count(*) FROM user; query.

Learn More