Skip to content

Instantly share code, notes, and snippets.

@ccamacho
Created September 27, 2020 08:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccamacho/7e709ae9ac3f7cd353d3cce84146ebe5 to your computer and use it in GitHub Desktop.
Save ccamacho/7e709ae9ac3f7cd353d3cce84146ebe5 to your computer and use it in GitHub Desktop.
example pv/pvc kubeinit

From the service machine run:

# Prepare the share PV folder
mkdir -p /var/nfsshare/test-nfs
chmod -R 777 /var/nfsshare/test-nfs
chown -R nobody:nobody /var/nfsshare/test-nfs

# Create the resources
cat << EOF > ~/test_nfs_pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: test-nfs-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  storageClassName: nfs01testnfs
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /var/nfsshare/test-nfs
    server: 10.0.0.100
EOF

cat << EOF > ~/test_nfs_pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-nfs-pvc
spec:
  volumeName: test-nfs-pv
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs01testnfs
  volumeMode: Filesystem
EOF

cat <<EOF > ~/pod.yaml
{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "name": "nfs-test",
        "labels": {
            "name": "frontendhttp"
        }
    },
    "spec": {
	"containers": [{
            "name": "myfrontend",
            "image": "nak3/nfs-test",
            "imagePullPolicy": "IfNotPresent",
            "ports": [{
                "containerPort": 80,
                "name": "http-server"
            }],
            "volumeMounts": [{
                "mountPath": "/var/nfsshare/testmount",
                "name": "pvol"
            }]
	}],
	"volumes": [{
            "name": "pvol",
            "persistentVolumeClaim": {
                "claimName": "test-nfs-pvc"
            }
	}]
    }
}
EOF

# Add the resources
export KUBECONFIG=~/install_dir/auth/kubeconfig
oc create -f ~/test_nfs_pv.yaml
oc create -f ~/test_nfs_pvc.yaml
oc create -f ~/pod.yaml

# Run some checks
oc get pv
oc get pvc
kubectl get pods
showmount -e 10.0.0.100

# Test the NFS share
podman exec -it nfs-test bash
# As you can see we have the NFS PV mounted in /var/nfsshare/testmount
# we connect to the container and put something in the PV
kubectl exec --stdin --tty nfs-test -- /bin/bash
cd /var/nfsshare/testmount
touch asdf
exit
ls /var/nfsshare/test-nfs/

# You should have there a file named asdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment