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

本文详细介绍如何在Kubernetes环境中部署单节点MySQL应用,包括创建Service、PersistentVolumeClaim和Deployment的步骤,以及通过YAML文件实现自动化部署的过程。同时,文章还解释了PersistentVolumeClaim中AccessModes的作用,并提供了检查和管理部署的实用命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提需求 / 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应用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值