: 构建一个Helm Chart

[[email protected] ~]# helm create mychart    

Creating mychart

[[email protected] ~]# ls mychart/

charts  Chart.yaml  templates  values.yaml

[[email protected] ~]#

启动创建的mychart

[[email protected] ~]# helm install test mychart/

NAME: test

LAST DEPLOYED: Tue Apr 28 14:07:53 2020

NAMESPACE: default

STATUS: deployed

REVISION: 1

NOTES:

1. Get the application URL by running these commands:

  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=test" -o jsonpath="{.items[0].metadata.name}")

  echo "Visit http://127.0.0.1:8080 to use your application"

  kubectl --namespace default port-forward $POD_NAME 8080:80

[[email protected] ~]#

[[email protected] ~]#

[[email protected] ~]# kubectl get pod|grep test

test-mychart-b5cd6d7c8-r8vf4              1/1     Running   0          56s

[[email protected] ~]#

[[email protected] ~]# kubectl get svc

NAME                  TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE

db01-mysql            ClusterIP   10.0.0.206   <none>        3306/TCP       2d

kubernetes            ClusterIP   10.0.0.1     <none>        443/TCP        6d22h

metrics-app           ClusterIP   10.0.0.254   <none>        80/TCP         4d4h

mysql                 ClusterIP   10.0.0.91    <none>        3306/TCP       61m

mysql-02              ClusterIP   10.0.0.169   <none>        3306/TCP       41m

nginx-service         NodePort    10.0.0.75    <none>        80:30574/TCP   6d4h

nginx-service-nfs02   NodePort    10.0.0.73    <none>        80:31835/TCP   6d21h

test-mychart          ClusterIP   10.0.0.24    <none>        80/TCP         6m49s

web                   ClusterIP   10.0.0.186   <none>        80/TCP         4d21h

[[email protected] ~]# curl 10.0.0.24

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

    body {

        width: 35em;

        margin: 0 auto;

        font-family: Tahoma, Verdana, Arial, sans-serif;

    }

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

 

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

 

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

[[email protected] ~]#

 

[[email protected] ~]# curl -I 10.0.0.24

HTTP/1.1 200 OK

Server: nginx/1.16.0

Date: Tue, 28 Apr 2020 06:16:32 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Tue, 23 Apr 2019 10:18:21 GMT

Connection: keep-alive

ETag: "5cbee66d-264"

Accept-Ranges: bytes

 

[[email protected] ~]#

 

[[email protected] ~]#

[[email protected] ~]# helm list

NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION

db01            default         1               2020-04-26 13:56:58.07831662 +0800 CST  deployed        mysql-1.6.3     5.7.28    

mysql           default         1               2020-04-28 13:13:23.036473313 +0800 CST deployed        mysql-1.6.3     5.7.28    

mysql-02        default         1               2020-04-28 13:33:06.455685197 +0800 CST deployed        mysql-1.6.3     5.7.28    

test            default         1               2020-04-28 14:07:53.478943141 +0800 CST deployed        mychart-0.1.0   1.16.0    

[[email protected] ~]#

[[email protected] ~]#

Helm Chart的构建

 

[[email protected] mychart]# tree

.

├── charts                                                

├── Chart.yaml                                       

├── templates                                     

│?? ├── deployment.yaml     

│?? ├── _helpers.tpl             

│?? ├── ingress.yaml

│?? ├── NOTES.txt                              

│?? ├── serviceaccount.yaml

│?? ├── service.yaml

│?? └── tests

│??     └── test-connection.yaml

└── values.yaml                                 

 

3 directories, 9 files

[[email protected] mychart]#

[[email protected] mychart]#

删除templates 里面的yaml ,重新部署一个charts

 

[[email protected] mychart]# tree .

.

├── charts

├── Chart.yaml

├── templates

└── values.yaml

 

2 directories, 2 files

[[email protected] mychart]#

 

创建一个deployment templates

 

[[email protected] templates]#

[[email protected] templates]#

[[email protected] templates]# kubectl create deployment mychart  --image=nginx:1.16 -o yaml --dry-run

apiVersion: apps/v1

kind: Deployment

metadata:

  creationTimestamp: null

  labels:

    app: mychart

  name: mychart

spec:

  replicas: 1

  selector:

    matchLabels:

      app: mychart

  strategy: {}

  template:

    metadata:

      creationTimestamp: null

      labels:

        app: mychart

    spec:

      containers:

      - image: nginx:1.16

        name: nginx

        resources: {}

status: {}

[[email protected] templates]#

重定向到本地的deployment.yaml文件

[[email protected] templates]#

[[email protected] templates]# kubectl create deployment mychart  --image=nginx:1.16 -o yaml --dry-run > deployment.yaml

[[email protected] templates]#

[[email protected] templates]#

修改deployment.yaml 文件,把不用的代码删除

[[email protected] templates]#

[[email protected] templates]# cat deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  labels:

    app: mychart

  name: mychart

spec:

  replicas: 1

  selector:

    matchLabels:

      app: mychart

  template:

    metadata:

          labels:

        app: mychart

    spec:

      containers:

      - image: nginx:1.16

        name: nginx

[[email protected] templates]#

deployment模板的 镜像和副本数设置变量

[[email protected] templates]# cat deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: {{ .Values.name }}

spec:

  replicas: {{ .Values.replicas }}

  selector:

    matchLabels:

      app: mychart

  template:

    metadata:

      labels:

        app: mychart

    spec:

      containers:

      - image: {{ .Values.image }}:{{ .Values.imageTag }}

        name: nginx

[[email protected] templates]#

变量在..valume.yaml 中定义,如变量的值如下:

[[email protected] templates]# vi ../values.yaml

[[email protected] mychart]# cat values.yaml

name: mychars

replicas: 3

image: nginx

imageTag: latest

[[email protected] mychart]#

接下来可以安装charts

[[email protected] ~]# helm install mycharts mychart/

NAME: mycharts

LAST DEPLOYED: Tue Apr 28 15:03:35 2020

NAMESPACE: default

STATUS: deployed

REVISION: 1

TEST SUITE: None

[[email protected] ~]#

[[email protected] ~]# kubectl get deployment|grep mychars

mychars                  3/3     3            3           95s

[[email protected] ~]

 

[[email protected] ~]# kubectl get pod|grep mychars

mychars-bc975b9d4-gdbr9                   1/1     Running   0          78s

mychars-bc975b9d4-v2dcg                   1/1     Running   0          78s

mychars-bc975b9d4-x6xz7                   1/1     Running   0          78s

[[email protected] ~]#

[[email protected] ~]# helm list

NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION

mycharts        default         1               2020-04-28 15:03:35.111744041 +0800 CST deployed        mychart-0.1.0   1.16.0    

[[email protected] ~]#

查看渲染后的代码结果

[[email protected] ~]#

[[email protected] ~]# helm get manifest mychars

Error: release: not found

[[email protected] ~]# helm get manifest mycharts

---

# Source: mychart/templates/deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: mychars

spec:

  replicas: 3

  selector:

    matchLabels:

      app: mychart

  template:

    metadata:

      labels:

        app: mychart

    spec:

      containers:

      - image: nginx:latest

        name: nginx

 

[[email protected] ~]#

 

升级、回滚和删除

发布新版本的chart时,或者当您要更改发布的配置时,可以使用该helm upgrade 命令。

# helm upgrade --set imageTag=1.17 web mychart

# helm upgrade -f values.yaml web mychart

如果在发布后没有达到预期的效果,则可以使用helm rollback回滚到之前的版本。

例如将应用回滚到第一个版本:

# helm rollback web 2

卸载发行版,请使用以下helm uninstall命令:

# helm uninstall web

查看历史版本配置信息

# helm get --revision 1 web

 

打包

[[email protected] ~]# helm package mychart

Successfully packaged chart and saved it to: /root/mychart-0.1.0.tgz

[[email protected] ~]#

[[email protected] ~]#

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2019-12-30
  • 2021-11-09
  • 2021-05-19
  • 2022-12-23
猜你喜欢
  • 2022-03-05
  • 2022-12-23
  • 2018-07-07
  • 2021-12-30
  • 2022-12-23
  • 2022-01-19
  • 2021-09-20
相关资源
相似解决方案