helm install cache-api --set image.version=1.0 . 查看已部署的应用
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 环境变量配置
将 matchLabels 与 labels 的值都改为 {{ .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 。
在 resources 的 limits 与 requests 中设置 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: {} 验证配置运行下面的命令验证配置是否正确
helm install cache-api --set image.version=1.0 --dry-run --debug . 部署应用如果配置验证通过,就可以用下面的命令部署应用了。
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 1Kubernetes 与 Helm:使用同一个 Chart 部署多个应用
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31101.html