1. 1. 说明
  2. 2. 常用命令
  3. 3. Chart说明
  4. 4. 内置对象
  5. 5. 提示和技巧
  6. 6. values文件
  7. 7. 流程控制
  8. 8. 变量
  9. 9. 命名模板
  10. 10. 访问模板文件
  11. 11. 子chart和全局值
    1. 11.1. 子chart
    2. 11.2. 全局chart值
    3. 11.3. 与子chart共享模板
  12. 12. Jumpserver 实例
    1. 12.1. 说明
    2. 12.2. 目录结构
    3. 12.3. YAML文件
    4. 12.4. 验证
  13. 13. 参考链接

说明

Helm 是 Kubernetes 生态系统中的软件包管理工具,方便编排

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
helm repo update # 更新repo
helm repo list # 查看repo仓库
helm repo remove stable # 移除repo
helm repo add stable https://cnych.github.io/kube-charts-mirror/ # 添加repo
helm search repo -l # 查找
helm inspect stable/mysql # 查看详细,描述信息,包括运行方式、配置信息
helm install mysql stable/mysql # 安装chart
helm list # 查看chart列表
helm pull # 下载应用安装包到本地
##### 自定义charts
helm create myapp # 创建chart
helm lint myapp # 验证chart是否符合规范
helm package myapp # 打包helm包
helm serve
helm install name repo-name --version 0.0.1 # 指定版本运行
helm install --dry-run --debug myapp or ./myapp or myapp.tgz # 干跑不会安装chart,会将渲染模板返回
helm get manifest xx # 检索版本并查看加载的实际模板
# 可通过 --set 来设变量, helm install ttxs --set foo=bar ./mychart

Chart说明

1
2
3
4
5
6
7
8
9
10
11
12
13
mychart
├── Chart.yaml # 包含 chart 的说明,可以从模板中查看访问它
├── charts # 该目录保存其他依赖的 chart(子 chart)
├── templates # chart 配置模板,用于渲染最终的 Kubernetes YAML 文件
│ ├── NOTES.txt # 用户运行 helm install 时候的提示信息
│ ├── _helpers.tpl # 用于创建模板时的帮助类
│ ├── deployment.yaml # Kubernetes deployment 配置
│ ├── ingress.yaml # Kubernetes ingress 配置
│ ├── service.yaml # Kubernetes service 配置
│ ├── serviceaccount.yaml # Kubernetes serviceaccount 配置
│ └── tests
│ └── test-connection.yaml
└── values.yaml # 定义 chart 模板中的自定义配置的默认值,可以在执行 helm install 或 helm update 的时候覆盖

内置对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Release.Name:release 名称
Release.Time:release 的时间
Release.Namespace:release 的 namespace(如果清单未覆盖)
Release.Service:release 服务的名称(始终是 Tiller)
Release.Revision:此 release 的修订版本号。它从 1 开始,每 helm upgrade 一次增加一个
Release.IsUpgrade:如果当前操作是升级或回滚,则将其设置为 true
Release.IsInstall:如果当前操作是安装,则设置为 true
Values:从 values.yaml 文件和用户提供的文件传入模板的值
Chart:Chart.yaml 文件的内容。任何数据 Chart.yaml 将在这里访问。例如 {{.Chart.Name}}-{{.Chart.Version}} 将打印出来 mychart-0.1.0
Files:提供对 chart 中所有非特殊文件的访问。虽然无法使用它来访问模板,但可以使用它来访问 chart 中的其他文件
Files.Get 是一个按名称获取文件的函数(.Files.Get config.ini)
Files.GetBytes 是将文件内容作为字节数组而不是字符串获取的函数
Capabilities.APIVersions 是一组版本信息
Capabilities.APIVersions.Has $version 指示是否在群集上启用版本(batch/v1)
Capabilities.KubeVersion 提供了查找 Kubernetes 版本的方法
Capabilities.TillerVersion 提供了查找 Tiller 版本的方法
Template:包含有关正在执行的当前模板的信息
Name:到当前模板的 namespace 文件路径
BasePath:当前 chart 模板目录的 namespace 路径

提示和技巧

  • include 函数允许引入另一个模板,然后将结果传递给其他模板函数
  • required 函数允许根据模板渲染的要求声明特定的值条目,如果该值为空,则显示定义的错误信息

    1
    value: {{required "A valid .Values.who entry required!" .Values.who}}
  • quote 为双引号将其包起来,并将字符串两边的空字符去掉

  • indent函数 ,对应缩进空格
  • tpl 函数允许开发人员将字符串计算为模板内的模板

    1
    {{tpl TEMPLATE_STRING VALUES}}
  • sha256sum 函数可用于确保在一个文件发生更改时更新 deployment 的注释

  • 在 templates/ 目录中,任何以下划线 “-“ 开头的文件都不会输出

values文件

  • chart中的values.yaml文件
  • 如果是子chart,来自父chart的values.yaml文件
  • value 文件通过helm install -f传递文件
  • 通过–set 来设置变量

流程控制

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
- if/else
{{if PIPELINE}}
# Do something
{{else if OTHER PIPELINE}}
# Do something else
{{else}}
# Default case
{{end}}
- with 指定范围
{{with PIPELINE}}
# restricted scope
{{end}}
- range 提供一个for each风格的循环
{{- range .Values.pizzaToppings}}
- {{. | title | quote}}
{{- end}}
- define 在模板中声明一个新的命名模板
- template 导入一个命名模板
- block 声明一种特殊的可填写模板区域
{{-(添加了破折号和空格)表示应该将格左移,而 -}} 意味着应该删除右空格。注意!换行符也是空格
- indent 4 # 4个空格 {{indent 2 "mug:true"}}

变量

1
2
3
4
5
6
7
8
9
10
变量是对另一个对象的命名引用。它遵循这个形式 $name。变量被赋予一个特殊的赋值操作符::=
{{- $relname := .Release.Name -}}
release: {{$relname}}
变量在 range 循环中特别有用
toppings: |-
{{- range $index, $topping := .Values.pizzaToppings}}
{{$index}}: {{ $topping }}
{{- end}}

命名模板

1
2
3
4
5
6
7
8
9
10
# 定义
{{ define "mychart.labels" }}
# body of template here
{{ end }}
# 使用
{{- template "mychart.labels" }}
# include函数
{{ include "mychart.labels" .| nindent 2}}

访问模板文件

Helm 通过 .Files 对象提供对文件的访问

  • 向Helm chart添加额外的文件是可以的,Chart必须小于1M
  • 某些文件不能通过.Files 对象访问, templates下无法访问文件, 使用.helmignore排除的文件不能被访问
  • chart 不保留UNIX模式信息,因此文件级权限在涉及.Files对象时不会影响文件的可用性
    1
    2
    3
    4
    5
    6
    7
    8
    apiVersion: v1
    kind: Secret
    metadata:
    name: {{.Release.Name}}-secret
    type: Opaque
    data:
    token: |-
    {{.Files.Get "files/config1.tom1" | b64enc}}

子chart和全局值

子chart
  • 子chart被认为是独立的, 子chart不能明确依赖于其父chart
  • 子chart无法访问其父项的值
  • 父chart 可以覆盖子chart的值
  • Helm有全局概念,可以被所有chart访问
全局chart值

全局值是可以从任何 chart 或子 chart 用完全相同的名称访问的值

values 数据类型有一个保留部分,称为 Values.global, 可以设置全局值

1
salad: {{.Values.global.salad}}

与子chart共享模板

父 chart 和子 chart 可以共享模板。任何 chart 中的任何定义块都可用于其他 chart

Jumpserver 实例

说明
  1. 主要通过deployment和service来实现
  2. 数据存储通过nfs来实现
  3. 通过子chart来定义不同应用,如下目录
  4. 通过_helpers.tpl中定义不同变量
    目录结构
    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    .
    ├── Chart.yaml
    ├── charts
    │   ├── core
    │   │   ├── Chart.yaml
    │   │   ├── templates
    │   │   │   ├── NOTES.txt
    │   │   │   ├── _helpers.tpl
    │   │   │   ├── deployment.yaml
    │   │   │   └── service.yaml
    │   │   └── values.yaml
    │   ├── guacamole
    │   │   ├── Chart.yaml
    │   │   ├── templates
    │   │   │   ├── NOTES.txt
    │   │   │   ├── _helpers.tpl
    │   │   │   ├── deployment.yaml
    │   │   │   └── service.yaml
    │   │   └── values.yaml
    │   ├── koko
    │   │   ├── Chart.yaml
    │   │   ├── templates
    │   │   │   ├── NOTES.txt
    │   │   │   ├── _helpers.tpl
    │   │   │   ├── deployment.yaml
    │   │   │   └── service.yaml
    │   │   └── values.yaml
    │   ├── mysql
    │   │   ├── Chart.yaml
    │   │   ├── templates
    │   │   │   ├── NOTES.txt
    │   │   │   ├── _helpers.tpl
    │   │   │   ├── deployment.yaml
    │   │   │   └── service.yaml
    │   │   └── values.yaml
    │   ├── nginx
    │   │   ├── Chart.yaml
    │   │   ├── templates
    │   │   │   ├── NOTES.txt
    │   │   │   ├── _helpers.tpl
    │   │   │   ├── deployment.yaml
    │   │   │   └── service.yaml
    │   │   └── values.yaml
    │   └── redis
    │   ├── Chart.yaml
    │   ├── templates
    │   │   ├── NOTES.txt
    │   │   ├── _helpers.tpl
    │   │   ├── deployment.yaml
    │   │   └── service.yaml
    │   └── values.yaml
    ├── templates
    │   ├── NOTES.txt
    │   ├── _helpers.tpl
    │   └── configmap.yaml
    └── values.yaml
YAML文件
  • mysql

    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    ########################## deployment.yaml ##########################
    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: {{ include "mysql.fullname" . }}
    labels:
    {{- include "mysql.labels" . | nindent 4 }}
    spec:
    {{- if not .Values.autoscaling.enabled }}
    replicas: {{ .Values.replicaCount }}
    {{- end }}
    selector:
    matchLabels:
    {{- include "mysql.selectorLabels" . | nindent 6 }}
    strategy:
    rollingUpdate: # 滚动更新
    maxSurge: {{ .Values.rollingUpdate.maxSurge }} # 指定可以超过期望的pod数量的最大百分比,也可以是个数
    maxUnavailable: {{ .Values.rollingUpdate.maxUnavailable }} # 指定升级过程中不可用Pod的最大数量
    type: {{ .Values.rollingUpdate.type }}
    template:
    metadata:
    {{- with .Values.podAnnotations }}
    annotations:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    labels:
    {{- include "mysql.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
    imagePullSecrets:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    securityContext:
    {{- toYaml .Values.podSecurityContext | nindent 8 }}
    containers:
    - name: {{ .Chart.Name }}
    securityContext:
    {{- toYaml .Values.securityContext | nindent 12 }}
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
    imagePullPolicy: {{ .Values.image.pullPolicy }}
    ports:
    - name: mysql
    containerPort: 3306
    protocol: TCP
    volumeMounts:
    - name: mysql
    mountPath: /var/lib/mysql
    env:
    - name: DB_PORT
    valueFrom:
    configMapKeyRef:
    name: {{.Release.Name}}-cfg
    key: db_port
    - name: DB_USER
    valueFrom:
    configMapKeyRef:
    name: {{.Release.Name}}-cfg
    key: db_user
    - name: DB_NAME
    valueFrom:
    configMapKeyRef:
    name: {{.Release.Name}}-cfg
    key: db_name
    - name: DB_PASSWORD
    valueFrom:
    configMapKeyRef:
    name: {{.Release.Name}}-cfg
    key: db_password
    resources:
    {{- toYaml .Values.resources | nindent 12 }}
    volumes:
    {{- include "mysql.persistence.volume" (list . "mysql") | nindent 6 }} # 这里使用_helpers.tpl定义的mysql.persistence.volume
    {{- with .Values.nodeSelector }}
    nodeSelector:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
    affinity:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
    tolerations:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    ########################## service.yaml ##########################
    # service.yaml
    apiVersion: v1
    kind: Service
    metadata:
    # name: {{ include "mysql.fullname" . }}
    name: mysql
    labels:
    {{- include "mysql.labels" . | nindent 4 }}
    spec:
    type: {{ .Values.service.type }}
    ports:
    - port: {{ .Values.service.port }}
    targetPort: mysql
    protocol: TCP
    name: mysql # http
    selector:
    {{- include "mysql.selectorLabels" . | nindent 4 }}
    sessionAffinity: ClientIP
    ########################## _helpers.tpl ##########################
    # _helpers.tpl
    {{/*
    Expand the name of the chart.
    */}}
    {{- define "mysql.name" -}}
    {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
    {{- end }}
    {{/*
    Create a default fully qualified app name.
    We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
    If release name contains chart name it will be used as a full name.
    */}}
    {{- define "mysql.fullname" -}}
    {{- if .Values.fullnameOverride }}
    {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
    {{- else }}
    {{- $name := default .Chart.Name .Values.nameOverride }}
    {{- if contains $name .Release.Name }}
    {{- .Release.Name | trunc 63 | trimSuffix "-" }}
    {{- else }}
    {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
    {{- end }}
    {{- end }}
    {{- end }}
    {{/*
    Create chart name and version as used by the chart label.
    */}}
    {{- define "mysql.chart" -}}
    {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
    {{- end }}
    {{/*
    Common labels
    */}}
    {{- define "mysql.labels" -}}
    helm.sh/chart: {{ include "mysql.chart" . }}
    {{ include "mysql.selectorLabels" . }}
    {{- if .Chart.AppVersion }}
    app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
    {{- end }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    {{- end }}
    {{/*
    Selector labels
    */}}
    {{- define "mysql.selectorLabels" -}}
    app.kubernetes.io/name: {{ include "mysql.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    {{- end }}
    {{/*
    Create the name of the service account to use
    */}}
    {{- define "mysql.serviceAccountName" -}}
    {{- if .Values.serviceAccount.create }}
    {{- default (include "mysql.fullname" .) .Values.serviceAccount.name }}
    {{- else }}
    {{- default "default" .Values.serviceAccount.name }}
    {{- end }}
    {{- end }}
    {{/*
    Define persistence volumes.
    */}}
    {{- define "mysql.persistence.volume" -}}
    {{- $dot := index . 0 -}}
    {{- $name := index . 1 -}}
    {{- if $dot.Values.persistence -}}
    {{- if $dot.Values.persistence.nfs -}}
    - name: {{ $name | quote }}
    nfs:
    server: {{ $dot.Values.persistence.nfsserver | quote }}
    path: {{ $dot.Values.persistence.nfspath | quote }}
    {{- else -}}
    - name: {{ $name | quote }}
    emptyDir: {}
    {{- end -}}
    {{- end -}}
    {{- end -}}
    ########################## values.yaml ##########################
    # values.yaml
    # Default values for mysql.
    # This is a YAML-formatted file.
    # Declare variables to be passed into your templates.
    replicaCount: 1
    image:
    repository: "wojiushixiaobai/jms_mysql"
    pullPolicy: IfNotPresent
    # Overrides the image tag whose default is the chart appVersion.
    tag: "1.5.6"
    imagePullSecrets: []
    nameOverride: ""
    fullnameOverride: ""
    serviceAccount:
    # Specifies whether a service account should be created
    create: true
    # Annotations to add to the service account
    annotations: {}
    # The name of the service account to use.
    # If not set and create is true, a name is generated using the fullname template
    name: ""
    podAnnotations: {}
    podSecurityContext: {}
    # fsGroup: 2000
    securityContext: {}
    # capabilities:
    # drop:
    # - ALL
    # readOnlyRootFilesystem: true
    # runAsNonRoot: true
    # runAsUser: 1000
    service:
    type: ClusterIP
    port: 3306
    ingress:
    enabled: false
    annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
    hosts:
    - host: chart-example.local
    paths: []
    tls: []
    # - secretName: chart-example-tls
    # hosts:
    # - chart-example.local
    resources: {}
    # We usually recommend not to specify default resources and to leave this as a conscious
    # choice for the user. This also increases chances charts run on environments with little
    # resources, such as Minikube. If you do want to specify resources, uncomment the following
    # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
    # limits:
    # cpu: 100m
    # memory: 128Mi
    # requests:
    # cpu: 100m
    # memory: 128Mi
    autoscaling:
    enabled: false
    minReplicas: 1
    maxReplicas: 100
    targetCPUUtilizationPercentage: 80
    # targetMemoryUtilizationPercentage: 80
    nodeSelector: {}
    tolerations: []
    affinity: {}
    rollingUpdate:
    maxSurge: 25%
    maxUnavailable: 25%
    type: RollingUpdate
    persistence:
    nfs: true
    nfsserver: 192.168.238.100
    nfspath: /data/mysql
    #nfs:
    # server: 192.168.238.100
    # path: /data/mysql
  • redis

    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    ########################## deployment.yaml ##########################
    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: {{ include "redis.fullname" . }}
    labels:
    {{- include "redis.labels" . | nindent 4 }}
    spec:
    {{- if not .Values.autoscaling.enabled }}
    replicas: {{ .Values.replicaCount }}
    {{- end }}
    selector:
    matchLabels:
    {{- include "redis.selectorLabels" . | nindent 6 }}
    strategy:
    rollingUpdate: # 滚动更新
    maxSurge: {{ .Values.rollingUpdate.maxSurge }} # 指定可以超过期望的pod数量的最大百分比,也可以是个数
    maxUnavailable: {{ .Values.rollingUpdate.maxUnavailable }} # 指定升级过程中不可用Pod的最大数量
    type: {{ .Values.rollingUpdate.type }}
    template:
    metadata:
    {{- with .Values.podAnnotations }}
    annotations:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    labels:
    {{- include "redis.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
    imagePullSecrets:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    securityContext:
    {{- toYaml .Values.podSecurityContext | nindent 8 }}
    containers:
    - name: {{ .Chart.Name }}
    securityContext:
    {{- toYaml .Values.securityContext | nindent 12 }}
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
    imagePullPolicy: {{ .Values.image.pullPolicy }}
    ports:
    - name: redis
    containerPort: 6379
    protocol: TCP
    volumeMounts:
    - name: redis
    mountPath: /var/lib/redis
    env:
    - name: REDIS_PORT
    valueFrom:
    configMapKeyRef:
    name: {{.Release.Name}}-cfg
    key: redis_port
    - name: REDIS_HOST
    valueFrom:
    configMapKeyRef:
    name: {{.Release.Name}}-cfg
    key: redis_host
    - name: REDIS_PASSWORD
    valueFrom:
    configMapKeyRef:
    name: {{.Release.Name}}-cfg
    key: redis_password
    resources:
    {{- toYaml .Values.resources | nindent 12 }}
    volumes: # 使用nfs来存储
    - name: redis
    nfs:
    path: {{ .Values.nfs.path }}
    server: {{ .Values.nfs.server }}
    {{- with .Values.nodeSelector }}
    nodeSelector:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
    affinity:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
    tolerations:
    {{- toYaml . | nindent 8 }}
    {{- end }}
    ########################## service.yaml ##########################
    # service.yaml
    apiVersion: v1
    kind: Service
    metadata:
    # name: {{ include "redis.fullname" . }}
    name: redis
    labels:
    {{- include "redis.labels" . | nindent 4 }}
    spec:
    type: {{ .Values.service.type }}
    ports:
    - port: {{ .Values.service.port }}
    targetPort: redis
    protocol: TCP
    name: redis
    selector:
    {{- include "redis.selectorLabels" . | nindent 4 }}
    sessionAffinity: ClientIP
    ########################## service.yaml ##########################
    # service.yaml
    # Default values for redis.
    # This is a YAML-formatted file.
    # Declare variables to be passed into your templates.
    replicaCount: 1
    image:
    repository: "wojiushixiaobai/jms_redis"
    pullPolicy: IfNotPresent
    # Overrides the image tag whose default is the chart appVersion.
    tag: "1.5.6"
    imagePullSecrets: []
    nameOverride: ""
    fullnameOverride: ""
    serviceAccount:
    # Specifies whether a service account should be created
    create: true
    # Annotations to add to the service account
    annotations: {}
    # The name of the service account to use.
    # If not set and create is true, a name is generated using the fullname template
    name: ""
    podAnnotations: {}
    podSecurityContext: {}
    # fsGroup: 2000
    securityContext: {}
    # capabilities:
    # drop:
    # - ALL
    # readOnlyRootFilesystem: true
    # runAsNonRoot: true
    # runAsUser: 1000
    service:
    type: ClusterIP
    port: 6379
    ingress:
    enabled: false
    annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
    hosts:
    - host: chart-example.local
    paths: []
    tls: []
    # - secretName: chart-example-tls
    # hosts:
    # - chart-example.local
    resources: {}
    # We usually recommend not to specify default resources and to leave this as a conscious
    # choice for the user. This also increases chances charts run on environments with little
    # resources, such as Minikube. If you do want to specify resources, uncomment the following
    # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
    # limits:
    # cpu: 100m
    # memory: 128Mi
    # requests:
    # cpu: 100m
    # memory: 128Mi
    autoscaling:
    enabled: false
    minReplicas: 1
    maxReplicas: 100
    targetCPUUtilizationPercentage: 80
    # targetMemoryUtilizationPercentage: 80
    nodeSelector: {}
    tolerations: []
    affinity: {}
    rollingUpdate:
    maxSurge: 25%
    maxUnavailable: 25%
    type: RollingUpdate
    nfs:
    server: 192.168.238.100
    path: /data/redis

其他应用core、guacamole、koko、nginx的实现方式见Github

验证
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
33
34
35
36
37
# install jms
# helm install jmst ./jms-helm
NAME: jmst
LAST DEPLOYED: Fri Aug 21 09:16:16 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
1. Get the application URL by running these commands:
export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services nginx)
export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
jmst default 1 2020-08-21 09:16:16.918124 +0800 CST deployed jms-helm-0.1.0 1.0.0
# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/jmst-core-567c775569-pr7lt 1/1 Running 0 2m3s
pod/jmst-guacamole-5ffc5d5795-8zgrg 1/1 Running 0 2m1s
pod/jmst-koko-6b6d7c8466-p8j78 1/1 Running 0 2m3s
pod/jmst-mysql-56986c795b-gpnrs 1/1 Running 0 2m3s
pod/jmst-nginx-54f7cfc887-c96kv 1/1 Running 0 2m3s
pod/jmst-redis-677878f7c4-nvp5l 1/1 Running 0 2m3s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/core ClusterIP 10.233.59.124 <none> 8080/TCP,8070/TCP 2m3s
service/guacamole ClusterIP 10.233.38.162 <none> 8080/TCP 2m3s
service/koko NodePort 10.233.24.158 <none> 2222:32222/TCP,5000:32220/TCP 2m3s
service/kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 8d
service/mysql ClusterIP 10.233.21.166 <none> 3306/TCP 2m3s
service/nginx NodePort 10.233.32.19 <none> 80:30080/TCP 2m3s
service/redis ClusterIP 10.233.19.248 <none> 6379/TCP 2m3s
# 访问http://192.168.238.100:30080即可访问服务

参考链接

https://hub.kubeapps.com
https://whmzsu.github.io/helm-doc-zh-cn/
https://github.com/helm/charts