From ed46c4db5119a0a6c50846ed30735061c76d845c Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Thu, 2 Apr 2015 10:47:21 +0200 Subject: [PATCH] Add NFS export/import pod examples. --- examples/nfs/README.md | 37 ++++++++++++++ examples/nfs/exporter/Dockerfile | 7 +++ examples/nfs/exporter/README.md | 10 ++++ examples/nfs/exporter/run_nfs | 72 ++++++++++++++++++++++++++++ examples/nfs/nfs-data/Dockerfile | 5 ++ examples/nfs/nfs-data/README.md | 7 +++ examples/nfs/nfs-data/index.html | 1 + examples/nfs/nfs-server-pod.yaml | 15 ++++++ examples/nfs/nfs-server-service.yaml | 9 ++++ examples/nfs/nfs-web-pod.yaml | 27 +++++++++++ examples/nfs/test.yaml | 21 -------- 11 files changed, 190 insertions(+), 21 deletions(-) create mode 100644 examples/nfs/README.md create mode 100644 examples/nfs/exporter/Dockerfile create mode 100644 examples/nfs/exporter/README.md create mode 100755 examples/nfs/exporter/run_nfs create mode 100644 examples/nfs/nfs-data/Dockerfile create mode 100644 examples/nfs/nfs-data/README.md create mode 100644 examples/nfs/nfs-data/index.html create mode 100644 examples/nfs/nfs-server-pod.yaml create mode 100644 examples/nfs/nfs-server-service.yaml create mode 100644 examples/nfs/nfs-web-pod.yaml delete mode 100644 examples/nfs/test.yaml diff --git a/examples/nfs/README.md b/examples/nfs/README.md new file mode 100644 index 00000000000..53a8ce7c664 --- /dev/null +++ b/examples/nfs/README.md @@ -0,0 +1,37 @@ +# Example of NFS volume + +See [nfs-web-pod.yaml](nfs-web-pod.yaml) for a quick example, how to use NFS volume +in a pod. + +## Complete setup + +The example below shows how to export a NFS share from a pod and import it +into another one. + +### NFS server part + +Define [NFS server pod](nfs-server-pod.yaml) and +[NFS service](nfs-server-service.yaml): + + $ kubectl create -f nfs-server-pod.yaml + $ kubectl create -f nfs-server-service.yaml + +The server exports `/mnt/data` directory as `/` (fsid=0). The directory contains +dummy `index.html`. Wait until the pod is running! + +### NFS client + +[WEB server pod](nfs-web-pod.yaml) uses the NFS share exported above as a NFS +volume and runs simple web server on it. The pod assumes your DNS is configured +and the NFS service is reachable as `nfs-server.default.kube.local`. Edit the +yaml file to supply another name or directly its IP address (use +`kubectl get services` to get it). + +Define the pod: + + $ kubectl create -f nfs-web-pod.yaml + +Now the pod serves `index.html` from the NFS server: + + $ curl http:/// + Hello World! diff --git a/examples/nfs/exporter/Dockerfile b/examples/nfs/exporter/Dockerfile new file mode 100644 index 00000000000..3cad57d8579 --- /dev/null +++ b/examples/nfs/exporter/Dockerfile @@ -0,0 +1,7 @@ +FROM fedora:21 +MAINTAINER Jan Safranek +EXPOSE 2049/tcp + +RUN yum -y install nfs-utils && yum clean all && run_nfs /usr/local/bin/run_nfs + +ENTRYPOINT ["/usr/local/bin/run_nfs"] \ No newline at end of file diff --git a/examples/nfs/exporter/README.md b/examples/nfs/exporter/README.md new file mode 100644 index 00000000000..e051e675394 --- /dev/null +++ b/examples/nfs/exporter/README.md @@ -0,0 +1,10 @@ +# NFS-exporter container + +Inspired by https://github.com/cpuguy83/docker-nfs-server. Rewritten for +Fedora. + +Serves NFS4 exports, defined on command line. At least one export must be defined! + +Usage:: + + docker run -d --name nfs --privileged jsafrane/nfsexporter /path/to/share /path/to/share2 ... diff --git a/examples/nfs/exporter/run_nfs b/examples/nfs/exporter/run_nfs new file mode 100755 index 00000000000..aef73b2001e --- /dev/null +++ b/examples/nfs/exporter/run_nfs @@ -0,0 +1,72 @@ +#!/bin/bash + +# Copyright 2015 Red Hat Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +function start() +{ + + # prepare /etc/exports + seq=0 + for i in "$@"; do + echo "$i *(rw,sync,no_root_squash,insecure,fsid=$seq)" >> /etc/exports + seq=$(($seq + 1)) + echo "Serving $i" + done + + # from /lib/systemd/system/proc-fs-nfsd.mount + mount -t nfsd nfds /proc/fs/nfsd + + # from /lib/systemd/system/nfs-config.service + /usr/lib/systemd/scripts/nfs-utils_env.sh + + # from /lib/systemd/system/nfs-mountd.service + . /run/sysconfig/nfs-utils + /usr/sbin/rpc.mountd $RPCMOUNTDARGS + + # from /lib/systemd/system/nfs-server.service + . /run/sysconfig/nfs-utils + /usr/sbin/exportfs -r + /usr/sbin/rpc.nfsd -N 2 -N 3 -V 4 -V 4.1 $RPCNFSDARGS + + echo "NFS started" +} + +function stop() +{ + echo "Stopping NFS" + + # from /lib/systemd/system/nfs-server.service + /usr/sbin/rpc.nfsd 0 + /usr/sbin/exportfs -au + /usr/sbin/exportfs -f + + # from /lib/systemd/system/nfs-mountd.service + kill $( pidof rpc.mountd ) + # from /lib/systemd/system/proc-fs-nfsd.mount + umount /proc/fs/nfsd + + echo > /etc/exports + exit 0 +} + + +trap stop TERM + +start "$@" + +# Ugly hack to do nothing and wait for SIGTERM +while true; do + read +done diff --git a/examples/nfs/nfs-data/Dockerfile b/examples/nfs/nfs-data/Dockerfile new file mode 100644 index 00000000000..33fd131a5c7 --- /dev/null +++ b/examples/nfs/nfs-data/Dockerfile @@ -0,0 +1,5 @@ +FROM jsafrane/nfsexporter +MAINTAINER Jan Safranek +ADD index.html /mnt/data/index.html + +ENTRYPOINT ["/usr/local/bin/run_nfs", "/mnt/data"] diff --git a/examples/nfs/nfs-data/README.md b/examples/nfs/nfs-data/README.md new file mode 100644 index 00000000000..4b5ab2b8569 --- /dev/null +++ b/examples/nfs/nfs-data/README.md @@ -0,0 +1,7 @@ +# NFS-exporter container with a file + +This container exports /mnt/data with index.html in it via NFSv4. Based on +../exporter. + +Available in dockerhub as +[jsafrane/nfs-data](https://registry.hub.docker.com/u/jsafrane/nfs-data/). \ No newline at end of file diff --git a/examples/nfs/nfs-data/index.html b/examples/nfs/nfs-data/index.html new file mode 100644 index 00000000000..cd0875583aa --- /dev/null +++ b/examples/nfs/nfs-data/index.html @@ -0,0 +1 @@ +Hello world! diff --git a/examples/nfs/nfs-server-pod.yaml b/examples/nfs/nfs-server-pod.yaml new file mode 100644 index 00000000000..e0bb565e6eb --- /dev/null +++ b/examples/nfs/nfs-server-pod.yaml @@ -0,0 +1,15 @@ +apiVersion: v1beta3 +kind: Pod +metadata: + name: nfs-server + labels: + role: nfs-server +spec: + containers: + - name: nfs-server + image: jsafrane/nfs-data + privileged: true + ports: + - name: nfs + containerPort: 2049 + protocol: tcp diff --git a/examples/nfs/nfs-server-service.yaml b/examples/nfs/nfs-server-service.yaml new file mode 100644 index 00000000000..634087122ef --- /dev/null +++ b/examples/nfs/nfs-server-service.yaml @@ -0,0 +1,9 @@ +kind: Service +apiVersion: v1beta3 +metadata: + name: nfs-server +spec: + ports: + - port: 2049 + selector: + role: nfs-server diff --git a/examples/nfs/nfs-web-pod.yaml b/examples/nfs/nfs-web-pod.yaml new file mode 100644 index 00000000000..efc2841c172 --- /dev/null +++ b/examples/nfs/nfs-web-pod.yaml @@ -0,0 +1,27 @@ +# +# This pod imports nfs-server.default.kube.local:/ into /var/www/html +# + +apiVersion: v1beta3 +kind: Pod +metadata: + name: nfs-web +spec: + containers: + - name: web + image: dockerfile/nginx + ports: + - name: web + containerPort: 80 + protocol: tcp + volumeMounts: + # name must match the volume name below + - name: nfs + mountPath: "/var/www/html" + volumes: + - name: nfs + nfs: + # FIXME: use the right hostname + server: nfs-server.default.kube.local + path: "/" + readOnly: false diff --git a/examples/nfs/test.yaml b/examples/nfs/test.yaml deleted file mode 100644 index cfdfface876..00000000000 --- a/examples/nfs/test.yaml +++ /dev/null @@ -1,21 +0,0 @@ -apiVersion: v1beta1 -desiredState: - manifest: - containers: - - name: testpd - image: dockerfile/nginx - volumeMounts: - # name must match the volume name below - - name: myshare - mountPath: "/var/www/html/mount-test" - id: nfspd - version: v1beta1 - volumes: - - name: myshare - source: - nfs: - server: "172.17.0.2" - path: "/tmp" - readOnly: false -id: nfspd -kind: Pod