k8s学习记录,包管理工具helm(十三)

Posted Hei蛋炒饭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s学习记录,包管理工具helm(十三)相关的知识,希望对你有一定的参考价值。

kubernetes包管理工具——Helm

安装helm

可以通过访问Helm官网获取安装文档

1、在github上下载安装tar包文件

tar包下载地址

2、拷贝到目标机器上【我是在本机科xue上网情况下 下载后再上传至k8s集群机器上,如果网络情况比较好的话也可以直接在集群上下载】

3、解压安装包,并移动到配置有环境变量的文件夹中【这里是移动到/usr/local/bin目录下】

#移动完文件夹后,使用命令查看helm的版本信息
helm version

helm常用命令

#添加仓库
helm repo add bitnami https://charts.bitnami.com/biitnami
helm repo add ali-stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
#查看helm镜像仓库列表
helm repo list

#查找相关包
helm search repo nginx

#拉取相关包
helm pull bitnami/nginx

#查看helm安装的应用列表
helm list
#helm创建一个chart
helm create helm-test

helm目录树结构

#新建的chart的目录结果
[root@master01 helm-test]# tree
.
├── charts  #依赖文件
├── Chart.yaml  这个 chart的版本信息
├── templates #模板文件夹
│   ├── deployment.yaml #模板文件
│   ├── _helpers.tpl  #自定义的函数或模板
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt # 这个chart的信息内容
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml  #配置全局变量或一些参数

helm中的语法

取值 .Values.replicaCount 取value.yaml中replicaCount的值

#模板文件中的参数值,下面replicas的取值是读取的chart根目录下values.yaml中的replicaCount配置的值
#helm-test/templates/deployment.yaml
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}


#helm-test/values.yaml

# Default values for helm-test.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1

with 取value.yaml中切片的数据

#helm-test/values.yaml
imagePullSecrets:  # [ \'{"name": "a"}\', \'{"name": "b"}\' ]
  - name: a
  - name: b

#helm-test/templates/deployment.yaml
# .Values.image.a.b.c.repository
# .Values.image.a.b.c.pullPolicy
#通过with语法,可以将前面重复的定位值省略,只写一次即可
# with .Values.image.a.b.c
#   .repository
#   .pullPolicy
spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}  
        #  toYaml后面的 “ . ” 就代表 .Values.imagePullSecrets,对应的是values.yaml中 imagePullSecrets的切片取值“a”和“b”
        # nindent 8 代表缩进8个空格

#使用命令测试下  --dry-run 参数代表只打印,不部署,查看语法命令是否正确
helm install test --dry-run .

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "helm-test.fullname" . }}
  labels:
    {{- include "helm-test.labels" . | nindent 4 }}

#include 引用的函数或模板  templates/_helpers.tpl  这个文件

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "helm-test.fullname" -}}     # 定义helm-test.fullname 这个模板
{{- if .Values.fullnameOverride }}   #判断如果有 .Values.fullnameOverride 这个取值
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}  #那么就取前面63个字符,去掉"-"
{{- else }}  # 否则
{{- $name := default .Chart.Name .Values.nameOverride }}   #定义变量name   如果  .Values.nameOverride为空的,那么就取默认default的值 .Chart.Name
{{- if contains $name .Release.Name }}  # 如果name中包含 .Release.Name
{{- .Release.Name | trunc 63 | trimSuffix "-" }}  # 就取.Release.Name的前63个字符,末尾去掉"-"
{{- else }}  #否则
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}  # 就以占位符的方式,输出 .Release.Name-$name  的前63个字符,末尾去掉"-"
{{- end }}
{{- end }}
{{- end }}

# define 定义一个模板
# - 代表去掉空格(加在前面代表去掉前面的,加在后面代表去掉后面的)
# trunc 63  只能取前面63个字符【最多63个字符,because some Kubernetes name fields are limited to this (by the DNS naming spec).】
# 数字为正是从前往后取,数字为负时,从后往前取
# trimSuffix "-"  字符串末尾去掉"-"
# trimPrefix "-"  字符串前面去掉"-"
# default  定义的变量的默认值   .Chart.Name      我这里的取值为: helm-test

# contains 如果$name中包含 .Release.Name  这个Release.Name就是helm install xxx 后面跟的名称
helm install zw --dry-run .

#如果命令后面跟的Release.Name包含了.Chart.Name,那么就以 Release.Name-Chart.Name
helm install zw-helm-test-new --dry-run .

#当我们修改了values.yaml中的fullnameOverride的值时,
nameOverride: ""
fullnameOverride: "change-values-name"

#再运行
helm install helm-test --dry-run .

#也可以通过在命令中使用--set来修改fullnameOverride的值
helm install helm-test --set fullnameOverride=set-fullnameOverride-new --dry-run .

指定名字的优先级 set >> values.yaml >> Release.Name

以上是关于k8s学习记录,包管理工具helm(十三)的主要内容,如果未能解决你的问题,请参考以下文章

Kubernetes(k8s)之k8s的应用的包管理工具Helm

云原生 • Kubernetes一文掌握 k8s 包管理工具 Helm

Helm 使用介绍(K8s 包管理器)

Helm 使用介绍(K8s 包管理器)

Helm 使用介绍(K8s 包管理器)

K8S集群中使用Helm管理应用分发