> For the complete documentation index, see [llms.txt](https://anida-huang.gitbook.io/cloud-communication/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anida-huang.gitbook.io/cloud-communication/qi-mo/20201222-kubernetes.md).

# 20201222 kubernetes (四)

## 課堂資料

{% embed url="<https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-persistent-volume-storage/>" %}

{% embed url="<https://www.opencli.com/linux/rhel-centos-7-install-nfs-server>" %}

{% embed url="<https://www.qikqiak.com/k8s-book/docs/28.ConfigMap.html>" %}

### NFS

#### nfs 網路存儲、pod重啟後數據依在

* **第一步：**
  * **install nfs-server**
  * **設置掛載路徑**
  * **掛載路徑需要創建出來**
* **第二步：在其他 node 也安裝 nfs**
* **第三步：啟動nfs-server**
* **第四步：在 k8s 集群部署應用使用 nfs 以持久網路儲存**

## 課堂練習

### NFS

{% tabs %}
{% tab title="vm1" %}

> #### 安裝 NFS **Server**&#x20;

```
yum install nfs-utils
```

> #### 設定分享目錄

```
mkdir -p /var/nfsshare
```

```
chmod -R 777 /var/nfsshare/
```

> #### 開啟 /etc/exports 檔案

```
vim /etc/exports
```

{% hint style="info" %}

```
/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)
```

{% endhint %}

![](/files/-MPQhy6xbaTy7w5wmL1S)

> #### 啟動 NFS Server, 設定開機自動執行及在 firewalld 開放 NFS

```
systemctl restart rpcbind
```

```
systemctl restart nfs-server
```

```
systemctl status rpcbind
```

![](/files/-MPQiKhxnZgCwU9Zyq17)

```
systemctl status nfs-server
```

```
cd /var/nfsshare/
```

![](/files/-MPQiVPd_A6y6kwDXF8r)

```
ls
```

```
touch 1 2 3
```

```
ls
```

![](/files/-MPQosbHQbMidu4ms5a5)

```
cd
```

```
gedit pv.yaml pvc.yaml
```

{% hint style="info" %}
**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
```

{% endhint %}

```
ls /var/nfsshare/
```

```
kubectl apply -f pv.yaml
```

```
kubectl get pv
```

```
cd /var/nfsshare/
```

```
ls
```

![](/files/-MPQu2xTv8gcVIJLfnd7)

```
echo "hi" > hi.htm
```

```
cd
```

```
ls
```

```
kubectl apply -f pvc.yaml
```

```
kubectl get pods -o wide
```

```
kubectl get deployment
```

```
kubectl delete deployment httpd
```

![](/files/-MPQuUI7Mky7KyG4XbMB)

```
kubectl get deployment
```

```
kubectl get pod -o wide
```

```
kubectl get deployment
```

```
kubectl describe deployment httpd-dep2
```

![](/files/-MPQuvi1iJ4WTVyOuGGd)

```
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
```

![](/files/-MPQvOac606fEfePebLz)

```
kubectl delete pod --all
```

```
kubectl get pod
```

```
kubectl describe deployment httpd-dep2
```

![](/files/-MPQw7-oi4ZLGqBvaJVW)

```
kubectl get pod -o wide
```

![](/files/-MPQwH7lq-6vZ5tottsu)

```
ls
```

```
kubectl get pvc
```

```
kubectl get deployment
```

```
kubectl describe deployment httpd-dep2
```

![](/files/-MPR3yXTLam7G2yjEP55)

```
kubectl get pod
```

```
gedit pvc.yaml
```

{% hint style="info" %}

#### 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
```

{% endhint %}

```
kubectl apply -f pvc.yaml
```

```
kubectl get deployment
```

![](/files/-MPR4fcUZiY5znRvvA6T)

```
kubectl get pod
```

```
kubectl get pod -o wide
```

```
curl [podID]
```

```
gedit pvc.yaml
```

{% hint style="info" %}

#### 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
```

{% endhint %}

![](/files/-MPR59MqknFnyznucHY2)

```
kubectl apply -f pvc.yaml
```

```
kubectl get deployment
```

```
kubectl get pod -o wide
```

```
curl [podID]
```

![](/files/-MPR5jhO_GvHLEA-6P_I)

```
gedit pvc.yaml
```

{% hint style="info" %}

#### 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
```

{% endhint %}

```
kubectl apply -f pvc.yaml
```

```
kubectl get deployment
```

```
kubectl get pod -o wide
```

![](/files/-MPR68sHJU2zc2WPeqAs)

```
curl [podID]
```

```
kubectl get pod
```

```
kubectl exec [podName] -it -- bash
```

```
cd htdocs/
```

```
ls
```

```
pwd
```

```
exit
```

```
kubectl get svc
```

![](/files/-MPR6vub8FdgkUYTihVs)

```
gedit pvc.yaml
```

{% hint style="info" %}

#### 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
```

{% endhint %}

```
kubectl apply -f pvc.yaml
```

```
kubectl get deployment
```

![](/files/-MPR7Nfk2c76P5Z3DyZq)

```
kubectl get pod
```

```
kubectl get pods
```

![](/files/-MPRAhx0rXED2jMk3C0T)

```
kubectl describe deployment httpd-dep2
```

![](/files/-MPRD94iQiP6IWqku2CQ)

```
cd ..
```

```
ls
```

```
cd
```

```
mkdir pv
```

```
cd pv
```

```
gedit 1.yaml 2.yaml 3.yaml &
```

{% hint style="info" %}

#### 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
```

{% endhint %}

![](/files/-MPRDb9qK7-SqaDabQOg)

```
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
```

![](/files/-MPRJ-613HUqDo5YPTxB)

```
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
```

![](/files/-MPRKleP-CRtEtwXzwjN)

```
cd
```

```
cd pv
```

```
kubectl apply -f 3.yaml
```

```
kubectl get pod
```

```
kubectl get pod -o wide
```

```
curl [podID]/hi.htm
```

```
curl [podID]/abc.htm
```

![](/files/-MPRLE0vyAdB4cG1B4M5)
{% endtab %}

{% tab title="vm2" %}

> #### 安裝 NFS**Client**&#x20;

```
yum install nfs-utils
```

> #### 建立 NFS 目錄掛載點

```
mkdir -p /mnt/nfs/var/nfsshare
```

> #### mount 指令掛載 192.168.8.129分享出來的目錄

```
mount -t nfs 192.168.8.129:/var/nfsshare /mnt/nfs/var/nfsshare/
```

```
cd /mnt/nfs/var/nfsshare/
```

```
touch a b cd
```

```
ls
```

![](/files/-MPQlpgh1D-fe7pQXWjY)

```
ls
```

![](/files/-MPQnNJs3Kq-BUKvGWvv)

```
cd
```

```
umount /mnt/nfs/var/nfsshare/
```

```
ls /mnt/nfs/var/nfsshare/
```

```
docker images
```

![](/files/-MPR-hIxiU21zL5_nHXv)
{% endtab %}

{% tab title="vm3" %}

> #### 安裝 NFS**Client**&#x20;

```
yum install nfs-utils
```

> #### 建立 NFS 目錄掛載點

```
mkdir -p /mnt/nfs/var/nfsshare
```

> #### mount 指令掛載 192.168.8.129分享出來的目錄

```
mount -t nfs 192.168.8.129:/var/nfsshare /mnt/nfs/var/nfsshare/
```

```
cd /mnt/nfs/var/nfsshare/
```

```
ls
```

```
cd
```

```
umount /mnt/nfs/var/nfsshare/
```

```
ls /mnt/nfs/var/nfsshare/
```

{% endtab %}
{% endtabs %}

### ConfigMap

{% tabs %}
{% tab title="vm1" %}

```
cd
```

```
mkdir /cm
```

```
cd cm
```

```
gedit myconfigmap.yaml
```

{% hint style="info" %}

```
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
```

{% endhint %}

![](/files/-MPRgc61vWqpHswjY9jk)

```
cd testcm
```

```
gedit mysql.conf redis.conf
```

{% hint style="info" %}

#### mysql.conf

```
host=127.0.0.1
port=3306
```

#### redis.conf

```
host=127.0.0.1
port=6379
```

{% endhint %}

```
cat mysql.conf
```

```
cat redis.conf
```

![](/files/-MPRliRfIKS5bYAiSNAu)

```
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
```

![](/files/-MPRno1i17ANgSmN_Hbw)

```
kubectl describe cm cm-demo3
```

```
gedit testpod.yaml
```

{% hint style="info" %}

```
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
```

{% endhint %}

![](/files/-MPRp4a0pn8qAT527xcu)

```
kubectl apply -f testpod.yaml
```

```
kubectl get pod
```

```
kubectl delete pod --all
```

```
gedit testpod.yaml
```

{% hint style="info" %}

```
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
```

{% endhint %}

![](/files/-MPRqQgnWRd4U7az9Sw5)

```
kubectl apply -f testpod.yaml
```

```
kubectl get pod
```

```
kubectl logs testcm2-pod
```

![](/files/-MPRqrt5LaNgGdvYdhf3)
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://anida-huang.gitbook.io/cloud-communication/qi-mo/20201222-kubernetes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
