Basic External Server Example
The usual patterns should work here:
- Provision a VM outside of Kubernetes/Terraform (e.g. Ubuntu 16.04 LTS)
- SSH in and install nfs-common
- Create your /exports and run an NFS server
- Open ports
2049
,20048
, and111
firewall using OpenStack security groups - Consume the NFS mount from Kubernetes
Consuming the Mount
volumes:
- name: nfs
nfs:
server: <NFS_SERVER_IP>
path: /
Basic In-Cluster Server Example
Following this example, I was able to easily get an NFS server pod running within a Kubernetes 1.9 cluster.
The original example was intended for use with GCE and included some nice features like backing your NFS server with PVCs.
Specific Modifications
The StorageClasses are specific to GCE, so we do not need to include those. Instead, we rely on our Terraform process mounting an external volume to the storage nodes.
Modifying the above example slightly to work with our Terraform/hostPath process, we have the following set of YAMLs:
kind: Service apiVersion: v1 metadata: name: nfs-server spec: ports: - name: nfs port: 2049 - name: mountd port: 20048 - name: rpcbind port: 111 selector: role: nfs-server --- apiVersion: v1 kind: ReplicationController metadata: name: nfs-server spec: replicas: 1 selector: role: nfs-server template: metadata: labels: role: nfs-server spec: nodeSelector: external-storage: "true" containers: - name: nfs-server image: gcr.io/google_containers/volume-nfs:0.8 ports: - name: nfs containerPort: 2049 - name: mountd containerPort: 20048 - name: rpcbind containerPort: 111 securityContext: privileged: true volumeMounts: - mountPath: /exports name: nfs-export-fast volumes: - name: nfs-export-fast hostPath: path: /data/nfs
Consuming the Mount
The following example Pod consumes our in-cluster NFS export.
Note that the server
field is populated with the Kubernetes Service IP (e.g. kubectl get svc
):
apiVersion: v1 kind: Pod metadata: name: web spec: containers: - name: web image: nginx volumeMounts: # name must match the volume name below - name: nfs mountPath: "/usr/share/nginx/html/" ports: - name: web containerPort: 80 protocol: TCP volumes: - name: nfs nfs: # FIXME: use the right name #server: nfs-server.default.kube.local server: "10.101.9.169" path: "/" readOnly: false
Testing it Out
After creating our NFS server and a pod consuming it, we can use kubectl exec
to test that our NFS is working as expected:
# Check the node and name of our web pod ubuntu@mltf-master:~$ kubectl get pods NAME READY STATUS RESTARTS AGE IP NODE nfs-server-wc8h6 1/1 Running 0 21m 10.244.1.4 mltf-storage0 web 1/1 Running 0 11m 10.244.3.2 mltf-storage1 # Exec into the container to test writing to the NFS ubuntu@mltf-master:~$ kubectl exec -it web -- bash root@web:/# cat /usr/share/nginx/html/index.html Hello from NFS! # Test writing to a file root@web:/# vi /usr/share/nginx/html/pod-write bash: vi: command not found # No "vi" included in nginx image, so we install it root@web:/# apt-get update && apt-get install vim Ign:1 http://cdn-fastly.deb.debian.org/debian stretch InRelease Get:2 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB] ... ... ... ... ... ... ... ... ... Reading state information... Done The following additional packages will be installed: libgpm2 vim-common vim-runtime xxd Suggested packages: gpm ctags vim-doc vim-scripts The following NEW packages will be installed: libgpm2 vim vim-common vim-runtime xxd 0 upgraded, 5 newly installed, 0 to remove and 2 not upgraded. Need to get 6766 kB of archives. After this operation, 31.2 MB of additional disk space will be used. Do you want to continue? [Y/n] Get:1 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 xxd amd64 2:8.0.0197-4+deb9u1 [132 kB] Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 vim-common all 2:8.0.0197-4+deb9u1 [159 kB] ... ... ... ... ... ... ... ... ... update-alternatives: warning: skip creation of /usr/share/man/ja/man1/editor.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group editor) doesn't exist update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist # Test writing to a file root@web:/# vi /usr/share/nginx/html/pod-write root@web:/# cat /usr/share/nginx/html/pod-write asdf 1234 Hello, World! root@web:/# exit exit # SSH into "storage0", where our NFS server pod is running ubuntu@mltf-master:~$ ssh 192.168.0.3 Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-127-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 14 packages can be updated. 7 updates are security updates. Last login: Mon Jun 11 16:28:49 2018 from 192.168.0.6 # Verify that the file created inside of our web pod (running on "storage1") was persisted to the NFS directory on "storage0": ubuntu@mltf-storage0:~$ cat /data/nfs/pod-write asdf 1234 Hello, World!