27, k8s 之helm 模板


1,Helm Chart模板
Helm最核心的就是模板,即模板化的K8S manifests文件。
它本质上就是一个Go的template模板。Helm在Go template模板的基础上,还会增加很多东西。
如一些自定义的元数据信息、扩展的库以及一些类似于编程形式的工作流,例如条件语句、管道等等。这些东西都会使得我们的模板变得更加丰富。

创建模板:
[root@centos7 ~]# helm create nginx
[root@centos7 ~]# tree nginx/
nginx/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   └── service.yaml
└── values.yaml

2 directories, 4 files
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   28h
[root@centos7 ~]# 
[root@centos7 ~]# helm list
NAME	NAMESPACE	REVISION	UPDATED	STATUS	CHART	APP VERSION
[root@centos7 ~]# 
[root@centos7 ~]# helm install web-chat /root/nginx/
NAME: web-chat
LAST DEPLOYED: Sun Jul 12 22:41:30 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get all
NAME                      READY   STATUS    RESTARTS   AGE
pod/web-d86c95cc9-jgv7r   1/1     Running   0          34s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        28h
service/web          NodePort    10.0.0.43    <none>        80:30008/TCP   34s

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/web   1/1     1            1           34s

NAME                            DESIRED   CURRENT   READY   AGE
replicaset.apps/web-d86c95cc9   1         1         1       34s
[root@centos7 ~]#
[root@centos7 ~]# helm ls
NAME    	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
web-chat	default  	1       	2020-07-12 22:41:30.415225373 +0800 CST	deployed	nginx-0.1.0	1.16.0     
[root@centos7 ~]# 
[root@centos7 ~]# helm get manifest web-chat
---
# Source: nginx/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default 
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30008
  selector:
    app: web
---
# Source: nginx/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx

[root@centos7 ~]# 





[root@centos7 nginx]# ll
total 8
drwxr-xr-x. 2 root root   6 Jul 12 15:55 charts
-rw-r--r--. 1 root root 120 Jul 12 17:05 Chart.yaml
drwxr-xr-x. 2 root root  49 Jul 12 22:50 templates
-rw-r--r--. 1 root root  53 Jul 12 22:52 values.yaml
[root@centos7 nginx]# 
[root@centos7 nginx]# 
[root@centos7 nginx]# 
[root@centos7 nginx]# cat Chart.yaml 
apiVersion: v2
name: nginx
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: 1.16.0
[root@centos7 nginx]# 
[root@centos7 nginx]# cat values.yaml 
replicas: 3
image: nginx
imageTag: 1.17
label: nginx
[root@centos7 nginx]# 
[root@centos7 nginx]# cat templates/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    chart: {{ .Chart.Name  }}
    app: {{ .Release.Name }}
  name: {{ .Release.Name }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Values.label  }}
  template:
    metadata:
      labels:
        app: {{ .Values.label  }}
    spec:
      containers:
      - image: {{ .Values.image  }}:{{ .Values.imageTag  }}
        name: {{ .Release.Name }}
[root@centos7 nginx]# 
[root@centos7 nginx]# cat templates/service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default 
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30008
  selector:
    app: web
[root@centos7 nginx]# 
[root@centos7 nginx]# helm install web-chart --dry-run /root/nginx/
NAME: web-chart
LAST DEPLOYED: Sun Jul 12 23:00:26 2020
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: nginx/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default 
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30008
  selector:
    app: web
---
# Source: nginx/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    chart: nginx
    app: web-chart
  name: web-chart
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.17
        name: web-chart

[root@centos7 nginx]# 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM