Fire up kubernetes from iOS using python

Punchh Technology Blog
5 min readJul 19, 2019

Author: Andrew Berry

This article describes how to create and use a cloud-hosted Kubernetes cluster by running python scripts on an iPad. The eventual goal is to be able to use an iPad as an end-user device for development of applications running in a cloud-hosted kubernetes cluster.

Hello from your kubernetes cluster

See https://github.com/drpump/devkube-python/tree/v1.0 for source code. The scripts should also work on Linux and OS X.

This is an abbreviated version of a previous article posted at https://drpump.github.io/devkube-part2/, which contains more detail on the process, steps and pitfalls.

Prerequisites

  • iSH on an iPad or an equivalent linux/OS X command line
  • git command line installed
  • Python3 (on iSH, apk add python-dev)
  • Public cert for your ssh key in ~/.ssh/id_rsa.pub
  • The python requests library (pip3 install requests)
  • A Digital Ocean account and API token

Code Overview

There are four python scripts in the git repo:

  1. docluster.py creates a Digital Ocean kubernetes cluster with two nodes then saves the cluster URL and public/private keys for access.
  2. objects.py has functions to construct python dict objects for each kubernetes object that we need in our cluster. This is extensible in a fairly simple fashion. The official python kubernetes library is quite heavyweight and difficult to work with on an iPad. See the original article for details.
  3. pykube.py defines a class to encapsulate a kubernetes API session using the requests python library and includes both generic and specific methods for calling the kubernetes api. It is somewhat extensible and suitable for use in an interactive python session.
  4. exec.py is intended for loading into an interactive python shell, importing the required libraries and creating a pykube instance pk that is connected to your kuberetes cluster.

The original goal was to create a simple script to construct the kubernetes cluster, however, I wrote this code on my iPad and couldn’t fall back on kubectl. Whenever something went wrong I had to build API calls to retrieve kubernetes objects and inspect them, or delete and recreate an object via the API. So pykube morphed into a set of functions that provide a subset of what you get from kubectl. For someone used to coding and REPL, it actually works really well and I like it better than using kubectl.

Caveat: I’ve been coding for a long time, but I am a relative novice at python. Code improvement suggestions from python gurus are most welcome (pull requests would be great).

Start it up

  $ git clone git@github.com:drpump/devkube-python
$ cd devkube-python
$ export DO_TOKEN=<my_token>
$ python3 docluster.py ### takes a while to create the cluster
...
$ source cluster.env ### load the cert and URL for cluster
$ python3 -i exec.py
...
>>> pk.deploy_nfs()
Waiting for nfs startup ...
Waiting for nfs startup ...
nfs running in cluster on 10.244.0.218
>>> pk.deploy_ssh()
ssh accessible on port 30022 on IPs ['165.22.128.219', '165.22.128.246']
>>> pk.deploy_web()
web server accessible at http://165.22.128.219:30080
>>> ^D
$ ssh -p 30022 root@<your_node_ip>
...
[root@sshd-012345678-9abcd]# cat > /mnt/nfs/index.html
Hello from your kubernetes cluster
^D
#

Then navigate to the URL printed by pk.deploy_web() above to see the result.

If you are using a Linux or OS X machine, you can also use Visual Studio Code for remote development via ssh in this cluster.

Concluding remarks

At this point you should have a working kubernetes cluster in DigitalOcean created entirely by python scripts and without kubectl, perhaps even running on an iPad. The scripts are straightforward and the cluster creation script should be adaptable to work with your favourite cloud kubernetes provider.

The good

  • You can create a working cluster from your iPad or even your iPhone, which significantly increases the convenience factor over kubectl.
  • You can provision and manage kubernetes using a structured, well-defined and well-known programmming language (python).
  • The configuration provides some ability to debug through retrieving cluster objects and ssh into the cluster. Further, you might even prefer using the pykube functions over kubectl, although admittedly the scope is more limited.
  • The python code has minimal dependencies and requires no C libraries, making it quite portable.
  • ssh access to the cluster is reasonably well secured.
  • If your source machine can run Visual Studio Code, you can use the remote development capability on the cluster.

The bad

  • We still don’t have a useful editing solution for an iPad: using vim via ssh is kinda clunky. Local editing (e.g. GoCoEdit or TextTastic) with rsync for synchronisation seems achievable via iSH but needs some scripting for usability and still doesn’t give you IDE capabilities.
  • The http service is still unsecured. Tunnelling via ssh is an option. Using https via an ingress controller or load balancer is also feasible.
  • We haven’t yet deployed a real application server, database or cache.
  • Digital Ocean doesn’t yet provide a way to re-attach an old volume to a new kubernetes cluster. So you need to save all of your code before destroying the cluster, and manually destroy the volume after the cluster is destroyed. Other kubernetes providers might be better.

The future

  • I’ll extend the ssh container image with additional tools for dev and debug.
  • With the pending iPadOS (iOS 13) browser changes, it becomes more feasible to deploy and use a browser-based IDE like coder.com. So I’ll deploy and test this browser-based IDE container
  • I’ll add an application server, database and cache: most likely Rails, MySQL and Redis in the first instance
  • I need to bootstrap the content in application server instances. In Rails, for example, we would checkout or upload the application, seed the database, etc. Ideally this can be automated so that if you’re using a set of microservices but only actively develop one of them, the others can be automatically deployed, seeded etc. Alternatively, we could deploy production docker images for read-only microservices.

About Punchh

Headquartered in San Mateo, CA, Punchh is the world leader in innovative digital marketing products for brick and mortar retailers, combining AI and machine learning technologies, mobile-first expertise, and Omni-Channel communications designed to dramatically increase lifetime customer value. Leading global chains in the restaurant, health and beauty sectors rely on Punchh to grow revenue by building customer relationships at every stage, from anonymous, to known, to brand loyalists, including more than 100 different chains representing more than $12 billion in annual spend.

About the Author

Dr Andrew Berry is a Senior Platform Architect at Punchh, but also mucks with technology in his spare time for both learning and enjoyment. This article has arisen from such mucking about.

--

--

Punchh Technology Blog

Punchh is a marketing & data platform. In the blog site, we will share our learnings from data and technology.