在kubernetes中運行單節點有狀態MySQL應用

前提需求 / Rquirements

  • 現成的kubernetes集羣
  • 持久存儲-PersistentVolume
  • 持久存儲容量聲明-PersistentVolumeClaim

創建yaml文件 / Create YAML file

https://raw.githubusercontent...

分別創建 Service、PersistentVolumeClaim、Deployment

apiVersion: v1 kind: Service metadata: name: mysql spec: ports: - port: 3306 selector: app: mysql clusterIP: None --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-pv-claim spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi --- apiVersion: apps/v1beta2 # for versions before 1.8.0 use apps/v1beta1 kind: Deployment metadata: name: mysql spec: selector: matchLabels: app: mysql strategy: type: Recreate template: metadata: labels: app: mysql spec: containers: - image: mysql:5.6 name: mysql env:  # Use secret in real usage - name: MYSQL_ROOT_PASSWORD value: password ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pv-claim

對於PersistentVolumeClaim中Access Modes的解釋:

k8s不會真正檢查存儲的訪問模式或根據訪問模式做訪問限制,只是對真實存儲的描述,最終的控制權在真實的存儲端。目前支持三種訪問模式:

  • ReadWriteOnce – PV以read-write 掛載到一個節點
  • ReadWriteMany – PV以read-write 方式掛載到多個節點
  • ReadOnlyMany – PV以read-only 方式掛載到多個節點

部署YAML文件 / Deploy the Deployment

kubectl create -f https://k8s.io/docs/tasks/run-application/mysql-deployment.yaml
or
kubectl create -f mysql-deployment.yaml

查看Deployment的詳細信息

kubectl describe deployment mysql

查看Deployment的pods信息

kubectl get pods -l app=mysql

檢查PersistentVolumeClaim的信息

kubectl describe pvc mysql-pv-claim

進入MySQL實例 / Connet to MySQL

啓動一個MySQL客戶端服務並連接到MySQL

kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword

升級MySQL應用 / Upgrade MySQL

可以使用 kubectl apply 命令對Deployment中的image或者其它部分進行升級;

針對有狀態應用StatefulSet, 需要注意以下幾點:

  • Don’t scale the app

This setup is for single-instance apps only. The underlying PersistentVolume can only be mounted to one Pod. For clustered stateful apps, see the StatefulSet documentation.

  • Use strategy: type: Recreate

in the Deployment configuration YAML file. This instructs Kubernetes to not use rolling updates. Rolling updates will not work, as you cannot have more than one Pod running at a time. The Recreate strategy will stop the first pod before creating a new one with the updated configuration

刪除Deployment / Delete the ployment

Delete the deployed objects by name:

kubectl delete deployment,svc mysql
kubectl delete pvc mysql-pv-claim
本文轉自SegmentFault-在kubernetes中運行單節點有狀態MySQL應用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章