基本概念
helm 类似于Linux系统下的包管理器,如yum/apt等,可以方便快捷的将之前打包好的yaml文件快速部署进kubernetes内,方便管理维护。
yum install -y net-tools
Helm 的首要目标一直是让“从零到 Kubernetes”变得轻松。无论是运维、开发人员、经验丰富的 DevOps工程师,还是刚刚入门的学生,Helm 的目标是让大家在两分钟内就可以在 Kubernetes 上安装应用程序。
Helm 可以解决的问题:
运维人员写好资源文件模板
交给开发人员填写参数即可
helm:一个命令行下客户端工具,主要用于kubernetes应用chart的创建/打包/发布已经创建和管理和远程Chart仓库。
Chart: helm 程序包,一系列用于描述 k8s 资源相关文件的集合,比方说我们部署 nginx,需要
deployment 的yaml,需要 service 的yaml,这两个清单文件就是一个 helm 程序包,在 k8s 中把这些
yaml 清单文件叫做chart 图表。
vlues.yaml 文件为模板中的文件赋值,可以实现我们自定义安装
如果是 chart 开发者需要自定义模板,如果是 chart 使用者只需要修改 values.yaml 即可
Release:在kubernetes中集群中运行的一个Chart实例,在同一个集群上,一个Chart可以安装多次,每次安装均会生成一个新的release。
Repository:用于发布和存储Chart的仓库
总结:
helm 把kubernetes 资源打包到一个 chart 中,制作并完成各个 chart 和chart 本身依赖关系并利用chart 仓库实现对外分发,而 helm 还可通过 values.yaml 文件完成可配置的发布,如果 chart 版本更新了,helm 自动支持滚更更新机制,还可以一键回滚,但是不是适合在生产环境使用,除非具有定义自制chart 的能力。
官方地址
官网:https://v3.helm.sh/zh/docs/
https://helm.sh/
helm 官方的 chart 站点:https://hub.kubeapps.com/
https://github.com/helm/helm/releases
找 Linux amd64 这个checksum 的,下载之后传到机器即可,目前 Helm 最新稳定版本是 3.8.2,这个压缩包我已经下载了,在课件,大家可以直接从课件传到自己机器,解压即可。
Helm v3 版本变化
2019 年 11 月 13 日,Helm 团队发布 Helmv3 的第一个稳定版本。
该版本主要变化如下: 架构变化:
1、Helm 服务端 Tiller 被删除
2、Release 名称可以在不同命名空间重用
3、支持将 Chart 推送至 Docker 镜像仓库中
4、使用 JSONSchema 验证chartvalues
安装 Helm v3
K8s 版本支持的各个 helm 版本对照表:
https://helm.sh/zh/docs/topics/version_skew/
tar zxvf helm-v3.8.2-linux-amd64.tar.gz
mv linux-amd64/helm /usr/bin/
helm version
配置仓库地址
阿里云仓库(https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts)
官方仓库(https://hub.kubeapps.com/charts/incubator)官方 chart 仓库,国内可能无法访问。
微软仓库(http://mirror.azure.cn/kubernetes/charts/)这个仓库推荐,基本上官网有的 chart这里都有,国内可能无法访问。
#添加阿里云的 chart 仓库
[root@s201 ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
#添加bitnami 的chart 仓库
[root@s201~]# helm repo add bitnami https://charts.bitnami.com/bitnami
#更新 chart 仓库
[root@s201~]# helm repo update
#查看配置的 chart 仓库有哪些
[root@s201~]# helm repo list
#删除 chart 仓库地址
[root@s201~]# helm repo remove aliyun
“aliyun” has been removed from your repositories
#重新添加阿里云的 chart 仓库
[root@s201~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
#更新 chart 仓库
[root@s201~]# helm repo update
# 从指定 chart 仓库地址搜索 chart
[root@s201~]# helm search repo aliyun
搜索和下载 Chart
helm search repo aliyun |grep memcached
helm show chart aliyun/memcached
helm pull aliyun/memcached
tar zxvf memcached-2.0.1.tgz
cd memcached
ls
Chart.yaml: chart 的基本信息,包括版本名字之类
templates: 存放 k8s 的部署资源模板,通过渲染变量得到部署文件
values.yaml:存放全局变量,templates 下的文件可以调用
cd templates/
ls
_helpers.tpl 存放能够复用的模板
NOTES.txt 为用户提供一个关于chart部署后使用说明的文件
helm 部署 memcached 服务
crictl pull memcached:1.4.36-alpine
cd memcached
cat templates/statefulset.yaml
apiVersion 后面的value 值变成 apps/v1
spec 下添加selector 字段
selector:
matchLabels:
app: {{ template “memcached.fullname” . }}
chart: “{{ .Chart.Name }}-{{ .Chart.Version }}”
release: “{{ .Release.Name }}”
heritage: “{{ .Release.Service }}”
#删除 affinity 亲和性配置
helm install memcached ./
kubectl get pods
kubectl get svc
export POD_NAME=$(kubectl get pods --namespace default -l "app=memcached-memcached" -o jsonpath="{.items[0].metadata.name}")
kubectl port-forward $POD_NAME 11211
yum install -y nc
echo -e 'set mykey 0 60 5\r\nhello\r' | nc localhost 11211
release 相关操作
helm list
helm delete memcached
自定义chart
helm create mychart
rm -rf *
生成配置文件
kubectl create deployment web1 --image=nginx --dry-run -o yaml > deployment.yaml
kubectl create deployment web1
kubectl expose deployment web1 --port=80 --target-port=80 --type=NodePort --dry-run -o yaml > servcie.yaml
kubectl delete deployment web1
helm install web1 mychart/
helm list
kubectl get pod
helm upgrade web1 mychart/
变量传递
replicas: 1
image: nginx
tag: latest
label: nginx
port: 80
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web1
name: {{ .Release.Name}}-deploy
spec:
replicas: 1
selector:
matchLabels:
app: {{ .Values.label}}
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: {{ .Values.label}}
spec:
containers:
- image: {{ .Values.image}}
name: nginx
resources: {}
status: {}
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: {{ .Values.label}}
name: {{ .Release.Name}}
spec:
ports:
- port: {{ .Values.port}}
protocol: TCP
targetPort: {{ .Values.label}}
selector:
app: {{ .Values.label}}
type: NodePort
status:
loadBalancer: {}
helm install —dry-run web2 mychart/
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: nginx
name: web2
spec:
ports:
- port: 80
protocol: TCP
targetPort: nginx
selector:
app: nginx
type: NodePort
status:
loadBalancer: {}
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web1
name: web2-deploy
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}