簡述
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,大家聊聊