k8s搭建mysql5.7

部署mysql
vi statefulset-mysql.yaml

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: plugins
  labels:
    app: mysql
data:
  my.cnf: |-
    [client]
    default-character-set=utf8mb4
    [mysql]
    default-character-set=utf8mb4
    [mysqld]
    character-set-server = utf8mb4
    collation-server = utf8mb4_unicode_ci
    init_connect='SET NAMES utf8mb4'
    skip-character-set-client-handshake = true
    max_connections=2000
    secure_file_priv=/var/lib/mysql
    bind-address=0.0.0.0
    symbolic-links=0
    sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysql
  name: mysql-svc
  namespace: plugins
spec:
  type: NodePort
  ports:
    - name: http
      port: 3306
      nodePort: 30021
      protocol: TCP
      targetPort: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: plugins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - args:
            - --datadir
            - /var/lib/mysql/datadir
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
            - name: MYSQL_USER
              value: user
            - name: MYSQL_PASSWORD
              value: user
          image: mysql:5.7
          name: mysql-container
          ports:
            - containerPort: 3306
              name: dbapi
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-storage
            - name: config
              mountPath: /etc/mysql/conf.d/my.cnf
              subPath: my.cnf
      volumes:
        - name: mysql-storage
          persistentVolumeClaim:
            claimName: nfs-plugins
        - name: config
          configMap:
            name: mysql-config
        - name: localtime
          hostPath:
            type: File
            path: /etc/localtime

部署
kubectl apply -f mysql-statefulset.yaml -n plugins

连接测试
在这里插入图片描述在这里插入图片描述
部署完成

Logo

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

更多推荐