-
【云原生 | Kubernetes 系列】— Kubernetes存儲(chǔ)方案
-
?? 一、基本存儲(chǔ)
- EmptyDir
- HostPath
-
NFS
- 搭建nfs服務(wù)器
-
?? 二、高級(jí)存儲(chǔ)
- PV和PVC
- pv
- pvc
- 演示 -NFS
-
StorageClass(動(dòng)態(tài)存儲(chǔ))
- StorageClass準(zhǔn)備
- 創(chuàng)建deployment應(yīng)用StorageClass
- 創(chuàng)建StatefulSet應(yīng)用StorageClass
-
??三、配置存儲(chǔ)
-
ConfigMap
- ConfigMap 的創(chuàng)建
- Pod 中使用 ConfigMap
-
Secret
- Service Account
- Opaque Secret
- kubernetes.io/dockerconfigjson
-
ConfigMap
-
?? 一、基本存儲(chǔ)
【云原生 | Kubernetes 系列】— Kubernetes存儲(chǔ)方案
版權(quán) 本文為云錄原創(chuàng)文章,轉(zhuǎn)載無(wú)需和我聯(lián)系,但請(qǐng)注明來(lái)自云錄 https://www.yunzhuan.site
?? 一、基本存儲(chǔ)
EmptyDir
當(dāng)Pod 被分配給節(jié)點(diǎn)時(shí),首先創(chuàng)建 emptyDir 卷,并且只要該 Pod 在該節(jié)點(diǎn)上運(yùn)行,該卷就會(huì)存在。正如卷的名字所述,
它最初是空的。Pod 中的容器可以讀取和寫入 emptyDir 卷中的相同文件,盡管該卷可以掛載到每個(gè)容器中的相同或不同路徑上。
當(dāng)出于任何原因從節(jié)點(diǎn)中刪除 Pod 時(shí), emptyDir 中的數(shù)據(jù)將被永久刪除
用途:
1: 緩存空間,例如基于磁盤的歸并排序。
2: 為耗時(shí)較長(zhǎng)的計(jì)算任務(wù)提供檢查點(diǎn),以便任務(wù)能方便地從崩潰前狀態(tài)恢復(fù)執(zhí)行。
3: 在 Web 服務(wù)器容器服務(wù)數(shù)據(jù)時(shí),保存內(nèi)容管理器容器獲取的文件。
apiVersion: v1
kind: Pod
metadata:
name: volume-emptydir
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
volumeMounts: # 將logs-volume掛載到nginx容器的/var/log/nginx目錄, nginx會(huì)將用戶的訪問(wèn)日志寫入到該目錄的access.log文件中
- name: logs-volume
mountPath: /var/log/nginx
- name: busybox
image: busybox:latest
command: ["/bin/sh", "-c", "tail -f /logs/access.log"]
volumeMounts: # 將logs-volume掛載到busybox容器中的/logs目錄
- name: logs-volume
mountPath: /logs
volumes: # 聲明volume
- name: logs-volume
emptyDir:
sizeLimit: 500Mi
[root@k8s-master01 nfs]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
volume-emptydir 2/2 Running 2 (47s ago) 75s 10.244.1.7 k8s-node1 <none> <none>
[root@k8s-master01 ~]# curl 10.244.1.7
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@k8s-master01 ~]# kubectl logs volume-emptydir -c busybox -f
10.244.0.0 - - [01/Jan/2024:05:20:38 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
HostPath
警告:
HostPath 卷存在許多安全風(fēng)險(xiǎn),最佳做法是盡可能避免使用 HostPath。 當(dāng)必須使用 HostPath 卷時(shí),它的范圍應(yīng)僅限于所需的文件或目錄,并以只讀方式掛載。
如果通過(guò) AdmissionPolicy 限制 HostPath 對(duì)特定目錄的訪問(wèn),則必須要求 volumeMounts 使用 readOnly 掛載以使策略生效。
hostPath 卷能將主機(jī)節(jié)點(diǎn)文件系統(tǒng)上的文件或目錄掛載到你的 Pod 中。 雖然這不是大多數(shù) Pod 需要的,
但是它為一些應(yīng)用程序提供了強(qiáng)大的逃生艙。
例如,hostPath 的一些用法有:
-
運(yùn)行一個(gè)需要訪問(wèn) Docker 內(nèi)部機(jī)制的容器;可使用 hostPath 掛載 /var/lib/docker 路徑。
-
在容器中運(yùn)行 cAdvisor 時(shí),以 hostPath 方式掛載 /sys。
-
允許 Pod 指定給定的 hostPath 在運(yùn)行 Pod 之前是否應(yīng)該存在,是否應(yīng)該創(chuàng)建以及應(yīng)該以什么方式存在。
除了必需的 path 屬性之外,你可以選擇性地為 hostPath 卷指定 type。取值 行為 空字符串(默認(rèn))用于向后兼容,這意味著在安裝 hostPath 卷之前不會(huì)執(zhí)行任何檢查。 DirectoryOrCreate 如果在給定路徑上什么都不存在,那么將根據(jù)需要?jiǎng)?chuàng)建空目錄,權(quán)限設(shè)置為 0755,具有與 kubelet 相同的組和屬主信息 Directory 在給定路徑上必須存在的目錄。 FileOrCreate 如果在給定路徑上什么都不存在,那么將在那里根據(jù)需要?jiǎng)?chuàng)建空文件,權(quán)限設(shè)置為 0644,具有與 kubelet 相同的組和所有權(quán)。 File 在給定路徑上必須存在的文件。 Socket 在給定路徑上必須存在的 UNIX 套接字。 CharDevice 在給定路徑上必須存在的字符設(shè)備。 BlockDevice 在給定路徑上必須存在的塊設(shè)備。
apiVersion: v1
kind: Pod
metadata:
name: test-webserver
spec:
containers:
- name: test-webserver
image: beicheng1892/test-webserver:latest
volumeMounts:
- mountPath: /var/local/aaa
name: mydir
- mountPath: /var/local/aaa/1.txt
name: myfile
volumes:
- name: mydir
hostPath:
# 確保文件所在目錄成功創(chuàng)建。
path: /var/local/aaa
type: DirectoryOrCreate
- name: myfile
hostPath:
path: /var/local/aaa/1.txt
type: FileOrCreate
-rw-r--r-- 1 root root 499 Dec 31 21:40 hostpath.yaml
[root@k8s-master01 nfs]# kubectl apply -f hostpath.yaml
pod/test-webserver created
[root@k8s-master01 nfs]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-webserver 1/1 Running 0 10s 10.244.1.8 k8s-node1 <none> <none>
volume-emptydir 2/2 Running 0 3h6m 10.244.1.7 k8s-node1 <none> <none>
[root@k8s-node1 ~]# cd /var/local/
[root@k8s-node1 local]# ll
total 0
drwxr-xr-x 2 root root 19 Dec 31 21:40 aaa
[root@k8s-node1 local]# cd aaa/
[root@k8s-node1 aaa]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 31 21:40 1.txt
NFS
nfs 卷能將 NFS (網(wǎng)絡(luò)文件系統(tǒng)) 掛載到你的 Pod 中。 不像 emptyDir 那樣會(huì)在刪除 Pod 的同時(shí)也會(huì)被刪除,
nfs 卷的內(nèi)容在刪除 Pod 時(shí)會(huì)被保存,卷只是被卸載。 這意味著 nfs 卷可以被預(yù)先填充數(shù)據(jù),并且這些數(shù)據(jù)可以在 Pod 之間共享。
搭建nfs服務(wù)器
1. 安裝NFS服務(wù)
[root@k8s-node3 ~]# yum -y install rpcbind nfs-utils
2. 準(zhǔn)備共享目錄
[root@k8s-node3 ~]# mkdir -p /data/nfs
[root@k8s-node3 ~]# chmod 777 -R /data/nfs
3. 將共享目錄以讀寫權(quán)限暴露給192.168.0.0/255網(wǎng)段中的所有主機(jī)
[root@k8s-node3 ~]# cat /etc/exports
[root@k8s-node3 ~]# vim /etc/exports
[root@k8s-node3 nfs]# cat /etc/exports
/data/nfs/ *(rw,sync,all_squash)
# 啟動(dòng)服務(wù)
systemctl start rpcbind && systemctl start nfs
# 設(shè)置開機(jī)啟動(dòng)
systemctl enable rpcbind && systemctl enable nfs
[root@k8s-node3 ~]# systemctl start rpcbind && systemctl start nfs
[root@k8s-node3 ~]# systemctl enable rpcbind && systemctl enable nfs
其中no_root_squash表示:NFS客戶端連接服務(wù)端時(shí)如果使用的是root的話,那么對(duì)服務(wù)端共享目錄也擁有root權(quán)限
# 客戶端安裝步驟
yum -y install rpcbind
# 檢測(cè)
showmount -e 192.168.0.105
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: beicheng1892/test-webserver:latest
name: test-container
volumeMounts:
- mountPath: /my-nfs-data
name: test-volume
volumes:
- name: test-volume
nfs:
server: 192.168.0.105
path: /data/nfs
readOnly: true
[root@k8s-master01 nfs]# kubectl apply -f nfs.yaml
pod/test-pd created
[root@k8s-master01 nfs]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-pd 1/1 Running 0 104s 10.244.2.6 k8s-node2 <none> <none>
test-webserver 1/1 Running 0 26m 10.244.1.8 k8s-node1 <none> <none>
volume-emptydir 2/2 Running 0 3h32m 10.244.1.7 k8s-node1 <none> <none>
?? 二、高級(jí)存儲(chǔ)
PV和PVC
- PersistentVolume (PV)
是由管理員設(shè)置的存儲(chǔ),它是群集的一部分。就像節(jié)點(diǎn)是集群中的資源一樣,PV 也是集群中的資源。 PV 是
Volume 之類的卷插件,但具有獨(dú)立于使用 PV 的 Pod 的生命周期。此 API 對(duì)象包含存儲(chǔ)實(shí)現(xiàn)的細(xì)節(jié),即 NFS、
iSCSI 或特定于云供應(yīng)商的存儲(chǔ)系統(tǒng)
- PersistentVolumeClaim (PVC)
是用戶存儲(chǔ)的請(qǐng)求。它與 Pod 相似。Pod 消耗節(jié)點(diǎn)資源,PVC 消耗 PV 資源。Pod 可以請(qǐng)求特定級(jí)別的資源
(CPU 和內(nèi)存)。聲明可以請(qǐng)求特定的大小和訪問(wèn)模式(例如,可以以讀/寫一次或 只讀多次模式掛載)
- 靜態(tài) pv
集群管理員創(chuàng)建一些 PV。它們帶有可供群集用戶使用的實(shí)際存儲(chǔ)的細(xì)節(jié)。它們存在于 Kubernetes API 中,可用
于消費(fèi)
- 動(dòng)態(tài)
當(dāng)管理員創(chuàng)建的靜態(tài) PV 都不匹配用戶的 PersistentVolumeClaim 時(shí),集群可能會(huì)嘗試動(dòng)態(tài)地為 PVC 創(chuàng)建卷。此
配置基于 StorageClasses :PVC 必須請(qǐng)求 [存儲(chǔ)類],并且管理員必須創(chuàng)建并配置該類才能進(jìn)行動(dòng)態(tài)創(chuàng)建。聲明該
類為 "" 可以有效地禁用其動(dòng)態(tài)配置
要啟用基于存儲(chǔ)級(jí)別的動(dòng)態(tài)存儲(chǔ)配置,集群管理員需要啟用 API server 上的 DefaultStorageClass [準(zhǔn)入控制器]
。例如,通過(guò)確保 DefaultStorageClass 位于 API server 組件的 --admission-control 標(biāo)志,使用逗號(hào)分隔的
有序值列表中,可以完成此操作
- 綁定
master 中的控制環(huán)路監(jiān)視新的 PVC,尋找匹配的 PV(如果可能),并將它們綁定在一起。如果為新的 PVC 動(dòng)態(tài)
調(diào)配 PV,則該環(huán)路將始終將該 PV 綁定到 PVC。否則,用戶總會(huì)得到他們所請(qǐng)求的存儲(chǔ),但是容量可能超出要求
的數(shù)量。一旦 PV 和 PVC 綁定后, PersistentVolumeClaim 綁定是排他性的,不管它們是如何綁定的。 PVC 跟
PV 綁定是一對(duì)一的映射
- 持久化卷聲明的保護(hù)
PVC 保護(hù)的目的是確保由 pod 正在使用的 PVC 不會(huì)從系統(tǒng)中移除,因?yàn)槿绻灰瞥脑捒赡軙?huì)導(dǎo)致數(shù)據(jù)丟失
當(dāng)啟用PVC 保護(hù) alpha 功能時(shí),如果用戶刪除了一個(gè) pod 正在使用的 PVC,則該 PVC 不會(huì)被立即刪除。PVC 的
刪除將被推遲,直到 PVC 不再被任何 pod 使用
- 持久化卷類型
PersistentVolume 類型以插件形式實(shí)現(xiàn)。Kubernetes 目前支持以下插件類型:
GCEPersistentDisk AWSElasticBlockStore AzureFile AzureDisk FC (Fibre Channel)
FlexVolume Flocker NFS iSCSI RBD (Ceph Block Device) CephFS
Cinder (OpenStack block storage) Glusterfs VsphereVolume Quobyte Volumes
HostPath VMware Photon Portworx Volumes ScaleIO Volumes StorageOS
pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
nfs: # 存儲(chǔ)類型,與底層真正存儲(chǔ)對(duì)應(yīng)
capacity: # 存儲(chǔ)能力,目前只支持存儲(chǔ)空間的設(shè)置
storage: 2Gi
accessModes: # 訪問(wèn)模式
storageClassName: # 存儲(chǔ)類別
persistentVolumeReclaimPolicy: # 回收策略
-
回收策略(persistentVolumeReclaimPolicy)
當(dāng)PV不再被使用了之后,對(duì)其的處理方式。目前支持三種策略:- Retain (保留) 保留數(shù)據(jù),需要管理員手工清理數(shù)據(jù)
- Recycle(回收) 清除 PV 中的數(shù)據(jù),效果相當(dāng)于執(zhí)行 rm -rf /thevolume/*(1.28 廢棄了)
- Delete (刪除) 與 PV 相連的后端存儲(chǔ)完成 volume 的刪除操作,當(dāng)然這常見(jiàn)于云服務(wù)商的存儲(chǔ)服務(wù)
-
訪問(wèn)模式(accessModes)
用于描述用戶應(yīng)用對(duì)存儲(chǔ)資源的訪問(wèn)權(quán)限,訪問(wèn)權(quán)限包括下面幾種方式:- ReadWriteOnce(RWO):讀寫權(quán)限,但是只能被單個(gè)節(jié)點(diǎn)掛載
- ReadOnlyMany(ROX): 只讀權(quán)限,可以被多個(gè)節(jié)點(diǎn)掛載
- ReadWriteMany(RWX):讀寫權(quán)限,可以被多個(gè)節(jié)點(diǎn)掛載
-
狀態(tài)
卷可以處于以下的某種狀態(tài):- Available(可用)——一塊空閑資源還沒(méi)有被任何聲明綁定
- Bound(已綁定)——卷已經(jīng)被聲明綁定
- Released(已釋放)——聲明被刪除,但是資源還未被集群重新聲明
pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc
namespace: dev
spec:
accessModes: # 訪問(wèn)模式
selector: # 采用標(biāo)簽對(duì)PV選擇
storageClassName: # 存儲(chǔ)類別
resources: # 請(qǐng)求空間
requests:
storage: 5Gi
PVC 的關(guān)鍵配置參數(shù)說(shuō)明:
-
訪問(wèn)模式(accessModes)
? 用于描述用戶應(yīng)用對(duì)存儲(chǔ)資源的訪問(wèn)權(quán)限 -
選擇條件(selector)
通過(guò)Label Selector的設(shè)置,可使PVC對(duì)于系統(tǒng)中己存在的PV進(jìn)行篩選 -
存儲(chǔ)類別(storageClassName) PVC在定義時(shí)可以設(shè)定需要的后端存儲(chǔ)的類別,只有設(shè)置了該class的pv才能被系統(tǒng)選出
-
資源請(qǐng)求(Resources ) 描述對(duì)存儲(chǔ)資源的請(qǐng)求
演示 -NFS
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
nfs:
path: /data/nfs
server: 192.168.0.105
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Pod
metadata:
name: test-web
spec:
containers:
- name: nginx
image: nginx:1.21.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: test-pvc
StorageClass(動(dòng)態(tài)存儲(chǔ))
StorageClass準(zhǔn)備
---
# sc安裝
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
# nfs client
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-client-provisioner
spec:
selector:
matchLabels:
app: nfs-client-provisioner
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
#serviceAccountName的 nfs-client-provisioner 名稱是 ServiceAccount 名稱
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
#image: quay.io/external_storage/nfs-client-provisioner:latest
#這里特別注意,在k8s-1.20以后版本中使用上面提供的包,并不好用,這里我折騰了好久,才解決,后來(lái)在官方的github上,別人提的問(wèn)題中建議使用下面這個(gè)包才解決的,我這里是下載后,傳到我自已的倉(cāng)庫(kù)里
image: gmoney23/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs # 指定分配器的名稱,創(chuàng)建storageclass會(huì)用到
- name: NFS_SERVER
value: 192.168.0.105
- name: NFS_PATH
value: /data/nfs
volumes:
- name: nfs-client-root
nfs:
server: 192.168.0.105
path: /data/nfs
---
# nfs 存儲(chǔ)類
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storage
provisioner: fuseim.pri/ifs #必須與provisioner.yaml中PROVISIONER_NAME的值一致
[root@k8s-master01 nfs]# kubectl get deploy nfs-client-provisioner
NAME READY UP-TO-DATE AVAILABLE AGE
nfs-client-provisioner 1/1 1 1 102s
[root@k8s-master01 nfs]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-54b55f79fd-qspkr 1/1 Running 0 108s
[root@k8s-master01 nfs]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-storage fuseim.pri/ifs Delete Immediate false 115s
創(chuàng)建deployment應(yīng)用StorageClass
---
# 手動(dòng)創(chuàng)建pvpvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-pvc
spec:
storageClassName: nfs-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
---
#創(chuàng)建deployment應(yīng)用NFS
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-1
spec:
selector:
matchLabels:
app: web-1
replicas: 2
template:
metadata:
labels:
app: web-1
spec:
containers:
- name: web-1
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: test-storage
mountPath: /data/html
volumes:
- name: test-storage
persistentVolumeClaim:
#claimName 的 名稱 是 PersistentVolumeClaim 的名字
claimName: test-pvc
[root@k8s-master01 nfs]# kubectl get po
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-876986df-j5c74 1/1 Running 0 118s
web-1-75685c5697-d2qsw 1/1 Running 0 13m
web-1-75685c5697-xftfd 1/1 Running 0 13m
[root@k8s-node3 data]# cd nfs/
[root@k8s-node3 nfs]# ll
total 4
drwxrwxrwx 2 nfsnobody nfsnobody 6 Jan 4 21:42 default-test-pvc-pvc-7c40ad07-b15f-42c6-a551-572b0bff8542
-rw-r--r-- 1 nfsnobody nfsnobody 17 Jan 4 01:39 test.txt
[root@k8s-node3 nfs]# ped
bash: ped: command not found...
[root@k8s-node3 nfs]# pwd
/data/nfs
創(chuàng)建StatefulSet應(yīng)用StorageClass
--- #服務(wù)
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "nfs-storage"
resources:
requests:
storage: 1Gi
[root@k8s-master01 nfs]# kubectl get sts
NAME READY AGE
web 1/3 8s
[root@k8s-master01 nfs]# kubectl get po
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-876986df-j5c74 1/1 Running 0 9m29s
web-0 1/1 Running 0 13s
web-1 1/1 Running 0 9s
web-2 0/1 ContainerCreating 0 3s
[root@k8s-master01 nfs]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pvc-47b8b191-2eaa-4032-9a62-f96f586e5330 1Gi RWO nfs-storage 22s
www-web-1 Bound pvc-a34e3185-8cd9-452b-9ec0-98ae65988b6a 1Gi RWO nfs-storage 18s
www-web-2 Bound pvc-989aff50-0602-468f-a939-0c14df7104f0 1Gi RWO nfs-storage 12s
[root@k8s-master01 nfs]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-47b8b191-2eaa-4032-9a62-f96f586e5330 1Gi RWO Delete Bound default/www-web-0 nfs-storage 23s
pvc-989aff50-0602-468f-a939-0c14df7104f0 1Gi RWO Delete Bound default/www-web-2 nfs-storage 13s
pvc-a34e3185-8cd9-452b-9ec0-98ae65988b6a 1Gi RWO Delete Bound default/www-web-1 nfs-storage 19s
??三、配置存儲(chǔ)
ConfigMap
ConfigMap 功能在 Kubernetes1.2 版本中引入,許多應(yīng)用程序會(huì)從配置文件、命令行參數(shù)或環(huán)境變量中讀取配
置信息。ConfigMap API 給我們提供了向容器中注入配置信息的機(jī)制,ConfigMap 可以被用來(lái)保存單個(gè)屬性,也
可以用來(lái)保存整個(gè)配置文件或者 JSON 二進(jìn)制大對(duì)象
ConfigMap 的創(chuàng)建
Ⅰ、使用目錄創(chuàng)建
$ ls docs/user-guide/configmap/kubectl/
game.properties
ui.properties
$ cat docs/user-guide/configmap/kubectl/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
$ cat docs/user-guide/configmap/kubectl/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl
Ⅱ、使用文件創(chuàng)建
$ kubectl create configmap game-config-2 --from-file=docs/user-
guide/configmap/kubectl/game.properties
$ kubectl get configmaps game-config-2 -o yaml
Ⅲ、使用字面值創(chuàng)建
$ kubectl create configmap special-config --from-literal=special.how=very --from-
literal=special.type=charm
$ kubectl get configmaps special-config -o yaml
Pod 中使用 ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log_level: INFO
---
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: hub.atguigu.com/library/myapp:v1
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom:
- configMapRef:
name: env-config
restartPolicy: Never
Secret
Secret 解決了密碼、token、密鑰等敏感數(shù)據(jù)的配置問(wèn)題,而不需要把這些敏感數(shù)據(jù)暴露到鏡像或者 Pod Spec
中。Secret 可以以 Volume 或者環(huán)境變量的方式使用
Secret 有三種類型:
-
Service Account :用來(lái)訪問(wèn) Kubernetes API,由 Kubernetes 自動(dòng)創(chuàng)建,并且會(huì)自動(dòng)掛載到 Pod 的 /run/secrets/kubernetes.io/serviceaccount 目錄中
-
Opaque :base64編碼格式的Secret,用來(lái)存儲(chǔ)密碼、密鑰等
-
kubernetes.io/dockerconfigjson :用來(lái)存儲(chǔ)私有 docker registry 的認(rèn)證信息
Service Account
$ kubectl run nginx --image nginx
deployment "nginx" created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-3137573019-md1u2 1/1 Running 0 13s
$ kubectl exec nginx-3137573019-md1u2 ls /run/secrets/kubernetes.io/serviceaccount
ca.crt
namespace
token
Opaque Secret
$ echo -n "admin" | base64
YWRtaW4=
$ echo -n "1f2d1e2e67df" | base64
MWYyZDFlMmU2N2Rm
# secrets.yml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: MWYyZDFlMmU2N2Rm
username: YWRtaW4=
kubernetes.io/dockerconfigjson
使用 Kuberctl 創(chuàng)建 docker registry 認(rèn)證的 secret文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-776973.html
$ kubectl create secret docker-registry myregistrykey --docker-server=DOCKER_REGISTRY_SERVER --
docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
secret "myregistrykey" created.
在創(chuàng)建 Pod 的時(shí)候,通過(guò) imagePullSecrets 來(lái)引用剛創(chuàng)建的 myregistrykey
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-776973.html
apiVersion: v1
kind: Pod
metadata:
name: foo
spec:
containers:
- name: foo
image: roc/awangyang:v1
imagePullSecrets:
- name: myregistrykey
如果有什么問(wèn)題,請(qǐng)聯(lián)系作者 beicheng1892@163.com
版權(quán) 本文為云錄原創(chuàng)文章,轉(zhuǎn)載無(wú)需和我聯(lián)系,但請(qǐng)注明來(lái)自云錄 https://www.yunzhuan.site
到了這里,關(guān)于【云原生 | Kubernetes 系列】— Kubernetes存儲(chǔ)方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!