k8s 集群搭建好了,准备将 docker swarm 上的应用都迁移到 k8s 上,但需要一个一个应用写 yaml 配置文件,不仅要编写 deployment.yaml 还要编写 service.yaml ,而很多应用的配置是差不多的,这个繁琐工作让人有些望而却步。

k8s 有没有针对这个问题的解救之道呢?发现了救星 Helm —— k8s 应用程序包管理器,实际操作体验一下。

首先在 k8s master 节点上安装 helm ,用下面的1行命令就可以搞定。

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

接下来创建一个 chart (chart 就是 helm 的包包)

helm create cnblogs-chart

注:准备基于这个 chart 部署多个不同的应用。

helm 会创建一个文件夹,我们来看看文件夹中的内容:

cnblogs-chart
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│   └── test-connection.yaml
└── values.yaml

关于这些文件的用途,详见园子里的博文 kubernetes实战篇之helm示例yaml文件详细介绍

下面根据我们的部署场景修改 chart 中的这些配置文件,由于我们想使用同一个 chart 部署很多个应用,需要尽可能减少重复配置,所以在配置时会更多地基于约定。假设我们部署的应用名称是 cache-api ,那 helm 的 release 名称也用 cache-api ,docker 镜像的名称也用 cache-api ,deployment 与 service 的名称也用 cache-api ,ConfigMap 的名称是 cache-api-appsettings 。

修改 templates 中的配置(共享公用配置)

1)修改 deployment.yaml 中的配置

  • metadata.name 的值修改为 .Release.Name
  • containers.name 的值改为 .Release.Name
  • containers. image 的值改为 {{ .Release.Name }}:{{ .Values.image.version }}
  • 添加 containers. workingDir 容器工作目录配置
  • 添加 containers.command 容器启动命令配置
  • 添加 containers.env 环境变量配置
  • matchLabelslabels 的值都改为 {{ .Release.Name }}
  • 添加将 configMap 安装为 volume 的配置用于应用读取 appsettings.Production.json 。
metadata:
name: {{ .Release.Name }}
labels:
name: {{ .Release.Name }}
spec:
selector:
matchLabels:
app: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ .Release.Name }}
spec:
containers:
- name: {{ .Release.Name }}
image: "{{ .Release.Name }}:{{ .Values.image.version }}"
workingDir: /app
command:
- sh
- run.sh
env:
- name: TZ
value: "Asia/Shanghai"
volumeMounts:
- name: appsettings
mountPath: /app/appsettings.Production.json
subPath: appsettings.Production.json
readOnly: true
volumes:
- name: appsettings
configMap:
name: "{{ .Release.Name }}-appsettings"

2)修改 service.yaml

也是用约定的应用名称 name: {{ .Release.Name }}

kind: Service
metadata:
name: {{ .Release.Name }}
labels:
name: {{ .Release.Name }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
app: {{ .Release.Name }}

修改 values.yaml 中的配置(共享默认配置)

  • image.pullPolicy 修改为 Always
  • 添加 image.version 并设置为 latest
  • imagePullSecrets 中添加 secret 名称。
  • serviceAccount.create 设置为 false 。
  • resourceslimitsrequests 中设置 CPU 与内存限制。
replicaCount: 1

image:
repository: {}
version: latest
pullPolicy: Always imagePullSecrets:
- name: regcred
nameOverride: ""
fullnameOverride: "" serviceAccount:
create: false
name: podSecurityContext: {}
securityContext: {} service:
type: ClusterIP
port: 80 ingress:
enabled: false resources:
limits:
cpu: 2
memory: 2G
requests:
cpu: 100m
memory: 64Mi nodeSelector: {}
tolerations: []
affinity: {}

部署应用

1)验证配置

运行下面的命令验证配置是否正确

helm install cache-api --set image.version=1.0 --dry-run --debug .

2)部署应用

如果配置验证通过,就可以用下面的命令部署应用了。

helm install cache-api --set image.version=1.0 .

查看已部署的应用。

helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
cache-api production 1 2020-01-22 17:17:30.414863452 +0800 CST deployed cnblogs-chart-0.1.0 1

3)部署多个应用

现在可以基于前面创建的 chart 部署多个应用,只需通过 helm install 命令上传参数传递应用的相关配置信息,比如部署 news-web 与 q-web 这2个应用,可以分别使用下面的命令:

helm install news-web --set image.version=1.0.4,resources.limits.cpu=1 --dry-run --debug cnblogs-chart/
helm install ing-web --set image.version=1.3.11,resources.limits.cpu=1.5 --dry-run --debug cnblogs-chart/
05-28 17:31