Helm模板常用组件

发布时间:2020-07-03 22:42:27 作者:蓝叶子Sheep
来源:网络 阅读:193

应用镜像

应用镜像是chart包的核心,必须包含:镜像仓库地址、镜像名称、镜像版本,values.yaml字段规范如下:

repository:
  hub: docker.io
  image: gitlab/gitlab-ce
  tag: 11.1.4-ce.0
deployment中引用:
image: {{ .Values.repository.hub }}/{{ .Values.repository.image }}:{{ .Values.repository.tag }}

探针

应用的探针用于检测该应用是否健康,是否准备好对外提供服务。健康状况是非常关键的属性,因此每个应用必须都明确表示如何进行健康检测。

values.yaml文件必须包含以下探针属性:

livenessProbe: # 存活探针
   enabled: true
   path: /  # 心跳检测的接口,默认都是httpGet请求
   initialDelaySeconds: 30 # 容器启动后第一次执行探测是需要等待多少秒。
   periodSeconds: 60 # 执行探测的频率。默认是10秒,最小1秒。
   timeoutSeconds: 5 # 探测超时时间。默认1秒,最小1秒。
   successThreshold: 1 # 探测失败后,最少连续探测成功多少次才被认定为成功。默认是1
   failureThreshold: 5 # 探测成功后,最少连续探测失败多少次才被认定为失败。默认是3
 readinessProbe: # 就绪探针
   enabled: true
   path: /  # 心跳检测的接口,默认都是httpGet请求
   initialDelaySeconds: 10
   periodSeconds: 10
   timeoutSeconds: 5
   successThreshold: 1
   failureThreshold: 5

deployment.yaml文件引用探针如下:

{{- if .Values.livenessProbe.enabled }}
livenessProbe:
  initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
  periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
  timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
  successThreshold: {{ .Values.livenessProbe.successThreshold }}
  failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
  httpGet:
      path: {{ .Values.livenessProbe.path }}
      port: http
{{- end }}
{{- if .Values.readinessProbe.enabled }}
readinessProbe:
  initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
  periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
  timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
  successThreshold: {{ .Values.readinessProbe.successThreshold }}
  failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
  httpGet:
      path: {{.Values.readinessProbe.path }}
      port: http
{{- end }}

资源限制

每个应用都需要使用到CPU和内存资源,如果不加以明确说明,则会导致资源的无畏浪费,并且在资源不足时第一时间就被回收。

因此应用都应当明确申明自己所需的资源大小。

values.yaml文件包含以下属性:

resources:
  requests:  # 声明最少使用的资源,不够的话则应用无法启动成功
    memory: 256Mi
    cpu: 100m
  limits:    # 声明最大使用的资源,超过即重启应用pod
    memory: 256Mi
    cpu: 100m

持久化存储

应用如果不使用持久化存储,那么当应用重启的时候,之前保存的数据将会都清空。在web无状态应用中,这是保持环境干净很好的一种方式。

但是如果我们应用有数据需要持久保留的话,就需要使用到持久化存储了。以下是持久化存储规范:

values.yaml文件需包含以下属性:

persistence:
  enabled: true
  local:
    enabled: false # 是否启用本地存储
    name: gitlab-pg  # 对应本地存储名称
  storageClass: "nfs-dynamic-class"    # 集群共享存储
  accessMode: ReadWriteOnce # 存储访问模式
  size: 30Mi   # 声明所需存储的大小
  annotations: {}

templates目录下新建auto-pvc.yaml文件,这里之所以要加一个auto前缀就是为了保证安装时首先执行安装pvc,默认helm是安装文件名顺序来执行安装的。

内容如下:

{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: {{ .Release.Name }}-pvc
{{- with .Values.persistence.annotations  }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
  labels:
    app: {{ .Release.Name }}
spec:
{{- if .Values.persistence.local.enabled }}
  volumeName: "{{ .Values.persistence.local.name }}"
{{- end }}
  accessModes:
    - {{ .Values.persistence.accessMode | quote }}
  resources:
    requests:
      storage: {{ .Values.persistence.size | quote }}
{{- if .Values.persistence.storageClass }}
{{- if (eq "-" .Values.persistence.storageClass) }}
  storageClassName: ""
{{- else }}
  storageClassName: "{{ .Values.persistence.storageClass }}"
{{- end }}
{{- end }}
{{- end }}

最后根据应用需要读写的路径,挂载到容器内,修改deployment.yaml文件,添加以下内容:

volumes:
- name: data
  {{- if .Values.persistence.enabled }}
  persistentVolumeClaim:
    claimName: {{ .Release.Name }}-pvc
  {{- else }}
  emptyDir: {}
  {{- end }}

# containers子标签中添加

volumeMounts:
- name: data
  mountPath: /mnt # 应用容器内需要读写的路径

本地存储

我们以后有很多应用都需要支持本地存储,本地存储修改内容如下:

values.yaml
persistence:
  enabled: true
  local:
    enabled: true    # 启用本地存储
    name: gitlab-pg  # 对应本地存储名称
  accessMode: ReadWriteOnce
  size: 20Gi
  storageClass: "-"  # 取消共享存储
  annotations: {}
nodeSelector:
  kubernetes.io/hostname: 10.160.144.72  # 对应本地运行主机

安全

设置Pod运行的用户,一般情况为了安全起见,容器内不建议使用root用户进行运行。但是如果需要使用本地存储,那么就必须要使用root用户运行了。

另外在使用共享存储时,可以将usePodSecurityContext设置为false。

设置以root运行,在values.yaml文件添加:

security:
  usePodSecurityContext: true
  runAsUser: 0
  fsGroup: 0

在deployment.yaml文件中添加:

{{- if .Values.security.usePodSecurityContext }}
      securityContext:
        runAsUser: {{ default 0 .Values.security.runAsUser }}
{{- if and (.Values.security.runAsUser) (.Values.security.fsGroup) }}
        fsGroup: {{ .Values.security.fsGroup }}
{{- end }}
{{- end }}
推荐阅读:
  1. Helm小技巧
  2. Helm安装及配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

helm模板常用组件 he

上一篇:每个程序员必须知道的十大机器学习算法

下一篇:删除30天以前的backup.sql

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》