k8s部署mysql

Posted Dream_it_possible!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s部署mysql相关的知识,希望对你有一定的参考价值。

单节点

        镜像使用mysql:5.7, imagePullPolicy设置为IfNotPresent, 表示有镜像的话不会从remote registry重新拉取。

        部署mysql时,我们需要使用数据挂载,否则会出现数据丢失的情况,这里采用nfs挂载的方式, 我们需要给挂载的目标文件路径/data/dev/mysql授权777, 同时设置为nfs对该路径mount时为insecure,编写mysql-dev-nfs.yaml文件: 

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-dev
spec:
  ports:
    - port: 3306
      nodePort: 30070
  selector:
    app: mysql-dev
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-dev
  labels:
    app: mysql-dev
spec:
  replicas: 1 # pod数量
  selector:
    matchLabels:
      app: mysql-dev
  template:
    metadata:
      labels:
        app: mysql-dev
    spec:
      containers:
        - name: mysql-dev
          image: nacos/nacos-mysql:5.7
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              memory: "0.5Gi"
              cpu: "1500m"
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "root"
            - name: MYSQL_DATABASE
              value: "my_db"
            - name: MYSQL_USER
              value: "nacos"
            - name: MYSQL_PASSWORD
              value: "nacos"
          volumeMounts:
            - name: mysql-data
              mountPath: /var/lib/mysql
            - name: mysql-dev-conf
              mountPath: /etc/mysql
      volumes:
        - name: mysql-data
          nfs:
            server: localhost
            path: /data/dev/mysql
        - name: mysql-dev-conf
          configMap:
            name: my-dev.cnf
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-dev.cnf
data:
  my.cnf: |
    [mysqld]
    port = 3306
    character-set-server=utf8mb4
    collation-server=utf8mb4_unicode_ci
    skip-character-set-client-handshake=1
    default-storage-engine=INNODB
    max_allowed_packet = 500M
    explicit_defaults_for_timestamp=1
    long_query_time = 10

         暴露端口为30070,执行mysql-dev-nfs.yaml,命令:

kubectl apply -f ./deploy/mysql/mysql-dev-nfs.yaml

对外的端口使用30070, 测试是否能连接成:

 

[nfs无法挂载问题] mount.nfs: access denied by server while mounting localhost:/data/dev/mysql_ Dream_it_possible!的博客-CSDN博客

以上是关于k8s部署mysql的主要内容,如果未能解决你的问题,请参考以下文章

k8s启动单点mysql

K8S系列第十篇(一键部署MySQL主从复制)

MySQL在K8S部署实践

MySQL在K8S部署实践

k8s 实战篇 - mysql部署 - 1

k8s部署mysql