构建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: httpprotocol: TCPname: httpselector:{{- 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 createdcreate: true# Annotations to add to the service accountannotations: {}# The name of the service account to use.# If not set and create is true, a name is generated using the fullname templatename: ""
​
podAnnotations: {}
​
podSecurityContext: {}# fsGroup: 2000
​
securityContext: {}# capabilities:#   drop:#   - ALL# readOnlyRootFilesystem: true# runAsNonRoot: true# runAsUser: 1000
​
service:    #service类型和端口type: ClusterIPport: 80
​
ingress:   #ingress的相关配置enabled: falseclassName: ""annotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: chart-example.localpaths:- path: /pathType: ImplementationSpecifictls: []#  - 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: falseminReplicas: 1maxReplicas: 100targetCPUUtilizationPercentage: 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: nginxpullPolicy: 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 createdcreate: true# Annotations to add to the service accountannotations: {}# The name of the service account to use.# If not set and create is true, a name is generated using the fullname templatename: ""
​
podAnnotations: {}
​
podSecurityContext: {}# fsGroup: 2000
​
securityContext: {}# capabilities:#   drop:#   - ALL# readOnlyRootFilesystem: true# runAsNonRoot: true# runAsUser: 1000
​
service:type: NodePort    #NodePort类型port: 80     #80端口
​
ingress:enabled: falseclassName: ""annotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: chart-example.localpaths:- path: /pathType: ImplementationSpecifictls: []#  - 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: falseminReplicas: 1maxReplicas: 100targetCPUUtilizationPercentage: 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 ImageImage:          nginx:1.17.5Image 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 ImageImage:          nginx:1.17.8Image 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 ImageImage:          nginx:1.17.5Image ID:       docker.io/library/nginx@sha256:922c815aa4df050d4df476e92daed4231f466acc8ee90e0e774951b0fd7195a4

三.helm模板内管道和函数

1.defautl

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

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

[root@k8s-master helm]# cat nginx/values.yaml | grep portport: 
#在deployment文件中引用了此变量的地方设置default
[root@k8s-master helm]# cat nginx/templates/deployment.yaml  | grep defaultimage: "{{ .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.mzph.cn/news/755775.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

软件工程-第6章 面向对象方法UML

UML是一种图形化语言&#xff0c;简称画图。 6.1 表达客观事物的术语 6.2 表达关系的术语 1.关联 表达关联语义相关术语&#xff1a;关联名、导航、角色、可见性、多重性、限定符、聚合、组合。 2.泛化 3.细化 6.3 组织信息的一种通用机制-包 6.4 模型表达工具 一个用况图包含6…

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

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

【Vue3】走进Pinia,学习Pinia,使用Pinia

&#x1f497;&#x1f497;&#x1f497;欢迎来到我的博客&#xff0c;你将找到有关如何使用技术解决问题的文章&#xff0c;也会找到某个技术的学习路线。无论你是何种职业&#xff0c;我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章&#xff0c;也欢…

杰发科技AC7801——读取Flash数据做CRC校验

查看Keil的编译结果发现总共6160个字节。计算结果如下&#xff0c; 代码如下 #include "ac780x_crc.h" #include "ac780x.h" #include "ac780x_debugout.h" #include "string.h" #include "ac780x_eflash.h"#define TestSi…

Leetcode 239 滑动窗口最大值

题目信息 LeetoCode地址: . - 力扣&#xff08;LeetCode&#xff09; 题目理解 题意是很好理解的&#xff0c;一个固定长度(k)的滑块从一个数组的左端一步一步向右滑&#xff0c;然后挑出滑块盖住的那些数字中最大的&#xff0c;组成的数组就是结果。 难点在于&#xff0c;…

html5cssjs代码 026 canvas示例

html5&css&js代码 026 canvas示例 一、代码二、解释 这段HTML代码定义了一个页面&#xff0c;其中包含一个容器和一个canvas元素。通过JavaScript代码&#xff0c;使用canvas绘制了一个矩形、一个填充了颜色的矩形、一个文本以及一个圆形。 一、代码 <!DOCTYPE ht…

nodejs基于vue超市信息管理系统flask-django-php

互联网的快速发展&#xff0c;使世界各地的各种组织的管理方式发生了根本性的变化&#xff0c;我国政府、企业等组织在上个世纪90年代就已开始考虑使用互联网来管理信息。由于以前的种种因素&#xff0c;比如网络的普及率不高&#xff0c;用户对它的认知度不够&#xff0c;以及…

Zenlayer如何将万台设备监控从Zabbix迁移到Flashcat

作为全球首家以超连接为核心的云服务商&#xff0c;Zenlayer 致力于将云计算、内容服务和边缘技术融合&#xff0c;为客户提供全面的解决方案。通过构建可靠的网络架构和高效的数据传输&#xff0c;Zenlayer 帮助客户实现更快速、更可靠的连接&#xff0c;提升用户体验和业务效…

局域网内监控别人电脑屏幕

想要在局域网内可以监控他人的屏幕的方法&#xff0c;无疑是使用一款&#xff0c;屏幕监控软件了。 什么是局域网屏幕监控软件&#xff1f; 局域网屏幕监控软件是一种专门用于监控局域网内电脑屏幕活动的软件工具。它通常集成在局域网监控系统中&#xff0c;能够实时捕捉和记…

使用Java JDBC连接数据库

在Java应用程序中&#xff0c;与数据库交互是一个常见的任务。Java数据库连接&#xff08;JDBC&#xff09;是一种用于在Java应用程序和数据库之间建立连接并执行SQL查询的标准API。通过JDBC&#xff0c;您可以轻松地执行各种数据库操作&#xff0c;如插入、更新、删除和查询数…

2024蓝桥杯每日一题(递归)

备战2024年蓝桥杯 -- 每日一题 Python大学A组 试题一&#xff1a;有序分数 试题二&#xff1a;正则问题 试题三&#xff1a;带分数 试题四&#xff1a;约数之和 试题五&#xff1a;分形之城 试题一&#xff1a;有序分数 【题目描述】 【输入格…

@Builder用法

一.场景模拟Data两个缺点 假如有一结果api结果返回值的类Result&#xff0c;其在代码中频繁被使用&#xff1a; Data public class Result<T> {private int code;private String message;private T data;public Result(int code, String message, T data) {this.code …

AI换脸软件rope最新更新的蓝宝石中文版下载

rope换脸软件蓝宝石版下载地址&#xff1a;点击下载 最近AI软件非常的火爆&#xff0c;今天就给大家带来一个可以AI替换人脸的工具rope&#xff0c;得益于机器学习技术的不断发展&#xff0c;rope经过深度神经网络的无数次迭代优化&#xff0c;最终得出的模型可以自动学习和识…

如何在一个阶段中后期调整心态

在阶段中后期调整心态和自我鼓励的方法涉及心理调适、目标回顾、计划调整和积极心理暗示等多个层面。以下是一些建议&#xff1a; 回顾和调整目标&#xff1a; 回顾初始目标是否依然符合当前情境和自身需求&#xff0c;如有必要&#xff0c;适度调整目标使之更具可行性&#…

【商业成长】Ai 中英字幕:英伟达 NVIDIA GTC 黄仁勋——见证 AI 的变革时刻,听他分享塑造未来的 AI 突破!

英伟达官网&#xff1a;人工智能计算领域的领导者 | NVIDIA Ai 中英字幕 仅供学习使用&#xff0c;不得商用&#xff0c;侵删&#xff0c;感谢&#xff01; 【Ai 中英 舒适字幕】英伟达 NVIDIA GTC 黄仁勋&#xff1a;见证 AI 的变革时刻&#xff0c;听他分享塑造未来的 AI 突破…

2022蓝桥杯/李白打酒加强版/c\c++

问题描述 话说大诗人李白&#xff0c;一生好饮。幸好他从不开车。一天&#xff0c;他提着酒壶&#xff0c;从家里出来&#xff0c;酒壶中有酒2斗。他边走边唱&#xff1a;无事街上走&#xff0c;提壶去打酒。逢店加一倍&#xff0c;遇花喝一斗。这一路上&#xff0c;他一共遇到…

Linux调试器-gdb的使用

. 个人主页&#xff1a;晓风飞 专栏&#xff1a;数据结构|Linux|C语言 路漫漫其修远兮&#xff0c;吾将上下而求索 文章目录 gdb简单基础指令Linux调试器-gdb使用背景调试准备工作写一个简单的myprocess.c程序makefile程序debug模式运行修改后的Makefile程序 调试(gdb)listruni…

Excalidraw:绘制图形的新利器

title: Excalidraw&#xff1a;绘制图形的新利器 date: 2024/3/19 17:18:08 updated: 2024/3/19 17:18:08 tags: 绘图工具多人协作数据安全简洁设计浏览器访问Docker部署插件扩展 摘要&#xff1a; Excalidraw是一款简洁设计、直观易用的绘图应用&#xff0c;用户可以通过它创…

C++中的this指针、访问控制和构造函数

C中的this指针、访问控制和构造函数 this指针 在C中&#xff0c;this指针是一个特殊的指针&#xff0c;它指向当前对象的地址。每个非静态成员函数&#xff08;包括成员函数模板&#xff09;都有一个this指针作为其隐含参数&#xff0c;这意味着在成员函数内部&#xff0c;th…

IO空间和内存空间的区别

1&#xff0c;IO空间&#xff1a;一个特有的空间&#xff0c;与内存空间独立的空间&#xff0c;同样利用IO空间可以操作数据&#xff0c;只不过是利用对应的IO端口操作函数。 2&#xff0c;内存空间&#xff1a;内存地址的寻址范围&#xff0c;例如32位操作系统内存空间为2的32…