포스팅 목차
# NFS Server/Client 세팅
# NFS Server/Client 세팅
# ----------------------------------
# master node : NFS Server
# worker node : NFS Client
# ----------------------------------
# master node : NFS Server 세팅
ps0107@k8smaster1:~$ sudo apt-get update && sudo apt-get install -y nfs-kernel-server
ps0107@k8smaster1:~$ sudo mkdir /opt/sfw
ps0107@k8smaster1:~$ sudo chmod 1777 /opt/sfw
ps0107@k8smaster1:~$ sudo bash -c 'echo software > /opt/sfw/hello.txt'
ps0107@k8smaster1:~$
ps0107@k8smaster1:~$ sudo vi /etc/exports
ps0107@k8smaster1:~$ cat /etc/exports
/opt/sfw/ *(rw,sync,no_root_squash,subtree_check)
ps0107@k8smaster1:~$ sudo exportfs -ra
# worker node : NFS Client 세팅
ps0107@k8sworker1:~$ sudo apt-get -y install nfs-common
ps0107@k8sworker1:~$ showmount -e k8smaster
Export list for k8smaster:
/opt/sfw *
ps0107@k8sworker1:~$ sudo mount k8smaster:/opt/sfw /mnt
ps0107@k8sworker1:~$ ls -l /mnt
total 4
-rw-r--r-- 1 root root 9 Feb 3 14:12 hello.txt
# Creating a Persistent NFS Volume (PV)
# PV 생성을 위한 yaml 파일 생성
ps0107@k8smaster1:~$ cat PVol.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pvvol-1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /opt/sfw
server: k8smaster
readOnly: false
# PV 생성
ps0107@k8smaster1:~$ kubectl create -f PVol.yaml
persistentvolume/pvvol-1 created
# PV 생성 확인
ps0107@k8smaster1:~$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvvol-1 1Gi RWX Retain Available 7s
# Creating a Persistent Volume Claim (PVC)
# pvc 리소스 확인. 현재는 아무것도 없음.
ps0107@k8smaster1:~$ kubectl get pvc
No resources found.
# pvc 생성을 위한 yaml 파일 생성
# pvc 200Mi 요청
ps0107@k8smaster1:~$ vi pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-one
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
# pvc 생성
ps0107@k8smaster1:~$ kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
# pvc-one Bound 상태 확인
ps0107@k8smaster1:~$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-one Bound pvvol-1 1Gi RWX 7s
# pvvol-1 이 CLAIM 에 보면 pvc-one 에 할당됨을 확인 가능
ps0107@k8smaster1:~$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvvol-1 1Gi RWX Retain Bound default/pvc-one 9m6s
# 실제 pvc를 사용하기 위해 새로운 deployment를 생성해 보자.
ps0107@k8smaster1:~$ vi nfs-pod.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
generation: 1
labels:
run: nginx
name: nginx-nfs
namespace: default
resourceVersion: "1411"
spec:
replicas: 1
selector:
matchLabels:
run: nginx
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: nginx
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
volumeMounts:
- name: nfs-vol
mountPath: /opt
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumes: # <- volumes 설정
- name: nfs-vol
persistentVolumeClaim:
claimName: pvc-one
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
# deployment 생성
ps0107@k8smaster1:~$ kubectl create -f nfs-pod.yaml
deployment.apps/nginx-nfs created
# pod 생성 확인
ps0107@k8smaster1:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
ds-one-65r2h 1/1 Running 0 11h
ds-one-hpx96 1/1 Running 0 11h
nginx-nfs-86845f4d55-fxqkt 1/1 Running 0 5s
# 생성된 pod에 mount, volume 확인
ps0107@k8smaster1:~$ kubectl describe pod nginx-nfs-86845f4d55-fxqkt
Name: nginx-nfs-86845f4d55-fxqkt
Namespace: default
Priority: 0
Node: k8sworker1/10.146.0.4
Start Time: Mon, 03 Feb 2020 14:51:15 +0000
Labels: pod-template-hash=86845f4d55
run=nginx
Annotations: cni.projectcalico.org/podIP: 192.168.1.84/32
Status: Running
IP: 192.168.1.84
Controlled By: ReplicaSet/nginx-nfs-86845f4d55
Containers:
nginx:
Container ID: docker://e3eb8a022421675c8a323dea8c1c4b6eb9a5f2b060f4231cd587a6bd821c7ff1
Image: nginx
Image ID: docker-pullable://nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 03 Feb 2020 14:51:19 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/opt from nfs-vol (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-76w5h (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
nfs-vol:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: pvc-one
ReadOnly: false
default-token-76w5h:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-76w5h
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 22s default-scheduler Successfully assigned default/nginx-nfs-86845f4d55-fxqkt to k8sworker1
Normal Pulling 21s kubelet, k8sworker1 Pulling image "nginx"
Normal Pulled 18s kubelet, k8sworker1 Successfully pulled image "nginx"
Normal Created 18s kubelet, k8sworker1 Created container nginx
Normal Started 18s kubelet, k8sworker1 Started container nginx
# pvc 상태를 확인 해본다. 정상적으로 bound 되었는지 확인 가능
ps0107@k8smaster1:~$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-one Bound pvvol-1 1Gi RWX 2m7s