DIY Cloud Storage: Running Nextcloud on a Kubernetes Home Lab

Jun 5, 2023 by Sameer Kulkarni
DIY Cloud Storage: Running Nextcloud on a Kubernetes Home Lab

Introduction

This is a new addition to our series of posts on building a Kubernetes Home Lab. If you’re just getting started, you can start with the first post.

We all use some or the other cloud based file backup and sharing solutions such as Google Drive, Microsoft OneDrive, Dropbox, etc. They are called Enterprise file synchronization and sharing (EFSS) solutions. EFSS tools are handy to easily and securely store, backup and share all your files, documents and photos. You can also use it to access those files on multiple devices and to collaborate with others. Though they’re quite useful, they all have their limitations. One of the prominent ones being the free storage capacity. In addition to that, they are also residing on some remote third party servers away from your direct control. This raises some security concerns and in the past there also have been some instances of data hacks from these servers.

One of the simplest ways of setting up a local network storage is by having a shared directory. Either using an NFS or Samba share or a router NAS. You can set this type of networked storage up in minutes and as long as you are using it on your local network it is a great way to access files. Obviously, accessing shared local directories or local file servers are not as sophisticated as a full fledged EFSS solutions. They don’t offer any web UI or apps to easily store, backup and access your files. You also need to spend time organizing all the files yourself, which would take up a lot of your time. Plus you obviously can’t use them if you’re away from the device/network where they’re set up.

Introducing Nextcloud

So with that in mind, what if you could host your own file storage and sharing service to store as much data as you liked with all the features of an EFSS solution on inexpensive hardware under your control? This is where Nextcloud comes in. Nextcloud is an EFSS as well as content collaboration platform. As opposed to most of the solutions in this space, Nextcloud is completely Free and Open Source Software (FOSS). You can see how Nextcloud compares to other similar solutions on their website. We can download and host it on our own hardware with as much storage capacity as we like.

Nextcloud is a fork of ownCloud. The primary difference between them is that, with ownCloud you need to have an Enterprise subscription for enterprise features. Below are some of the things you can do using Nextcloud.

  1. Manage all emails from multiple accounts, in a single place.
  2. Communicate using text, voice and video calls
  3. Store and share documents, photos, etc.
  4. Create/Edit documents
  5. Install and use many other utility applications from the Nextcloud apps store

You can install Nextcloud on any hardware you have, including any single board computers such as Raspberry Pi. This tutorial will walk you through the process to install it on a Kubernetes cluster and configure it.

Installing Nextcloud

There are a few ways of installing Nextcloud. As the most basic option you can download the Nextcloud archive package and host it on your own web server, such as apache, Nginx, etc. or you can use the Nextcloud all-in-one Docker image. It also has additional hosting options such as all-in-one VM image, Web installer, etc. Please visit the server section of the Nextcloud install page for a complete list of available options.

In this post we will go over installing Nextcloud using the all-in-one Docker image. As I shared in a previous series, I already have a Home Lab setup with a Kubernetes cluster in it, I will use the docker image and host it in the Kubernetes cluster using Acorn.

Acorn makes installing Kubernetes apps simple and painless. Here are a couple of reasons why it’s specially useful for web applications like Nextcloud.

  1. Easier application management through Acorn CLI and Acornfile
  2. It readily exposes our web app outside the cluster by configuring Kubernetes ingress controller and also returns a URL to access the app

Nextcloud Installation components

Nextcloud mainly needs three components to function.

  1. The Nextcloud web archive package
  2. A web server
  3. A relational database

The Nextcloud web archive contains the php pages and other code files needed for the Nextcloud server. The web server runs the Nextcloud web archive and serves the Nextcloud pages to the user. The relational database is used to store some of the user data.

As the name suggests, the Nextcloud all-in-one Docker image has all three of these components bundled together and is enough to start a Nextcloud server. Although it uses an SQLite instance for the database which is only suitable for light to medium traffic and also has limitations on the DB size. Hence it is recommended to use an external database instead of the inbuilt one. In addition to SQLite, Nextcloud supports MySQL and PostgreSQL as its database. To give us a bit more scale, we’re going to use a MySQL database as Nextcloud backend.

In addition to above, it is also recommended to attach a volume to the Nextcloud Docker image where it stores the user files, as well as to the database container. Otherwise the files and/or the database data will be lost if the container goes down. Hence in addition to the Nextcloud Docker image, a running database, we also need two sufficiently large volumes. One of the volumes would store all the user files and Nextcloud configuration while the second volume would hold the supporting database data.

Prerequisites

Following are the things you’ll need to have in order to follow along.

  • A Kubernetes cluster
  • Sufficient storage space for storing data and files

I have set up a home lab with four machines with decent compute and storage capacity. I have also installed a Kubernetes cluster on the same. If you don’t have any of the above, you can also create a Kubernetes cluster on a cloud provider of your choice.

Creating volumes

One of the machines in my home lab has a dedicated 2 TB of storage to share between all applications. I will be using that to create persistent volumes for the two containers in the installation using NFS.

To do this, I will create two directories on the specific machine where the 2TB drive is mounted and share it using NFS. Please refer to my earlier post in the Home Lab series, specifically Hosting Plex media server on my home lab, for the full process of creating NFS shares in your environment.

Create the two directories at the appropriate location.

$ sudo mkdir -p /mnt/sda1/nextcloud/{data,dbdata} # Create the 2 NFS share directories

Add the lines below to the /etc/exports file to configure these directories on the NFS service. Replace the directory path with the path to above directories and the CIDR range with the range for your router.

/mnt/sda1/nextcloud/data 192.168.31.0/24(rw,sync,no_root_squash,no_subtree_check) /mnt/sda1/nextcloud/dbdata 192.168.31.0/24(rw,sync,no_root_squash,no_subtree_check)

Restart the NFS service for the above change to take effect.

$ sudo systemctl restart nfs-kernel-server

With the NFS shares ready, we can now use them to create NFS persistent volumes in the Kubernetes cluster. Clone my github repo for the Nextcloud installation and run below commands to update the yaml files with your NFS share details.

$ git clone <a href="https://github.com/samkulkarni20/nextcloud-acorn.git">https://github.com/samkulkarni20/nextcloud-acorn.git</a> #clone the repo $ cd nextcloud-acorn $ sed -i 's/NFS_SERVER_IP/<YOUR_NFS_SERVER_IP>/g' {sc.yaml,pv.yaml} $ sed -i 's/NEXTCLOUD_DATA_NFS_PATH/<DATA_DIR_PATH_ON_SERVER>/g' {sc.yaml,pv.yaml} $ sed -i 's/NEXTCLOUD_DBDATA_NFS_PATH/<DBDATA_DIR_PATH_ON_SERVER>/g' {sc.yaml,pv.yaml}

Create the storage classes and PVs on the Kubernetes cluster.

$ kubectl create -f sc.yaml # Create storage classes $ kubectl create -f pv.yaml # Create the persistent volumes

Run Nextcloud Server

We will use Acorn to install Nextcloud. Acorn is a simple application deployment framework for Kubernetes. With this, developers can easily start making use of the powerful capabilities of Kubernetes, without having to dive into the complexities. To use Acorn to install applications on your Kubernetes cluster you need to have below prerequisites.

I have already completed the first two steps and you can find the completed Acornfile in my github repository, which you have already cloned above. The Acornfile has 3 sections.

  • Containers
  • Secrets
  • Volumes

The containers section defines each container to run in Kubernetes. It typically runs each as Kubernetes deployments, unless you specify it as a stateful application. Each container object contains specific details required to run it, such as the image to use, environment variables, volume mounts, etc. Find more details about the container objects in Acorn documentation.

Secrets is a special object meant to store/generate sensitive information. The token type secrets used in this Acornfiles will generate random 54 character long tokens to be used as user and root password for the mysql container. As the name suggests, it will create Kubernetes Secret objects to be used with the container. Acorn Secrets documentation has more details on the same.

The last object is the volumes to use with the containers. It creates Kubernetes Persistent Volume Claims, with the given parameters. The volume attributes mentioned in the Acornfile for this installation, must match the storage class and persistent volumes created above. That will ensure that the PVCs are bound successfully to the PVs created before. You can find more details on the Acorn Volumes documentation.

Run the below command to start the Nextcloud server. It will run two deployments in the Kubernetes cluster. One for mysql database and another for Nextcloud.

$ acorn run -n nextcloud .

Installing Nextcloud on Kubernetes

In a few seconds after running the above command, Acorn will return a url to access the nextcloud server. Copy and paste the URL in your browser to access Nextcloud. Create a Nextcloud admin account diy01.png

Before you can start using Nextcloud, you first need to finish a few steps to install it, starting with creating an admin account. Input the admin username, password and click “install”. The installation will take a few seconds to complete. Once it’s done you’ll be shown a list of recommended apps to install. Install recommended apps diy02.png

Click “Install recommended apps” on the second screen to continue. Nextcloud welcome screen diy03.png

After installing the recommended apps, Nextcloud will greet you with a welcome message and a short tutorial of the application. Go through the tutorial if required or cancel it out to land on the Nextcloud dashboard. Nextcloud dashboard diy04.png

This completes the Nextcloud installation, but using an admin account to do anything other than the administrative tasks isn’t ideal. Hence we need to add some user accounts first. Click on the username on the top right hand corner, click on “Users” and then click on “New user” in the following screen. Add Nextcloud user diy05.png

Fill in the form and click “Add a new user” to finish. Add new user form diy06.png

This completes our Nextcloud installation. You can now log in using one of the user accounts and start using Nextcloud. You can login to Nextcloud from multiple devices such as laptops, smartphones, etc. to upload and use your documents, photos or any type of file on it. Below I have installed the Nextcloud iOS app on my phone and logged in with my user. Nextcloud app on ios smartphone diy07.jpg

Conclusion

Here we saw what Nextcloud is and how it is different from standard hosted file sharing and cloud storage solutions. We saw some of the advantages of hosting Nextcloud yourself. We discussed the process for configuring and running Nextcloud on a Kubernetes cluster using Acorn.

You can learn more about Acorn at https://docs.acorn.io/ or by joining a hands-on training class.

You would have noticed that although we installed the Nextcloud server, it’s currently being served over an insecure connection. In an upcoming post we’ll see the process to secure it on our home lab cluster and bind it to a hostname of your choice.

This was the seventh post in our series on home labs for If you haven’t checked out the other posts in this series you can go through them using the list below.

  1. Building a Kubernetes Home Lab from the Ground Up
  2. Hardware and Networking Setup for My Home Lab
  3. Software Setup for My Home Lab
  4. Hosting Plex Media Server On My Home Lab
  5. Hosting Pi-hole on My Home Lab
  6. Creating an Application Dashboard for your Home Lab
  7. DIY Cloud Storage: Running Nextcloud on a Kubernetes Home Lab

If you want to learn more about Acorn you can go through the Acorn documentation or join an upcoming Acorn training class.

Sameer Kulkarni is a software engineer with 14+ years of experience and is working as a Principal Engineer at InfraCloud. You can chat with him on Twitter and read more of his work on Medium.

Header Photo by Farzad on Unsplash