Navigation

Subscribe

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!

Moodle on Kubernetes - Part 1

So it's summertime, and that means project-time for teachers. My project this summer, besides my usual reading and travel, is to setup a LMS to keep my instructional materials on. My purpose is to have a platform that I can create and house materials on so I can reuse it from year to year, much like I did when I taught college.

I have decided to use a Kubernetes cluster for this project, because I want to have some automated scalability and elasticity. If I have not provisioned enough resources for the server, I want it be able to fix itself rather than have it crash when I'm directing students to use it. I also wanted infrastructure with good reliability, and Kubernetes pods provide some excellent redundancy.

Why Moodle? It's free and open source; enough said. Let's get to the fun part.

Pre-Tutorial: First things first

Before you attempt this you're going to need to know a few things. This tutorial is making the following assumptions (although there may be more than I can list here):

  • You know how to interact with a remote machine through terminal in Linux.
  • You have a Google Cloud account.
  • You have some clue what Moodle is.
  • You've at least watched a quick video on what Kubernetes is.

Tutorial: Second things second

You will need to provision a Kubernetes cluster to get things rolling. Head over to your neighborhood Google Cloud Console to get started. Once there, you will want to create a new project. Give it a good name so you know what it's for. You can see mine below (MoodleKube).New Project

Kubernetes

Next you're going to need to get yourself a cluster provisioned. This step will create your compute engines, load balancer, auto-scaler, and everything that makes Kubernetes great.
Kubernetes-engine-clusters
On the menu on your left, select Kubernetes Engine > Kubernetes clusters aaaaannnd...


Options

Don't freak out!

It's a mess, I know. It gets worse when you click the "more" link at the bottom, but we need to do just that. I've marked what I changed, or wished I had changed in RED in the image above. We'll go through the options one-by-one to show just what we need to change and why.

  1. Name - This should be pretty self-explanitory. What do you want to call your cluster? Mine is called MoodleCluster.
  2. Zone - This will be important. You want to choose a zone that is close to a majority of your users. Different zones also have different options for compute engines, but that shouldn't matter for this Kubernetes cluster. Here is some information on the different zones and their locations.
  3. Customize - Here you can change some things about the virtual machines that will be provisioned for this cluster. What I should've done when I made mine is dial back the memory. You really don't need that much RAM to run Moodle and MariaDB. 2GB should be just fine, and may save you a little bit of money. I will probably transition my cluster to a smaller machine type sometime in the near future. You don't have to change this, but I would.
  4. Autoscaling - If you want this cluster to be able to grow as demand waxes and shrink back to normal when it wanes you'll want to enable this. If you enable it (which you really should), you will need to add a value to the next section. I recommend doubling it and making the maximum size 6 nodes.
    Autoscaling
    If you don't want it to scale (seriously, why would you not want this?) then leave this and the next two sections alone and skip to the end.
  5. Maximum number of nodes per pool - Kubernetes is smart enough to not overload the pool (too many containers in an environment), but you can also set a cap on it here. For this use-case you could set it to 2, or just leave it alone and put nothing. You'll probably be fine...

You made it!

Ok, time to click Create. It's ok if you mess it all up and have to kill the cluster, we've only invested a small amount of time so far. If you like what you've done, or you haven't realized you've messed up yet, we just need to wait for everything to automatically setup.

Once that is done, you'll be greeted with a wonderful site on your Kubernetes Clusters page in your Google Cloud Console. You can now connect to your cluster! Click on the connect button and choose one of the two options.
Connect

Ways-to-connect
Option A - if you already have the Google Cloud SDK installed in your environment, you can copy and paste this command into a terminal to retrieve the credentials so you can work on your cluster from your own terminal. If this sounds intriguing or exciting to you: good luck, have fun. I'm going to recommend the next option because it's quicker and easier to explain.
cloud-shell
Option B - clicking Run in Cloud Shell will open a small terminal emulator at the bottom of your browser. It will already be populated with the script listed above, so you can just click into the terminal to capture your keyboard strokes and press RETURN on your keyboard to connect. Your Google Cloud Shell is now connected to your Kubernetes Cluster, and we can now send commands to it - huzzah! Each node is running Container Optimized OS, which is just a low-key Debian.

By now we should have our Kubernetes cluster up and running, and we should be able to send Kubectl commands to it through either the Google Cloud Shell or your own terminal. The next thing we have to do is setup Helm and Tiller, so we can manage the packages on our nodes. This step is necessary to install Moodle. So here we go:

Getting Helm and Tiller ready

  1. Preview the bash script here.
    Don't ever pipe anything to bash unless you've read it or you're prepared to lose everything.
    If it looks good to you, move on to the next step. If not, you can install the binary manually by downloading the tar.gz and unpacking it.
  2. If you liked the script idea:
    curl "https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get" | sh
  3. Get Helm and Tiller started:
    helm init
  4. Add the repo we need for Moodle:
    helm repo add stable https://kubernetes-charts.storage.googleapis.com
  5. Make Tiller a special accound because for some reason this version of tiller isn't happy working with our setup:
    kubectl create serviceaccount --namespace kube-system tiller
  6. Give the special Tiller account a role:
    $kubectl create clusterrolebinding tiller-cluster-role --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
  7. Send it spec info about it's super-special role so it's happy:
    kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"ServiceAccount":"tiller"}}}}

Almost there!

Now we need to update our repositories for helm, and get Moodle! Then we should be able to connect to our Moodle installation. A little more fun in the console.

  1. Update it:
    helm repo update
  2. Get the package:
    helm install stable/moodle
  3. Relax for a few minutes while everything warms up.

    Photo by Nik MacMillan / Unsplash

Now the backend is done. Stay tuned for Moodle on Kubernetes - Part 2, where I'll go through the setup process from inside Moodle, or just link you to their tutorial...I'm not certain yet. But you should be able to connect to your Moodle cluster at IP address of your load balancer. We'll go over that next time.

Comments: