菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
22
0

k8s~helm构建一个应用

原创
05/13 14:22
阅读数 78733

三个概念

  1. chart:包含了创建Kubernetes的一个应用实例的必要信息
  2. config:包含了应用发布配置信息
  3. release:是一个chart及其配置的一个运行实例

建立一个helm charts

helm create hello-world
  • Chart.yaml 用于描述这个Chart的相关信息,包括名字、描述信息以及版本等。
    仅仅是一些简单的文本描述

  • values.yaml 用于存储 templates 目录中模板文件中用到变量的值。

  • NOTES.txt 用于介绍 Chart 部署后的一些信息,例如:如何使用这个 Chart、列出缺省的设置等。

  • Templates 目录下是 YAML 文件的模板,该模板文件遵循 Go template 语法。

Templates 目录下 YAML 文件模板的值默认都是在 values.yaml 里定义的,比如在 deployment.yaml 中定义的容器镜像。
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
其中的 .Values.image.repository 的值就是在 values.yaml 里定义的 nginx,.Values.image.tag 的值就是 stable。
以上两个变量值是在 create chart 的时候就自动生成的默认值,你可以根据实际情况进行修改。实际上都是静态文本,只在是执行的时候才被解析.

构建一个helm应用

打开 Chart.yaml,可以看到内容如下,配置名称和版本

apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: mychart
version: 0.1.0

编辑 values.yaml,它默认会在 Kubernetes 部署一个 Nginx。下面是 mychart 应用的 values.yaml 文件的内容:

$ cat mychart/values.yaml
# Default values for mychart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: nginx
  tag: stable
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - chart-example.local
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}

检查模块配置

$ helm lint hello-world/

打包

helm package hello-world
helm package mychart --debug #显示详细信息

启动helm本地仓库(helm3已被移除)

helm serve  --repo-path /data/helm/repository/ --url http://172.17.0.22:8879/charts/ &

仓库刷新和查询应用

$ helm repo update
$ helm search mychart
NAME         	CHART VERSION	APP VERSION	DESCRIPTION
local/hello-world	0.1.0        	1.0        	A Helm chart for Kubernetes

在 Kubernetes 中部署应用

Chart 被发布到仓储后,就可以通过 helm install 命令部署该 Chart。

helm install  hello local/hello-world

查看Release的状态信息

 helm status wordpress

升级charts

helm upgrade  wordpress stable/wordpress
helm upgrade --install --force hello-world ./hello.tgz --namespace test  # 也可以指定命名空间和它的taz包

回滚到上一个版本

helm rollback hello-world 1 # 向上归滚一个版本

发表评论

0/200
22 点赞
0 评论
收藏