K8S容器编排之StatefulSet持久化部署MySQL

1.创建对应的pvpvc

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql
  labels:
    pv: mysql
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "models"
    readOnly: false
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dev-volume-claim-mysql
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      pv: mysql

2.创建secret对象存储密码

apiVersion: v1
kind: Secret
metadata:
  name: k8s-dev-mysql
type: Opaque
data:
  mysql-root-password: root
  mysql-password: test

3.创建deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-dev-mysql
  labels:
    app: k8s-dev-mysql
spec:
  selector:
    matchLabels:
      app: k8s-dev-mysql
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: k8s-dev-mysql
    spec:
      containers:
      - image: mysql
        name: k8s-dev-mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              key: mysql-root-password
              name: k8s-dev-mysql
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              key: mysql-password
              name: k8s-dev-mysql
        - name: MYSQL_USER
          value: test
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: dev-volume-claim-mysql

4.打包service

apiVersion: v1
kind: Service
metadata:
  name: k8s-dev-mysql
  labels:
    app: k8s-dev-mysql
spec:
  type: NodePort
  ports:
    - port: 3306
  selector:
    app: k8s-dev-mysql

你可能感兴趣的:(K8S容器编排)