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/

posted @ 2020-03-25 22:43  cjw1219  阅读(1108)  评论(0编辑  收藏  举报