天天看點

在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應用