原创

K8s 启动 持久化 mysql

温馨提示:
本文最后更新于 2025年05月30日,已超过 379 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

在主节点安装NFS

yum install -y nfs-utils rpcbind

在主节点创建目录

mkdir -p /nfs
chmod 777 /nfs

更改归属组与用户

chown -R nfsnobody:nfsnobody /nfs

配置共享目录

echo "/nfs *(insecure,rw,sync,no_root_squash)" > /etc/exports

创建mysql共享目录

mkdir -p /nfs/mysql

启动服务

systemctl start rpcbind
systemctl start nfssystemctl restart nfs

设置开启启动

  systemctl enable rpcbind
  systemctl enable nfs

检查配置是否生效

exportfs
showmount  -e 10.1.24.12

创建负载

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations: {}
  labels:
    app: mysql
  name: mysql
  namespace: default
  resourceVersion: '232091'
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: mysql
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
          image: 'mysql:5.7'
          imagePullPolicy: IfNotPresent
          name: mysql
          ports:
            - containerPort: 3306
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /etc/localtime
              name: time-zone
            - mountPath: /var/lib/mysql
              name: mysql-data
            - mountPath: /var/log/mysql
              name: mysql-logs
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
        - hostPath:
            path: /etc/localtime
            type: ''
          name: time-zone
        - hostPath:
            path: /data/mysql/data
            type: ''
          name: mysql-data
        - hostPath:
            path: /data/mysql/logs
            type: ''
          name: mysql-logs

---
apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels:
    name: mysql
  name: mysql
  namespace: default
  resourceVersion: '229481'
spec:
  clusterIP: 10.10.31.123
  clusterIPs:
    - 10.10.31.123
  externalTrafficPolicy: Cluster
  internalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  ports:
    - nodePort: 30001
      port: 3306
      protocol: TCP
      targetPort: 3306
  selector:
    app: mysql
  sessionAffinity: None
  type: NodePort

创建 service


apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  type: NodePort
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 30001
  selector:
    app: mysql
正文到此结束