helm k8s包管理器常用指南

299 阅读4分钟

摘要:本文主要介绍helm的入门使用

创建helm空项目

helm create nginx-chart

打包helm项目

  • 直接打包:helm package ./nginx-chart
  • 保存到本地仓库:helm repo index ./nginx-chart --url <repository-url>
  • 如果你想要打包一个特定版本的 Chart 并指定应用版本:helm package ./nginx-chart --version 1.2.3 --app-version 4.5.6

模板渲染

用于输出k8s可读的yaml文件 使用以下命令来渲染模板:

helm template [RELEASE_NAME] [CHART_PATH] [flags]
  • [RELEASE_NAME] 是你给 Helm release 指定的名称。
  • [CHART_PATH] 是你的 Chart 路径,可以是本地路径或者远程仓库中的 Chart 名称。
  • [flags] 是可选参数,例如设置 values、定义命名空间等。

案例

  • 保存渲染结果:helm template [RELEASE_NAME] [CHART_PATH] > [OUTPUT_FILE].yaml
helm template v1.0.0 ./nginx-chart > k8s-nginx.yaml
  • 使用自定义 values:helm template [RELEASE_NAME] [CHART_PATH] -f myvalues.yaml
helm template v1.0.0 ./nginx-chart -f myvalues.yaml > k8s-nginx.yaml
  • 输出到指定文件夹: helm template v1.0.0 ./nginx-chart --output-dir ./k8s-nginx

展示helm包的结构数据

  • helm show all [chart] - 显示chart的所有信息
  • helm show chart [chart] - 显示chart定义
  • helm show crds [chart] - 显示chart的CRD
  • helm show readme [chart] - 显示chart的README
  • helm show values [chart] - 显示chart的values

案例

  • 从文件夹获取信息展示:helm show all ./nginx-chart
  • .tgz包中获取信息展示:helm show all nginx-chart-0.1.0.tgz

依赖构建

一般用于复杂多模块的项目使用。文档地址:helm.sh/zh/docs/hel…

Chart.yaml

  • 本地依赖
# Chart.yaml
dependencies:
- name: nginx
  version: "1.2.3"
  repository: "file://../dependency_chart/nginx"
  • 仓库依赖
# Chart.yaml
dependencies:
- name: nginx
  version: "1.2.3"
  repository: "https://example.com/charts"
- name: memcached
  version: "3.2.1"
  repository: "https://another.example.com/charts"
  • helm dependency build - 基于Chart.lock文件重新构建charts/目录
  • helm dependency list - 列出给定chart的依赖
  • helm dependency update - 基于Chart.yaml内容升级charts/

案例

  • 构建依赖的chart.tgzhelm dependency build nisec-chart

构建本地的helm仓库

直接使用nginx做静态资源服务器即可,或者ChartMuseumchartmuseum.com/docs/#using…

  • 创建一个空文件夹nginx-charts
  • 复制nginx-chart-xxx.tgz包到这个文件夹
  • 生成index.yaml文件:helm repo index nginx-charts/ --url https://example.xxx.com/nginx-charts

helm repos仓库相关使用

  • 搜索公开仓库的包:helm search hub nginx

搜索本地添加的repo中的包

  • 新增本地仓库:helm repo add local-chart http://localhost:9999
  • 查看本地仓库列表:helm repo list
  • 搜索本地仓库中的charthelm search repo nginx
  • 更新本地仓库:helm repo update
  • 删除本地仓库:helm repo remove local-chart

helm pull下载包到本地

命令行格式helm pull [CHART] [flags] [CHART]:指定要下载的 Chart 的名称和版本,格式通常为 repository/chart-name。如果版本未指定,默认会下载最新的版本。 常用选项

  • --version:指定要下载的 Chart 的版本。
  • --repo:指定 Chart 所在的远程仓库名称。
  • --output:指定下载 Chart 的保存路径和文件名。
  • --no-update:不更新仓库的本地缓存。
  • --untar:下载后解压缩 Chart 到指定目录。
  • --cert-file:指定客户端证书文件路径,用于 TLS 认证。
  • --key-file:指定客户端私钥文件路径,用于 TLS 认证。
  • --ca-file:指定 CA 证书文件路径,用于 TLS 认证。

假设你想从 local-chart 仓库下载 nginx-chart 的 v1.0.1 版本,并将其保存到当前目录:

helm pull local-chart/nginx-chart --version v1.0.1

如果你想将下载的 Chart 解压缩到当前目录:

helm pull local-chart/nginx-chart --version v1.0.1 --untar

如果你想将下载的 Chart 保存到特定的文件:

helm pull local-chart/nginx-chart --version v1.0.1 --output my-nginx-1.0.1.tgz

helm push推送打包好的chart到远端仓库免去人工生成index.yaml文件

这里推送使用ChartMuseum仓库

生产过程中使用案例

ChartMuseum仓库安装

我访问的地址是http://192.168.137.131:8080/

docker-compose.yml

version: '3'
services:
  chartmuseum:
    image: chartmuseum/chartmuseum:latest
    container_name: chartmuseum
    networks:
      - default
    volumes:
      - ./charts:/charts
    environment:
      - "DEBUG=1"
      - "STORAGE=local"
      - "STORAGE_LOCAL_ROOTDIR=/charts"
    ports:
      - "8080:8080"
networks:
  default:
    external:
      name: nisec
  • 添加到本地仓库:helm repo add chartmuseum http://192.168.137.131:8080
  • 每次使用推荐更新仓库:helm repo update,或更新指定仓库:helm repo update chartmuseum

创建新的chart并推送到远程仓库

创建新的chart

helm create nise-frontend
helm create nise-backend
helm create nise-deploy

先打包frontendbackend

我把里面Chart.yamlversionappVersion版本都改为了1.0.0

helm package ./nise-frontend
helm package ./nise-backend

curl --data-binary "@nise-frontend-1.0.0.tgz" http://192.168.137.131:8080/api/charts
curl --data-binary "@nise-backend-1.0.0.tgz" http://192.168.137.131:8080/api/charts

修改nise-deploy依赖其他两个项目,并且把templatesvalues.yaml文件删掉

Chart.yaml文件内容如下

apiVersion: v2
name: nise-deploy
description: A Helm chart for Kubernetes
type: application
version: 1.0.0
appVersion: "1.0.0"
dependencies:
  - name: nise-frontend
    version: 1.0.0
    repository: http://192.168.137.131:8080
  - name: nise-backend
    version: 1.0.0
    repository: http://192.168.137.131:8080
  • 更新依赖
helm dependency update
  • 打包
helm package ./nise-deploy
  • 发布
curl --data-binary "@nise-deploy-1.0.0.tgz" http://192.168.137.131:8080/api/charts

新环境拉取chart并部署

添加远端地址到本地仓库

helm repo add chartmuseum http://192.168.137.131:8080

搜索chart

root@u-131:/alidata/helm# helm search repo nise
NAME                            CHART VERSION   APP VERSION     DESCRIPTION                
chartmuseum/nise-backend        1.0.0           1.0.0           A Helm chart for Kubernetes
chartmuseum/nise-frontend       1.0.0           1.0.0           A Helm chart for Kubernetes
  • 如果要查询指定chart的所有版本,后面加上 --versions参数

下载指定chart并解压

helm pull chartmuseum/nise-deploy --version 1.0.0 --untar

根据自己的nise-values.yaml构建部署文件

helm template v1.0.0 ./nise-deploy -f myvalues.yaml > k8s-nise.yaml

部署

kubectl apply -f k8s-nise.yaml

注意事项

  • 如果一个chart依赖了其他多个chart,自定义的values.yaml文件可以按照依赖的chart名称来层级定义,如下我依赖了nginx-chart,则可以层级定义
image:
  repository: nginx
  pullPolicy: IfNotPresent
  tag: "latest"
nginx-chart:
  image:
    repository: newnginx