helm3


Helm客戶端


安裝

wget https://get.helm.sh/helm-v3.0.0-linux-amd64.tar.gz
tar zxvf helm-v3.0.0-linux-amd64.tar.gz 
mv linux-amd64/helm /usr/bin/

配置倉庫

helm repo add stable http://mirror.azure.cn/kubernetes/charts
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 
helm repo update

  • 查看配置的存儲庫
helm repo list
helm search repo stable

  • 刪除存儲庫
helm repo remove aliyun

案例1


helm create mychart
cd mychart/
rm -rf /root/mychart/templates/*

  • Chart.yaml (helm create mychart 自動生成的文件)
apiVersion: v2
name: nginx
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: 1.16.0

1. 生成模板deployment文件

kubectl create deployment mychart --image=nginx:1.16 -o yml -o yaml --dry-run > deployment.yaml

2. 修改deployment文件

  • 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
        resources: {}

  • values.yaml
replicas: 3
name: hello
image: nginx
imageTag: 1.17

3. 安裝

helm install hello /root/mychart/

4. 驗證

kubectl get pods -owide


curl -I 192.168.236.233


5. 修改nginx版本並升級

  • values.yaml
replicas: 3
name: hello
image: nginx
imageTag: 1.12

helm upgrade hello /root/mychart/

helm history hello


6. 回滾到上一個版本

helm rollback hello 1

kubectl get pods -owide


curl -I 192.168.236.240


7. 將nginx版本下降到1.8

helm upgrade --set imageTag=1.8 hello /root/mychart/

helm history hello



案例2


 helm create nginx
 cd nginx/

  • 目錄結構
├── charts
├── Chart.yaml   # 可以被templates下面的文件引用
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml  # 定義變量, 可以被templates下面的文件引用

手動deploy

rm -rf /root/nginx/templates/*
kubectl create deployment web --image=nginx --dry-run -o yaml>/root/nginx/templates/deployment.yaml
kubectl apply -f /root/nginx/templates/deployment.yaml
kubectl expose deployment web --port=80 --target-port=80 --dry-run -o yaml > service.yaml

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

  • service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
status:
  loadBalancer: {}

  • 查看pod
kubectl get pods


  • 查看service
kubectl get svc


helm 安裝

kubectl delete -f /root/nginx/templates/
kubectl delete -f /root/nginx/
helm install web /root/nginx/


  • 查看部署狀態
helm ls


  • 查看dashboard


helm定義變量安裝


  • helm全局變量
Release.Name release 名稱
Release.Name helm install時候 的 release 名字
Release.Namespace release 命名空間
Release.Service release 服務的名稱
Release.Revision release 修訂版本號,從1開始累加

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    chart: {{ .Chart.Name }}
    app: {{ .Release.Name }}
  name: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.label }}
  template:
    metadata:
      labels:
        app: {{ .Values.label }}
    spec:
      containers:
      - image: {{ .Values.image }}:{{ .Values.imageTag}}
        name: {{ .Release.Name }}
        resources: {}
status: {}

  • service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    chart: {{ .Chart.Name }}
    app: {{ .Release.Name }}
  name: {{ .Release.Name }}
spec:
  ports:
  - port: {{ .Values.port }}
    protocol: TCP
    targetPort: {{ .Values.targetPort }}
  selector:
    app: {{ .Values.label }}

  • values.yaml
replicas: 3
image: nginx
imageTag: 1.17
label: nginx_label
port: 80
targetPort: 80

  • Chart.yaml
apiVersion: v2
name: nginx
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: 1.16.0

  • 安裝
 helm install lyysb  /root/nginx/

  • 更新
helm upgrade lyysb --set replicas=1   /root/nginx/

--set 定義的變量的值的優先級最高


  • 刪除
helm delete lyysb

helm語法


條件判斷(if)


條件判斷中被對比的對象不支持數字, 單引號'', 必須使用雙引號""

案例1

  • deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    chart: {{ .Chart.Name }}
    app: {{ .Release.Name }} 
  name: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ quote .Values.label }}
  template:
    metadata:
      labels:
        app: {{ .Values.label }} 
        {{- if eq .Values.test "lyysb" }}
        develop: lyysb
        {{- else }}
        develop: lyysupersb
        {{- end }}
    spec:
      containers:
      - image: {{ .Values.image }}:{{ .Values.imageTag}}
        name: {{ .Release.Name }}
        resources: {}
status: {}

  • values.yaml
replicas: 3
image: nginx
imageTag: 1.17
label: nginx_label
port: 80
targetPort: 80
test: 'lyysb'

案例2

  • values.yaml
replicaCount: 3 
image:
  repository: nginx
  tag: 1.17
  pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
  create: true
  name:
podSecurityContext: {}
securityContext: {}
service:
  type: ClusterIP
  port: 80
ingress:
  enabled: true 
  annotations: {}
  hosts:
    - host: chart-example.local
      paths: []
  tls: []
resources: {}
nodeSelector: {}
tolerations: []
affinity: {}

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    chart: {{ .Chart.Name }}
    app: {{ .Release.Name }} 
  name: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app:  {{ .Release.Name }}
      chart:  {{ .Chart.Name }}
  template:
    metadata:
      labels:
        app:  {{ .Release.Name }}
        chart:  {{ .Chart.Name }}
    spec:
      containers:
      - image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        name: {{ .Release.Name }}
        resources: {}
status: {}

{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /testpath
        backend:
          serviceName: test
          servicePort: 80
{{- end }}

條件判斷(with)


  • values.yaml
nodeSelector: 
  team: a
  gpu: ok 

  • deployment.yaml

    {{- toYaml . | nindent 8 }} 可以寫成 如下形式

    {{ .team}}

    {{ .gpu }}

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    chart: {{ .Chart.Name }}
    app: {{ .Release.Name }} 
  name: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app:  {{ .Release.Name }}
      chart:  {{ .Chart.Name }}
  template:
    metadata:
      labels:
        app:  {{ .Release.Name }}
        chart:  {{ .Chart.Name }}
    spec:
      {{-  with .Values.nodeSelector }}
      nodeSelector:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        name: {{ .Release.Name }}
        resources: {}
status: {}

  • 驗證下
helm install web2 /root/helm/nginx/ --dry-run 


循環(range)


  • values.yaml
test: 
  - 'lzzsb'
  - 'lyysb'
  - 'lxxsb'

  • configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}
data: 
  test: 
    {{- range .Values.test }}
    {{ . }}
    {{- end }}

  • 驗證
helm install web2 /root/helm/nginx/ --dry-run


變量


with 無法引用全局變量的解決辦法

  • values.yaml
nodeSelector: 
  team: a
  gpu: ok 

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    chart: {{ .Chart.Name }}
    app: {{ .Release.Name }} 
  name: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app:  {{ .Release.Name }}
      chart:  {{ .Chart.Name }}
  template:
    metadata:
      labels:
        app:  {{ .Release.Name }}
        chart:  {{ .Chart.Name }}
    spec:
      {{-  with .Values.nodeSelector }}
      nodeSelector:
        name: {{ .Release.Name }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        name: {{ .Release.Name }}
        resources: {}
status: {}

  • 此時執行部署會出現報錯

    Error: template: nginx/templates/deployment.yaml:22:25: executing "nginx/templates/deployment.yaml" at <.Release.Name>: nil pointer evaluating interface {}.Name


  • 解決方法

    在全局變量前面加上$ 或者 在with 前面將全局變量的值賦值給一個變量, with內調用這個變量

spec:
      {{-  with .Values.nodeSelector }}
      nodeSelector:
        name: {{ $.Release.Name }}
      {{- toYaml . | nindent 8 }}
      {{- end }}

spec:
      {{- $releasename := $.Release.Name -}}
      {{-  with .Values.nodeSelector }}
      nodeSelector:
        name: {{ $releasename }}
      {{- toYaml . | nindent 8 }}
      {{- end }}

range解壓賦值

  • values.yaml
env:
  jvm.options:
    -Xms128M
    -Xmx128M
  path: /usr/local/elasticsearch-6.6.0/data
  log: /usr/local/elasticsearch-6.6.0/logs
  network: 127.0.0.1
  port: 9200

  • deployment.yaml
spec:
      {{- $releasename := $.Release.Name -}}
      {{-  with .Values.nodeSelector }}
      nodeSelector:
        name: {{ $releasename }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        name: {{ .Release.Name }}
        env:
        {{- range $k, $v := .Values.env }}
        - name: {{ $k }}
          value: {{ $v }}
        {{- end }}
        resources: {}

  • 驗證下
helm install web2 /root/helm/nginx/ --dry-run


命名模板


  • helper.tpl
{{- define "name" -}}
{{ .Chart.Name }}-{{ .Release.Name }}
{{- end -}}

{{- define "labels" -}}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app: {{ template "name" . }}
{{- end -}}

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    {{- include "labels" . | nindent 4 }}
  name: {{ template "name" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app:  {{ .Release.Name }}
      chart:  {{ .Chart.Name }}
  template:
    metadata:
      labels:
        app:  {{ .Release.Name }}
        chart:  {{ .Chart.Name }}
    spec:
      {{- with .Values.nodeSelector }}
      nodeSelector:
      {{- toYaml . |nindent 8 }}
      {{- end }}
      containers:
      - image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        name: {{ .Release.Name }}
        resources: {}
status: {}

  • 驗證
helm install web /root/helm/nginx/ --dry-run


helm搭建私有倉庫


參考: https://blog.51cto.com/14268033/2455006?source=dra


安裝minio服務端

wget  https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
mkdir  -p  /chart
./minio server /chart


  • 輸入 http://172.16.240.100:9000/minio/login ,通過瀏覽器訪問


  • 在啟動日志中獲取access key和secret key


安裝minio客戶端

wget  https://dl.min.io/client/mc/release/linux-amd64/mc
chmod +x mc

連接到服務端

命令在啟動日志中有

./mc config host add myminio http://192.168.189.217:9000 minioadmin minioadmin

創建bucket

./mc mb myminio/minio-helm-repo

設置bucket和objects匿名訪問

./mc policy set download myminio/minio-helm-repo
mkdir /root/helm/repo
helm repo index helm/repo/
./mc  cp  helm/repo/index.yaml  myminio/minio-helm-repo

helm創建與倉庫連接的index.yaml文件

mkdir /root/helm/repo
helm repo index helm/repo/

helm與minio倉庫進行連接


1. 將index.yaml文件推送到backet中去

./mc  cp  helm/repo/index.yaml  myminio/minio-helm-repo

2. helm連接私有倉庫

helm repo add myrepo http://172.16.240.100:9000/minio-helm-repo


3. 更新repo倉庫

helm  repo  update


4 .查看repo

helm repo list


5. 查看repo中的文件

 ./mc ls helm/repo/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM