20201222 kubernetes (四)
課堂資料
NFS
nfs 網路存儲、pod重啟後數據依在
第一步:
install nfs-server
設置掛載路徑
掛載路徑需要創建出來
第二步:在其他 node 也安裝 nfs
第三步:啟動nfs-server
第四步:在 k8s 集群部署應用使用 nfs 以持久網路儲存
課堂練習
NFS
安裝 NFS Server
yum install nfs-utils
設定分享目錄
mkdir -p /var/nfsshare
chmod -R 777 /var/nfsshare/
開啟 /etc/exports 檔案
vim /etc/exports

啟動 NFS Server, 設定開機自動執行及在 firewalld 開放 NFS
systemctl restart rpcbind
systemctl restart nfs-server
systemctl status rpcbind

systemctl status nfs-server
cd /var/nfsshare/

ls
touch 1 2 3
ls

cd
gedit pv.yaml pvc.yaml
ls /var/nfsshare/
kubectl apply -f pv.yaml
kubectl get pv
cd /var/nfsshare/
ls

echo "hi" > hi.htm
cd
ls
kubectl apply -f pvc.yaml
kubectl get pods -o wide
kubectl get deployment
kubectl delete deployment httpd

kubectl get deployment
kubectl get pod -o wide
kubectl get deployment
kubectl describe deployment httpd-dep2

kubectl get pvc
kubectl get deployment
kubectl get deployment
kubectl get svc
kubectl delete svc httpd
kubectl delete svc httpd2
kubectl get svc
kubectl get pod

kubectl delete pod --all
kubectl get pod
kubectl describe deployment httpd-dep2

kubectl get pod -o wide

ls
kubectl get pvc
kubectl get deployment
kubectl describe deployment httpd-dep2

kubectl get pod
gedit pvc.yaml
pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-dep2
spec:
selector:
matchLabels:
app: httpd
replicas: 1
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.46
ports:
- containerPort: 80
# volumeMounts:
# - name: wwwroot
# mountPath: /usr/local/apache2/htdocs
# ports:
# - containerPort: 80
# volumes:
# - name: wwwroot
# persistentVolumeClaim:
# claimName: my-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
kubectl apply -f pvc.yaml
kubectl get deployment

kubectl get pod
kubectl get pod -o wide
curl [podID]
gedit pvc.yaml
pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-dep2
spec:
selector:
matchLabels:
app: httpd
replicas: 1
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.46
ports:
- containerPort: 80
# volumeMounts:
# - name: wwwroot
# mountPath: /usr/local/apache2/htdocs
# ports:
# - containerPort: 80
volumes:
- name: wwwroot
persistentVolumeClaim:
claimName: my-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi

kubectl apply -f pvc.yaml
kubectl get deployment
kubectl get pod -o wide
curl [podID]

gedit pvc.yaml
pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-dep2
spec:
selector:
matchLabels:
app: httpd
replicas: 1
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.46
ports:
- containerPort: 80
# volumeMounts:
# - name: wwwroot
# mountPath: /usr/local/apache2/htdocs
ports:
- containerPort: 80
volumes:
- name: wwwroot
persistentVolumeClaim:
claimName: my-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
kubectl apply -f pvc.yaml
kubectl get deployment
kubectl get pod -o wide

curl [podID]
kubectl get pod
kubectl exec [podName] -it -- bash
cd htdocs/
ls
pwd
exit
kubectl get svc

gedit pvc.yaml
pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-dep2
spec:
selector:
matchLabels:
app: httpd
replicas: 1
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.46
ports:
- containerPort: 80
volumeMounts:
- name: wwwroot
mountPath: /usr/local/apache2/htdocs
ports:
- containerPort: 80
volumes:
- name: wwwroot
persistentVolumeClaim:
claimName: my-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
kubectl apply -f pvc.yaml
kubectl get deployment

kubectl get pod
kubectl get pods

kubectl describe deployment httpd-dep2

cd ..
ls
cd
mkdir pv
cd pv
gedit 1.yaml 2.yaml 3.yaml &
1.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
storageClassName: test
nfs:
path: /var/nfsshare
server: 192.168.8.129
2.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: test
3.yaml
apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: my-pvc
containers:
- name: task-pv-container
image: httpd:2.4.46
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/local/apache2/htdocs"
name: task-pv-storage

kubectl apply -f 1.yaml
kubectl get pv
kubectl apply -f 2.yaml
kubectl get pvc
kubectl apply -f 3.yaml
kubectl get pod
kubectl get pod -o wide

cd /var/nfsshare/
ls
curl [podID]/hi.htm
echo "abc" > abc.htm
curl [podID]/abc.htm
ls
kubectl get pod
kubectl delete pod task-pv-pod
kubectl get pod

cd
cd pv
kubectl apply -f 3.yaml
kubectl get pod
kubectl get pod -o wide
curl [podID]/hi.htm
curl [podID]/abc.htm

ConfigMap
cd
mkdir /cm
cd cm
gedit myconfigmap.yaml

cd testcm
gedit mysql.conf redis.conf
cat mysql.conf
cat redis.conf

cd ..
kubectl create configmap cm-demo1 --from-file=testcm
kubectl get cm
kubectl describe cm cm-demo1
kubectl create configmap cm-demo3 --from-literal=db.host=localhost --from-literal=db.port=3306
kubectl get cm

kubectl describe cm cm-demo3
gedit testpod.yaml

kubectl apply -f testpod.yaml
kubectl get pod
kubectl delete pod --all
gedit testpod.yaml

kubectl apply -f testpod.yaml
kubectl get pod
kubectl logs testcm2-pod

Last updated
Was this helpful?