创建自己的chart
前面我们快速使用了helm,包括安装开源的chart、练习了基础指令。我们的目标也包括让自己的应用使用chart模式管理起来。
这里体验一下

helm create创建chart模版

1
$ helm create <myfirstchart可以是自己应用的名字>
2
Creating myfirstchart
3
4
$ tree myfirstchart
5
myfirstchart
6
├── Chart.yaml
7
├── charts
8
├── templates
9
│   ├── NOTES.txt
10
│   ├── _helpers.tpl
11
│   ├── deployment.yaml
12
│   ├── ingress.yaml
13
│   ├── service.yaml
14
│   ├── serviceaccount.yaml
15
│   └── tests
16
│   └── test-connection.yaml
17
└── values.yaml
Copied!
可以看到指令生成了好多模版文件,比较熟悉的是deployment、ingress、service、account这些,包含了应用常用的编排文件。
1
# 我们什么也不改动,直接安装试试
2
$ helm install firstrelase ./myfirstchart
3
NAME: firstrelase
4
LAST DEPLOYED: Mon Feb 10 21:54:01 2020
5
NAMESPACE: liuzongxian
6
STATUS: deployed
7
REVISION: 1
8
NOTES:
9
1. Get the application URL by running these commands:
10
export POD_NAME=$(kubectl get pods --namespace liuzongxian -l "app.kubernetes.io/name=myfirstchart,app.kubernetes.io/instance=firstrelase" -o jsonpath="{.items[0].metadata.name}")
11
echo "Visit http://127.0.0.1:8080 to use your application"
12
kubectl --namespace liuzongxian port-forward $POD_NAME 8080:80
Copied!
看到可以安装成功,查看一下
1
$ k get all
2
NAME READY STATUS RESTARTS AGE
3
pod/firstrelase-myfirstchart-77577bc9c5-kdksg 1/1 Running 0 47s
4
5
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
6
service/firstrelase-myfirstchart ClusterIP 172.22.7.136 <none> 80/TCP 48s
7
8
NAME READY UP-TO-DATE AVAILABLE AGE
9
deployment.apps/firstrelase-myfirstchart 1/1 1 1 48s
10
11
NAME DESIRED CURRENT READY AGE
12
replicaset.apps/firstrelase-myfirstchart-77577bc9c5 1 1 1 48s
Copied!
deployment/replicaset/pod/service,都创建好了,所以一般应用来讲,只需要我们修改模版中的参数就好了。
常见的修改是直接修改values.yaml,参数不复杂,例如可以修改image、nodeSelector、亲和性等信息。
1
# cat values.yaml
2
# Default values for myfirstchart.
3
# This is a YAML-formatted file.
4
# Declare variables to be passed into your templates.
5
6
replicaCount: 1
7
8
image:
9
repository: nginx
10
pullPolicy: IfNotPresent
11
12
imagePullSecrets: []
13
nameOverride: ""
14
fullnameOverride: ""
15
16
serviceAccount:
17
# Specifies whether a service account should be created
18
create: true
19
# The name of the service account to use.
20
# If not set and create is true, a name is generated using the fullname template
21
name:
22
23
podSecurityContext: {}
24
# fsGroup: 2000
25
26
securityContext: {}
27
# capabilities:
28
# drop:
29
# - ALL
30
# readOnlyRootFilesystem: true
31
# runAsNonRoot: true
32
# runAsUser: 1000
33
34
service:
35
type: ClusterIP
36
port: 80
37
38
ingress:
39
enabled: false
40
annotations: {}
41
# kubernetes.io/ingress.class: nginx
42
# kubernetes.io/tls-acme: "true"
43
hosts:
44
- host: chart-example.local
45
paths: []
46
tls: []
47
# - secretName: chart-example-tls
48
# hosts:
49
# - chart-example.local
50
51
resources: {}
52
# We usually recommend not to specify default resources and to leave this as a conscious
53
# choice for the user. This also increases chances charts run on environments with little
54
# resources, such as Minikube. If you do want to specify resources, uncomment the following
55
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
56
# limits:
57
# cpu: 100m
58
# memory: 128Mi
59
# requests:
60
# cpu: 100m
61
# memory: 128Mi
62
63
nodeSelector: {}
64
65
tolerations: []
66
67
affinity: {}
Copied!
我们给自己的应用改造成chart,一般都会从helm create开始,并根据实际情况做些修改。
我们在根据实际应用编写chart时,可以经常做些语法检查,查看是否正确。
1
$ helm lint ./myfirstchart
2
==> Linting ./myfirstchart
3
[INFO] Chart.yaml: icon is recommended
4
5
1 chart(s) linted, 0 chart(s) failed
Copied!
到这里,我们创建并使用了自己的第一个chart!