构建Helm chart和chart使用管道与函数简介

目录

一.创建helm chart(以nginx为例)

1.通过create去创建模板

2.查看模板下的文件

3.用chart模版安装nginx

二.版本更新和回滚问题

1.使用upgrade -f values.yaml或者命令行--set来设置

2.查看历史版本并回滚

三.helm模板内管道和函数

1.defautl

2.quote

3.indent和nindent

4.upper

5.title

6.toYaml


上一篇文章说到,我们可以通过helm将众多已经初步配置好的yaml文件下载来整合使用,甚至还可以自己定义好需要的安装参数用于下载完成后直接使用而不需要过多更改,现在仍然可以在这些功能上继续推进。创建helm chart模板,现成的yaml文件,自己只需要更改values变量文件即可,如下:

一.创建helm chart(以nginx为例)

1.通过create去创建模板

[root@k8s-master helm]# helm create nginx  #创建完成后会在本地创建一个nginx目录
drwxr-xr-x 4 root    root         93 Mar 18 19:58 nginx
[root@k8s-master helm]# tree nginx
nginx
├── charts   #存放的依赖的子chart
├── 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   #存储的是你在的templates中文件需要用到的变量
​
3 directories, 10 files

2.查看模板下的文件

[root@k8s-master helm]# cat nginx/templates/service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: {{ include "nginx.fullname" . }}    #这些使用{{}}括起来的变量就是你需要在values.yaml文件中用值替换的变量
  labels:
    {{- include "nginx.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "nginx.selectorLabels" . | nindent 4 }}
​

3.用chart模版安装nginx

(1)在values.yaml中定义变量,如下是nginx模版的原始文件

[root@k8s-master nginx]# cat values.yaml 
# Default values for nginx.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
​
replicaCount: 1
​
image:
  repository: nginx    #找到需要的地方,这里是镜像名
  pullPolicy: IfNotPresent   #镜像拉取策略
  # Overrides the image tag whose default is the chart appVersion.
  tag: ""    #版本信息
​
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
​
serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name: ""
​
podAnnotations: {}
​
podSecurityContext: {}
  # fsGroup: 2000
​
securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000
​
service:    #service类型和端口
  type: ClusterIP
  port: 80
​
ingress:   #ingress的相关配置
  enabled: false
  className: ""
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  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
​
autoscaling:
  enabled: false
  minReplicas: 1
  maxReplicas: 100
  targetCPUUtilizationPercentage: 80
  # targetMemoryUtilizationPercentage: 80
​
nodeSelector: {}
​
tolerations: []
​
affinity: {}

(2)配置后的values.yaml

[root@k8s-master helm]# cat nginx/values.yaml 
# Default values for nginx.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
​
replicaCount: 1
​
image:
  repository: nginx
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: "1.17.3"   #版本为1.17.3
  
​
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
​
serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name: ""
​
podAnnotations: {}
​
podSecurityContext: {}
  # fsGroup: 2000
​
securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000
​
service:
  type: NodePort    #NodePort类型
  port: 80     #80端口
​
ingress:
  enabled: false
  className: ""
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  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
​
autoscaling:
  enabled: false
  minReplicas: 1
  maxReplicas: 100
  targetCPUUtilizationPercentage: 80
  # targetMemoryUtilizationPercentage: 80
​
nodeSelector: {}
​
tolerations: []
​
affinity: {}

(3)安装命令

同k8s运行pod差不多,都可以先测试一下但不实际运行helm install --dry-run my-nginx nginx/,卸载将install替换为uninstall即可

[root@k8s-master helm]# helm install my-nginx nginx/
#指定好安装后的名称后要指定这个nginx模版目录,安装完成后还会提示你访问方式
NAME: my-nginx
LAST DEPLOYED: Mon Mar 18 20:27:00 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services my-nginx)
  export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT
  
  [root@k8s-master helm]# kubectl  get pods,svc
NAME                           READY   STATUS    RESTARTS   AGE
pod/my-nginx-9d774fb48-94wd8   1/1     Running   0          3m29s
​
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        90m
service/my-nginx     NodePort    10.97.171.192   <none>        80:30140/TCP   3m29s
[root@k8s-master helm]# curl http://192.168.2.151:30140
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
​
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
​
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

二.版本更新和回滚问题

1.使用upgrade -f values.yaml或者命令行--set来设置

[root@k8s-master helm]# kubectl  describe pod my-nginx-9d774fb48-94wd8 | grep Image
    Image:          nginx:1.17.5
    Image ID:       docker.io/library/nginx@sha256:922c815aa4df050d4df476e92daed4231f466acc8ee90e0e774951b0fd7195a4
[root@k8s-master helm]# vim nginx/values.yaml 
[root@k8s-master helm]# vim nginx/values.yaml 
[root@k8s-master helm]# helm upgrade -f nginx/values.yaml my-nginx nginx/
Release "my-nginx" has been upgraded. Happy Helming!
NAME: my-nginx
LAST DEPLOYED: Mon Mar 18 20:35:40 2024
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
  export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services my-nginx)
  export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT
[root@k8s-master helm]# kubectl  get pods
NAME                        READY   STATUS              RESTARTS   AGE
my-nginx-698cb48f59-v4pbb   0/1     ContainerCreating   0          7s
my-nginx-9d774fb48-94wd8    1/1     Running             0          8m48s
[root@k8s-master helm]# kubectl  get pods
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-698cb48f59-v4pbb   1/1     Running   0          64s
[root@k8s-master helm]# kubectl  describe pod my-nginx-698cb48f59-v4pbb | grep Image
    Image:          nginx:1.17.8
    Image ID:       docker.io/library/nginx@sha256:380eb808e2a3b0dd954f92c1cae2f845e6558a15037efefcabc5b4e03d666d03

2.查看历史版本并回滚

[root@k8s-master helm]# helm history my-nginx
REVISION    UPDATED                     STATUS      CHART       APP VERSION DESCRIPTION     
1           Mon Mar 18 20:27:00 2024    superseded  nginx-0.1.0 1.16.0      Install complete
2           Mon Mar 18 20:35:40 2024    deployed    nginx-0.1.0 1.16.0      Upgrade complete
​
[root@k8s-master helm]# helm rollback my-nginx 1
Rollback was a success! Happy Helming!
[root@k8s-master helm]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
my-nginx-9d774fb48-9pv7q   1/1     Running   0          5s
[root@k8s-master helm]# kubectl  describe pods my-nginx-9d774fb48-9pv7q | grep Image
    Image:          nginx:1.17.5
    Image ID:       docker.io/library/nginx@sha256:922c815aa4df050d4df476e92daed4231f466acc8ee90e0e774951b0fd7195a4

三.helm模板内管道和函数

1.defautl

也就是和管道符号配合,当你values中的值未指定时,就使用此默认值

以nginx模版的port为例,将其设置为空,但设置了默认值为80,创建过后仍然会暴露80端口

[root@k8s-master helm]# cat nginx/values.yaml | grep port
  port: 
#在deployment文件中引用了此变量的地方设置default
[root@k8s-master helm]# cat nginx/templates/deployment.yaml  | grep default
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
              containerPort: {{ .Values.service.port | default 80 }}   #这样写
              
#在service文件中引用了此变量的地方设置default
[root@k8s-master helm]# cat nginx/templates/service.yaml | grep default
    - port: {{ .Values.service.port | default 80 }}
[root@k8s-master helm]# helm install nginx nginx/
[root@k8s-master helm]# kubectl get pods,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6467995c7d-gt4g5   1/1     Running   0          8s
​
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        113m
service/nginx        NodePort    10.106.164.22   <none>        80:32598/TCP   8s

2.quote

这个表示的是为某个变量设置后,无论设定的变量值是什么类型,都将其识别为字符串类型,书写格式如下

{{ quote .Values.service.port }}

3.indent和nindent

indent表示在同一行变量值前设定多少个缩进值,nindent表示换行后再在变量值前设置多少个缩进值,书写格式如下

{{ .Values.service.port | indent/nindent 10 }}

4.upper

将值都变为大写,格式如下

{{ upper .Values.service.port }}

5.title

将首字母设置为大写,格式如下

{{ title .Values.service.port }}

6.toYaml

将一整个yaml块都取值取过来,一般还需要陪着缩进一起使用,格式如下

{{ toYaml .Values.service.port | indent/nindent 10 }}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/274313.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

(008)Unity StateMachineBehaviour的坑

文章目录 StateMachineBehaviour同名函数的调用问题StateMachineBehaviour 的 OnState*、OnStateMachine* 的区别 StateMachineBehaviour同名函数的调用问题 1.如果脚本中&#xff0c;两个同名的函数都存在&#xff0c;那么两个函数都会被调用&#xff1b;如果只有其中一个同名…

自动驾驶决策 - 规划 - 控制 (持续更新!!!)

总目录 Frenet与Cartesian坐标系 Apollo基础 - Frenet坐标系 车辆模型 车辆运动学和动力学模型 控制算法 PID控制器轨迹跟随实现 Pure Pursuit控制器路径跟随 路径跟踪算法Stanley 实现 c 无人驾驶LQR控制算法 c 实现 MPC自动驾驶横向控制算法实现 c 双环PID控制详细讲解 …

人外周血单核细胞来源树突状细胞(MoDC)的制备(一)

MoDC制备方法简图 背景 DC是“Dendritic Cells”的缩写&#xff0c;中文全称为“树突状细胞”&#xff0c;因其成熟时伸出许多树突样或伪足样突起而得名。DC 是由 2011 年诺贝尔奖获得者、加拿大籍科学家 Ralph M. Steinman 于1973 年发现的&#xff0c;是目前发现的功能最强的…

下拉树级带搜索功能

可以直接复制粘贴到自己的项目里,方法处把接口替换一下 <template><div><el-popoverplacement"bottom"width"200"trigger"click"><el-inputslot"reference"class"mrInput":placeholder"placehol…

虚拟机VMware上 centos7 的网络配置

第一步&#xff1a;权限的切换 由普通用户切换到管理者/超级用户 用户名为&#xff1a;root 密码为&#xff1a;自己安装 linux 时第一次设置的密码 su -root管理者/超级用户的命令提示符是“#”&#xff0c;普通用户的命令提示符是“$”。当看到你的命令提示符为“$”时&…

2、鸿蒙学习-申请调试证书和调试Profile文件

申请发布证书 发布证书由AGC颁发的、为HarmonyOS应用配置签名信息的数字证书&#xff0c;可保障软件代码完整性和发布者身份真实性。证书格式为.cer&#xff0c;包含公钥、证书指纹等信息。 说明 请确保您的开发者帐号已实名认证。每个帐号最多申请1个发布证书。 1、登录AppGa…

0基础学习VR全景平台篇第145篇:图层控件功能

大家好&#xff0c;欢迎观看蛙色VR官方——后台使用系列课程&#xff01;这期&#xff0c;我们将为大家介绍如何使用图层控件功能。 一.如何使用图层控件功能&#xff1f; 进入作品编辑页面&#xff0c;点击左边的控件后就可以在右边进行相应设置。 二.图层控件有哪些功能&am…

综合练习(python)

前言 有了前面的知识积累&#xff0c;我们这里做两个小练习&#xff0c;都要灵活运用前面的知识。 First 需求 根据美国/英国各自YouTube的数据&#xff0c;绘制出各自的评论数量的直方图 第一版 import numpy as np from matplotlib import pyplot as plt import matplo…

GAMES101 学习3

Lecture 13 ~ 16 Shadow mapping 一种图像空间算法生成阴影时不需要知道场景中的几何信息会产生走样现象 最重要的思想&#xff1a;如果有的点不在阴影里你又能看到这个点&#xff0c;那么说明摄像机可以看到这个点&#xff0c;光源也可以看到这个点 经典的Shadow mapping …

【计算机考研】408全年复习保姆级规划+资料

基础阶段 408一共只分为选择题和大题&#xff0c;选择题80分&#xff0c;大题70分。 基础阶段应该要形成相对完整的知识体系&#xff0c;基础知识大概都需要有印象。 在基础阶段&#xff0c;建议不做大题&#xff0c;把课后选择题都好好的做一遍 第一遍的正确率无需过于关注…

.Net Core 中间件验签

文章目录 为什么是用中间件而不是筛选器&#xff1f;代码实现技术要点context.Request.EnableBuffering()指针问题 小结 为什么是用中间件而不是筛选器&#xff1f; 为什么要用中间件验签&#xff0c;而不是筛选器去验签? 1、根据上图我们可以看到&#xff0c;中间件在筛选器之…

华为组网:核心交换机旁挂防火墙,基于ACL重定向配置实验

如图所示&#xff0c;由于业务需要&#xff0c;用户有访问Internet的需求。 用户通过接入层交换机SwitchB和核心层交换机SwitchA以及接入网关Router与Internet进行通信。为了保证数据和网络的安全性&#xff0c;用户希望保证Internet到服务器全部流量的安全性&#xff0c;配置重…

Maven项目如何导入依赖包

一、导入依赖包 导入mysql依赖包 第一步&#xff1a;登录Maven官网 Maven官网&#xff1a;https://mvnrepository.com/search?qmysql 第二步&#xff1a;点击MySql Connector Java 第三步&#xff1a;点击任意一个版本 第四步&#xff1a;将以下内容复制到pom.xml中 导入j…

Springboot+Redis:实现缓存 减少对数据库的压力

&#x1f389;&#x1f389;欢迎光临&#xff0c;终于等到你啦&#x1f389;&#x1f389; &#x1f3c5;我是苏泽&#xff0c;一位对技术充满热情的探索者和分享者。&#x1f680;&#x1f680; &#x1f31f;持续更新的专栏Redis实战与进阶 本专栏讲解Redis从原理到实践 …

fastjson反序列化攻略

漏洞原理 Json.parseObject(json, User.class)方法中&#xff0c;通过指定type的值实现定位某类&#xff0c;会执行User类的构造方法和属性中的get&#xff0c;set方法 判断是否是fastjson/&#xff08;jackson&#xff09; 1.2.24-1.2.83都会有dnslog的payload {"zer…

解决重装系统之后,开始菜单找不到Anaconda3相关图标

一、anaconda3安装后在开始菜单找不到&#xff0c;如下图所示 二、进入Anaconda3安装的位置 在安装位置按住shift键鼠标右键&#xff0c;打开poworshell&#xff0c;输入 start cmd最后的结果如图。

快速从0-1完成聊天室开发——环信ChatroomUIKit功能详解

聊天室是当下泛娱乐社交应用中最经典的玩法&#xff0c;通过调用环信的 IM SDK 接口&#xff0c;可以快速创建聊天室。如果想根据自己业务需求对聊天室应用的 UI界面、弹幕消息、礼物打赏系统等进行自定义设计&#xff0c;最高效的方式则是使用环信的 ChatroomUIKit 。 文档地址…

Java-SpringAop 编程式事物实现

SpringAop 编程式事物实现 1. 数据库事物特性 原子性 多个数据库操作是不可分割的&#xff0c;只有所有的操作都执行成功&#xff0c;事物才能被提交&#xff1b;只要有一个操作执行失败&#xff0c;那么所有的操作都要回滚&#xff0c;数据库状态必须回复到操作之前的状态 …

如何在三个简单步骤中为对象检测标注图像

初始通过彻底清洗和处理原始图像数据来奠定有效对象检测注释的基础。选择适合的工具、方法和清晰的注释过程指南来建立注释工作空间。通过在图像中划定对象并附上类别标签来执行注释&#xff0c;随后进行细致的核验&#xff0c;以确保数据集的精确性和完整性。 图像注释是计算…

C语言---指针的两个运算符:点和箭头

目录 点&#xff08;.&#xff09;运算符箭头&#xff08;->&#xff09;运算符需要注意实际例子 C语言中的指针是一种特殊的变量&#xff0c;它存储了一个内存地址。点&#xff08;.&#xff09;和箭头&#xff08;->&#xff09;是用于访问结构体和联合体成员的运算符。…
最新文章