您现在的位置是:首页 > 文章详情

在kubernetes中运行单节点有状态MySQL应用

日期:2018-12-16点击:382

前提需求 / 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应用
原文链接:https://yq.aliyun.com/articles/680003
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章