helm引入
之前方式部署应用基本过程
- 使用helm可以解决哪些问题?
- 使用helm可以把这些yaml作为一个整体管理
- 实现yaml高效复用
- 使用helm应用级别的版本管理
Helm介绍
Helm是一个Kubernetes的包管理工具,就像Linux中的yum/apt等,可以很方便的将之前打包好的yaml文件部署到kubernetes上
Helm三个重要概念
- helm:是一个命令行客户端工具
- Chart:把yaml打包,yaml集合
- Release:基于chart部署实体,应用级别的版本管理
V3版本架构
1. Helm安装
官方文档https://helm.sh/zh/docs/intro/install/
每个Helm 版本都提供了各种操作系统的二进制版本,这些版本可以手动下载和安装。
- 下载 curl -LO https://get.helm.sh/helm-v3.0.0-linux-amd64.tar.gz
- 解压(
tar -zxvf helm-v3.0.0-linux-amd64.tar.gz
) - 在解压目中找到
helm
程序,移动到需要的目录中(mv linux-amd64/helm /usr/local/bin/helm
) - 添加仓库(
helm repo add stable http://mirror.azure.cn/kubernetes/charts
) - 仓库查看(
helm repo list
)
helm 常用命令
命令 | 描述 |
---|---|
dependency | 管理 chart 依赖 |
get | 下载一个 release。可用子命令:all、hooks、manifest、notes、values |
history | 获取 release 历史 |
install | 安装一个 chart |
list | 列出 release |
package | 将 chart 目录打包到 chart 存档文件中 |
pull | 从远程仓库中下载 chart 并解压到本地 # helm pull stable/mysql – untar |
repo | 添加,列出,移除,更新和索引 chart 仓库。可用子命令:add、index、 list、remove、update |
rollback | 从之前版本回滚 |
search | 根据关键字搜索 chart。可用子命令:hub、repo |
show | 查看 chart 详细信息。可用子命令:all、chart、readme、values |
status | 显示已命名版本的状态 |
template | 本地呈现模板 |
uninstall | 卸载一个 release |
upgrade | 更新一个 release |
version | 查看 helm 客户端版本 |
一键部署weavescope应用
helm search repo weave
helm install ui stable/weave-scope
kubectl get all
修改weave的service资源 讲ClusterIP改为NodePort
2. 如何自己创建Chart
2.1 使用命令创建chart
helm create [名称]
[root@k8s-master01 mychart]# ll
总用量 8
drwxr-xr-x 2 root root 6 5月 14 19:02 charts
-rw-r--r-- 1 root root 905 5月 14 19:02 Chart.yaml # chart属性配置信息
drwxr-xr-x 3 root root 146 5月 14 19:02 templates # 编写yaml文件放到这个目录中
-rw-r--r-- 1 root root 1490 5月 14 19:02 values.yaml # yaml可以使用全局变量
2.2 在templates目录中创建yaml
自行创建
[root@k8s-master01 templates]# ll
总用量 8
-rw-r--r-- 1 root root 389 5月 14 21:52 web01-deployment.yaml
-rw-r--r-- 1 root root 240 5月 14 21:55 web01-svc.yaml
2.3 安装mychart
cd ~
helm install [名称] [目录]
helm install web02 mychart/
2.4 应用升级
helm upgrade [名称] [目录]
helm upgrade web1 mychart/
3. 实现yaml高效复用
通过传递参数,动态渲染模板yaml内容动态传入参数生成
3.1 在values.yaml定义变量值
-
yaml 文件大体有几个地方不同
* image * tag * label * port * name
replicas: 1 image: nginx tag: 1.16 label: nginx port: 80
3.2 在templates的文件使用values.yaml定义变量
通过表达式形式使用全局变量
{{ .Values.变量名称}}
{{ .Release.Name}}
cat > web-deployment.yaml <<-EOF
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: {{ .Values.label}}
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}}:{{ .Values.tag}}
name: {{ .Release.Name}}-deploy
resources: {}
status: {}
EOF
cat > web-svc.yaml <<-EOF
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: {{ .Values.label}}
name: {{ .Release.Name}}-svc
spec:
ports:
- port: {{ .Values.port}}
protocol: TCP
targetPort: {{ .Values.port}}
selector:
app: {{ .Values.label}}
type: NodePort
status:
loadBalancer: {}
EOF
安装并执行
helm install web03 mychart/
helm install web04 mychart/