Ops工具
Helm
Helm 包管理工具
Helm进阶
Ansible
Ansible入门
Ansible 常用模块指令
Ansible playbook详解
Vdbench
vdbench基础使用指南
vdbench在ARM服务器上出现共享库aarch64.so问题
GitLab
Gitlab自定义机器人
Gitlab安装和使用
CosBench
Cosbench测试
s3curl
s3curl常用命令大全
S3curl测试
FIO
FIO安装和使用方法
本文档使用 MrDoc 发布
-
+
首页
Helm进阶
# 1、Helm 基础语法 ## 1.1 内置对象,下面是常用的 | 内置 | Release名字 | | ----------------- | --------------------------------- | | Release.Name | release 名称 | | Release.Time | release 的时间 | | Release.Namespace | release 的命名空间 | | Release.Service | release 服务的名称 | | Release.Revision | release 的修订版本号,从1开始累加 | ## 1.2 常用的内置函数 **1、quote and squote** 该函数将值转**换成字符串**, 用**双引号**(`quote`) 或者**单引号**(`squote`) 括起来。示例如 下: ```yaml ./values.yaml name: soulchild favorite: drink: coffee food: pizza ./configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | quote }} food: {{ .Values.favorite.food | upper | squote }} ``` 使用`--dry-run`输出效果 ```bash $ helm install --dry-run mytestapp . # Source: mytestapp/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: mytestapp-configmap data: myvalue: "Hello World" drink: "coffee" food: 'PIZZA' ``` **2、default** 这个函数允许你在模板中指定一个默认值,以防这个值被忽略。 ```yaml # 如果.Values.favorite.drink是非空值,则使用它,否则会返回tea。 drink: {{ .Values.favorite.drink | default "tea" | quote }} ``` **3、indent和nindent** `indent`和`nindent`都是缩进字符串,主要区别在于nindent会在缩进前多添加一个换行符 ```yaml # values.yaml resources: limits: cpu: 200m memory: 500m requests: cpu: 300m memory: 800m # templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" resource1: {{ .Values.resources.limits.memory | indent 4 }} # 上述结果会在当前位置开始缩进4个空格。 resource2: {{ .Values.resources.limits.memory | nindent 4 }} # 上述结果会在换行后的开头位置开始缩进4个空格。 ``` **4、date** date函数格式化日期,日期格式化为YEAR-MONTH-DAY: ```yaml now | date "2006-01-02" ``` **5、lower** 将整个字符串转换成小写: ```yaml lower "HELLO" 结果为: hello ``` **6、upper** 将整个字符串转换成大写: ```yaml upper "hello" 结果为: HELLO ``` **7、title** 首字母转换成大写: ```yaml title "hello world" 结果为: Hello World ``` **8、toYaml** 引用一块YAML内容 在values.yaml里写结构化数据,引用内容块 在values增加resources资源配额。 ```yaml ./values.yaml name: soulchild favorite: drink: coffee food: pizza resources: limits: cpu: 200m memory: 500m requests: cpu: 300m memory: 800m apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | quote }} food: {{ .Values.favorite.food | upper | squote }} resources: {{ toYaml .Values.resources | nindent 4 }} ``` 查看效果 ```bash $ helm install --dry-run mytestapp . # Source: mytestapp/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: mytestapp-configmap data: myvalue: "Hello World" drink: "coffee" food: 'PIZZA' resources: limits: cpu: 200m memory: 500m requests: cpu: 300m memory: 800m ``` ## 1.3 条件语句 **运算符:** ```bash eq: 等于(equal to) ne: 不等于(not equal to) lt: 小于(less than) le: 小于等于(less than or equal to) gt: 大于(greater than) ge: 大于等于(greater than or equal to) ``` **if/else 用法:** ```yaml {{ if PIPELINE }} # Do something {{ else if OTHER PIPELINE }} # Do something else {{ else }} # Default case {{ end }} ``` 当返回值是以下值时,管道会被设置为 false: ```bash 布尔false 数字0 空字符串 nil (空或null) 空集合(map, slice, tuple, dict, array) ``` 【示例】:要求`.Values.favorite.drink`的值等于`coffee`,则输出`mug: true` ```yaml ./values.yaml name: soulchild favorite: drink: coffee food: pizza ./configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | default "tea" | quote }} food: {{ .Values.favorite.food | upper | quote }} {{ if and .Values.favorite.drink (eq .Values.favorite.drink "coffee") }}mug: true{{ end }} ``` **注意**: `空白行` ```yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | default "tea" | quote }} food: {{ .Values.favorite.food | upper | quote }} {{if eq .Values.favorite.drink "coffee"}} mug: true {{end}} ``` 当模板引擎运行时,它会删除`{{`和`}}`中的内容,但保留其余空白。通过新增{- if ...}} 的 方式消除此空行,修改后: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | default "tea" | quote }} food: {{ .Values.favorite.food | upper | quote }} {{- if eq .Values.favorite.drink "coffee"}} mug: true {{- end}} ``` 谨慎使用:如:`-}}`这将会把如上的结果生成在同一行`food: "PIZZA"mug:true`,因为它消除了两边的换行。 ## 1.4 变更作用域 with 这个用来 **控制变量范围**。 **with** 的语法与 **if** 语句类似: ```yaml {{ with PIPELINE }} # restricted scope {{ end }} ``` 作用域可以被改变。with允许你为特定对象设定当前作用域(`.`)。比如,我们已经在使 用`.Values.favorite`。 修改配置映射中的.的作用域指向`.Values.favorite`: ```yaml ./values.yaml name: soulchild favorite: drink: coffee food: pizza ./configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" {{- with .Values.favorite }} drink: {{ .drink | default "tea" | quote }} food: {{ .food | upper | quote }} {{- end }} ``` 但是这里有个注意事项,在限定的作用域内,**无法使用`.`访问父作用域**的对象。错误示例如下: ```yaml {{- with .Values.favorite }} drink: {{ .drink | default "tea" | quote }} food: {{ .food | upper | quote }} release: {{ .Release.Name }} //release不在作用域内 {{- end }} ``` 这样会报错因为**不在限定的作用域内**。但是如果对调最后两行就是正 常的, 因为在`{{ end }}`之后作用域被重置了。 ```yaml {{- with .Values.favorite }} drink: {{ .drink | default "tea" | quote }} food: {{ .food | upper | quote }} {{- end }} release: {{ .Release.Name }} //脱离作用域,可以获取到Release.Name ``` 或者,我们可以使用`$`**从父作用域中访问Release.Name对象**。当模板开始执行后`$`会被映射到**根作用域**,且执行过程中不会更改。 下面这种方式也可以正常工作: ```yaml {{- with .Values.favorite }} drink: {{ .drink | default "tea" | quote }} food: {{ .food | upper | quote }} release: {{ $.Release.Name }} //$类似全局参数 {{- end }} ``` ## 1.5 rang循环语句 在一个集合中迭代的方式是使用`range`操作符。 ```yaml ./values.yaml favorite: drink: coffee food: pizza pizzaTypes: - mushrooms - cheese - peppers - onions ``` 现在我们有了一个pizzaTypes列表(模板中称为切片)。修改模板把这个列表打印到配置映射中: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" {{- with .Values.favorite }} drink: {{ .drink | default "tea" | quote }} food: {{ .food | upper | quote }} {{- end }} toppings: |- {{- range .Values.pizzaTypes }} - {{ . | title | quote }} {{- end }} ``` 该range函数将遍历pizzaTypes列表。每次通过循环,.的值都会发生改变,即第一次`.`为mushrooms。将第二个迭代为cheese,依此类推。第二步继续使用后续title及quote函数: ```yaml # Source: mychart/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: drinking-configmap data: myvalue: "Hello World" drink: "coffee" food: "PIZZA" toppings: |- - "Mushrooms" - "Cheese" - "Peppers" - "Onions" ``` **注意:** YAML中的`|-`标记意为采用多行字符串。这对于在清单中嵌入大数据块是一种有用的技术,如例子中所示。 有时,在模板中快速创建一个列表,然后遍历该列表是很有用的。Helm模板有一个名为`list`的函数。 ```yaml sizes: |- {{- range list "small" "medium" "large" }} - {{ . }} {{- end }} ``` 结果: ```yaml sizes: |- - small - medium - large ``` ## 1.6 命名模板 **命名模板类似于开发语言中的函数,指一段可以直接被另一段程序或代码引用的程序或代码。** 在编写chart时,可以将一些重复使用的内容写在命名模板文件中供公共使用,这样可减少重复编写程序段和简化代码结构。 命名模块使用define定义,template(不支持管道)或 include 引入,在templates目录中默认下划线开头的文件为公共模板(_helpers.tpl)。 ```yaml # cat templates/_helpers.tpl {{- define "fullname" -}} {{- .Chart.Name -}}-{{ .Release.Name }} {{- end -}} # cat templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app: {{ .Release.Name |indent 6}} app: {{ .Release.Name |nindent 6}} name: {{ include "fullname" . }} ``` #### 1、用define和template声明和使用模板 define操作允许我们在模板文件中创建一个命名模板,**语法格式** 如下: ```yaml {{- define "MY.NAME" }} # body of template here {{- end }} ``` 比如我们可以定义一个模板用来封装控制器的标签: ```yaml # templates/_helpers.tpl {{- define "mychart.labels" }} labels: generator: helm date: {{ now | htmlDate }} {{- end }} ``` 现在我们将模板嵌入到了已有的配置映射中,然后使用`template`包含进来: ```yaml # templates/_helpers.tpl {{- define "mychart.labels" }} labels: generator: helm date: {{ now | htmlDate }} //用于将日期格式化为 HTML 可接受的格式。 {{- end }} # templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }}-configmap {{- template "mychart.labels" }} data: myvalue: "Hello World" {{- range $key, $val := .Values.favorite }} {{ $key }}: {{ $val | quote }} {{- end }} ``` 当模板引擎读取该文件时,它会存储mychart.labels的引用直到template "mychart.labels"被调用。 然后会按行渲染模板,因此结果类似这样: ```yaml # Source: mychart/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: myfirsthelm-configmap labels: generator: helm date: 2023-03-25 data: myvalue: "Hello World" drink: "coffee" food: "pizza" ``` > 注意:define不会有输出,除非像本示例一样用模板调用它。 按照惯例,Helm chart将这些模板放置在局部文件中,一般是`_helpers.tpl`。 ```yaml {{/* Generate basic labels */}} {{- define "mychart.labels" }} labels: generator: helm date: {{ now | htmlDate }} {{- end }} ``` #### 2、include 方法 以下是 include 指令的语法: ```yaml {{- include "path/to/template" . }} ``` 其中,第一个参数是要包含的子模板的路径(可以是相对路径或绝对路径)。第二个参数是当前上下文中可用的数据。 注意,当引用相对路径时,Helm 会在指定路径中寻找一个名为`_helpers.tpl`的文 件,并将其视为项的一部分,以便在包含的模板中使用。 以下是一个具体的示例,其中`{{- include "configmap.tpl" . }}`指令包含了`templates/configmap.tpl`文件中的模板: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: {{ .Values.configMapName }} data: {{- include "configmap.tpl" . }} ``` > `include`相较于使用template,在helm中**使用include**被认为是更好的方式 只是为了更好地处理YAML文档的输出格式。 ## 1.7 NOTES.txt文件 > 在helm install 或 helm upgrade命令的最后,**Helm会打印出对用户有用的信息**。 使用模板可以高度自定义这部分信息。 要在chart添加安装说明,只需创建`templates/NOTES.txt`文件即可。**该文件是纯文本,但会像模板一样处理**, 所有正常的模板函数和对象都是可用的。让我们创建一个简单的NOTES.txt文件: ```yaml Thank you for installing {{ .Chart.Name }}. Your release is named {{ .Release.Name }}. To learn more about the release, try: $ helm status {{ .Release.Name }} $ helm get all {{ .Release.Name }} ``` 执行`helm install myfirsthelm ./mychart`会在底部看到: ```bash RESOURCES: ==> v1/Secret NAME TYPE DATA AGE myfirsthelm-secret Opaque 1 0s ==> v1/ConfigMap NAME DATA AGE myfirsthelm-configmap 3 0s NOTES: Thank you for installing mychart. Your release is named myfirsthelm. To learn more about the release, try: $ helm status myfirsthelm $ helm get all myfirsthelm ``` 使用NOTES.txt这种方式是给用户提供关于如何使用新安装的chart细节信息的好方法。 尽管并不是必需的, **强烈建议创建一个`NOTES.txt`文件**。 # 2、Helm模板调试 ## 2.1 `htlm --set`函数 通过`--set`选项,用户可以设置一些应用程序的值,这些值是可以替代values.yaml中 的默认值; 例如,如果用户需要在部署 Helm Chart 时动态地设置 Redis 密码和端口号,可以使用以下命令: ```bash $ helm install my-release bitnami/redis --set password=thisispwd ``` 这将为 Redis Chart 安装一个新的 Release,并将密码设置为“secretpassword” ## 2.2 `helm --dry-run`函数 调试模板可能很棘手,因为渲染后的模板发送给了Kubernetes API server,可能会以格式化以外的原因拒绝YAML文件。以下命令有助于调试: - `helm install --dry-run`或`helm template --debug`: 我们已经看过这个技巧了, 这是让服务器渲染模板的好方法,然后返回生成的清单文件。 - `helm get manifest`: 这是查看安装在服务器上的模板的好方法。 当你的YAML文件解析失败,但你想知道生成了什么,检索YAML一个简单的方式是**注释掉模板中有问题的部分**, 然后重新运行`helm install --dry-run` # 3、Helm Chart 详解 ## 3.1 Chart 目录结构 ```bash # helm create nginx # tree nginx nginx/ ├── charts #依赖其他包的charts文件 ├── Chart.yaml # 该chart的描述文件,包括ico地址,版本信息等 ├── templates #存放k8s模板文件目录 │ ├── deployment.yaml # 创建k8s资源的yaml 模板 │ ├── _helpers.tpl # 下划线开头的文件,可以被其他模板引用 │ ├── hpa.yaml # 弹性扩缩容,配置服务资源CPU 内存 │ ├── ingress.yaml # ingress 配合service域名访问的配置 │ ├── NOTES.txt # 说明文件,helm install之后展示给用户看的内容 │ ├── serviceaccount.yaml # 服务账号配置 │ ├── service.yaml # kubernetes Serivce yaml 模板 │ └── │ tests # 测试模块 │ └── test-connection.yaml └── values.yaml # 给模板文件使用的变量 ``` ### 1、Chart.yaml 文件 ```yaml apiVersion: chart API 版本 (必需) name: chart名称 (必需) version: chart 版本号(必需) kubeVersion: 兼容Kubernetes版本(可选) description: 一句话对这个项目的描述(可选) type: chart类型 (可选) keywords: - 关于项目的一组关键字(可选) home: 项目home页面的URL (可选) sources: - 项目源码的URL列表(可选) dependencies: # chart 必要条件列表 (可选) - name: chart名称 (nginx) version: chart版本 ("1.2.3") repository: (可选)仓库URL ("https://example.com/charts") 或别名 ("@repo-name") condition: (可选) 解析为布尔值的yaml路径,用于启用/禁用chart (e.g. subchart1.enabled ) tags: # (可选) - 用于一次启用/禁用 一组chart的tag import-values: # (可选) - ImportValue 保存源值到导入父键的映射。每项可以是字符串或者一对子/父列表项 alias: (可选) chart中使用的别名。当你要多次添加相同的chart时会很有用 maintainers: # (可选) - name: 维护者名字 (每个维护者都需要) email: 维护者邮箱 (每个维护者可选) url: 维护者URL (每个维护者可选) icon: 用做icon的SVG或PNG图片URL (可选) appVersion: 包含的应用版本(可选)。不需要是语义化,建议使用引号 deprecated: 不被推荐的chart (可选,布尔值) annotations: example: 按名称输入的批注列表 (可选) ``` 注意点: 每个chart都必须有个版本号(version) ## 3.2 Chart 依赖管理(dependencies) 当前chart依赖的其他chart会在dependencies字段定义为一个列表。 ```yaml dependencies: - name: apache version: 1.2.3 repository: https://example.com/charts - name: mysql version: 3.2.1 repository: https://another.example.com/charts ``` - **name**: 字段是你需要的chart的名称; - **version**: 字段是你需要的chart的版本; - **repository**: 字段是chart仓库的完整URL。必须使用helm repo add在本地添加仓 库,也可以使用仓库的名称代替URL; # 4、Helm开发实践 以下是基于Helm开发一个常规应用的步骤及YAML文件: **1、安装Helm** 首先需要安装Helm,可以参考官方文档进行安装。 **2、创建Helm chart** 执行以下命令创建一个新的Helm chart: ```bash $ helm create myapp ``` 其中,`myapp`是chart的名称,可以根据需要进行修改。 **3、编辑values.yaml文件** 进入chart目录,编辑`values.yaml`文件,修改其中的配置项,例如应用名称、端口号、镜像名称等。 **4、编辑deployment.yaml文件** 编辑`templates/deployment.yaml`文件,定义应用的Deployment。以下是一个示例: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Values.appName }}-deployment spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: app: {{ .Values.appName }} template: metadata: labels: app: {{ .Values.appName }} spec: containers: - name: {{ .Values.appName }} image: {{ .Values.image.repository }}:{{ .Values.image.tag }} ports: - containerPort: {{ .Values.containerPort }} env: - name: DB_HOST value: {{ .Values.db.host }} - name: DB_PORT value: {{ .Values.db.port }} - name: DB_USER value: {{ .Values.db.user }} - name: DB_PASSWORD value: {{ .Values.db.password }} ``` 在这个示例中,我们定义了一个Deployment,使用了`values.yaml `文件中定义的配置项。其中,`replicas`表示副本数,`selector`表示选择器,`template`表示Pod的模板,`containers`表示容器列表,`env`表示环境变量。 **5、编辑service.yaml文件** 编辑`templates/service.yaml`文件,定义应用的Service。以下是一个示例: ```yaml apiVersion: v1 kind: Service metadata: name: {{ .Values.appName }}-service spec: selector: app: {{ .Values.appName }} ports: - name: http port: {{ .Values.servicePort }} targetPort: {{ .Values.containerPort }} type: LoadBalancer ``` 在这个示例中,我们定义了一个Service,使用了`values.yaml`文件中定义的配置项。 其中,`selector`表示选择器,`ports`表示端口映射,`type`表示Service类型。 **6、打包Helm chart** 执行以下命令将Helm chart打包: ```bash $ helm package my-app ``` **7、部署Helm chart** 执行以下命令部署Helm chart: ```bash $ helm install my-app ./my-app-0.1.0.tgz ``` 其中,`my-app`是部署的名称,可以根据需要进行修改。 以上就是基于Helm开发一个常规应用的步骤及所用到的YAML文件。需要注意的是,这只是一个示例,实际开发中可能需要根据具体需求进行修改。 # 5、基于helm的WordPress **安装helm** 参考官方文档: https://helm.sh/docs/intro/install/ ## 5.1 方式一 1. 添加WordPress chart仓库 执行以下命令添加WordPress chart仓库: ```bash $ helm repo add bitnami https://charts.bitnami.com/bitnami ``` 创建一个名为wordpress的命名空间(如有需要): ```bash $ kubectl create namespace wordpress ``` 2. 创建values.yaml文件 在本地创建一个values.yaml文件,用于配置WordPress的相关参数。以下是一个示例: ```yaml # WordPress相关配置 wordpressUsername: admin wordpressPassword: password wordpressEmail: admin@example.com wordpressBlogName: My 123Blog # Service相关配置 service: type: NodePort port: 80 nodePorts: http: "32088" # MySQL相关配置 mysql: image: repository: bitnami/mysql tag: 8.0.26-debian-10-r0 pullPolicy: IfNotPresent user: wordpress database: wordpress port: 3306 persistence: enabled: true size: 8Gi storageClass: "alibabacloud-cnfs-nas" # WordPress相关配置 wordpress: image: repository: bitnami/wordpress tag: 5.8.1-debian-10-r0 pullPolicy: IfNotPresent replicaCount: 1 persistence: enabled: true size: 8Gi storageClass: "alibabacloud-cnfs-nas" ``` 3. 安装WordPress 执行以下命令安装WordPress: ```bash $ helm install my-wordpress bitnami/wordpress -f values.yaml -n wordpress ``` 其中,my-wordpress是安装的WordPress的名称,stable/wordpress是Helm官方提供 的WordPress Chart,-f values.yaml指定了使用上一步创建的values.yaml文件,-n wordpress指定了安装的命名空间为wordpress。 4. 查看安装状态 执行以下命令查看安装状态: ```bash $ kubectl get pods,services,pvc ``` 5. 如果需要升级WordPress,可以使用以下命令: ```bash $ helm upgrade my-wordpress bitnami/wordpress -f values.yaml ``` 如果需要删除WordPress,可以使用以下命令: ```bash # 删除WordPress helm uninstall my-wordpress ``` 6. 访问: ```bash # 输出WordPress的URL echo "WordPress URL: http://$(kubectl get svc my-wordpress -n wordpress -o jsonpath='{.status.loadBalancer.ingress[0].ip}')" ```  ## 5.2 方式二 以下是一个基于Helm的WordPress chart包的示例: 1. 下载WordPress chart 执行以下命令将WordPress chart下载到本地: ```bash $ helm fetch bitnami/wordpress ``` 2. 解压WordPress chart 执行以下命令解压WordPress chart: ```bash $ tar -zxvf wordpress-*.tgz ``` 也可以在pull chat包时加上`--untar`命令自动解压`helm pull bitnami/wordpress --untar` 3. 修改values.yaml文件 在wordpress目录下,打开values.yaml文件,并根据需要修改WordPress的配置参数, 以下是一个示例: ```yaml global: imagePullSecrets: [] storageClass: "alibabacloud-cnfs-nas" wordpressPassword: "wppwd" wordpressUsername: wpuser wordpressBlogName: ZHDYA's Blog! wordpressScheme: http wordpressSkipInstall: true replicaCount: 1 updateStrategy: type: RollingUpdate hostAliases: initContainers: [] pod/node亲和相关: resources: livenessProbe: readinessProbe: startupProbe: service: ingress: nodePorts: http: "32088" https: "" ... ``` 4. 修改Deployment.yaml文件 在wordpress/templates目录下,查看Deployment.yaml的文件: ```yaml apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }} kind: Deployment metadata: name: {{ include "common.names.fullname" . }} namespace: {{ .Release.Namespace | quote }} labels: {{- include "common.labels.standard" . | nindent 4 }} {{- if .Values.commonLabels }} {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }} {{- end }} {{- if .Values.commonAnnotations }} annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }} {{- end }} spec: selector: matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }} {{- if .Values.updateStrategy }} strategy: {{- toYaml .Values.updateStrategy | nindent 4 }} {{- end }} {{- if not .Values.autoscaling.enabled }} replicas: {{ .Values.replicaCount }} ``` 在这个文件中,使用include函数引用了其他资源的名称,例如使用include "common.names.fullname"来引用charts包(`charts/common/templates`)中定义的 Deployment资源的名称。 这样可以使不同的资源之间产生关联,从而实现更加复杂的应用程序部署。 5. 修改Service.yaml文件 在wordpress/templates目录下: ```yaml spec: type: {{ .Values.service.type }} {{- if and .Values.service.clusterIP (eq .Values.service.type "ClusterIP") }} clusterIP: {{ .Values.service.clusterIP }} {{- end }} {{- if or (eq .Values.service.type "LoadBalancer") (eq .Values.service.type "NodePort") }} externalTrafficPolicy: {{ .Values.service.externalTrafficPolicy | quote }} {{- end }} {{- if and (eq .Values.service.type "LoadBalancer") (not (empty .Values.service.loadBalancerSourceRanges)) }} loadBalancerSourceRanges: {{ .Values.service.loadBalancerSourceRanges }} {{- end }} {{- if and (eq .Values.service.type "LoadBalancer") (not (empty .Values.service.loadBalancerIP)) }} loadBalancerIP: {{ .Values.service.loadBalancerIP }} {{- end }} {{- if .Values.service.sessionAffinity }} sessionAffinity: {{ .Values.service.sessionAffinity }} ``` 6. 修改Ingress.yaml文件 在wordpress/templates目录下: ```yaml {{- if .Values.ingress.enabled }} apiVersion: {{ include "common.capabilities.ingress.apiVersion" . }} kind: Ingress metadata: name: {{ include "common.names.fullname" . }} namespace: {{ .Release.Namespace | quote }} labels: {{- include "common.labels.standard" . | nindent 4 }} {{- if .Values.commonLabels }} {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }} {{- end }} annotations: {{- if .Values.ingress.annotations }} {{- include "common.tplvalues.render" (dict "value" .Values.ingress.annotations "context" $) | nindent 4 }} {{- end }} {{- if .Values.commonAnnotations }} {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }} {{- end }} ... {{- end }} ``` 在这个文件中,使用了 if 的小开关模式; 7. 修改PersistentVolumeClaim.yaml文件 在wordpress/templates目录下: ```yaml {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }} kind: PersistentVolumeClaim apiVersion: v1 metadata: name: {{ include "common.names.fullname" . }} namespace: {{ .Release.Namespace | quote }} labels: {{- include "common.labels.standard" . | nindent 4 }} {{- if .Values.commonLabels }} {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }} {{- end }} {{- if or .Values.persistence.annotations .Values.commonAnnotations }} annotations: {{- if .Values.persistence.annotations }} {{- include "common.tplvalues.render" ( dict "value" .Values.persistence.annotations "context" $ ) | nindent 4 }} {{- end }} {{- if .Values.commonAnnotations }} {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }} {{- end }} ... {{- end }} ``` 在这个文件中,使用了PersistentVolumeClaim资源来定义WordPress的持久化存储配置。 大量使用了`if`,`include`,`.Release`等。 8. 修改Secret.yaml文件 在wordpress/templates目录下: ```yaml data: {{- if not .Values.existingSecret }} wordpress-password: {{ include "common.secrets.passwords.manage" (dict "secret" (include "common.names.fullname" .) "key" "wordpress-password" "providedValues" (list "wordpressPassword") "context" $) }} {{- end }} {{- if and .Values.smtpPassword (not .Values.smtpExistingSecret) }} {{- if .Values.smtpPassword }} smtp-password: {{ .Values.smtpPassword | b64enc | quote }} {{- end }} {{- end }} {{- end }} ``` 在这个文件中,使用了Secret资源来存储MySQL的密码信息。 在这个示例中,`Values.smtpPassword`是一个字符串类型的变量,使用`b64enc`函数将其编码为`Base64`格式,并将其存储在名为`password`的 Secret 数据中。在使用这个 Secret 数据时,可以使用`base64decode`函数将其解码为原始字符串格式。 注意,`b64enc`函数只能用于字符串类型的变量,如果需要编码其他类型的变量,需要先将其转换为字符串类型。 9. 修改Chart.yaml文件 在wordpress目录下: ```yaml annotations: category: CMS licenses: Apache-2.0 apiVersion: v2 appVersion: 6.1.1 dependencies: - condition: memcached.enabled name: memcached repository: https://charts.bitnami.com/bitnami version: 6.x.x - condition: mariadb.enabled name: mariadb repository: https://charts.bitnami.com/bitnami version: 11.x.x - name: common repository: https://charts.bitnami.com/bitnami tags: - bitnami-common version: 2.x.x description: WordPress is the world's most popular blogging and content management platform. Powerful yet simple, everyone from students to global corporations use it to build beautiful, functional websites. ``` 在这个文件中,定义了一个名称模板,用于生成应用程序的名称。同时定义了wordpress依赖Chart的信息,以及维护者和源代码信息。 注意: 这边我们使用到了dependencies(作为应用的启动基础依赖项),如果手动开发 Chart文件在运行前,我们需要运行 helm dependency update 将依赖下载到chart目 录中。 以下是一个包含你所需的模板函数的 _helpers.tpl 文件示例: ```yaml {{/* vim: set filetype=mustache: */}} {{/* 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). */}} {{- define "wordpress.mariadb.fullname" -}} {{- include "common.names.dependency.fullname" (dict "chartName" "mariadb" "chartValues" .Values.mariadb "context" $) -}} {{- end -}} {{/* 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). */}} {{- define "wordpress.memcached.fullname" -}} {{- include "common.names.dependency.fullname" (dict "chartName" "memcached" "chartValues" .Values.memcached "context" $) -}} {{- end -}} ``` 这个文件定义了很多模板函数和变量: - `wordpress.mariadb.fullname`: 用于定义 Mariadb Chart 的完整名称,可以在 其他模板文件中引用。 你可以将这个文件放在 WordPress Chart 的 templates 目录下,然后在其他模板文件 中使用`{{ include "wordpress.mariadb.fullname" . }}`的方式来引用`wordpress.mariadb.fullname`变量。 10. 安装Chart 使用以下命令安装Chart: ```bash $ helm install --dry-run my-wordpress ./wordpress ``` `helm --dry-run`选项用于模拟 Helm 安装过程,但不会实际执行任何操作。它会输出 Helm 安装过程中生成的 Kubernetes YAML 文件,以便用户可以检查这些文件是否正 确。使用`--dry-run`选项时,Helm 不会创建任何 Kubernetes 资源,也不会更新状态,因此它是一个非常安全的选项,可以用于测试 Helm Chart 的正确性。 这个命令会使用values.yaml文件中定义的配置参数,安装一个名为my-wordpress的新 Chart实例。 **正式部署:** ```bash $ helm install my-wordpress . -n wordpress ** Please be patient while the chart is being deployed ** Your WordPress site can be accessed through the following DNS name from within your cluster: my-wordpress.default.svc.cluster.local (port 80) To access your WordPress site from outside the cluster follow the steps below: 1. Get the WordPress URL by running these commands: export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services my-wordpress) export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}") echo "WordPress URL: http://$NODE_IP:$NODE_PORT/" echo "WordPress Admin URL: http://$NODE_IP:$NODE_PORT/admin" 2. Open a browser and access WordPress using the obtained URL. 3. Login with the following credentials below to see your blog: echo Username: wpuser echo Password: $(kubectl get secret --namespace default my-wordpress -o jsonpath="{.data.wordpress-password}" | base64 -d) ``` 其中,my-wordpress是部署的名称,可以根据需要进行修改。 **查看部署状态:** 11. 访问WordPress 节点内任意`宿主机IP:32088`即可访问; 
阿星
2024年1月27日 19:05
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码