How to deploy a Flask RESTPlus API on CIVO hosted k3s Kubernetes Cluster with Rancher RIO in 10 Minues
Goal of this Write-Up is to show how to setup a working, scalable API service on k3s Kubernetes cluster with the help of a managed Kubernetes instances in the CIVO cloud.
Currently (November 2019) in a Beta Preview Program, CIVO is offering managed Kubernetes services based on Ranchers k3s distribution. k3s just hit the v1.0.0 milestone recently and gets a lot of attention in the Kubernetes space. Next to k3s, Rancher is also building a MicroPaaS called RIO which will help to ease the steep learning curve into the world of Kubernetes.
So, what does it take to setup a real world API service with this new tools at hand?
Working API service
I'm in the progress to build an API for a side project anyway and started with Flask and the Flask RestPlus library. However this Write-Up just shows off the final result which we can use to see how the k3s cluster behaves and how scaling works. To have a complete reference, here are the docs I used to build the API:
- pyenv - Simple Python Version Management
- Pipenv: Python Dev Workflow for Humans
- Flask RESTPlus
- uWSGI-NGINX-Flask Docker Container to run Flask
Installed KUBECTL on your local machine
For this project I'm working on my Windows 10 PC with Ubuntu - WSL (Windows Subsystem for Linux) Here are the links to the corresponding setup instructions:
Installed RIO CLI
Ranchers MicroPaas comes with its own CLI. This can be installed on our Linux system easily and will communicate with our k3s cluster over KUBECTL later:
Account on CIVO
At the time of writing this, the Managed Kubernetes Services of CIVO are in Beta Preview. I got the chance to be part of this Beta Program - Kube100 and therefore I'm able to show the steps to setup this service.
Let's get started
Install k3s cluster
Login to the CIVO Dashboard and switch to the Kubernetes Tab. Select the
+ Create Cluster button.
- Give your cluster a name, we choose
- Choose the number of nodes you want to setup. We go for a
3 node cluster
- Choose the size of the cluster. For this demo we take a
Smallinstance here. This wil give us 3 nodes with 1 CPU, 2GB of RAM, 25GB of SSD storage and 1TB transfer per month.
- Scrolling a bit further down on this page, we see that this cluster will be charged at $30.- per month.
- With CIVO you have the possibility to add some Applications from the Marketplace. This will give you a head start if you want some storage solutions or a database in your setup. As a default there is
Traefikselected. Traefik is a Edge Router which can automatically get Let's Encrypt certificates for you. However, we want to install Rancher RIO later on and RIO is suggesting to install k3s without Traefik.
- We unselect the Traefik option in the Architecture Tab of the CIVO Marketplace
- Hit the CREATE button (scroll further down)
- Approximately 12 Minutes later we have the cluster ready
- Now we can download the Kubeconfig file to our local computer and switch to our WSL Terminal.
- Copy the downloaded file (it is called
civo-nameofyourcluster) to your working directory and let KUBECTL know where to find it:
- check out that we can access the cluster with KUBECTL:
kubectl get node
# will display something like that
NAME STATUS ROLES AGE VERSION
kube-node-4f22 Ready <none> 19m v1.16.3-k3s.2
kube-node-bd05 Ready <none> 20m v1.16.3-k3s.2
kube-master-4bb5 Ready master 21m v1.16.3-k3s.2
Install Rancher RIO onto the cluster
For the next step you have to make sure RIO is installed on your local workstation. To check this just...
# should show the version you installed
rio version v0.6.0 (c1bff129)
Since KUBECONFIG points to our downloaded Kubeconfig file from the CIVO k3s cluster we can easily install RIO. To make sure RIO will point to our external accesible IP-addresses when we deploy services we have to tell RIO which addresses to use. We can get those IP-addresses from the CIVO Dashboard (just see last screenshot above). The we install RIO like so:
rio install --ip-address 220.127.116.11,18.104.22.168,22.214.171.124
# after about 2 minutes we should get the confirmation that RIO loaded
Generating clusterDomain for this cluster: 4yp14n.on-rio.io. Verified clusterDomain is reachable.
rio controller version v0.6.0 (c1bff129) installed into namespace rio-system
Controller logs are available from `rio systemlogs`
Welcome to Rio!
Run `rio run -p 80:8080 https://github.com/rancher/rio-demo` as an example
To check out if everything works, we can deploy the example project:
rio run -p 80:8080 https://github.com/rancher/rio-demo
# watch till the example project is deployed and ready
watch rio ps
Once the deployment is done and
ready we can head over to the URL which is provided from RIO and should see something like this in our browser:
Hi there, I'm running in Rio.
Notice that we get a
https URL and have a proper Let's Encryp certificate issued with it. All done from RIO.
Before we move on, let's get rid of this Example application trough the RIO CLI:
# get the running applications and look for the name of the container
# remove the deployment
rio rm eloquent-chatelet
# to get help about all or specific RIO commands:
rio ps --help
rio run --help
Install our API project to RIO
The moment of truth. Let's deploy our own API and see if we can access it trough the domain provided from RIO.
Our Flask RESTPlus API project is hosted on GitHub and has a Dockerfile in the root directory. Hint: make sure
Dockerfile starts with a capital D. Took me some debugging when I wrote it all-lowercase :-)
Let RIO run it:
rio run -n flaskapi -p 80:80 https://github.com/cellerich/flask-restplus-on-civo-k3s-with-rio.git
- RIO will first launch a container to build the image of the application
- afterwards the newly built image is deployed (standard is one replica)
- we will get the URL from
rio psas usual:
# will give us
NAME IMAGE ENDPOINT
flaskapi default-flaskapi-v04wl9p:956e5 https://flaskapi-v0-default.4yp14n ...
If we enter the URL in our browser we will find the Swagger Documentation of our API and can try out the endpoints directly in the browser:
With CIVO as a Kubernetes cluster provider, Rancher RIO as the MicroPaas "Framework" you are able to deploy a workload to the Cloud very easy without to dig to much into the internals of Kubernetes.
Where to go from here?
Check out the following features from CIVO and Rancher RIO once you got your feet wet with this example:
- install RIO Dashboard ->
- add some more k3s nodes trough the CIVO Dashboard
- scale your service with RIO (trough the CLI or RIO Dashboard)
- load test the API and see how the load balancing works
If you like this Write-Up, let me know. I might follow up with some more insights about CIVO, Rancher RIO, k3s or some other topis around the Cloud Universe.