Helm tutorial 1
Helm 是 Kubernetes 的包管理工具,旨在简化应用程序在 Kubernetes 集群中的部署和管理。 citeturn0search3 它通过将应用程序的 Kubernetes 资源定义打包成称为 Chart 的格式,提供了自动化的应用分发方式。
核心概念:
-
Chart:Helm 包的基本单位,包含了在 Kubernetes 集群中运行应用程序所需的所有资源定义。
-
Repository(仓库):存放和共享 Charts 的地方,类似于 Perl 的 CPAN 或 Fedora 的软件包仓库。
-
Release:在 Kubernetes 集群中运行的 Chart 的一个实例。一个 Chart 可以在同一个集群中安装多次,每次安装都会创建一个新的 Release。
使用 Helm 的基本步骤:
-
安装 Helm:首先,需要在本地环境中安装 Helm 客户端。可以通过多种方式安装,例如使用包管理工具或从 GitHub 发布页面下载。
-
添加 Chart 仓库:安装完成后,可以添加 Chart 仓库。例如,添加 Bitnami 仓库:
helm repo add bitnami https://charts.bitnami.com/bitnami
-
搜索 Charts:使用
helm search
命令查找可用的 Charts。例如,搜索 WordPress:helm search repo wordpress
-
安装 Chart:使用
helm install
命令安装所需的 Chart。例如,安装名为my-release
的 WordPress:helm install my-release bitnami/wordpress
-
管理 Release:安装后,可以使用
helm list
查看所有的 Releases,使用helm upgrade
升级 Release,使用helm rollback
回滚到之前的版本,使用helm uninstall
卸载 Release。
通过 Helm,开发者和运维人员可以更高效地管理 Kubernetes 应用程序的生命周期,简化部署过程,提高工作效率。
Example 1
To install MySQL using Helm in the current namespace of your Kubernetes (k8s) cluster, follow these steps:
Step 1: Add the Bitnami Helm Repository
Bitnami provides a well-maintained MySQL Helm chart.
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
Step 2: Install MySQL in the Current Namespace
To install MySQL in the current namespace, run:
helm install my-mysql bitnami/mysql
Note: If you are using a custom namespace, ensure that your
kubectl
context is set to the desired namespace:kubectl config set-context --current --namespace=
Step 3: Verify Installation
Check the status of the MySQL deployment:
kubectl get pods
To see the Helm release:
helm list
Step 4: Retrieve the MySQL Root Password
MySQL generates a random root password by default. You can retrieve it using:
kubectl get secret my-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode
Step 5: Connect to MySQL
To connect to MySQL from a pod inside the cluster:
kubectl run my-mysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:latest --namespace default -- \
mysql -h my-mysql -uroot -p
For external access, you may need to configure a LoadBalancer
or NodePort
service.
Would you like to customize MySQL with specific configurations (e.g., storage size, replication, custom user/passwords)? 🚀
Example 2
To install Redis using Helm in the current namespace of your Kubernetes cluster, follow these steps:
Step 1: Add the Bitnami Helm Repository
Bitnami provides an official Helm chart for Redis.
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
Step 2: Install Redis in the Current Namespace
To install Redis in the current namespace, simply run:
helm install my-redis bitnami/redis
Note: If you are using a specific namespace, ensure your
kubectl
context is set to it:kubectl config set-context --current --namespace=
Step 3: Verify the Installation
Check if the Redis pods are running:
kubectl get pods
To see the Helm release:
helm list
Step 4: Retrieve the Redis Password
The default Redis installation requires authentication. Retrieve the Redis password:
kubectl get secret my-redis -o jsonpath="{.data.redis-password}" | base64 --decode
Step 5: Connect to Redis
To access Redis from within the cluster, you can use:
kubectl run my-redis-client --rm --tty -i --restart='Never' --image docker.io/bitnami/redis:latest -- \
redis-cli -h my-redis-master -a <your-redis-password>
For external access, consider setting the service type to LoadBalancer
or NodePort
by modifying the values.yaml
or using:
helm install my-redis bitnami/redis --set service.type=LoadBalancer
Optional: Customizing Redis Installation
You can customize Redis with parameters like persistence, cluster mode, and custom passwords. Example:
helm install my-redis bitnami/redis --set auth.password=mysecurepassword
install mysql
Best practice
Helm 是 Kubernetes 的包管理工具,旨在简化应用程序在 Kubernetes 集群中的部署和管理。为了有效地使用 Helm,以下是一些最佳实践建议:
-
结构化 Chart 文件:确保您的 Chart 目录结构清晰,包括
templates
、values.yaml
、Chart.yaml
等必要文件。这有助于维护和理解 Chart 的内容。 -
使用
values.yaml
管理配置:将可配置参数放入values.yaml
文件中,使 Chart 更加灵活,方便在不同环境中复用。 -
模板化资源定义:利用 Helm 的模板功能,将 Kubernetes 资源的定义模板化,以适应不同的部署需求。
-
遵循 YAML 语法:模板应遵循 YAML 语法,除非使用 JSON 语法能显著降低格式问题的风险。
-
谨慎生成随机值:在模板中生成随机数据(如加密密钥)时,要注意在升级或重新执行模板时可能导致资源不一致。
-
使用
helm upgrade --install
命令:该命令可以在不存在 Release 时安装,存在时升级,简化部署流程。 -
管理依赖关系:在
Chart.yaml
中明确列出依赖的 Charts,并使用helm dependency
命令管理这些依赖关系。 -
使用钩子(Hooks):利用 Helm 提供的钩子机制,在部署过程中执行特定操作,如迁移数据库或清理资源。
-
版本控制:为您的 Charts 制定明确的版本策略,确保在不同环境中使用正确的版本。
-
定期更新 Charts:及时更新您的 Charts,以包含最新的功能和安全修复。
遵循这些最佳实践,可以提高 Helm Charts 的可维护性和可复用性,确保在 Kubernetes 环境中的稳定部署。