k8s helm 安装mysql8.0,单节点,一主两从架构

前言

环境:centos 7.9 k8s-1.22.15 dockr-20.10.9 helm3

前提条件

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

安装mysql

拉去helm包

[root@master ~]# helm repo add bitnami https://charts.bitnami.com/bitnami
helm search repo mysql 
bitnami/mysql                   	9.7.0        	8.0.32     	MySQL is a fast, reliable, scalable, and easy t...
bitnami/phpmyadmin              	10.4.5       	5.2.1      	phpMyAdmin is a free software tool written in P...
bitnami/mariadb                 	11.5.5       	10.6.12    	MariaDB is an open source, community-developed ...
bitnami/mariadb-galera          	7.5.4        	10.6.12    	MariaDB Galera is a multi-primary database clus...

[root@master ~]# helm pull bitnami/mysql --version=9.7.0
[root@master ~]#ls
mysql-9.7.0.tgz
[root@master ~]# tar xf mysql-9.7.0.tgz
[root@master ~]# cd  mysql
[root@master mysql]# cp values.yaml values_original.yaml

修改values.yaml文件

[root@master mysql]# grep -Ev "$^|#" values.yaml 
global:
  imageRegistry: ""
  imagePullSecrets: []
  storageClass: ""
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.32-debian-11-r14			#安装的是mysql-8.0.32数据库
  digest: ""
  pullPolicy: IfNotPresent
  pullSecrets: []
  debug: false
architecture: replication			#architecture参数表示mysql的架构,本次部署架构是主从复制,取值replication
auth:								#如果部署的是单机版,取值standalone即可,下面secondary部分不用改
  rootPassword: "123456"			#root密码
  createDatabase: true				#是否创建数据库,设置true
  database: "sn"					#要创建的数据库名称
  username: "sn"					#数据库用户
  password: "123456"				#数据库用户密码
  replicationUser: replicator		#主从复制的专用用户
  replicationPassword: "123456"		#主从复制的专用用户密码
  existingSecret: ""
  usePasswordFiles: false
  customPasswordFiles: {}
initdbScripts: {}
initdbScriptsConfigMap: ""
primary:							#这块是主库配置,这里没有定义主库的pod副本数,是在主库模板文件里面定义的
  name: primary
  command: []
  args: []
  lifecycleHooks: {}
  hostAliases: []
  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=3306
    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
    collation-server=utf8_general_ci
    slow_query_log=0
    slow_query_log_file=/opt/bitnami/mysql/logs/mysqld.log
    long_query_time=10.0
    [client]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    default-character-set=UTF8
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    [manager]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
  existingConfigmap: ""
  updateStrategy:
    type: RollingUpdate
  podAnnotations: {}
  podAffinityPreset: ""
  podAntiAffinityPreset: soft
  nodeAffinityPreset:
    type: ""
    key: ""
    values: []
  affinity: {}
  nodeSelector: {}
  tolerations: []
  priorityClassName: ""
  runtimeClassName: ""
  schedulerName: ""
  terminationGracePeriodSeconds: ""
  topologySpreadConstraints: []
  podManagementPolicy: ""
  podSecurityContext:
    enabled: true
    fsGroup: 1001
  containerSecurityContext:
    enabled: true
    runAsUser: 1001
    runAsNonRoot: true
  resources:
    limits: {}
    requests: {}
  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: ""
  extraPorts: []
  persistence:
    enabled: true
    existingClaim: ""
    subPath: ""
    storageClass: "nfs-storageclass"	#存储类,持久化mysql主库数据
    annotations: {}
    accessModes:
      - ReadWriteOnce
    size: 1Gi							#pv的大小,根据实际修改
    selector: {}
  extraVolumes: []
  extraVolumeMounts: []
  initContainers: []
  sidecars: []
  service:
    type: ClusterIP
    ports:
      mysql: 3306
    nodePorts:
      mysql: ""
    clusterIP: ""
    loadBalancerIP: ""
    externalTrafficPolicy: Cluster
    loadBalancerSourceRanges: []
    extraPorts: []
    annotations: {}
    sessionAffinity: None
    sessionAffinityConfig: {}
    headless:
      annotations: {}
  pdb:
    create: false
    minAvailable: 1
    maxUnavailable: ""
  podLabels: {}
secondary:							#这块是从库的相关配置
  name: secondary
  replicaCount: 2					#从库的副本数,这里设为2,默认是1
  hostAliases: []
  command: []
  args: []
  lifecycleHooks: {}
  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=3306
    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
    collation-server=utf8_general_ci
    slow_query_log=0
    slow_query_log_file=/opt/bitnami/mysql/logs/mysqld.log
    long_query_time=10.0
    [client]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    default-character-set=UTF8
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    [manager]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
  existingConfigmap: ""
  updateStrategy:
    type: RollingUpdate
  podAnnotations: {}
  podAffinityPreset: ""
  podAntiAffinityPreset: soft
  nodeAffinityPreset:
    type: ""
    key: ""
    values: []
  affinity: {}
  nodeSelector: {}
  tolerations: []
  priorityClassName: ""
  runtimeClassName: ""
  schedulerName: ""
  terminationGracePeriodSeconds: ""
  topologySpreadConstraints: []
  podManagementPolicy: ""
  podSecurityContext:
    enabled: true
    fsGroup: 1001
  containerSecurityContext:
    enabled: true
    runAsUser: 1001
    runAsNonRoot: true
  resources:
    limits: {}
    requests: {}
  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: ""
  extraPorts: []
  persistence:
    enabled: true
    existingClaim: ""
    subPath: ""
    storageClass: "nfs-storageclass"		#存储类,用于从库持久化数据
    annotations: {}
    accessModes:
      - ReadWriteOnce
    size: 1Gi								#pv的大小,根据实际修改
    selector: {}
  extraVolumes: []
  extraVolumeMounts: []
  initContainers: []
  sidecars: []
  service:
    type: ClusterIP
    ports:
      mysql: 3306
    nodePorts:
      mysql: ""
    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: true
rbac:
  create: false
  rules: []
networkPolicy:
  enabled: false
  allowExternal: true
  explicitNamespacesSelector: {}
volumePermissions:
  enabled: false
  image:
    registry: docker.io
    repository: bitnami/bitnami-shell
    tag: 11-debian-11-r94
    digest: ""
    pullPolicy: IfNotPresent
    pullSecrets: []
  resources: {}
metrics:
  enabled: false
  image:
    registry: docker.io
    repository: bitnami/mysqld-exporter
    tag: 0.14.0-debian-11-r96
    digest: ""
    pullPolicy: IfNotPresent
    pullSecrets: []
  containerSecurityContext:
    enabled: true
    runAsUser: 1001
    runAsNonRoot: true
  service:
    type: ClusterIP
    port: 9104
    annotations:
      prometheus.io/scrape: "true"
      prometheus.io/port: "{{ .Values.metrics.service.port }}"
  extraArgs:
    primary: []
    secondary: []
  resources:
    limits: {}
    requests: {}
  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: []
[root@master mysql]# 

查看模板文件

[root@master mysql-主从]# 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

2 directories, 22 files
[root@master mysql]# 

安装mysql

[root@master ~]#  helm  install mysql  mysql/  -n default 

查看mysql pod

#查看mysql的pod 正常running即可,可以登录主库创建数据,然后检查从库是否已经同步,此处略
[root@master ~]# kubectl  get pod -n default 

你可能感兴趣的:(kubernetes,kubernetes,数据库)