通过 python 生成 mysql 对应的 yaml 文件。

# -*- coding: utf-8 -*-
# 需要 python 3.6 以上版本
import base64
import argparse


def get_k8s_mysql_yaml(namespace, name, password, image, pvc_name, subpath):
    mysql_root_password = base64.b64encode(password.encode("utf-8")).decode("utf-8")
    if subpath != "" and pvc_name != "":
        sub_path = f"subPath: {subpath}"
    else:
        sub_path = ""
    if pvc_name == "":
        volume = f"""- name: vol-data
        emptyDir: {{}}"""
    else:
        volume = f"""- name: vol-data
        persistentVolumeClaim:
          claimName: {pvc_name}"""
    yaml_content = f"""cat > {name}.yaml << \EOF
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-{name}
  namespace: {namespace}
  labels:
    k8s.kuboard.cn/name: {name}
data:
  mysqld.cnf: |
    [mysqld]
    datadir = /var/lib/mysql
    bind-address = 0.0.0.0
    # 禁止使用软连接
    symbolic-links=0
    max_allowed_packet=64M
    max_connections=2000
    default_storage_engine=INNODB
    # 表名忽略大小写
    lower_case_table_names=1
    skip_name_resolve
    character_set_server=utf8mb4
    # 开启 binlog
    server-id = 107
    log-bin = mysql-bin
    binlog_format = ROW
    binlog_row_image = FULL
    expire_logs_days = 5

---
apiVersion: v1
kind: Secret
metadata:
  name: secret-{name}
  namespace: {namespace}
  labels:
    k8s.kuboard.cn/name: {name}
type: Opaque
data:
  password: {mysql_root_password}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-{name}
  namespace: {namespace}
  labels:
    k8s.kuboard.cn/name: {name}
spec:
  selector:
    matchLabels:
      k8s.kuboard.cn/name: {name}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        k8s.kuboard.cn/name: {name}
    spec:
      containers:
      - image: {image}
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: secret-{name}
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: vol-data
          mountPath: /var/lib/mysql
          {sub_path}
        - name: vol-config
          mountPath: /etc/mysql/conf.d
      volumes:
      {volume}
      - name: vol-config
        configMap:
          name: cm-{name}
          items:
          - key: mysqld.cnf
            path: mysqld.cnf

---
apiVersion: v1
kind: Service
metadata:
  name: svc-{name}
  namespace: {namespace}
  labels:
    k8s.kuboard.cn/name: {name}
spec:
  ports:
    - port: 3306
  selector:
    k8s.kuboard.cn/name: {name}
  clusterIP: None

---
# mysql -h svc-{name}.{namespace} -uroot -p{password}
EOF
"""
    return yaml_content


# =================================================================================================
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--namespace", help="namespace", default="database")
parser.add_argument("-a", "--name", help="name", default="mysql-app1")
parser.add_argument("-p", "--password", help="password", default="123abc")
parser.add_argument("-i", "--image", help="image", default="mysql:5.7.43")
parser.add_argument("-v", "--pvc_name", help="pvc_name", default="")
parser.add_argument("-s", "--subpath", help="subpath", default="")
args = parser.parse_args()

yaml_content = get_k8s_mysql_yaml(
    args.namespace, args.name, args.password, args.image, args.pvc_name, args.subpath
)
print(yaml_content)