目录
一.Helm的出现
二.Helm工具
1.部署helm
2.helm可用命令介绍
三.chart
1.添加、查看、删除存储库
2.查找chart、查看chart信息、安装chart等
3.安装chart后产生的release
四.安装mysql举例
1.固定chart安装
2.自定义chart安装
一.Helm的出现
在前面的k8s部署pod等资源的学习中,多数通过yaml文件进行部署,最多的也就部署七八个yaml文件共同搭配,但是像一些应用需要几十个yaml文件一起生效那么我们写起来、改起来并且配起来的复杂度就变高了,我们就需要一个工具去将这些资源整合成我么要部署的应用从而可以更方便地部署这个应用并实现yanl文件高可用减少复杂度,这种情况下就需要用到helm(类似于yum/dnf)。
二.Helm工具
helm一个命令行客户端工具(需要自行安装的),主要用于 Kubernetes 应用 chart (后面会提及)的创建、打包、发布和管理等
1.部署helm
下载包并解压将helm文件拷贝或移动/usr/local/bin下并配置命令补全
[root@k8s-master ~]# wget https://get.helm.sh/helm-v3.12.3-linux-amd64.tar.gz
[root@k8s-master ~]# tar -zxvf helm-v3.12.3-linux-amd64.tar.gz
[root@k8s-master linux-amd64]# ll
total 49428
-rwxr-xr-x 1 1001 docker 50597888 Aug 11 2023 helm
-rw-r--r-- 1 1001 docker 11373 Aug 11 2023 LICENSE
-rw-r--r-- 1 1001 docker 3397 Aug 11 2023 README.md
[root@k8s-master linux-amd64]# cp helm /usr/local/bin/
[root@k8s-master linux-amd64]# helm completion bash > ~/.helmrc
[root@k8s-master linux-amd64]# source ~/.helmrc
[root@k8s-master linux-amd64]# helm version
version.BuildInfo{Version:"v3.12.3", GitCommit:"3a31588ad33fe3b89af5a2a54ee1d25bfe6eaa5e", GitTreeState:"clean", GoVersion:"go1.20.7"}
2.helm可用命令介绍
#helm --help
Usage:helm [command]
Available Commands:completion 为指定的shell生成自动补全脚本create 创建一个指定了名称的chartdependency 管理chart依赖env Helm客户端环境信息get 下载release,可接all、hooks、manifest、notes、valuesall-获取所有已安装的release的信息。名称、状态、版本等hooks-获取指定 release 的 pre-install、post-install、pre-delete 和 post-delete 钩子的信息manifest-获取指定 release 的 Kubernetes 资源清单(manifest)。这个选项会显示 Helm chart 渲染后生成的所有 Kubernetes 资源清单,包括 Deployment、Service、ConfigMap 等资源的详细配置notes-获取指定 release 的笔记(notes)。这个选项会显示与 Helm chart 关联的任何备注或说明,通常包括有关如何访问应用程序、配置细节或其他相关信息values-获取指定 release 的配置值(values)。这个选项会显示 Helm chart 中定义的所有配置值,包括默认值和用户自定义的配置值,帮助了解release内的配置help 获取对后面命令的帮助history 获取发布历史install 安装一个chartlint 检查图表中可能存在的问题list 列出releasepackage 将chart目录存档plugin 安装、列出或卸载Helm插件pull 从远程仓库下载chart并解压push 将chart推到远程registry 从注册表登录或注销repo 添加、列表、删除、更新和索引chart存储库rollback 版本回滚search 查找chart,可用hub或reposhow 查看chart详细信息,可用all、chart、readme、valuesstatus 显示已命名版本的状态template 本地呈现模版test 运行测试uninstall 卸载releaseupgrade 更新releaseverify 验证给定路径上的图表是否已签名且有效version 查看版本信息
三.chart
用来描述k8s资源的文件集合,接下来进行配置chart资源存储库(对于资源库主要使用helm repo)和chart的一些操作。这里又设计到一个概念release(基于 Chart 的部署实体,一个 chart 被 Helm 运行后将会生成对应的一个release,将在 k8s 中创建出真实运行的资源对象。)
1.添加、查看、删除存储库
[root@k8s-master helm]# helm repo add stable http://mirror.azure.cn/kubernetes/charts
#微软
[root@k8s-master helm]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
#阿里云
[root@k8s-master helm]# helm repo list
NAME URL
stable http://mirror.azure.cn/kubernetes/charts
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
[root@k8s-master helm]# helm search repo stable #列出改存储库中所有的资源清单
[root@k8s-master helm]# helm repo list #删除指定名称存储库
NAME URL
stable http://mirror.azure.cn/kubernetes/charts
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami https://charts.bitnami.com/bitnami
[root@k8s-master helm]# helm repo remove bitnami
"bitnami" has been removed from your repositories
[root@k8s-master helm]# helm repo list
NAME URL
stable http://mirror.azure.cn/kubernetes/charts
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
2.查找chart、查看chart信息、安装chart等
[root@k8s-master helm]# helm search repo stable/mysql #可以指定存储库也可以不指定
[root@k8s-master helm]# helm search repo mysql
[root@k8s-master helm]# helm show chart stable/mysql #查看具体某个chart的信息
apiVersion: v1
appVersion: 5.7.30
deprecated: true
description: DEPRECATED - Fast, reliable, scalable, and easy to use open-source relationaldatabase system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.9
[root@k8s-master helm]# helm install my-mysql stable/mysql
#安装chart,格式为helm install 安装到本机的名称 (存储库)/chart名称
[root@k8s-master helm]# helm status my-mysql #获取发布状态,这里面的额信息会告诉你的安装完成后如何获取所需参数,如安装的mysql机会提示你获取临时登录密码和外部访问方式等
[root@k8s-master helm]# helm show values stable/mysql #使用此命令查看安装完成后需要配置的PV等,如mysql的就需要你配置一个8Gi的PV,因为你安装过后会产生一个pvc,但是缺少pv,你要创建好合适的pv并且和pvc成功绑定后,pod和svc才能正常使用
## Persist data to a persistent volume
persistence:enabled: true## database data Persistent Volume Storage Class## If defined, storageClassName: <storageClass>## If set to "-", storageClassName: "", which disables dynamic provisioning## If undefined (the default) or set to null, no storageClassName spec is## set, choosing the default provisioner. (gp2 on AWS, standard on## GKE, AWS & OpenStack)### storageClass: "-"accessMode: ReadWriteOncesize: 8Giannotations: {}
3.安装chart后产生的release
[root@k8s-master helm]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
my-mysql default 1 2024-03-17 10:07:52.665967704 +0800 CST deployed mysql-1.6.9 5.7.30
四.安装mysql举例
1.固定chart安装
(1)安装mysql
[root@k8s-master helm]# helm install db stable/mysqlTo get your root password run:#获取临时登录密码MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)
To connect to your database:
1. Run an Ubuntu pod that you can use as a client:
kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il
2. Install the mysql client:
$ apt-get update && apt-get install mysql-client -y
3. Connect using the mysql cli, then provide your password:$ mysql -h my-mysql -p
To connect to your database directly from outside the K8s cluster:MYSQL_HOST=127.0.0.1MYSQL_PORT=3306
# Execute the following command to route the connection:kubectl port-forward svc/my-mysql 3306
mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}#外部连接方式
(2)查看pod和svc状态
[root@k8s-master helm]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/my-mysql-b6999b8b5-h6vlr 0/1 Pendding 0 22m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h23m
service/my-mysql ClusterIP 10.106.24.9 <none> 3306/TCP 4h22m
#pod处于Pendding状态,describe一下pod查看问题所在,如下这种问题就是缺少存储等条件
Warning FailedScheduling 2m default-scheduler 0/4 nodes are available:
4 pod has unbound immediate PersistentVolumeClaims. preemption: 0/3 nodes are
available: 3 Preemption is not helpful for scheduling.
#查看有没有pvc请求,然后进去查看到是有一个8Gi的pvc的(也可用通过上面提到的使用helm show values stable/mysql查看到),但是我们现在并没有pv去给他绑定,所以先创建一个符号需求的pv,查看绑定状态
[root@k8s-master helm]# kubectl get pvc my-mysql -o yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:annotations:meta.helm.sh/release-name: my-mysqlmeta.helm.sh/release-namespace: defaultpv.kubernetes.io/bind-completed: "yes"pv.kubernetes.io/bound-by-controller: "yes"creationTimestamp: "2024-03-17T02:07:53Z"finalizers:- kubernetes.io/pvc-protectionlabels:app: my-mysqlapp.kubernetes.io/managed-by: Helmchart: mysql-1.6.9heritage: Helmrelease: my-mysqlname: my-mysqlnamespace: defaultresourceVersion: "1478"uid: e6456c1d-f4ea-44e3-9372-6fd875d81e9f
spec:accessModes:- ReadWriteOnceresources:requests:storage: 8GivolumeMode: FilesystemvolumeName: mysql
status:accessModes:- ReadWriteOncecapacity:storage: 8Giphase: Bound
[root@k8s-master helm]# cat mysql-pv.yaml #pvc和pv已绑定
apiVersion: v1
kind: PersistentVolume
metadata:name: mysql
spec:capacity:storage: 8Gi accessModes:- ReadWriteOncenfs:path: /root/helm/shareserver: 192.168.2.150
[root@k8s-master helm]# ll
total 15660
-rw-r--r-- 1 root root 16028423 Aug 11 2023 helm-v3.12.3-linux-amd64.tar.gz
drwxr-xr-x 2 1001 docker 50 Aug 11 2023 linux-amd64
-rw-r--r-- 1 root root 191 Mar 17 10:13 mysql-pv.yaml
drwxr-xr-x 5 polkitd root 314 Mar 17 14:59 share
[root@k8s-master helm]# cat /etc/exports
/root/helm/share 192.168.2.0/24(rw,no_root_squash)
[root@k8s-master helm]# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/my-mysql Bound mysql 8Gi RWO 4h29m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql 8Gi RWO Retain Bound default/my-mysql 4h23m
[root@k8s-master helm]# kubectl get pods #pod运行起来了
NAME READY STATUS RESTARTS AGE
my-mysql-b6999b8b5-h6vlr 1/1 Running 0 4h30m
(3)获取临时密码并进入pod测试是否能够登录
[root@k8s-master helm]# kubectl get secret --namespace default my-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo
dwcyEybl7l
[root@k8s-master helm]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/my-mysql-b6999b8b5-h6vlr 1/1 Running 0 4h34m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h35m
service/my-mysql ClusterIP 10.106.24.9 <none> 3306/TCP 4h34m
[root@k8s-master helm]# kubectl exec -it my-mysql-b6999b8b5-h6vlr -- /bin/bash
Defaulted container "my-mysql" out of: my-mysql, remove-lost-found (init)
root@my-mysql-b6999b8b5-h6vlr:/# mysql -uroot -p'dwcyEybl7l'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3245
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
[root@k8s-master helm]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-mysql-b6999b8b5-h6vlr 1/1 Running 0 4h37m 10.244.36.65 k8s-node1 <none> <none>
#在k8s-node1这个node上进行登录测试
[root@k8s-node1 ~]# mysql -h 10.106.24.9 -P3306 -uroot -p #使用my-mysql的clusterip+端口
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3226
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
(4)集群外部navicat也可登录的配置测试
[root@k8s-master helm]# kubectl edit svc my-mysql #将svc的type改为NodePort,如果还有特定端口需求请自行添加指定端口号
service/my-mysql edited
[root@k8s-master helm]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h42m
my-mysql NodePort 10.106.24.9 <none> 3306:31641/TCP 4h41m
使用k8s-node1的宿主机地址+nodeport映射的端口号登录
2.自定义chart安装
通过-f指定yaml配置文件或者在命令行通过set来设定安装前你需要指定的参数,请注意,以下的my-mysql-config.yaml等内容都是可以在helm show values stable/mysql中查询到的
(1)同样是定义好nfs共享存储信息和所需pv
[root@k8s-master helm]# tail -1 /etc/exports
/root/helm/share1 192.168.2.0/24(rw,no_root_squash)
[root@k8s-master helm]# kubectl apply -f mysql-pv.yaml
persistentvolume/mysql created
[root@k8s-master helm]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql 8Gi RWO Retain Available 10s
(2)创建好要自定义的参数等
[root@k8s-master helm]# cat my-mysql-config.yaml
persistence:enabled: trueaccessMode: ReadWriteOncesize: 8Gi
mysqlUser: "sulibao"
mysqlPassword: "sulibao"
mysqlRootPassword: "sulibao"
mysqlDatabase: "k8s"
[root@k8s-master helm]# helm install my-mysql -f my-mysql-config.yaml stable/mysql #pod直接运行成功
[root@k8s-master helm]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/my-mysql-56856b55c7-cj76j 1/1 Running 0 60s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h6m
service/my-mysql ClusterIP 10.103.197.187 <none> 3306/TCP 60s
(3)进入pod测试配置的参数是否正确
[root@k8s-master helm]# kubectl exec -it my-mysql-56856b55c7-cj76j -- /bin/bash
Defaulted container "my-mysql" out of: my-mysql, remove-lost-found (init)
root@my-mysql-56856b55c7-cj76j:/# mysql -uroot -psulibao
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| k8s |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> select user from mysql.user;
+---------------+
| user |
+---------------+
| root |
| sulibao |
| mysql.session |
| mysql.sys |
| root |
+---------------+
5 rows in set (0.01 sec)
mysql>
(4)此外是可以通过set来进行替换参数的
如上使用yaml文件进行安装的就可以改为这样
[root@k8s-master helm]# helm install my-mysql \
> --set persistence.enabled=true \
> --set persistence.size=8Gi \
> --set mysqlUser="sulibao" \
> --set mysqlPassword="sulibao" \
> --set mysqlRootPassword="sulibao" \
> --set mysqlDatabase="k8s" stable/mysql