参考 / Reference
前提需求 / 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