暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Kubernetes 与 Helm:使用同一个 Chart 部署多个应用

51reboot运维开发 2020-09-22
1512



点击标题下「蓝色微信名」可快速关注



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 文件详细介绍(https://www.cnblogs.com/tylerzhou/p/11141538.html 。

下面根据我们的部署场景修改 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: {}



部署应用


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/



出处:http://dwz.date/cAjn


最新 k8s 直播课火热招生中 
扫码咨询>>>


整理 kubernetes 各种问题汇总

使用 Elastic 技术栈构建 Kubernetes 全栈监控(完结)

Istio 的部署模型介绍(二)

云开发-web应用中使用数据库

运维精华


文章转载自51reboot运维开发,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论