mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Merge pull request #6350 from jsafrane/nfs-example
Add NFS export/import pod examples.
This commit is contained in:
		
							
								
								
									
										37
									
								
								examples/nfs/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								examples/nfs/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -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://<the container IP address>/
 | 
			
		||||
    Hello World!
 | 
			
		||||
							
								
								
									
										7
									
								
								examples/nfs/exporter/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								examples/nfs/exporter/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
FROM fedora:21
 | 
			
		||||
MAINTAINER Jan Safranek <jsafrane@redhat.com>
 | 
			
		||||
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"]
 | 
			
		||||
							
								
								
									
										10
									
								
								examples/nfs/exporter/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								examples/nfs/exporter/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -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 ...
 | 
			
		||||
							
								
								
									
										72
									
								
								examples/nfs/exporter/run_nfs
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										72
									
								
								examples/nfs/exporter/run_nfs
									
									
									
									
									
										Executable file
									
								
							@@ -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
 | 
			
		||||
							
								
								
									
										5
									
								
								examples/nfs/nfs-data/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								examples/nfs/nfs-data/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
FROM jsafrane/nfsexporter
 | 
			
		||||
MAINTAINER Jan Safranek <jsafrane@redhat.com>
 | 
			
		||||
ADD index.html /mnt/data/index.html
 | 
			
		||||
 | 
			
		||||
ENTRYPOINT ["/usr/local/bin/run_nfs", "/mnt/data"]
 | 
			
		||||
							
								
								
									
										7
									
								
								examples/nfs/nfs-data/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								examples/nfs/nfs-data/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -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/).
 | 
			
		||||
							
								
								
									
										1
									
								
								examples/nfs/nfs-data/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/nfs/nfs-data/index.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
Hello world!
 | 
			
		||||
							
								
								
									
										15
									
								
								examples/nfs/nfs-server-pod.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								examples/nfs/nfs-server-pod.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
							
								
								
									
										9
									
								
								examples/nfs/nfs-server-service.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								examples/nfs/nfs-server-service.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
kind: Service
 | 
			
		||||
apiVersion: v1beta3
 | 
			
		||||
metadata:
 | 
			
		||||
  name: nfs-server
 | 
			
		||||
spec:
 | 
			
		||||
  ports:
 | 
			
		||||
    - port: 2049
 | 
			
		||||
  selector:
 | 
			
		||||
    role: nfs-server
 | 
			
		||||
							
								
								
									
										27
									
								
								examples/nfs/nfs-web-pod.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								examples/nfs/nfs-web-pod.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
@@ -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
 | 
			
		||||
		Reference in New Issue
	
	Block a user