创建 PersistentVolume

首先在资源管理器创建好存储目录,然后编写脚本
注意,挂在的 Windows 目录可通过 wsl 指令查询

在这里插入图片描述

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-volume
  labels:
    type: local
spec:
  storageClassName: local-storage
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/host/e/k8s_data/spring_cloud_mysql"
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage

通过 powershell 执行脚本

kubectl apply -f pv-volume.yaml

在这里插入图片描述

创建 PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: spring-cloud-demo
  name: mysql-claim
spec:
  accessModes:
    - ReadWriteOnce  
  resources:
    requests:
      storage: 2Gi
  storageClassName: local-storage

通过 powershell 执行脚本

kubectl apply -f pvc-volume.yaml

创建 Secret

1、获得用户名,密码的base-64编码

echo -n 'root' | base64
echo -n '123456' | base64

在这里插入图片描述

apiVersion: v1
kind: Secret
metadata:
  namespace: spring-cloud-demo
  name: mysql-secret
data:
  username: cm9vdA==
  password: MTIzNDU2

在这里插入图片描述

准备 mysqld.cnf 并创建 ConfigMap

[mysqld]
explicit_defaults_for_timestamp=1
lower_case_table_names=1
max_allowed_packet=32M
net_write_timeout=600
net_read_timeout=300
symbolic-links=0
max_connections=1000 
innodb_strict_mode = 0
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

powershell

kubectl create configmap mysql-config --from-file=mysqld.cnf -n spring-cloud-demo

在这里插入图片描述

创建MYSQL POD

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: spring-cloud-demo
  name: db-mysql
  labels:
    app: db-mysql
spec:
  selector:
    matchLabels:
      app: db-mysql
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db-mysql
        tier: mysql
    spec:
      containers:
      - image: mysql:5.7.16
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
        - name: mysql-config
          mountPath: /etc/mysql/conf.d
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-claim
      - name: mysql-config
        configMap:
          name: mysql-config

创建 mysql Service

kind: Service
apiVersion: v1
metadata:
  labels:
    app: db-mysql
  name: mysql-db-svc
  namespace: spring-cloud-demo
spec:
  type: NodePort
  selector:
    app: db-mysql
  ports:
    - name: tcp-3306
      protocol: TCP
      port: 3306
      targetPort: 3306
      nodePort: 30007

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐