④使用Helm开发一个Chart模板

文章目录

      • 1. 先创建模板
      • 2. 修改Chart.yaml,Values.yaml,添加常用的变量
      • 3. 修改NOTES.txt
      • 4. 在templates目录下创建部署镜像所需要的yaml文件
      • 5. 在 _helpers.tpl 重新定义三个模板
      • 6. 修改service.yaml中的字段
      • 8. 修改ingress.yaml中的字段
      • 9. 测试生成
      • 10. 正式生成
    • 3.8 使用Harbor作为Chart仓库
      • 1、启用Harbor的Chart仓库服务
      • 2、安装push插件
      • 3、添加repo
      • 4、推送与安装Chart

1. 先创建模板

helm create demo

[root@k8s-master helm]# tree demo
demo
├── charts
├── Chart.yaml
├── templates
├──├── deployment.yaml
├──├── _helpers.tpl
├──├── ingress.yaml
├──├── NOTES.txt
├──└── service.yaml
└── values.yaml

2 directories, 7 files

2. 修改Chart.yaml,Values.yaml,添加常用的变量

[root@k8s-master demo]# cat Chart.yaml 
apiVersion: v2
name: demo
description: java demo
type: application
version: 0.1.0
appVersion: 1.16.0
[root@k8s-master helm]# cat demo/values.yaml 
replicaCount: 1

image:
  repository: nginx
  tag: 1.17
  pullPolicy: IfNotPresent

imagePullSecrets: []

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: true
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  host: example.ctnrs.com
  tls:
    secretName: example-ctnrs-com

resources:
   limits:
     cpu: 100m
     memory: 128Mi
   requests:
     cpu: 100m
     memory: 128Mi

nodeSelector: {}

tolerations: []

3. 修改NOTES.txt

[root@k8s-master demo]# cat templates/NOTES.txt 
访问地址:
{{- if .Values.ingress.enabled }}
  http{{ if .Values.ingress.tls }}s{{ end }}://{{ .Values.ingress.host }}
{{- end }}

{{- if contains "NodePort" .Values.service.type }}
  export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "demo.fullname" . }})
  export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT
{{- end }}

4. 在templates目录下创建部署镜像所需要的yaml文件

[root@k8s-master templates]# ls
deployment.yaml  _helpers.tpl  hpa.yaml  ingress.yaml  NOTES.txt  serviceaccount.yaml  service.yaml  tests
#删除不需要的
[root@k8s-master templates]# rm -rf serviceaccount.yaml hpa.yaml tests/ 
[root@k8s-master demo]# cat templates/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "name" . }}
  labels:
    {{- include "labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

5. 在 _helpers.tpl 重新定义三个模板

[root@k8s-master demo]# cat templates/_helpers.tpl 
{{/*
资源名字
*/}}
{{- define "name" -}}
{{ .Chart.Name }}-{{ .Release.Name }}
{{- end -}}

{{/*
资源标签
*/}}
{{- define "labels" -}}
app: {{ template "name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
release: {{ .Release.Name }}
{{- end -}}

{{/*
Pod标签
*/}}
{{- define "selectorLabels" -}}
app: {{ template "name" . }}
release: {{ .Release.Name }}
{{- end -}}

6. 修改service.yaml中的字段

[root@k8s-master demo]# cat templates/service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: {{ include "name" . }}
  labels: 
    {{- include "labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "selectorLabels" . | nindent 4 }}

8. 修改ingress.yaml中的字段

[root@k8s-master demo]# cat templates/ingress.yaml 
{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: {{ include "name" . }}
  labels:
    {{- include "labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
{{- if .Values.ingress.tls }}
  tls:
    - hosts:
      - {{ .Values.ingress.host }}
      secretName: {{ .Values.ingress.tls.secretName }}
{{- end }}
  rules:
    - host: {{ .Values.ingress.host | quote }}
      http:
        paths:
          - path: /
            backend:
              serviceName: {{ include "name" . }}
              servicePort: {{ .Values.service.port }}
{{- end }}

9. 测试生成

[root@k8s-master helm]# helm install web --dry-run demo/
NAME: web
LAST DEPLOYED: Wed Aug  5 15:00:54 2020
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: demo/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: demo-web
  labels:
    app: demo-web
    chart: demo-0.1.0
    release: web
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app: demo-web
    release: web
---
# Source: demo/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-web
  labels:
    app: demo-web
    chart: demo-0.1.0
    release: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-web
      release: web
  template:
    metadata:
      labels:
        app: demo-web
        release: web
    spec:
      containers:
        - name: demo
          image: "nginx:1.17"
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          resources:
            limits:
              cpu: 100m
              memory: 128Mi
            requests:
              cpu: 100m
              memory: 128Mi
---
# Source: demo/templates/ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: demo-web
  labels:
    app: demo-web
    chart: demo-0.1.0
    release: web
spec:
  tls:
    - hosts:
      - example.ctnrs.com
      secretName: example-ctnrs-com
  rules:
    - host: "example.ctnrs.com"
      http:
        paths:
          - path: /
            backend:
              serviceName: demo-web
              servicePort: 80

NOTES:
访问地址:
  https://example.ctnrs.com

10. 正式生成

[root@k8s-master helm]# helm install nginx  demo/
NAME: nginx 
LAST DEPLOYED: Wed Aug  5 14:37:38 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
访问地址:
  https://example.ctnrs.com


[root@k8s-master helm]# kubectl get pod
NAME                                     READY   STATUS    RESTARTS   AGE
demo-nginx-8575c89769-xhcds              1/1     Running   0          6s


[root@k8s-master helm]# kubectl get svc
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
demo-nginx    ClusterIP   10.10.133.169   <none>        80/TCP         19s


[root@k8s-master helm]# kubectl get ingress
NAME         CLASS    HOSTS               ADDRESS   PORTS     AGE
demo-nginx   <none>   example.ctnrs.com             80, 443   2m25s

添加hosts解析并访问

测试完成后将demo打包成helm的压缩包

[root@k8s-master helm]# helm package demo/
Successfully packaged chart and saved it to: /root/helm/demo-0.1.0.tgz

3.8 使用Harbor作为Chart仓库

https://github.com/goharbor/harbor/releases

#安装docker compose
yum install -y docker-compose
docker-compose --version
docker-compose version 1.18.0, build 8dd22a9

#这里使用1.9版本的harbor
tar xf harbor-offline-installer-v1.9.1.tgz -C /opt/
cd /opt/harbor/

# 修改配置文件
vim harbor.yml
hostname: 192.168.0.10
harbor_admin_password: 123456

#执行prepare脚本
./prepare

1、启用Harbor的Chart仓库服务

./install.sh --with-chartmuseum
[Step 0]: checking installation environment ...
Note: docker version: 19.03.11
Note: docker-compose version: 1.18.0
[Step 1]: loading Harbor images ...

启用后,默认创建的项目就带有helm charts功能了。

④使用Helm开发一个Chart模板_第1张图片

④使用Helm开发一个Chart模板_第2张图片

2、安装push插件

https://github.com/chartmuseum/helm-push

[root@k8s-master harbor]# helm plugin install https://github.com/chartmuseum/helm-push
Downloading and installing helm-push v0.8.1 ...
https://github.com/chartmuseum/helm-push/releases/download/v0.8.1/helm-push_0.8.1_linux_amd64.tar.gz
Installed plugin: push

3、添加repo

#harbor仓库的登陆账号密码
helm repo add  --username admin --password 123456 myharbor http://192.168.0.10/chartrepo/library

[root@k8s-master harbor]# helm repo list
NAME    	URL                                     
stable  	http://mirror.azure.cn/kubernetes/charts
myharbor	http://192.168.0.10/chartrepo/library 

4、推送与安装Chart

推送

helm push demo-0.1.0.tgz --username=admin --password=123456 http://192.168.0.10/chartrepo/library

④使用Helm开发一个Chart模板_第3张图片

④使用Helm开发一个Chart模板_第4张图片

安装

#要先更新repo仓库
[root@k8s-master helm]# helm repo update
#安装harbor上传的charts的包
[root@k8s-master helm]# helm install web --version 0.1.0 myharbor/demo
NAME: web
LAST DEPLOYED: Wed Aug  5 16:18:23 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
访问地址:
  https://example.ctnrs.com

[root@k8s-master helm]# kubectl get pod,svc,ingress
NAME                                         READY   STATUS    RESTARTS   AGE
pod/demo-web-745db5d6d9-dxpmd                1/1     Running   0          8s

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/demo-web      ClusterIP   10.10.90.74     <none>        80/TCP         8s

NAME                          CLASS    HOSTS               ADDRESS   PORTS     AGE
ingress.extensions/demo-web   <none>   example.ctnrs.com             80, 443   8s

[root@k8s-master helm]# curl -I 10.10.90.74 
HTTP/1.1 200 OK
Server: nginx/1.17.10
Date: Wed, 05 Aug 2020 08:21:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 14 Apr 2020 14:19:26 GMT
Connection: keep-alive
ETag: "5e95c66e-264"
Accept-Ranges: bytes

你可能感兴趣的:(k8s)