④使用Helm開發一個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功能了。

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

安裝

#要先更新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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章