使用Helm3管理复杂应用的部署
认识Helm
-
为什么有helm?
-
Helm是什么?
kubernetes的包管理器,“可以将Helm看作Linux系统下的apt-get/yum”。
-
对于应用发布者而言,可以通过Helm打包应用,管理应用依赖关系,管理应用版本并发布应用到软件仓库。
-
对于使用者而言,使用Helm后不用需要了解Kubernetes的Yaml语法并编写应用部署文件,可以通过Helm下载并在kubernetes上安装需要的应用。
-
-
Helm的版本
-
helm2
C/S架构,helm通过Tiller与k8s交互
-
helm3
-
从安全性和易用性方面考虑,移除了Tiller服务端,helm3直接使用kubeconfig文件鉴权访问APIServer服务器
-
由二路合并升级成为三路合并补丁策略( 旧的配置,线上状态,新的配置 )
helm install very_important_app ./very_important_app
这个应用的副本数量设置为 3 。现在,如果有人不小心执行了
kubectl edit
或:kubectl scale -replicas=0 deployment/very_important_app
然后,团队中的某个人发现 very_important_app 莫名其妙宕机了,尝试执行命令:
helm rollback very_important_app
在 Helm 2 中,这个操作将比较旧的配置与新的配置,然后生成一个更新补丁。由于,误操作的人仅修改了应用的线上状态(旧的配置并未更新)。Helm 在回滚时,什么事情也不会做。因为旧的配置与新的配置没有差别(都是 3 个副本)。然后,Helm 不执行回滚,副本数继续保持为 0
-
移除了helm server本地repo仓库
-
-
-
Helm的重要概念
helm 是包管理工具,包就是指 chart,helm 能够:
- 从零创建chart
- 与仓库交互,拉取、保存、更新 chart
- 在kubernetes集群中安装、卸载 release
- 更新、回滚、测试 release
安装与快速入门实践
下载最新的稳定版本:https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz
更多版本可以参考: https://github.com/helm/helm/releases
# k8s-master节点
$ wget https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz
$ tar -zxf helm-v3.2.4-linux-amd64.tar.gz
$ cp linux-amd64/helm /usr/local/bin/
# 验证安装
$ helm version
version.BuildInfo{Version:"v3.2.4", GitCommit:"0ad800ef43d3b826f31a5ad8dfbb4fe05d143688", GitTreeState:"clean", GoVersion:"go1.13.12"}
$ helm env
# 添加仓库
$ helm repo add stable https://charts.bitnami.com/bitnami
# 同步最新charts信息到本地
$ helm repo update
快速入门实践:
示例一:使用helm安装wordpress应用
# helm 搜索chart包
$ helm search repo wordpress
$ kubectl create namespace wordpress
# 从仓库安装
$ helm -n wordpress install wordpress stable/wordpress --set mariadb.primary.persistence.enabled=false --set service.type=ClusterIP --set ingress.enabled=true --set persistence.enabled=false --set ingress.hostname=wordpress.luffy.com
$ helm -n wordpress ls
$ kubectl -n wordpress get all
# 从chart仓库中把chart包下载到本地
$ helm pull stable/wordpress
$ tree MysqL
示例二:新建Nginx的chart并安装
$ helm create Nginx
# 从本地安装
$ helm install Nginx ./Nginx
# 安装到别的命名空间luffy
$ helm -n luffy install Nginx ./Nginx --set replicaCount=2 --set image.tag=alpine
# 查看
$ helm ls
$ helm -n luffy ls
#
$ kubectl -n luffy get all
Chart的模板语法及开发
nginx的chart实现分析
格式:
$ tree Nginx/
Nginx/
├── charts # 存放子chart
├── Chart.yaml # 该chart的全局定义信息
├── templates # chart运行所需的资源清单模板,用于和values做渲染
│ ├── deployment.yaml
│ ├── _helpers.tpl # 定义全局的命名模板,方便在其他模板中引入使用
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── NOTES.txt # helm安装完成后终端的提示信息
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml # 模板使用的默认值信息
很明显,资源清单都在templates中,数据来源于values.yaml,安装的过程就是将模板与数据融合成k8s可识别的资源清单,然后部署到k8s环境中。
$ helm install debug-Nginx ./ --dry-run --set replicaCount=2 --debug
分析模板文件的实现:
-
引用命名模板并传递作用域
{{ include "Nginx.fullname" . }}
include从_helpers.tpl中引用命名模板,并传递顶级作用域.
-
内置对象
.Values .Release.Name .Chart
-
模板定义
{{- define "Nginx.fullname" -}} {{- if .Values.fullnameOverride }} {{- .Values.fullnameOverride | trunc 63 | trimsuffix "-" }} {{- else }} {{- $name := default .Chart.Name .Values.nameOverride }} {{- if contains $name .Release.Name }} {{- .Release.Name | trunc 63 | trimsuffix "-" }} {{- else }} {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimsuffix "-" }} {{- end }} {{- end }} {{- end }}
-
{{- 去掉左边的空格及换行,-}} 去掉右侧的空格及换行
-
示例
apiVersion: v1 kind: ConfigMap Metadata: name: {{ .Release.Name }}-configmap data: myvalue: "Hello World" drink: {{ .Values.favorite.drink | default "tea" | quote }} food: {{ .Values.favorite.food | upper | quote }} {{ if eq .Values.favorite.drink "coffee" }} mug: true {{ end }}
渲染完后是:
apiVersion: v1 kind: ConfigMap Metadata: name: mychart-1575971172-configmap data: myvalue: "Hello World" drink: "coffee" food: "PIZZA" mug: true
-
-
管道及方法
-
{{- if .Values.fullnameOverride }} ... {{- else }} ... {{- end }}
通常用来根据values.yaml中定义的开关来控制模板中的显示:
{{- if not .Values.autoscaling.enabled }} replicas: {{ .Values.replicaCount }} {{- end }}
-
定义变量,模板中可以通过变量名字去引用
{{- $name := default .Chart.Name .Values.nameOverride }}
-
遍历values的数据
{{- with .Values.nodeselector }} nodeselector: {{- toYaml . | nindent 8 }} {{- end }}
toYaml处理值中的转义及特殊字符, "kubernetes.io/role"=master , name="value1,value2" 类似的情况
-
default设置默认值
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
Helm template
hpa.yaml
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
Metadata:
name: {{ include "Nginx.fullname" . }}
labels:
{{- include "Nginx.labels" . | nindent 4 }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "Nginx.fullname" . }}
minReplicas: {{ .Values.autoscaling.minReplicas }}
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
metrics:
{{- if .Values.autoscaling.targetcpuutilizationPercentage }}
- type: Resource
resource:
name: cpu
targetAverageutilization: {{ .Values.autoscaling.targetcpuutilizationPercentage }}
{{- end }}
{{- if .Values.autoscaling.targetMemoryutilizationPercentage }}
- type: Resource
resource:
name: memory
targetAverageutilization: {{ .Values.autoscaling.targetMemoryutilizationPercentage }}
{{- end }}
{{- end }}
创建Release的时候赋值
- set的方式
# 改变副本数和resource值
$ helm install Nginx-2 ./Nginx --set replicaCount=2 --set resources.limits.cpu=200m --set resources.limits.memory=256Mi
-
value文件的方式
$ cat Nginx-values.yaml resources: limits: cpu: 100m memory: 128Mi requests: cpu: 100m memory: 128Mi autoscaling: enabled: true minReplicas: 1 maxReplicas: 3 targetcpuutilizationPercentage: 80 ingress: enabled: true hosts: - host: chart-example.luffy.com paths: - / $ helm install -f Nginx-values.yaml Nginx-3 ./Nginx
使用helm template查看渲染模板
$ helm -n luffy template Nginx ./Nginx --set replicaCount=2 --set image.tag=alpine --set autoscaling.enabled=true
更多语法参考:
https://helm.sh/docs/topics/charts/
实战:使用Helm部署Harbor镜像及chart仓库
harbor部署
架构 https://github.com/goharbor/harbor/wiki/Architecture-Overview-of-Harbor
- Core,核心组件
- Job Service,执行异步任务,如同步镜像信息
- Log Collector,统一日志收集器,收集各模块日志
- GC Controller
- Chart Museum,chart仓库服务,第三方
- Docker Registry,镜像仓库服务
- kv-storage,redis缓存服务,job service使用,存储job Metadata
- local/remote storage,存储服务,比较镜像存储
- sql Database,postgresl,存储用户、项目等元数据
通常用作企业级镜像仓库服务,实际功能强大很多。
组件众多,因此使用helm部署
# 添加harbor chart仓库
$ helm repo add harbor https://helm.goharbor.io
# 搜索harbor的chart
$ helm search repo harbor
# 不知道如何部署,因此拉到本地
$ helm pull harbor/harbor
创建pvc
$ kubectl create namespace harbor
$ cat harbor-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
Metadata:
name: harbor-data
namespace: harbor
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi
修改harbor配置:
- 开启ingress访问
- externalURL,web访问入口,和ingress的域名相同
- 持久化,使用PVC对接的cephfs
- harborAdminPassword: "Harbor12345",管理员默认账户 admin/Harbor12345
- 开启chartmuseum
- clair和trivy漏洞扫描组件,暂不启用
helm创建:
# 使用本地chart安装
$ helm install harbor ./harbor -n harbor
数据权限问题:
解决:
$ mount -t ceph 172.21.51.55:6789:/ /mnt/cephfs -o name=admin,secret=AQBPTstgc078NBAA78D1/KABglIZHKh7+G2X8w==
$ chown -R 999:999 database
$ chown -R 999:999 redis
$ chown -R 10000:10000 chartmuseum
$ chown -R 10000:10000 registry
$ chown -R 10000:10000 jobservice
推送镜像到Harbor仓库
配置hosts及docker非安全仓库:
$ cat /etc/hosts
...
172.21.51.143 k8s-master harbor.luffy.com
...
$ cat /etc/docker/daemon.json
{
"insecure-registries": [
"172.21.51.143:5000",
"harbor.luffy.com"
],
"registry-mirrors" : [
"https://8xpk5wnt.mirror.aliyuncs.com"
]
}
#
$ systemctl restart docker
# 使用账户密码登录admin/Harbor12345
$ docker login harbor.luffy.com
$ docker tag Nginx:alpine harbor.luffy.com/library/Nginx:alpine
$ docker push harbor.luffy.com/library/Nginx:alpine
推送chart到Harbor仓库
helm3默认没有安装helm push插件,需要手动安装。插件地址 https://github.com/chartmuseum/helm-push
安装插件:
$ helm plugin install https://github.com/chartmuseum/helm-push
离线安装:
$ mkdir helm-push
$ wget https://github.com/chartmuseum/helm-push/releases/download/v0.8.1/helm-push_0.8.1_linux_amd64.tar.gz
$ tar zxf helm-push_0.8.1_linux_amd64.tar.gz -C helm-push
$ helm plugin install ./helm-push
添加repo
$ helm repo add myharbor https://harbor.luffy.com/chartrepo/luffy
# x509错误
# 添加证书信任,根证书为配置给ingress使用的证书
$ kubectl get secret harbor-ingress -n harbor -o jsonpath="{.data.ca\.crt}" | base64 -d >harbor.ca.crt
$ cp harbor.ca.crt /etc/pki/ca-trust/source/anchors
$ update-ca-trust enable; update-ca-trust extract
# 再次添加
$ helm repo add luffy https://harbor.luffy.com/chartrepo/luffy --ca-file=harbor.ca.crt --username admin --password Harbor12345
$ helm repo ls
推送chart到仓库:
$ helm push harbor luffy --ca-file=harbor.ca.crt -u admin -p Harbor12345
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。