简述
helm是k8s的包管理工具,类似于我们手机上的华为市场或者小米的应用商店。他提供了包的查找,安装,卸载,状态查看一条龙的服务。最近学了一下,自己想了个例子,练练手
例子涵盖的技能点
- 从文件中读取内容
- Hook
- 变量的定义,使用
- NOTES.txt
- 流程控制操作符的应用
开始操练起来
- 创建一个chart
helm create nginx-base-package
- 删除多余的文件
rm -rf templates/*
- 编写变量文件values.yaml
deploymentLabel:
app: nginx
type: middleware
environment: test
deploymentSpec:
replicas: !!int "1"
deploymentTemplate:
image: nginx
ports:
containerPort: !!int "80"
env:
shell: bash
author: hananmin
- 创建两个文件用于存储configmap
#deployment-configmap-02.toml
action = "The config of deployment 02"
#deployment-configmap.toml
action = "The config of deployment"
remark = "The remark of deployment"
- 创建configmap的模板
#templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-{{ .Release.Revision }}-configmap
data:
{{- $file := .Files }}
{{- range list "deployment-configmap.toml"}}
{{ . }}: |-
{{ $file.Get .|quote }}
{{- end }}
{{- (.Files.Glob "deployment-configmap-02.toml").AsConfig | nindent 2 }}
- 创建deployment的模板
#templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-{{ .Release.Revision }}-deployment
labels:
{{- range $key, $val := .Values.deploymentLabel }}
{{ $key }}: {{ $val | quote }}
{{- end}}
spec:
replicas: {{ .Values.deploymentSpec.replicas }}
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
env:
{{- range $key, $val := .Values.deploymentLabel }}
- name: {{ $key }}
value: {{ $val | title|quote }}
{{- end}}
ports:
- containerPort: {{ .Values.deploymentTemplate.ports.containerPort }}
volumeMounts:
- name: config-volume
mountPath: /tmp/config
volumes:
- name: config-volume
configMap:
name: {{ .Release.Name }}-{{ .Release.Revision }}-configmap
- 创建Hook的模板
#templates/pre-install.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: "{{ .Release.Name }}"
labels:
helm.sh/chart: "{{ .Release.Name }}-{{ .Chart.Version }}"
annotations:
# This is what defines this resource as a hook. Without this line, the
# job is considered part of the release.
"helm.sh/hook": pre-install
"helm.sh/hook-weight": "5"
"helm.sh/hook-delete-policy": before-hook-creation
spec:
template:
metadata:
name: "{{ .Release.Name }}"
helm.sh/chart: "{{ .Release.Name }}-{{ .Chart.Version }}"
spec:
restartPolicy: Never
containers:
- name: pre-install-job
image: "busybox:latest"
command: ["echo","I am pre-install Job,time:","`date`"]
- 创建NOTE.TXT
# templates/NOTES.txt
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 {{ .Release.Name }}
- 检查一下
helm install --dry-run --debug
运行完命令你会看到,给k8s的MANIFEST,如果输出中没有报错,可以进行下面的操作
- 安装
helm install nginx-base-package/
helm install命令不会等待所有的pod都启动,后续查看release的状态,需要用到helm status
在k8s层面检查一下
- 首先,我们的pre-install启动了一个job
- 其次,应该有一副本的deployment启动
- 因为deployment是一副本,所以应该有一个pod启动
先写的这里了,如果有问题或者异议,请进QQ群630300475,大家聊聊