星期六, 2月 09, 2019

Dynamic Volume Provisioning - Azure Files with AKS in Azure

Dynamic Volume Provisioning - Azure Files with AKS in Azure

上次 實做 Azure Disks with AKS in Azure.
  • 針對單一 Pod
  • 可以使用 HDD 或是 SSD

今天來實做 Azure Files with AKS in Azure
  • 可以針對多個 Pod
  • 只能使用 LRS, 只有 1000 IOPS 上限

一樣參考官方網站

==== Azure 檔案 - 動態 ====

首先要查詢 節點的資源群組

透過 az aks 指令來查詢
> az aks show --resource-group sakanak8s --name myAKScluster20180824 --query nodeResourceGroup  -o  tsv

MC_sakanaK8s_myAKScluster20180824_japaneast

  • 這個部份其實使用 az aks show 然後過濾 MC_ 開頭應該也可以 :)

建立儲存體帳戶

透過 az  storage 指令來建立
> az  storage  account  create  --resource-group MC_sakanaK8s_myAKScluster20180824_japaneast  --name dvp20190209 --sku Standard_LRS

  • Resource Group 名稱請照上面名稱
  • 儲存體名稱可以自己取
  • 目前只支援 Standard_LRS

可以到 Azure Portal 上面觀察一下

建立 StorageClass, storageAccount 名稱要對應上面建立的儲存體名稱

> vi   azure-file-sc.yaml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
 name: azurefile
provisioner: kubernetes.io/azure-file
mountOptions:
 - dir_mode=0777
 - file_mode=0777
 - uid=1000
 - gid=1000
parameters:
 skuName: Standard_LRS
 storageAccount: dvp20190209

還沒有套用前先觀察資訊

> kubectl  get  sc

NAME                PROVISIONER       AGE
default (default)   kubernetes.io/azure-disk   21h
managed-premium     kubernetes.io/azure-disk   199d

使用 kubectl  apply 指令建立
> kubectl   apply  -f  azure-file-sc.yaml
storageclass.storage.k8s.io/azurefile created

再次觀察資訊
> kubectl  get  sc

NAME                PROVISIONER       AGE
azurefile           kubernetes.io/azure-file   58s
default (default)   kubernetes.io/azure-disk   21h
managed-premium     kubernetes.io/azure-disk   199d

因為不是給單一 pod 使用, AKS Cluster 有使用 RABC 來進行限制

所以接下來就是建立 RABC

複製上面的範例, 建立相關的物件
> vi   azure-pvc-roles.yaml

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
 name: system:azure-cloud-provider
rules:
- apiGroups: ['']
 resources: ['secrets']
 verbs:     ['get','create']
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
 name: system:azure-cloud-provider
roleRef:
 kind: ClusterRole
 apiGroup: rbac.authorization.k8s.io
 name: system:azure-cloud-provider
subjects:
- kind: ServiceAccount
 name: persistent-volume-binder
 namespace: kube-system


開始之前先觀察
> kubectl  get  clusterroles

NAME                                                       AGE
addon-http-application-routing-external-dns                199d
addon-http-application-routing-nginx-ingress-clusterrole   199d
omsagent-reader                                            199d
system:kube-dns-autoscaler                                 8d
system:metrics-server                                      147d

> kubectl  get  clusterrolebindings
NAME                                                               AGE
addon-http-application-routing-nginx-ingress-clusterrole-binding   199d
addon-http-application-routing-viewer                              199d
metrics-server:system:auth-delegator                               147d
omsagentclusterrolebinding                                         199d
system:discovery                                                   69d
system:kube-dns-autoscaler                                         8d
system:metrics-server                                              147d


使用 kubectl  apply 指令指派
> kubectl  apply  -f  azure-pvc-roles.yaml

clusterrole.rbac.authorization.k8s.io/system:azure-cloud-provider created
clusterrolebinding.rbac.authorization.k8s.io/system:azure-cloud-provider created

再次觀察

> kubectl  get clusterroles
NAME                                                       AGE
addon-http-application-routing-external-dns                199d
addon-http-application-routing-nginx-ingress-clusterrole   199d
omsagent-reader                                            199d
system:azure-cloud-provider                                1m
system:kube-dns-autoscaler                                 8d
system:metrics-server                                      147d

> kubectl  get  clusterrolebindings
NAME                                                               AGE
addon-http-application-routing-nginx-ingress-clusterrole-binding   199d
addon-http-application-routing-viewer                              199d
metrics-server:system:auth-delegator                               147d
omsagentclusterrolebinding                                         199d
system:azure-cloud-provider                                        1m
system:discovery                                                   69d
system:kube-dns-autoscaler                                         8d
system:metrics-server                                              147d

接下來建立 PVC

參考官網的範例建立 pvc, 他的 accessModes 是 ReadWriteMany

> vi  azure-file-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: azurefile
spec:
 accessModes:
   - ReadWriteMany
 storageClassName: azurefile
 resources:
   requests:
     storage: 5Gi

還沒套用之前觀察 pvc

> kubectl  get  pvc
NAME                         STATUS VOLUME                        CAPACITY ACCESS MODES STORAGECLASS AGE
azure-managed-disk20190207   Bound pvc-23cc8866-2aea-11e9-9cae-2e861ae6099d   5Gi RWO managed-premium 1d

使用 kubectl apply 指令建立

> kubectl  apply  -f  azure-file-pvc.yaml
persistentvolumeclaim/azurefile created

> kubectl  get pvc
NAME                         STATUS VOLUME                        CAPACITY ACCESS MODES STORAGECLASS AGE
azure-managed-disk20190207   Bound pvc-23cc8866-2aea-11e9-9cae-2e861ae6099d   5Gi RWO managed-premium 1d
azurefile                    Bound pvc-d31f9757-2bbe-11e9-bdd5-eaa8011fdfa2   5Gi RWX azurefile 50s

可以在 vscode 上面觀察剛剛建立的 StorageClass, 以及 PersistentVolumeClaim



最後建立一個 pod 來掛在這個磁區, 重點是 claimName 要跟 pvc 名稱一致

> vi   azure-pvc-files.yaml

kind: Pod
apiVersion: v1
metadata:
 name: mypod
spec:
 containers:
 - name: mypod
   image: nginx:1.15.5
   resources:
     requests:
       cpu: 100m
       memory: 128Mi
     limits:
       cpu: 250m
       memory: 256Mi
   volumeMounts:
   - mountPath: "/mnt/azure"
     name: volume
 volumes:
   - name: volume
     persistentVolumeClaim:
       claimName: azurefile

使用 kubectl  apply 指令來建立
> kubectl  apply  -f  azure-pvc-files.yaml
pod/mypod created

可以觀察 pod 的建立, 但是如何知道 這個 pod 是否乖乖掛載這個 storage 呢?
這個時候真的要說 vscode 很方便

可以在 vscode 上面
針對剛剛建立的 mypod -- > 按滑鼠右鍵 -- > Terminal


這樣就可以進入這個 pod 的 terminal
可以下 df -h 觀察
root@mypod:/# df  -h
Filesystem                                                                                       Size Used Avail Use% Mounted on
overlay                                                                                           30G 13G 17G 43% /
tmpfs                                                                                            954M 0 954M 0% /dev
tmpfs                                                                                            954M 0 954M 0% /sys/fs/cgroup
//dvp20190209.file.core.windows.net/kubernetes-dynamic-pvc-d31f9757-2bbe-11e9-bdd5-eaa8011fdfa2  5.0G 0 5.0G 0% /mnt/azure
/dev/sda1                                                                                         30G 13G 17G 43% /etc/hosts
shm                                                                                               64M 0 64M 0% /dev/shm
tmpfs                                                                                            954M 12K 954M 1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                                                                                            954M 0 954M 0% /sys/firmware

果然有掛載 5 G 空間

接下來建立測試檔案
root@mypod:/# touch  /mnt/azure/20190209.txt
root@mypod:/# ls /mnt/azure/
20190209.txt

為何要這樣做呢?
因為 Azure-files 可以讓多個 pods 進行掛載, 所以接下來要進行證明
回到主機的 command line

複製剛剛的 yaml 檔案
> cp  azure-pvc-files.yaml   azure-pvc-files-2.yaml

修改被複製的 azure-pvc-files-2.yaml 簡單的改個名稱為 mypod2
> vi azure-pvc-files-2.yaml

kind: Pod
apiVersion: v1
metadata:
 name: mypod2
spec:
 containers:
 - name: mypod
   image: nginx:1.15.5
   resources:
     requests:
       cpu: 100m
       memory: 128Mi
     limits:
       cpu: 250m
       memory: 256Mi
   volumeMounts:
   - mountPath: "/mnt/azure"
     name: volume
 volumes:
   - name: volume
     persistentVolumeClaim:
       claimName: azurefile

使用 kubectl apply 指令建立 pod

> kubectl  apply  -f  azure-pvc-files-2.yaml
pod/mypod2 created

進入 mypod2 終端機進行觀察
root@mypod2:/# ls  /mnt/azure
20190209.txt

這樣也算是完成另外一種 storage 的實做 :)

~ enjoy it


Reference:

沒有留言: