目 录CONTENT

文章目录

heml 安装mysql 8.0主从

Seven
2025-08-20 / 0 评论 / 0 点赞 / 2 阅读 / 19145 字 / 正在检测是否收录...

前提条件

有k8s集群、有helm工具、有持久存储

安装mysql

1、helm配置bitnami仓库并更新

helm repo add bitnami https://charts.bitnami.com/bitnami ##添加仓库

helm repo update ##更新仓库
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "bitnami" chart repository

2、搜索并拉取自己想要的版本

helm search repo bitnami/mysql --versions ##搜索可用的mysql版本
NAME            CHART VERSION   APP VERSION     DESCRIPTION                                       
bitnami/mysql   14.0.3          9.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   14.0.2          9.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   14.0.0          9.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   13.0.4          9.3.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   13.0.3          9.3.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   13.0.2          9.3.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   13.0.1          9.3.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   13.0.0          9.3.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.3.5          8.4.5           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.3.4          8.4.5           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.3.3          8.4.4           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.3.2          8.4.4           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.3.1          8.4.4           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.3.0          8.4.4           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.2.4          8.4.4           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.2.2          8.4.4           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.2.1          8.4.3           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.2.0          8.4.3           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.1.0          8.4.3           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.0.1          8.4.3           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   12.0.0          8.4.3           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.20         8.4.3           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.19         8.4.3           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.18         8.4.2           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.17         8.4.2           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.16         8.4.2           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.15         8.4.2           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.14         8.4.2           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.13         8.4.2           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.12         8.4.2           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.11         8.4.1           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.10         8.4.1           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.9          8.4.1           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.8          8.4.1           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.7          8.4.1           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.6          8.4.1           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.5          8.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.4          8.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.3          8.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.2          8.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.1          8.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.1.0          8.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   11.0.0          8.4.0           MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.3.0          8.0.37          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.2.4          8.0.37          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.2.3          8.0.37          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.2.2          8.0.37          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.2.1          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.1.1          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.1.0          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   10.0.0          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.23.0          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.22.0          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.21.2          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.21.1          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.19.1          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.19.0          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.18.2          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.18.0          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
bitnami/mysql   9.17.1          8.0.36          MySQL is a fast, reliable, scalable, and easy t...
......

helm pull bitnami/mysql --version=10.3.0 ##拉取包到本地,选择的是mysql 8.0.37
tar -zxvf mysql-10.3.0.tgz ##解压
cp mysql/values.yaml mysql/values-default.yaml ##备份原始配置文件

3、修改values.yaml文件

grep -Ev "$^|#" values.yaml

global:
  imageRegistry: ""
  imagePullSecrets: []
  storageClass: ""
  compatibility:
    openshift:
      adaptSecurityContext: auto
kubeVersion: ""
nameOverride: ""
fullnameOverride: ""
namespaceOverride: ""
clusterDomain: cluster.local
commonAnnotations: {}
commonLabels: {}
extraDeploy: []
serviceBindings:
  enabled: false
diagnosticMode:
  enabled: false
  command:
    - sleep
  args:
    - infinity
image:
  registry: docker.io
  repository: bitnami/mysql
  tag: 8.0.37-debian-12-r2
  digest: ""
  pullPolicy: IfNotPresent
  pullSecrets: []
  debug: false
architecture: replication
auth:
  volumeName: backend-logs
  rootPassword: "112233" #root用户密码
  createDatabase: true
  database: "my_database" #需要创建的数据库名
  username: "" #数据库用户
  password: "" #数据库用户密码
  replicationUser: replicator ##主从复制的专用用户
  replicationPassword: "112233" #主从复制的专用用户密码
  existingSecret: ""
  usePasswordFiles: false
  customPasswordFiles: {}
  defaultAuthenticationPlugin: ""
initdbScripts: {}
initdbScriptsConfigMap: ""
startdbScripts: {}
startdbScriptsConfigMap: ""
primary:       #这个是主库配置,这里没有定义主库的pod副本数,是在主库模板文件里面定义的
  name: primary
  command: []
  args: []
  lifecycleHooks: {}
  automountServiceAccountToken: false
  hostAliases: []
  enableMySQLX: false
  configuration: |-
    [mysqld]
    default_authentication_plugin={{- .Values.auth.defaultAuthenticationPlugin | default "mysql_native_password" }}
    skip-name-resolve
    explicit_defaults_for_timestamp
    basedir=/opt/bitnami/mysql
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    port={{ .Values.primary.containerPorts.mysql }}
    mysqlx={{ ternary 1 0 .Values.primary.enableMySQLX }}
    mysqlx_port={{ .Values.primary.containerPorts.mysqlx }}
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    datadir=/bitnami/mysql/data
    tmpdir=/opt/bitnami/mysql/tmp
    max_allowed_packet=16M
    bind-address=*
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
    log-error=/opt/bitnami/mysql/logs/mysqld.log
    character-set-server=UTF8
    slow_query_log=0
    long_query_time=10.0
    [client]
    port={{ .Values.primary.containerPorts.mysql }}
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    default-character-set=UTF8
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    [manager]
    port={{ .Values.primary.containerPorts.mysql }}
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
  existingConfigmap: ""
  containerPorts:
    mysql: 3306
    mysqlx: 33060
  updateStrategy:
    type: RollingUpdate
  podAnnotations: {}
  podAffinityPreset: ""
  podAntiAffinityPreset: soft
  nodeAffinityPreset:
    type: ""
    key: ""
    values: []
  affinity: {}
  nodeSelector: {}
  tolerations: []
  priorityClassName: ""
  runtimeClassName: ""
  schedulerName: ""
  terminationGracePeriodSeconds: ""
  topologySpreadConstraints: []
  podManagementPolicy: ""
  podSecurityContext:
    enabled: true
    fsGroupChangePolicy: Always
    sysctls: []
    supplementalGroups: []
    fsGroup: 1001
  containerSecurityContext:
    enabled: true
    seLinuxOptions: {}
    runAsUser: 1001
    runAsGroup: 1001
    runAsNonRoot: true
    allowPrivilegeEscalation: false
    capabilities:
      drop: ["ALL"]
    seccompProfile:
      type: "RuntimeDefault"
    readOnlyRootFilesystem: true
  resourcesPreset: "small"
  resources: {}
  livenessProbe:
    enabled: true
    initialDelaySeconds: 5
    periodSeconds: 10
    timeoutSeconds: 1
    failureThreshold: 3
    successThreshold: 1
  readinessProbe:
    enabled: true
    initialDelaySeconds: 5
    periodSeconds: 10
    timeoutSeconds: 1
    failureThreshold: 3
    successThreshold: 1
  startupProbe:
    enabled: true
    initialDelaySeconds: 15
    periodSeconds: 10
    timeoutSeconds: 1
    failureThreshold: 10
    successThreshold: 1
  customLivenessProbe: {}
  customReadinessProbe: {}
  customStartupProbe: {}
  extraFlags: ""
  extraEnvVars: []
  extraEnvVarsCM: ""
  extraEnvVarsSecret: ""
  extraPodSpec: {}
  extraPorts: []
  persistence:
    enabled: true
    existingClaim: ""
    subPath: ""
    storageClass: "nas-nfs"  #存储类,持久化mysql主库数据(这里是cs)
    annotations: {}
    accessModes:
      - ReadWriteMany #权限模式(阿里云nas必须用这个模式,不然报错。)
    size: 20Gi    #pv的大小,自己定义
    selector: {}
  persistentVolumeClaimRetentionPolicy:
    enabled: false
    whenScaled: Retain
    whenDeleted: Retain
  extraVolumes: []
  extraVolumeMounts: []
  initContainers: []
  sidecars: []
  service:
    type: ClusterIP
    ports:
      mysql: 3306
      mysqlx: 33060
    nodePorts:
      mysql: ""
      mysqlx: ""
    clusterIP: ""
    loadBalancerIP: ""
    externalTrafficPolicy: Cluster
    loadBalancerSourceRanges: []
    extraPorts: []
    annotations: {}
    sessionAffinity: None
    sessionAffinityConfig: {}
    headless:
      annotations: {}
  pdb:
    create: false
    minAvailable: 1
    maxUnavailable: ""
  podLabels: {}
secondary:       #这里开始是从库的配置。
  name: secondary
  replicaCount: 1   #从库的副本数。
  automountServiceAccountToken: false
  hostAliases: []
  command: []
  args: []
  lifecycleHooks: {}
  enableMySQLX: false
  configuration: |-
    [mysqld]
    default_authentication_plugin=mysql_native_password
    skip-name-resolve
    explicit_defaults_for_timestamp
    basedir=/opt/bitnami/mysql
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    port={{ .Values.secondary.containerPorts.mysql }}
    mysqlx={{ ternary 1 0 .Values.secondary.enableMySQLX }}
    mysqlx_port={{ .Values.secondary.containerPorts.mysqlx }}
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    datadir=/bitnami/mysql/data
    tmpdir=/opt/bitnami/mysql/tmp
    max_allowed_packet=16M
    bind-address=*
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
    log-error=/opt/bitnami/mysql/logs/mysqld.log
    character-set-server=UTF8
    slow_query_log=0
    long_query_time=10.0
    [client]
    port={{ .Values.secondary.containerPorts.mysql }}
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    default-character-set=UTF8
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    [manager]
    port={{ .Values.secondary.containerPorts.mysql }}
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
  existingConfigmap: ""
  containerPorts:
    mysql: 3306
    mysqlx: 33060
  updateStrategy:
    type: RollingUpdate
  podAnnotations: {}
  podAffinityPreset: ""
  podAntiAffinityPreset: soft
  nodeAffinityPreset:
    type: ""
    key: ""
    values: []
  affinity: {}
  nodeSelector: {}
  tolerations: []
  priorityClassName: ""
  runtimeClassName: ""
  schedulerName: ""
  terminationGracePeriodSeconds: ""
  topologySpreadConstraints: []
  podManagementPolicy: ""
  podSecurityContext:
    enabled: true
    fsGroupChangePolicy: Always
    sysctls: []
    supplementalGroups: []
    fsGroup: 1001
  containerSecurityContext:
    enabled: true
    seLinuxOptions: {}
    runAsUser: 1001
    runAsGroup: 1001
    runAsNonRoot: true
    allowPrivilegeEscalation: false
    capabilities:
      drop: ["ALL"]
    seccompProfile:
      type: "RuntimeDefault"
    readOnlyRootFilesystem: true
  resourcesPreset: "small"
  resources: {}
  livenessProbe:
    enabled: true
    initialDelaySeconds: 5
    periodSeconds: 10
    timeoutSeconds: 1
    failureThreshold: 3
    successThreshold: 1
  readinessProbe:
    enabled: true
    initialDelaySeconds: 5
    periodSeconds: 10
    timeoutSeconds: 1
    failureThreshold: 3
    successThreshold: 1
  startupProbe:
    enabled: true
    initialDelaySeconds: 15
    periodSeconds: 10
    timeoutSeconds: 1
    failureThreshold: 15
    successThreshold: 1
  customLivenessProbe: {}
  customReadinessProbe: {}
  customStartupProbe: {}
  extraFlags: ""
  extraEnvVars: []
  extraEnvVarsCM: ""
  extraEnvVarsSecret: ""
  extraPodSpec: {}
  extraPorts: []
  persistence:
    enabled: true
    existingClaim: ""
    subPath: ""
    storageClass: "nas-nfs" #存储类,用于从库持久化数据
    annotations: {}
    accessModes:
      - ReadWriteMany #权限模式
    size: 10Gi     #pvc的大小。
    selector: {}
  persistentVolumeClaimRetentionPolicy:
    enabled: false
    whenScaled: Retain
    whenDeleted: Retain
  extraVolumes: []
  extraVolumeMounts: []
  initContainers: []
  sidecars: []
  service:
    type: ClusterIP
    ports:
      mysql: 3306
      mysqlx: 33060
    nodePorts:
      mysql: ""
      mysqlx: ""
    clusterIP: ""
    loadBalancerIP: ""
    externalTrafficPolicy: Cluster
    loadBalancerSourceRanges: []
    extraPorts: []
    annotations: {}
    sessionAffinity: None
    sessionAffinityConfig: {}
    headless:
      annotations: {}
  pdb:
    create: false
    minAvailable: 1
    maxUnavailable: ""
  podLabels: {}
serviceAccount:
  create: true
  name: ""
  annotations: {}
  automountServiceAccountToken: false
rbac:
  create: false
  rules: []
networkPolicy:
  enabled: true
  allowExternal: true
  allowExternalEgress: true
  extraIngress: []
  extraEgress: []
  ingressNSMatchLabels: {}
  ingressNSPodMatchLabels: {}
volumePermissions:
  enabled: false
  image:
    registry: docker.io
    repository: bitnami/os-shell
    tag: 12-debian-12-r21
    digest: ""
    pullPolicy: IfNotPresent
    pullSecrets: []
  resourcesPreset: "nano"
  resources: {}
metrics:
  enabled: false
  image:
    registry: docker.io
    repository: bitnami/mysqld-exporter
    tag: 0.15.1-debian-12-r16
    digest: ""
    pullPolicy: IfNotPresent
    pullSecrets: []
  containerSecurityContext:
    enabled: true
    seLinuxOptions: {}
    runAsUser: 1001
    runAsGroup: 1001
    runAsNonRoot: true
    allowPrivilegeEscalation: false
    capabilities:
      drop: ["ALL"]
    seccompProfile:
      type: "RuntimeDefault"
    readOnlyRootFilesystem: true
  containerPorts:
    http: 9104
  service:
    type: ClusterIP
    port: 9104
    clusterIP: ""
    annotations:
      prometheus.io/scrape: "true"
      prometheus.io/port: "{{ .Values.metrics.service.port }}"
  extraArgs:
    primary: []
    secondary: []
  resourcesPreset: "nano"
  resources: {}
  livenessProbe:
    enabled: true
    initialDelaySeconds: 120
    periodSeconds: 10
    timeoutSeconds: 1
    successThreshold: 1
    failureThreshold: 3
  readinessProbe:
    enabled: true
    initialDelaySeconds: 30
    periodSeconds: 10
    timeoutSeconds: 1
    successThreshold: 1
    failureThreshold: 3
  serviceMonitor:
    enabled: false
    namespace: ""
    jobLabel: ""
    interval: 30s
    scrapeTimeout: ""
    relabelings: []
    metricRelabelings: []
    selector: {}
    honorLabels: false
    labels: {}
    annotations: {}
  prometheusRule:
    enabled: false
    namespace: ""
    additionalLabels: {}
    rules: []

4、查看目录结构:

tree templates/
templates/
├── extra-list.yaml
├── _helpers.tpl
├── metrics-svc.yaml
├── networkpolicy.yaml
├── NOTES.txt
├── primary								#这个目录里面存放的就是主库的模板
│   ├── configmap.yaml
│   ├── initialization-configmap.yaml
│   ├── pdb.yaml
│   ├── statefulset.yaml				#主库的sts模板,里面定义了replicas副本数是1
│   ├── svc-headless.yaml
│   └── svc.yaml
├── prometheusrule.yaml
├── rolebinding.yaml
├── role.yaml
├── secondary							#这个目录里面存放的就是从库的模板
│   ├── configmap.yaml
│   ├── pdb.yaml
│   ├── statefulset.yaml
│   ├── svc-headless.yaml
│   └── svc.yaml
├── secrets.yaml
├── serviceaccount.yaml
└── servicemonitor.yaml

5、安装mysql

helm  install mysql  mysql/  -n mysql-db #安装mysql
kubectl get po -n mysql-db #查看安装结果,状态都为running就成功,我这里出现过创建文件权限问题。

最后

如果出现权限问题:

比如:mkdir: cannot create directory '/bitnami/mysql/data': Permission denied

将nas里的目录权限改为1001:1001就可以了

chown -R 1001:1001 nas-for-mysql

0

评论区