iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 19
1
Kubernetes

在地端建置Angular+ASP.NET Core的DevOps環境系列 第 19

day19_k8s05_儲存篇_上集_Volumes(Storage)

  • 分享至 

  • xImage
  •  

前言

今天來講儲存篇,
為了撐滿30天,保險起見,儲存篇,依據內容順序分2天po
真怕PO錯篇@@~金恐布

Storage(Volumes)

Kubernetes Volume主要解決2個需求:

  • 在container中的資料是短暫的,當container掛掉的時候,kubelet會重啟container,檔案就不見囉~
  • 1個pod可以跑n個container,這些container通常需要共用檔案

參考:
https://kubernetes.io/docs/concepts/storage/
為了作Managing Storage,PersistentVolume提供2個API resources:PV、PVC

PersistentVolume (PV) 你家開一口「井」

另外寫一個volumes的yaml,用$ kubectl create -f volumes.yaml,
要簡化的話也可以跟deployment寫在一起

  • 1、在cluster級中由administrator配置的storage
  • 2、或者當pvc要求時動態配置(dynamically provisioned),當pvc刪掉時也會自動消失

PV定義要mount外部的storage:(不只這些,詳細看官網喔)

  • Local Disk
  • Directory on the Host(只適合測試,不要用在production喔)
  • Network Mount(公司內部的)
  • Cloud Storage service(provider的)
  • PersistentVolumeClaim (PVC) 從「井」弄「1盆水」來用

定義在deployment的yaml裡

層級較小POD,PVC 去用 PV 的資源,要求size、讀/寫模式(accessModes)、使用次數

PV、PVC都獨立於pod lifecycles之外

PersistenVolume

先定義PV

kind:PersistentVolume
apiVersion:v1
metadata:
	name:task-pv-volume
	labels:
		type:local

spec:
	storageClassName:manual
	capacity:
		sotrage:10Gi
	accessModes:
		-ReadWriteOnce
	hostPath:
		path:"/mnt/data"
  • run一下寫好的yaml
$ kubectl create -f local-volumes.yaml

再定義PVC # 寫在deployment裡,例如:nginx-deployment.yaml

  • 用Claim去PV找有無符合需求的:
    (a)有就分配:binds the claim to the volume
    (b)沒有就報錯:error
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/
完整步驟請看文件喔,我只截要說明的

  • application/wordpress/mysql-deployment.yaml
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

提早出現的secret,後面還會介紹

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

其他類型的 Volumes

範例請參考:
https://github.com/kubernetes/examples/tree/master/staging/volumes

emptyDir

掛一個空的目錄,同一個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、空目錄寫法

hostPath

  • 掛host的目錄
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

local

  • 掛載local storage,例如:磁碟
    跟hostPath很像,但在其他文件有看到不太建議使用直接掛local storage
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,當然要有個iSCSI server
家裡有NAS的可以玩看看,
iSCSI initiator(windows內建,macOS還要另外買(globalSAN initiator)
有遇到這情境再參考範例:
https://github.com/kubernetes/examples/tree/master/staging/volumes/iscsi


上一篇
day18_k8s04_kubectl,pod state,deployment,label&selector,health checks
下一篇
day20_k8s06_儲存篇_下集_Secrets,ConfigMap
系列文
在地端建置Angular+ASP.NET Core的DevOps環境31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言