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
/data/ 192.168.8.0/24(rw,sync,no_root_squash,no_all_squash)
/var/nfsshare/ 192.168.8.0/24(rw,sync,no_root_squash,no_all_squash)

啟動 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

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /var/nfsshare/
    server: 192.168.8.129

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
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
kind: ConfigMap
apiVersion: v1
metadata:
  name: cm-demo
  namespace: default
data:
  data.1: hello
  data.2: world
  config: |
    property.1=value-1
    property.2=value-2
    property.3=value-3
cd testcm
gedit mysql.conf redis.conf

mysql.conf

host=127.0.0.1
port=3306

redis.conf

host=127.0.0.1
port=6379
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
apiVersion: v1
kind: Pod
metadata:
  name: testcm1-pod
spec:
  containers:
    - name: testcm
      image: busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.host
        - name: DB_PORT
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.port
      envFrom:
        - configMapRef:
            name: cm-demo1
kubectl apply -f testpod.yaml
kubectl get pod
kubectl delete pod --all
gedit testpod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: testcm2-pod
spec:
  containers:
    - name: testcm2
      image: busybox
      command: [ "/bin/sh", "-c", "echo $(DB_HOST) $(DB_PORT)" ]
      env:
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.host
        - name: DB_PORT
          valueFrom:
            configMapKeyRef:
              name: cm-demo3
              key: db.port
kubectl apply -f testpod.yaml
kubectl get pod
kubectl logs testcm2-pod

Last updated

Was this helpful?