You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

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, and 111 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:

nfs-server-rc.yaml
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):

web-pod.yaml
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!

We can also ensure that these NFS volumes can be mounted into multiple pods simultaneously (e.g. ReadWriteMany):

# Create some duplicate pods consuming the same NFS mount
ubuntu@mltf-master:~/kubernetes-nfs-server$ kubectl create -f web-pod2.yaml -f web-pod3.yaml 
pod "web2" created
pod "web3" created


# Wait for containers to start
ubuntu@mltf-master:~/kubernetes-nfs-server$ kubectl get pods -o wide
NAME               READY     STATUS              RESTARTS   AGE       IP           NODE
nfs-server-wc8h6   1/1       Running             0          49m       10.244.1.4   mltf-storage0
web                1/1       Running             0          39m       10.244.3.2   mltf-storage1
web2               0/1       ContainerCreating   0          8s        <none>       mltf-storage0
web3               0/1       ContainerCreating   0          8s        <none>       mltf-worker0


# Verify that the file created inside of our original web pod (running on "storage1") also shows up here
ubuntu@mltf-master:~/kubernetes-nfs-server$ kubectl exec -it web2 -- cat /usr/share/nginx/html/pod-write
asdf 1234 Hello, World!
ubuntu@mltf-master:~/kubernetes-nfs-server$ kubectl exec -it web3 -- cat /usr/share/nginx/html/pod-write
asdf 1234 Hello, World!

Dynamic Volumes using the NFS Provisioner

Coming soon!

See https://github.com/kubernetes-incubator/external-storage/tree/master/nfs

  • No labels