storage-mysql

  • 必须存在动态存储(例如动态存储名: local)

# 动态存储不需要手动去创建 PV ,直接创建 PVC 即可
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: local

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
spec:
  clusterIP: None
  ports:
    - name: mysql
      port: 3306
  selector:
    app: mysql

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-sta
spec:
  serviceName: mysql-svc
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  # For Deployment
  #strategy:
  #  type:  RollingUpdate
  #  rollingUpdate:
  #    maxSurge: 25%
  #    maxUnavailable: 25%
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8.0
          imagePullPolicy: IfNotPresent
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
            - name: MYSQL_ROOT_HOST
              value: "%"
            - name: MYSQL_DEFAULT_AUTHENTICATION_PLUGIN
              value: mysql_native_password
            - name: TZ
              value: Asia/Shanghai
          ports:
            - containerPort: 3306
          resources:
            requests:
              memory: "200Mi"
              cpu: "0.1"
            limits:
              memory: "2Gi"
              cpu: "1"
          livenessProbe:
            exec:
              command:
              - mysqladmin
              - ping
              - -h127.0.0.1
              - -uroot
              - -p${MYSQL_ROOT_PASSWORD}
            initialDelaySeconds: 60
            timeoutSeconds: 5
            periodSeconds: 10
            failureThreshold: 3
          readinessProbe:
            exec:
              command:
              - mysqladmin
              - ping
              - -h127.0.0.1
              - -uroot
              - -p${MYSQL_ROOT_PASSWORD}
            initialDelaySeconds: 60
            timeoutSeconds: 5
            periodSeconds: 10
            failureThreshold: 3
          volumeMounts:
            - name: mysql-data
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-pvc

# 临时容器测试
# [root@master pv_pvc_test]# kubectl run mysql-client --rm -it --image=mysql:8.0 --restart=Never -- mysql -h mysql-svc.default.svc.cluster.local -uroot -p123456 -e "select version()"
# mysql: [Warning] Using a password on the command line interface can be insecure.
# +-----------+
# | version() |
# +-----------+
# | 8.0.37    |
# +-----------+
# pod "mysql-client" deleted

Last updated