k8s集群部署mysql笔记

Posted 今夜月色很美

tags:

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

一、简化版yaml文件(去掉了nodeport,持久卷等)

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: mysql
  name: mysql-10.1-67b46fcb75-qctkm
  namespace: xxx-dev
spec:
  containers:
  - env:
    - name: MYSQL_ROOT_PASSWORD
      value: mypassword
    - name: MY_DEPLOY_NAME
      value: mysql
    image: xxxx/mysql:10.1
    imagePullPolicy: Always
    name: mysql
    ports:
    - containerPort: 3306
      protocol: TCP
    resources:
      limits:
        cpu: "4"
        memory: 8000Mi
      requests:
        cpu: "2"
        memory: 4000Mi

imagePullPolicy镜像拉取策略

Always总是拉取 pull
IfNotPresent默认值,本地有则使用本地镜像,不拉取
Never只使用本地镜像,从不拉取

数据库文件存放路径

/var/lib/mysql

使用persistentVolumeClaim持久卷需要NFS 共享存储,如果没有需要先安装

yum -y install  nfs-utils rpcbind

二、nodeport挂载宿主机目录yaml

mysql pod yaml文件

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: mysql
  name: mysql-10.1-67b46fcb75-qctkm
spec:
  containers:
  - env:
    - name: MYSQL_ROOT_PASSWORD
      value: abcdefghijklmn
    - name: MY_DEPLOY_NAME
      value: mysql
    image: mysql
    imagePullPolicy: Always
    name: mysql
    ports:
    - containerPort: 3306
      protocol: TCP
    resources:
      limits:
        cpu: "1"
        memory: 2000Mi
      requests:
        cpu: "1"
        memory: 1000Mi
    volumeMounts:
    - mountPath: /var/lib/mysql
      name: mysql-data
  volumes:
  - name: mysql-data
    hostPath:
      path: /mnt/mysql/data
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels:
    name: mysql-service
spec:
  selector:
    app: mysql
  type: NodePort
  ports:
  - nodePort: 32016
    port: 3306
    targetPort: 3306

环境变量MYSQL_ROOT_PASSWORD设置默认root密码

三、修改root用户允许远程访问

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
查看用户表
select host,user,plugin,authentication_string from mysql.user;

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

如何访问k8s集群内部署的mysql服务

Docker 与 K8S学习笔记(十七)—— 在K8S集群中部署应用

云原生之kubernetes实战在k8s集群环境下部署Tomcat应用

k8s部署mysql集群master--slave

高可用集群篇-- K8S部署微服务

搭建k8s集群(平台规划和部署方式介绍)