helm 部署并配置

helm 部署并配置

helm 部署

之前都是编辑一大堆 yaml 文件来部署服务到 k8s,什么 dployment, service, ingress 这些都得写,太麻烦了所以现在改造一下用 helm来管理 k8s 的依赖管理

首先创建一个 chart

1
helm create xxx


简单解释下里面的文件夹都是啥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
xxx
├─.helmignore
├─Chart.yaml # 创建的chart 一些信息
├─values.yaml # tempalet 里面需要的一些值
├─templates # 一些k8s中的模版 ingress,service,deployment 最主要用到的可能就这三个
| ├─NOTES.txt
| ├─_helpers.tpl
| ├─deployment.yaml
| ├─hpa.yaml
| ├─ingress.yaml
| ├─service.yaml
| ├─serviceaccount.yaml
| ├─tests
| | └test-connection.yaml
├─charts

那我们就留 deployment 和 service 两个模版由于我是用的是 k3s 里面的流量控制器不是 nginx 而是 traefik,所以我们用 crd 编辑一下,再把 deployment 里面不需要用到的删了,就跟配置 k8s 的 yaml 一样,如果不会的话最好看看 k8s 小编也不太精通还只是到会用的层面 QAQ ~
ingress

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: my-ingress
spec:
entryPoints:
- web
routes:
- match: Host(`{{ .Values.ingress.host }}`) && PathPrefix(`{{ .Values.ingress.path }}`)
kind: Rule
services:
- name: { { include "deploy.fullname" . } }
port: { { .Values.service.port } }

deployment

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
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "blog.fullname" . }}
labels:
{{- include "blog.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "blog.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "blog.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 80
protocol: TCP

service

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: { { include "deploy.fullname" . } }
labels: { { - include "deploy.labels" . | nindent 4 } }
spec:
type: { { .Values.service.type } }
ports:
- port: { { .Values.service.port } }
targetPort: http
protocol: TCP
name: http
selector: { { - include "deploy.selectorLabels" . | nindent 4 } }

.Values.xxx 就是在 values.yaml 中定义的一些常量,然后在使用 helm 部署上去

1
2
3
4
5
6
7
helm upgrade <chart实例名称> <chart路径> \
-n <k8s pod> \
--set 'image.repository=$(APP_IMAGE_NAME)' \
--set 'image.tag=$(APP_IMAGE_TAG)' \
--install

我这里是用了makefile 来执行,镜像名称和tag 都是自动生成的所以 chart中的 repository 和 tag 我都没写在这里替换一下,你们可以直接写死或者跟我一样


最后这样就成功啦!!整个 helm 部署的过程还是很简单的,就是里面有很多高级的配置项值得去研究一下,helm 总的来说就是方便了我们部署,并不需要写一大堆文件然后挨个 kubectl apply 上去,所以赶紧用起来吧~