Kubernetes集群中部署单实例MySQL

Mysql服务属于有状态的单实例应用程序,因为/var/lib/mysql目录需要持久化存储。
我们在部署mysql服务之前,首先创建了一个查找20G卷的PersistentVolumeClaim,使用PersistentVolumeClaim将其连接到现有PersistentVolume来运行有状态应用程序。

创建PV和PVC

kubectl create -f https://k8s.io/examples/application/mysql/mysql-pv.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

kubectl get pv
Kubectl get pic

创建mysql服务deployment

kubectl create -f https://k8s.io/examples/application/mysql/mysql-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
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

kubectl get pods -l app=mysql
kubectl describe deployment mysql

使用客户端链接mysql

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

删除服务及存储

kubectl delete deployment,svc mysql
kubectl delete pvc mysql-pv-claim
kubectl delete pv mysql-pv-volume

参考文档:
https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/

发表评论

电子邮件地址不会被公开。 必填项已用*标注