一、简化版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;
Logo

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

更多推荐