今天來講儲存篇,
為了撐滿30天,保險起見,儲存篇,依據內容順序分2天po
真怕PO錯篇@@~金恐布
Kubernetes Volume主要解決2個需求:
參考:
https://kubernetes.io/docs/concepts/storage/
為了作Managing Storage,PersistentVolume提供2個API resources:PV、PVC
另外寫一個volumes的yaml,用$ kubectl create -f volumes.yaml,
要簡化的話也可以跟deployment寫在一起
層級較小POD,PVC 去用 PV 的資源,要求size、讀/寫模式(accessModes)、使用次數
PV、PVC都獨立於pod lifecycles之外
kind:PersistentVolume
apiVersion:v1
metadata:
name:task-pv-volume
labels:
type:local
spec:
storageClassName:manual
capacity:
sotrage:10Gi
accessModes:
-ReadWriteOnce
hostPath:
path:"/mnt/data"
$ kubectl create -f local-volumes.yaml
kind:PersistentVolumeClaim # 用Claim去PV找有無符合需求的,有就分配,沒有就報錯
apiVersion:v1
metadata:
name:task-pv-claim # 跟pv要資源
spec:
storageClassName:manual
accessModes:
-ReadWriteOnce
resources:
requests:
storage:2Gi
$ kubectl apply -f nginx-deployment.yaml
$ minikube service nginx --url # 看連結,用browser或curl跑看看
我們來找個範例,google關鍵字「kubernetes wordpress deployment」
因為wordpress會用到資料庫,這裡介紹的東西其實都寫在deployment
所以這樣一查,不只yaml檔,連指令都有
所以我找到了官網文件 = =…
https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/
完整步驟請看文件喔,我只截要說明的
apiVersion: v1
kind: Service # 我要在pod跑1個container
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim # PVC
metadata:
name: mysql-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector: # 前面介紹過的selector,所以設定label很重要
matchLabels:
app: wordpress # 前端ap
tier: mysql # 後端資料庫,放wordpress的資料
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef: # 寫在secrets裡面,以後在介紹
name: mysql-pass
key: password # 因為yaml檔可能會push到github,總不能把password放進去吧?
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage # 也是要取名字的
mountPath: /var/lib/mysql # mysql預設放data的地方
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim # claim也是要取名字的,以後可下$ kubectl get pvc
# 把MySQL的password放在secret
# secret 後面還會介紹
# secret object name: mysql-pass
# key: password
# 範例上面有喔
$ kubectl create secret generic mysql-pass --from-literal=password=YOUR_PASSWORD
$ kubectl get secrets
# deploy mysql
$ kubectl create -f https://k8s.io/examples/application/wordpress/mysql-deployment.yaml
# 各種delete
$ kubectl delete secret mysql-pass
$ kubectl delete deployment -l app=mysql
$ kubectl delete service -l app=mysql
# 連PersistentVolumeClaims(PVC)也能刪,動態配置的PersistentVolumes也會自動刪除。
$ kubectl delete pvc -l app=mysql
範例請參考:
https://github.com/kubernetes/examples/tree/master/staging/volumes
掛一個空的目錄,同一個Pod中container都可以存取此目錄,Pod刪掉就沒了
常用來當暫存區,/tmp
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /cache # 掛到/cache
name: cache-volume
volumes:
- name: cache-volume
emptyDir: {} # 1、空目錄寫法
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
# 其他的type
# DirectoryOrCreate:沒果沒這個目錄,會自動建一個空目錄,權限是0755
# FileOrCreate:如果沒有這個檔案,會建一個空檔案,權限0644
# Socket:A UNIX socket
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
annotations:
"volume.alpha.kubernetes.io/node-affinity": '{
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{ "matchExpressions": [
{ "key": "kubernetes.io/hostname",
"operator": "In",
"values": ["example-node"]
}
]}
]}
}'
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage # <=== here
local: # <=== here
path: /mnt/disks/ssd1 # <=== here
要掛iSCSI,當然要有個iSCSI server
家裡有NAS的可以玩看看,
iSCSI initiator(windows內建,macOS還要另外買(globalSAN initiator)
有遇到這情境再參考範例:
https://github.com/kubernetes/examples/tree/master/staging/volumes/iscsi