說明:html
vsphere cloud provider
;VMDK
作爲其持久卷。vCenternode
IP | Version | USER | PASSWORD | DataCenter | DataStore | Remark |
---|---|---|---|---|---|---|
172.20.0.16 | 6.0.0 內部版本5112533 | administrator@vsphere.local | AIUc63$#v4LZ | Mcloud | - | |
- | - | k8s@vsphere.local | k8S!@Vcp123 | Mcloud | vsanDatastore |
disk UUID
;vsphere cloud provider(vcp)
帳號賦權;vcp
帳號與vCenter交互;kubernetes-cluster nodeslinux
Hostname | IP | Roles | Version | Remark |
---|---|---|---|---|
kubemaster01 | 172.30.200.101 | master&minion | v1.12.3 | |
kubemaster02 | 172.30.200.102 | master&minion | v1.12.3 | |
kubemaster03 | 172.30.200.103 | master&minion | v1.12.3 |
govc nodegit
Hostname | IP | Roles | Version | Remark |
---|---|---|---|---|
ansible01 | 172.30.200.46 | govc | v0.19.0 |
vsphere vm folder
,在vCenter的虛擬機與模板
下建立,文件夾名自定義,這裏定義爲kubernetes-cluster
;name
與其操做系統hostname
一致(實際驗證後,並不是強制要求);disk UUID
須要設置爲enabled
(見下文詳細設置);name
須要遵照如下regex:[a-z](([-0-9a-z]+)?[0-9a-z])?(\.[a-z0-9](([-0-9a-z]+)?[0-9a-z])?)*
:
vSphere cloud provider user
,這裏vcp
帳號設置爲k8s@vsphere.local
:
k8s
帳號 ;k8s-vcp
角色,角色按照官方說明中dynamic persistent volume provisioning with storage policy
賦權;System.*
權限,此類權限不用特別賦權,默認即帶,且在角色清單中也找不到System.*
權限。Failed to provision volume with StorageClass "xxxxxx": ServerFaultCode: Permission to perform this operation was denied.
錯,可以使用administrator帳號替代。經過govc
工具,能夠設置disk UUID
,參考:GitHub vmware/govmomigithub
# 選擇版本,直接安裝binaries curl -L https://github.com/vmware/govmomi/releases/download/v0.19.0/govc_linux_amd64.gz | gunzip > /usr/local/bin/govc chmod +x /usr/local/bin/govc
# 設置環境變量 export GOVC_URL='172.20.0.16' # vCenter ip地址或FQDN export GOVC_USERNAME='administrator@vsphere.local' # vCenter administrator帳號 export GOVC_PASSWORD='AIUc63$#v4LZ' # vCenter administrator帳號密碼 export GOVC_INSECURE=1 # 若是使用了自簽名證書,設置爲"1" # 設置環境變量後,可查看govc變量與版本 govc env govc about
經過govc能夠肯定kubernetes集羣vm節點位置:web
# 格式:/<datacenter>/vm/<vsphere vm folder> # <datacenter>:vsphere中datacenter名,根據實際環境填寫,這裏取值"Mcloud"; # vm:固定參數,如:vm,network,host,datastore; # <vsphere vm folder>:自定義名字的"vsphere vm folder",這裏取值"kubernetes-cluster" govc ls /Mcloud/vm/kubernetes-cluster
設置kubernetes集羣vm節點的disk UUID
爲true
:shell
# "disk.enableUUID=1"即爲"true"; # 將已設置"disk.enableUUID=1"的vm打成模板,經過模板克隆出新的vm可繼承此屬性 govc vm.change -e="disk.enableUUID=1" -vm='/Mcloud/vm/kubernetes-cluster/kubemaster01' govc vm.change -e="disk.enableUUID=1" -vm='/Mcloud/vm/kubernetes-cluster/kubemaster02' govc vm.change -e="disk.enableUUID=1" -vm='/Mcloud/vm/kubernetes-cluster/kubemaster03'
補充govc
用法(與本實驗無關):手工建立vmdkvim
# 格式:datastore.disk.create -ds <DataStore> -size xG <Folder>/<Name>.vmdk # <DataStore>:須要調用的後端存儲名字,這裏取值"vsanDatastore"; # <Folder>:<DataStore>中存放新建存儲卷的目錄,這裏自定義目錄名"k8sData",須要注意的是目錄須要提早建立,不然會報錯; # vmdk文件建立以後的初始大小爲0kb govc datastore.disk.create -ds vsanDatastore -size 10G k8sData/MySQLDisk.vmdk
在全部kubernetes master節點建立文件(自定義路徑&名字):/etc/kubernetes/vsphere.conf後端
vim /etc/kubernetes/vsphere.conf # 全局屬性 [Global] # 從安全角度考慮,生產環境應使用針對性的帳號 user = "k8s@vsphere.local" password = "k8S!@Vcp123" port = "443" # 若是使用自簽名證書,應設置爲"1" insecure-flag = "1" datacenters = "Mcloud" # 針對kubernetes集羣節點跨多vCenter的狀況,可設置多"VirtualCenter"; # "user","password","datacenters"也可設置在此,可覆蓋全局屬性 [VirtualCenter "172.20.0.16"] # vSphere Cloud Provider使用"SPBM"存儲策略配置存儲卷 [Workspace] # mandatory server = "172.20.0.16" # mandatory datacenter = "Mcloud" # mandatory folder = "kubernetes-cluster" # option,本實驗省略; # kubernetes建立pvc資源時,若是選項中未指定數據存儲或存儲策略,則使用默認數據存儲放置vmdk #default-datastore="vsanDatastore" # option,本實驗省略; # 資源池文件夾,放置建立的vmdk #resourcepool-path="kubernetes-cluster" [Disk] # option; # lsilogic-sas or pvscsi, default: pvscsi scsicontrollertype = pvscsi [Network] # option,本實驗省略; # Name of public VM Network to which the VMs in the cluster are connected. Used to determine public IP addresses of VMs. #public-network = "VM Network"
在全部kubernetes master節點,爲kube-apiserver,kube-controller-manager,kubelet等3個服務添加參數:api
--cloud-provider=vsphere --cloud-config=/etc/kubernetes/vsphere.conf
在全部kubernetes minion節點,爲kubelet添加參數:
--cloud-provider=vsphere
--cloud-provider
參數,--hostname-override
參數將失效;--hostname-override
參數,必須刪除狀態爲NotReady
的節點,不然建立pvc時報錯Failed to provision volume with StorageClass "storageclass01": No VM found
,經過kubectl delete node <IP>
執行刪除。kubernetes能夠經過如下方式調用vSAN:
cat vsphere-storageclass-spbm.yaml apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: vsphere-storageclass-spbm # 存儲分配器,根據後端存儲不一樣而變動 provisioner: kubernetes.io/vsphere-volume # 默認即"Delete",刪除pvc後,相應的pv及後端的volume等一塊兒刪除; # 設置爲"Retain"時則保留數據,須要手工處理 reclaimPolicy: Delete parameters: # 指定具體的datastore,若是不指定則採用在"vsphere-cloud-config-file(vsphere.conf)"中設置的默認"default-datastore" datastore: vsanDatastore # 默認即"thin",另有"zeroedthick"與"eagerzeroedthick"可選 diskformat: thin # 磁盤格式,如:xfs,ext3,ext4等 fstype: xfs # 如下兩條是調用SPBM策略,SPBM策略有:cacheReservation,diskStripes,forceProvisioning,hostFailuresToTolerate,iopsLimit,objectSpaceReservation # 如下設置是vCenter中默認的"Virtual SAN Default Storage Policy"的設置 hostFailuresToTolerate: "1" diskStripes: "1" # 建立StorageClass kubectl create -f vsphere-storageclass-spbm.yaml # 查看StorageClass kubectl describe sc vsphere-storageclass-spbm
建立pvc
cat vsphere-pvc002.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: vsphere-pvc002 # 調用StrageClass annotations: volume.beta.kubernetes.io/storage-class: vsphere-storageclass-spbm spec: # ReadWriteOnce:簡寫RWO,讀寫權限,且只能被單個node掛載; # ReadOnlyMany:簡寫ROX,只讀權限,容許被多個node掛載; # ReadWriteMany:簡寫RWX,讀寫權限,容許被多個node掛載 accessModes: - ReadWriteOnce resources: requests: # 注意格式,不能寫「GB」 storage: 10Gi # 建立PVC kubectl create -f vsphere-pvc002.yaml # 查看PVC kubectl describe pvc vsphere-pvc002
PV由PVC調用StorageClass自動建立
# 查看自動建立的PV kubectl get pv kubecet describe pv pvc-b3754406-0359-11e9-b1f2-005056bfac1e
同步查看vCenter信息
查看在vSAN DataStore上生成的vmdk
cat vsphere-pod002.yaml apiVersion: v1 kind: Pod metadata: name: vsphere-pod002 spec: containers: - name: test-container # 使用中科大的鏡像倉庫 image: gcr.mirrors.ustc.edu.cn/google_containers/test-webserver volumeMounts: - name: test-volume mountPath: /test-vmdk volumes: - name: test-volume # Pod掛載PVC persistentVolumeClaim: claimName: vsphere-pvc002 # 建立Pod kubectl create -f vsphere-pod002.yaml # 查看Pod kubectl describe pod vsphere-pod002