当前位置:首页 > 文章列表 > 数据库 > MySQL > Deploy MySQL using Helm in Kubernetes with Persistent Volume

Deploy MySQL using Helm in Kubernetes with Persistent Volume

来源:SegmentFault 2023-02-20 20:38:53 0浏览 收藏

小伙伴们对数据库编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《Deploy MySQL using Helm in Kubernetes with Persistent Volume》,就很适合你,本篇文章讲解的知识点主要包括MySQL、docker、yaml、kubernetes。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

How to deploy MySQL-Server Docker using Helm Chart in Kubernetes with Persistent Volume!

  1. Pull MySQL Server docker image from Docker repository, tag it as
    appreciate, then push to private repository.

    image:
      pullPolicy: IfNotPresent
      repository: qio01:5000/mysql-server
      tag: latest
    persistence:
      accessMode: ReadWriteOnce
      enabled: true
      size: 40Gi
      storageClass: standard
    resources:
      requests:
        memory: 512Mi
        cpu: 500m
    serviceType: ClusterIP
    mysqldbPassword: password
  2. Create secret.yaml. If you have defined the mysqlRootPassword, the password will be configured. You could define a different password for root in the values.yaml by setting mysqlRootPassword.

    apiVersion: v1
    kind: Secret
    metadata:
      name: {{ template "fullname" . }}
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    type: Opaque
    data:
      mysqldb-root-password: {{ default "" .Values.mysqlRootPassword | b64enc | quote }}
      mysqldb-password: {{ default "" .Values.mysqldbPassword | b64enc | quote }}
    {{- if .Values.mysqlRootPassword }}
      data-source-name: {{ printf "root%s@(localhost:3306)/" .Values.mysqlRootPassword | b64enc | quote}}
    {{- else }}
      data-source-name: {{ printf "root@(localhost:3306)/" | b64enc | quote}}
    {{- end }}
  3. Edit deployment.yaml. A few things you might be interested to look at. The env configurations. MySQL user password, root password, default database, and allow for empty password all can be found here. The most important configuration is the mountPath of volumeMounts. This is for persistent storage, you need to set the mountPath correctly, different MySQL distribution will use different path.

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: {{ template "fullname" . }}
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    spec:
      template:
        metadata:
          labels:
            app: {{ template "fullname" . }}
            release: {{ .Release.Name }}
            component: "{{.Release.Name}}"
            nautilian.snapshot.enabled: "true"
        spec:
          containers:
          - name: {{ template "fullname" . }}
            image: {{ .Values.image.repository}}:{{ .Values.image.tag}}
            imagePullPolicy: {{ .Values.image.pullPolicy }}
            env:
            - name: MYSQLDB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "fullname" . }}
                  key: mysqldb-root-password
            - name: MYSQLDB_USER
              value: {{ default "" .Values.mysqldbUser | quote }}
            - name: MYSQLDB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "fullname" . }}
                  key: mysqldb-password
            - name: MYSQLDB_DATABASE
              value: {{ default "" .Values.mysqldbDatabase | quote }}
            - name: ALLOW_EMPTY_PASSWORD
              value: "yes"
            ports:
            - name: mysql
              containerPort: 3306
            livenessProbe:
              exec:
                command:
                - mysqladmin
                - ping
              initialDelaySeconds: 30
              timeoutSeconds: 5
            readinessProbe:
              exec:
                command:
                - mysqladmin
                - ping
              initialDelaySeconds: 5
              timeoutSeconds: 1
            resources:
    {{ toYaml .Values.resources | indent 10 }}
            volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
          volumes:
          - name: config
            configMap:
              name: {{ template "fullname" . }}
          - name: data
          {{- if .Values.persistence.enabled }}
            persistentVolumeClaim:
              claimName: {{ .Values.persistence.existingClaim | default (include "fullname" .) }}
          {{- else }}
            emptyDir: {}
          {{- end -}}
  4. Create svc.yaml to create a service in kubernetes.

    apiVersion: v1
    kind: Service
    metadata:
      name: {{ template "fullname" . }}
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
    spec:
      type: {{ .Values.serviceType }}
      ports:
      - name: mysql
        port: 3306
        targetPort: 3306
      selector:
        app: {{ template "fullname" . }}
  5. Create pvc.yaml for the persistent volume.

    {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: {{ template "fullname" . }}
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
        heritage: "{{ .Release.Service }}"
      annotations:
      {{- if .Values.persistence.storageClass }}
        volume.beta.kubernetes.io/storage-class: {{ .Values.persistence.storageClass | quote }}
      {{- else }}
        volume.alpha.kubernetes.io/storage-class: default
      {{- end }}
    spec:
      accessModes:
        - {{ .Values.persistence.accessMode | quote }}
      resources:
        requests:
          storage: {{ .Values.persistence.size | quote }}
    {{- end }}
  6. Deploy using helm install.

    $ helm install  --name  --namespace  name
  7. Login to the shell of the Pod to do MySQL configurations. Grant all privileges on that database and (in the future) tables. WITH GRANT OPTION creates a MySQL user that can edit the permissions of other users.

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    FLUSH PRIVILEGES;

今天关于《Deploy MySQL using Helm in Kubernetes with Persistent Volume》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

版本声明
本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
Centos 7 解压安装mysql5.7
下一篇
Centos 7 解压安装mysql5.7
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    543次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    516次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    500次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    485次学习
查看更多
AI推荐
  • SuperCLUE中文大模型评测基准:功能、能力维度与应用指南
    SuperCLUE
    SuperCLUE是权威的中文大语言模型综合评测基准,涵盖语言理解、知识应用、AI Agent智能体及安全性等12项核心能力。通过多轮对话与客观测试,定期发布榜单与技术报告,为模型研发、优化及行业选型提供科学依据。
    166次使用
  • C-Eval中文评测基准:大语言模型多学科能力评估指南
    C-Eval
    深入了解C-Eval中文评估套件,涵盖52个学科与4级难度。本文详解其功能特点、Zero-shot/Few-shot使用方法及代码示例,助您全面评测LLM中文理解与泛化能力。
    90次使用
  • AI Prompt Library:免费AI提示词库,助力ChatGPT高效创作与营销
    AI Prompt Library
    探索AI Prompt Library免费资源库,涵盖营销、写作及多场景AI提示词。兼容ChatGPT、Claude等工具,一键复制优化输出,提升工作效率。
    11次使用
  • LangGPT提示词框架:结构化Prompt设计方法与开源工具指南
    LangGPT
    LangGPT是一种受编程语言启发的结构化提示词设计工具,提供双层框架、模块化模板及变量功能,帮助用户高效编写高质量Prompt。该项目已在GitHub免费开源,适用于内容创作、编程辅助等多场景。
    27次使用
  • ClickPrompt:AI提示词生成与优化工具,支持Stable Diffusion、ChatGPT及代码辅助
    ClickPrompt
    ClickPrompt是一款专为AI提示词编写者设计的开源在线工具,支持Stable Diffusion绘图、ChatGPT对话及GitHub Copilot代码辅助。提供Prompt自动生成、一键运行、社区分享及可视化优化功能,帮助用户高效获取精准AI输出。
    56次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码