Note: change hostPath
in nfs-server-deployment-service.yaml
.
Last active
July 22, 2022 16:06
-
-
Save kbakk/097453c445a72e99573a5089c1c3d0dc to your computer and use it in GitHub Desktop.
docker-nfs-server-kubernetes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: PersistentVolume | |
metadata: | |
name: nfs-media-pv | |
spec: | |
capacity: | |
storage: 1Mi | |
accessModes: | |
- ReadOnlyMany | |
nfs: | |
path: "/mnt/media" | |
# kubectl get service nfs-server -o json | jq ".spec.clusterIP" | |
server: "10.101.180.186" | |
# server: nfs-server.default.svc.cluster.local | |
mountOptions: | |
- nfsvers=4 | |
--- | |
apiVersion: v1 | |
kind: PersistentVolumeClaim | |
metadata: | |
name: nfs-media | |
spec: | |
accessModes: | |
- ReadOnlyMany | |
storageClassName: "" | |
resources: | |
requests: | |
storage: 1Mi | |
volumeName: nfs-media-pv | |
--- | |
apiVersion: v1 | |
kind: PersistentVolume | |
metadata: | |
name: nfs-stage-pv | |
spec: | |
capacity: | |
storage: 1Mi | |
accessModes: | |
- ReadWriteMany | |
nfs: | |
path: "/mnt/stage" | |
# kubectl get service nfs-server -o json | jq ".spec.clusterIP" | |
server: "10.101.180.186" | |
# server: nfs-server.default.svc.cluster.local | |
mountOptions: | |
- nfsvers=4 | |
--- | |
apiVersion: v1 | |
kind: PersistentVolumeClaim | |
metadata: | |
name: nfs-stage | |
spec: | |
accessModes: | |
- ReadWriteMany | |
storageClassName: "" | |
resources: | |
requests: | |
storage: 1Mi | |
volumeName: nfs-stage-pv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kind: Service | |
apiVersion: v1 | |
metadata: | |
name: nfs-server | |
spec:apiVersion: batch/v1 | |
kind: Job | |
metadata: | |
name: nfs-client-job | |
spec: | |
backoffLimit: 0 | |
template: | |
spec: | |
containers: | |
- image: busybox | |
resources: | |
limits: | |
cpu: 100m | |
memory: 100Mi | |
command: | |
- sh | |
- -c | |
- 'set -e; find /mnt/; date > /mnt/stage/nfs-client-job-$(date +%Y-%m-%dT%H%M%S)' | |
imagePullPolicy: IfNotPresent | |
name: busybox | |
volumeMounts: | |
- name: nfs-stage-vol | |
mountPath: "/mnt/stage" | |
- name: nfs-media-vol | |
mountPath: "/mnt/media" | |
restartPolicy: Never | |
terminationGracePeriodSeconds: 3 | |
volumes: | |
- name: nfs-media-vol | |
persistentVolumeClaim: | |
claimName: nfs-media | |
- name: nfs-stage-vol | |
persistentVolumeClaim: | |
claimName: nfs-stage | |
ports: | |
- name: nfs-tcp | |
port: 2049 | |
- name: nfs-udp | |
protocol: UDP | |
port: 2049 | |
- name: 32765-tcp | |
port: 32765 | |
- name: 32765-udp | |
protocol: UDP | |
port: 32765 | |
- name: 32767-tcp | |
port: 32767 | |
- name: 32767-udp | |
protocol: UDP | |
port: 32767 | |
- name: rpcbind-tcp | |
port: 111 | |
- name: rpcbind-udp | |
protocol: UDP | |
port: 111 | |
selector: | |
role: nfs-server | |
--- | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: nfs-server | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
role: nfs-server | |
template: | |
metadata: | |
labels: | |
role: nfs-server | |
spec: | |
containers: | |
- name: nfs-server | |
image: erichough/nfs-server | |
env: | |
- name: NFS_EXPORT_1 | |
value: '/mnt/media *(rw,sync,no_subtree_check,fsid=100)' | |
- name: NFS_EXPORT_2 | |
# NOTE: must have different fsid, else it will be interpreted as the same | |
value: '/mnt/stage *(rw,sync,no_subtree_check,fsid=101)' | |
- name: NFS_LOG_LEVEL | |
value: DEBUG # doesn't debug client connections for some reason | |
securityContext: | |
privileged: true | |
volumeMounts: | |
- mountPath: /mnt/media | |
name: nfs-media-src | |
- mountPath: /mnt/stage | |
name: nfs-stage-src | |
initContainers: | |
- name: init-identify | |
image: busybox | |
command: ['sh', '-c', 'echo > /mnt/stage/$(date "+%Y-%m-%d %H:%M:%S")_$(hostname)'] | |
volumeMounts: | |
- mountPath: /mnt/stage | |
name: nfs-stage-src | |
volumes: | |
- name: nfs-media-src | |
hostPath: | |
path: /Users/krisb/Library/Caches/docker-share/media-vol | |
type: Directory | |
- name: nfs-stage-src | |
emptyDir: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euxo pipefail | |
OP=$1 | |
function _clear_workloads() { | |
kubectl get jobs | grep nfs-client-job | cut -f 1 -d" " | xargs kubectl delete jobs --timeout 1s || true | |
kubectl get pods | grep nfs-client-job- | cut -f 1 -d" " | xargs kubectl delete pods --force --timeout 1s || true | |
} | |
function _down () { | |
_clear_workloads | |
kubectl delete -f nfs-pv-pvc.yaml --timeout 1s || true | |
kubectl get pvc nfs-media-pv && exit 1 || true | |
kubectl delete -f nfs-server-deployment-service.yaml --timeout 1s || true | |
} | |
function _up () { | |
kubectl apply -f nfs-server-deployment-service.yaml | |
export NFS_SERVER_IP=$(kubectl get service nfs-server -o json | jq ".spec.clusterIP") | |
yq eval '. | select(.kind == "PersistentVolume") as $pv | |
| select(.kind != "PersistentVolume") as $oth | |
| $pv.spec.nfs.server = env(NFS_SERVER_IP) | |
| ($pv, $oth)' \ | |
nfs-pv-pvc.yaml | kubectl apply -f - | |
} | |
function _test () { | |
TS=$(date +%Y%m%d-%H%M%S) yq eval '.metadata.name += "-" + env(TS)' \ | |
test-nfs-client-job.yaml | kubectl apply -f - | |
} | |
case $OP in | |
down) | |
_down | |
;; | |
up) | |
_up | |
;; | |
test) | |
_test | |
;; | |
clear-test) | |
_clear_workloads | |
;; | |
*) | |
# pass | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: batch/v1 | |
kind: Job | |
metadata: | |
name: nfs-client-job | |
spec: | |
backoffLimit: 0 | |
template: | |
spec: | |
containers: | |
- image: busybox | |
resources: | |
limits: | |
cpu: 100m | |
memory: 100Mi | |
command: | |
- sh | |
- -c | |
- 'set -e; find /mnt/; date > /mnt/stage/nfs-client-job-$(date +%Y-%m-%dT%H%M%S)' | |
imagePullPolicy: IfNotPresent | |
name: busybox | |
volumeMounts: | |
- name: nfs-stage-vol | |
mountPath: "/mnt/stage" | |
- name: nfs-media-vol | |
mountPath: "/mnt/media" | |
restartPolicy: Never | |
terminationGracePeriodSeconds: 3 | |
volumes: | |
- name: nfs-media-vol | |
persistentVolumeClaim: | |
claimName: nfs-media | |
- name: nfs-stage-vol | |
persistentVolumeClaim: | |
claimName: nfs-stage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment