helm3 chart开发

安装helm3

1
2
3
4
5
6
7
#wget https://docs.rancher.cn/download/helm/helm-v3.3.0-linux-amd64.tar.gz 
wget https://get.helm.sh/helm-v3.9.2-linux-amd64.tar.gz #下载helm安装包

tar -zxvf helm-v3.3.0-linux-amd64.tar.gz #解压安装包
cp linux-amd64/helm /usr/local/bin/

helm version #检验是否安装成功

helm自动补全

1
2
3
source <(helm completion bash)
echo "source <(helm completion bash)" >> ~/.bashrc
source ~/.bashrc

helm常用命令

命令 用法 描述
create helm create NAME [flags] create a new chart with the given name
install helm install [NAME] [CHART] [flags] installs a chart
pull helm pull [chart URL |repo/chartname] […] [flags] download a chart from a repository and (optionally) unpack it in local directory
repo helm repo … add, list, remove, update, and index chart repositories
search helm search [command] ( repo/hub ) search for a keyword in charts
uninstall helm uninstall RELEASE_NAME […] [flags] uninstall a release
upgrade helm upgrade [RELEASE] [CHART] [flags] upgrade a release

创建chart包

1
helm create httpbin  #创建httpbin chart

查看httpbin的目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sudo apt install tree #安装tree

tree httpbin -a
httpbin
├── charts
├── Chart.yaml
├── .helmignore
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml

3 directories, 10 files

Chart包文件结构

Helm规范了Chart的目录和文件结构,这些目录或者文件都有确定的用途。

修改values.yaml

修改values.yaml如下,用于Helm渲染template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
cd httpbin/
vi values.yaml

# Default values for httpbin.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
repository: docker.io/kennethreitz/httpbin #修改镜像为httpbin
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: latest

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
# Specifies whether a service account should be created
create: false #不创建serviceAccount,改为false
# 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 #将type改为NodePort
port: 80
nodePort: 30080 #为NodePort配置一个固定端口


ingress:
enabled: false
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: chart-example.local
paths: []
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: {}

修改service.yaml

由于配置了固定的nodePort,所以在service.yaml中增加该参数,并引用了对应的value值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vi templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ include "httpbin.fullname" . }}
labels:
{{- include "httpbin.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
nodePort: {{ .Values.service.nodePort }} #新增参数
selector:
{{- include "httpbin.selectorLabels" . | nindent 4 }}

查看渲染后结果

1
helm template my-release httpbin

安装httpbin

渲染没有问题后开始安装httpbin

1
helm install my-release httpbin

访问httpbin

1
kubectl get pod #观察httpbin是否部署成功

1
访问 http://IP:30080

打包chart

部署成功后可将chart进行打包

1
helm package httpbin