Kubernetes Storage config Simplified
Tags: beginners, devops, kubernetes
Published: 2019-09-22 12:17:25

---
title: Kubernetes Storage config Simplified
published: true
description: Kubernetes storage configuration can be confusing, let's look at the major options we have and what is relevant for our software architecture.
tags: beginners, devops, kubernetes
cover_image: https://images.unsplash.com/photo-1565462905102-140e712045aa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80

---

* new to Kubernetes? read [Kubernetes in a nutshell](https://dev.to/adipolak/kubernetes-and-virtual-kubelet-in-a-nutshell-gn4)

From Kubernetes website:
> On-disk files in a Container are ephemeral, which presents some problems for non-trivial applications when running in Containers.

That means that since pods are disposable, we can't save files to the container or pod file systems. Upon crash, the kubelet managing it will restart the container/pod and will result in deleting all the files.

👉 **Why we need storage?**

Some best practices mention that Microservices should be stateless, but reality shows that sometimes applications need to be able to store and retrieve data. It can be from a file system or NoSQL / SQL storage.

In order to have this data available, we will need to configure [_volumes_](https://kubernetes.io/docs/concepts/storage/volumes/).

## What are volumes?
When discussing data storage, **volume** refers to the logical drive. An actual physical file system on a hard drive.

For any kind of storage, we would like to work with, we will need to define them first using YAML file.

There are many types of volumes, we will focus on **3** of them:

### 💾 emptyDir 

 Using `emptydir` we can define a directory on the node itself. 
 However, if the node is down, the content of the `emptydir` is erased.

in YAML file it will look like this:

```YAML
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}<o:p></o:p>
```


### 💽 hostPath 
Another one is `hostPath`.

With `hostPath` we mount one of the directories or files from the host node filesystem into the pod.
The major difference between `hostPath` and `emptyDir` is that `hostPath` might not be an empty directory. 
BTW, `hostPath`, together with a privileged Kubernetes [DaemonSet](https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/) can be used to configure the underlying VM's by running commands on the host.
However, we should be careful doing so, since it can cause security breaches.

* **DeamonSet** - set of processes that are running in the background on defined nodes. For example DeamonSet of type logs collection. Meaning we would like to have log collection processes running in the background for all of our pods so we can collect the logs and store them in a dedicated place.

in YAML file it will look like this:

```YAML
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory
```

Last but not least: 

### 🌤️ Public Clouds
If you are using one of the public clouds, you can take advantage of many existing services like AWS Elastic Block Store and Azure [Disk Storage](https://azure.microsoft.com/en-us/services/storage/disks/?WT.mc_id=devto-blog-adpola).




## 💡 That's a wrap! Resources and Learn More: 
* [SQL server container in Kuberenetes with AKS](https://docs.microsoft.com/en-us/sql/linux/tutorial-sql-server-containers-kubernetes?view=sql-server-2017/?WT.mc_id=devto-blog-adpola)
* [Kubernetes volumes](https://kubernetes.io/docs/concepts/storage/volumes/#emptydir)
* [Best practices for storage and backups in AKS](https://docs.microsoft.com/en-us/azure/aks/operator-best-practices-storage/?WT.mc_id=devto-blog-adpola)
* This post is inspired from this: [How do I configure storage for a bare metal Kubernetes cluster?](https://www.weave.works/blog/kubernetes-faq-configure-storage-for-bare-metal-cluster)
* all YAML file example are from [Kubernetes.io](https://kubernetes.io) docs.


![](https://media.giphy.com/media/3oGRFKhwAmd94V8HUQ/giphy.gif)