云原生应用管理:原理与实践
上QQ阅读APP看书,第一时间看更新

2.6 创建自己的Chart

前面介绍了Chart的各种功能和写法,这里我们尝试创建一个自己的Chart。操作方法非常简单,仅需创建一个nginx deployment。


[root@iZ8vb0qditk1qw27yu4k5nZ ~]# helm create myChart
Creating myChart
[root@iZ8vb0qditk1qw27yu4k5nZ myChart]# ll
total 16
drwxr-xr-x 2 root root 4096 Aug  6 22:28 Charts
-rw-r--r-- 1 root root  103 Aug  6 22:28 Chart.yaml
drwxr-xr-x 3 root root 4096 Aug  6 22:28 templates
-rw-r--r-- 1 root root 1099 Aug  6 22:28 values.yaml

使用helm create命令就能创建一个Chart实例,可以看到基本的values Chart.yaml文件都已经具备了。


apiVersion: v1
appVersion: "1.0"
description: A Helm Chart for Kubernetes
name: myChart
version: 0.1.0

默认的Chart.yaml已经具备,那么我们便不再改动了。把template文件夹清空,添加一个自己的deployment.yaml。


piVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        ports:
        - containerPort: 80

上例是一个很简单的nginx deployment,下面我们修改value.yaml。


# Default values for myChart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: nginx
  tag: stable

下面安装我们的Chart。


[root@iZ8vb0qditk1qw27yu4k5nZ ~]# helm install myChart/
NAME:   funky-armadillo
LAST DEPLOYED: Tue Aug  6 22:40:00 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Deployment
NAME              READY  UP-TO-DATE  AVAILABLE  AGE
nginx-deployment  0/1    0           0          0s

==> v1/Pod(related)
NAME                               READY  STATUS   RESTARTS  AGE
nginx-deployment-59c847455d-gqkst  0/1    Pending  0         0s



root@iZ8vb0qditk1qw27yu4k5nZ ~]# kubectl get deploy nginx-deployment -o yaml | grep image
      - image: nginx:stable
        imagePullPolicy: IfNotPresent
[root@iZ8vb0qditk1qw27yu4k5nZ ~]# kubectl get deploy nginx-deployment -o yaml | grep repli
  replicas: 1
  replicas: 1

至此,我们成功创建了一个最简单的Chart。