A kubernetes cluster is of course useless if you don't have storage to put your data on. To keep kubernetes flexible and scalable you don't store data on your pods. You will loose data and run into issues every time you upgrade, delete or scale the pods if you do that. There are options to store data within the cluster, but I will discuss them in later posts. For now, I have chosen to provide datastorage to the cluster via NFS on my Synology NAS. It is not that hard to install, and it makes it fairly easy to explain how Persistent Volumes en Persistent Volume Claims work within a kubernetes cluster.
So let's start providing storage to our home kubernetes cluster.
- Prepare NFS on your Synology NAS
The following action should be executed on your Synology NAS console
Firs you need to enable NFS via Control Panel -> Files Services and the select the SMB/AFP/NFS tab. Scroll down until you NFS section. Then select the Enable NFS option and select Enable NFSv4.1 support (Synology DSM 6) or Maximum NFS protocal NFSv4 (Synology DSM 7) like the screen shot below, and Click Apply.
Next goto the Shared Folder option in the control and create a new share (for example kubedata). Open the share in the Control Panel and select the NFS Permissions Tab. Click create and use the following value:
- Hostname of IP: *
- Privilege: Read Write
- Squash: No Mapping
- Security: sys
- Enable asynchronous
- Enable Allow users to access mounted subfolder
See also the screenshot blow.
Note the value behind Mount path. In the example above this is /volume1/kubedata.
- Create subfolder for the kubernetes application (deployment)
Create in the kubedata share a folder called nginx-1
Nginx is the name of a webserver, which we will use later in the blogpost.
We now have created a nfs share with a folder on the Synology NAS.
(If you require more information to configure NFS on your Synology NAS, you can find it here )
Now that we have created an NFS share we are able to use it on our kubernetes cluster. We do this using a so called persistent volume, and a persistent volume claim.
- Create a persistent volume in kubernetes
Execute the following command on your k8s-master server.
nano pv-nfs-kubedata-nginx-1.yamlCut and past the text below and save the file
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-kubedata-nginx-1  # < Name of the persistent volume
  namespace: default    
spec:
  storageClassName: ""
  capacity:
    storage: 1Gi # < Maximum storage size you want to reserve
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    server: xxx.xxx.xxx.xxx  # < The ip adress of your NAS (NFS Server)
    path: "/volume1/kubedata/nginx-1"  # < The NFS volumename 
    readOnly: false
Apply the file via the following commandkubectl apply -f pv-nfs-kubedata-nginx-1.yamlkubectl get pv pv-nfs-kubedata-nginx-1nano pvc-nfs-kubedata-nginx-1.yamlapiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nfs-kubedata-nginx-1
  namespace: default
spec:
  storageClassName: ""
  volumeName: pv-nfs-kubedata-nginx-1 # < The volumename needs correpond with the persistent volume
  accessModes:
   - ReadWriteOnce 
  resources:
    requests:
      storage: 1Gikubectl apply -f pvc-nfs-kubedata-nginx-1.yamlkubectl get pvc pvc-nfs-kubedata-nginx-1 -n default<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h1>Kubernetes - Webtest 1</h1>
<p>This page is located on a persistent volume, and run on a k8s-cluster!</p>
</body>
</html>nano deploy-nginx-1-k8s.yamlapiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-1
  labels:
    app: nginx-1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-1
  template:
    metadata:
      labels:
        app: nginx-1
    spec:
      volumes:
      - name: nginx-1-volume
        persistentVolumeClaim:
          claimName: pvc-nfs-kubedata-nginx-1
      containers:
        - image: nginx
          name: nginx-1
          imagePullPolicy: Always
          resources:
           limits:
             memory: 512Mi
             cpu: "1"
           requests:
             memory: 256Mi
             cpu: "0.2"
          volumeMounts:
          - name: nginx-1-volume
            mountPath: /usr/share/nginx/html
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-1-service
spec:
  selector:
    app: nginx-1
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80 
  type: LoadBalancer
---
kubectl apply -f deploy-nginx-1-k8s.yamlkubectl get service -n defaulterikdebont@k8s-master:~$ kubectl get service -n default
NAME              TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
kubernetes        ClusterIP      10.96.0.1       <none>          443/TCP        46d
nginx-1-service   LoadBalancer   10.103.42.209   xxx.xxx.xxx.xxx 80:31520/TCP   18m
erikdebont@k8s-master:~$





Comments
Post a Comment