新增nfs docker化容器

This commit is contained in:
KennyLee 2017-05-17 14:27:08 +08:00
parent 163a3e7d2d
commit 4a2a7bac0a
6 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,63 @@
---
layout: post
title: 'Docker Quicktip #4 - Remote volumes'
date: 2014-03-29 22:21:40.000000000 +00:00
categories: []
tags: []
status: publish
type: post
published: true
meta:
dsq_thread_id: '2546228828'
_edit_last: '2'
author:
login: cpuguy83
email: cpuguy83@gmail.com
display_name: cpuguy83
first_name: Brian
last_name: Goff
---
This one builds off the idea of using [data-only containers](http://www.tech-d.net/2013/12/16/persistent-volumes-with-docker-container-as-volume-pattern/ "Persistent volumes with Docker Data-only container pattern"). Let's step into the unknown and add a second host into the mix.
What do you use when you need to share data with containers across hosts?
The answer? Well... as you normally would... NFS (or insert your file share service of choice).
<!--break-->
First, let's startup an NFS server... [it just so happens I created an image for just this purpose](https://index.docker.io/u/cpuguy83/nfs-server/). You should check out the github repo if you want the details in how it works... but essentially all you need to do is add each directory you want to to the end of your run command.
** I should note, this nfs server is not secured or optimized, use at your own risk **
`docker run -d --name nfs --privileged cpuguy83/nfs-server /tmp /home`
Here, the `/tmp` folder and the `/home` folder are being shared by NFS. You can add however many dirs you want, but they must exist on the server.
Now let's fire up the nfs client:
`docker run -d --link nfs:nfs --privileged -v /mnt cpuguy83/nfs-client /home:/mnt`
Here, you specify the s source mount and the mount point in the container as /path/in/server:/mount/to/here. So `/home` on the nfs-server is mounted to `/mnt` on the client.
We are also linking the containers, what's important is that the internal side is called nfs as we are using the env var generated by this link to get the IP of the nfs server.
Now, links don't currently work across docker hosts, so what good does this do? Not much locally (no point in using NFS on a single host)... but you can either use the ambassador pattern or manually provide the env var in the run command (`NFS_PORT_2049_TCP_ADDR`) with the IP of the nfs server when doing multi-host.
When you combine this with using volumes-from things begin to get a bit more powerful.
```bash
# NFS Server
docker run -d -v /tmp ubuntu --name foo bash -c "echo foo &gt; /tmp/foo"
docker run -d --name nfs-server --privileged --volumes-from foo cpuguy83/nfs-server /tmp
docker inspect --format '{{ .NetworkSettings.IPAddress }}' nfs-server
10.0.1.100
```
```bash
# Remote NFS Client
docker run -d --name nfs-client --privileged -e NFS_PORT_2049_TCP_ADDR=10.0.1.100 -v /tmp cpuguy83/nfs-client /tmp:/tmp
docker run --rm --volumes-from nfs-client ubuntu cat /tmp/foo
foo
```
You'll notice you must use `--privileged` for both the nfs-server and client. In the (near) future Docker will have finer grained control of the capabilities available to a specific container and we can just add the required ones here instead of opening up the full `--privileged`.

19
nfs/Dockerfile Executable file
View File

@ -0,0 +1,19 @@
FROM registry.cn-hangzhou.aliyuncs.com/kennylee/ubuntu
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -qq && apt-get install -y nfs-kernel-server runit inotify-tools -qq
RUN mkdir -p /exports
RUN mkdir -p /etc/sv/nfs
ADD nfs.init /etc/sv/nfs/run
ADD nfs.stop /etc/sv/nfs/finish
ADD nfs_setup.sh /usr/local/bin/nfs_setup
RUN echo "nfs 2049/tcp" >> /etc/services
RUN echo "nfs 111/udp" >> /etc/services
VOLUME /exports
EXPOSE 111/udp 2049/tcp
ENTRYPOINT ["/usr/local/bin/nfs_setup"]

21
nfs/README.md Executable file
View File

@ -0,0 +1,21 @@
Docker NFS Server
================
fork for [cpuguy83/docker-nfs-server](https://github.com/cpuguy83/docker-nfs-server)
Usage
----
```bash
docker run -d --name nfs --privileged cpuguy83/nfs-server /path/to/share /path/to/share2 /path/to/shareN
```
```bash
docker run -d --name nfs-client --privileged --link nfs:nfs cpuguy83/nfs-client /path/on/nfs/server:/path/on/client
```
More Info
=========
See [docker-quicktip-4-remote-volumes](./2014-03-29-docker-quicktip-4-remote-volumes.md)

9
nfs/nfs.init Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
. /etc/default/nfs-kernel-server
. /etc/default/nfs-common
rpcbind
service nfs-kernel-server start
exec inotifywait -m /exports

5
nfs/nfs.stop Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
service nfs-kernel-server stop
kill $(pidof rpcbind)
service rsyslog stop

15
nfs/nfs_setup.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/bash
set -e
mounts="${@}"
echo "#NFS Exports" > /etc/exports
for mnt in "${mounts[@]}"; do
src=$(echo $mnt | awk -F':' '{ print $1 }')
mkdir -p $src
echo "$src *(rw,sync,no_subtree_check,fsid=0,no_root_squash)" >> /etc/exports
done
exec runsvdir /etc/sv