1 helm概述
helm概述
Helm是Kubernetes的包管理器,Helm 让我们能够像 yum 管理 rpm 包那样安装、部署、升级和删除容器化应用。
Helm的核心术语:
Chart:一个helm程序包,是创建一个应用的信息集合,包含各种Kubernetes对象的配置模板、参数定义、依赖关系、文档说明等。可以将Chart比喻为yum中的软件安装包;
Repository:Charts仓库,用于集中存储和分发Charts;
Config:应用程序实例化安装运行时所需要的配置信息;
Release:特定的Chart部署于目标集群上的一个实例,代表这一个正在运行的应用。当chart被安装到Kubernetes集群,就会生成一个release,chart可以多次安装到同一个集群,每次安装都是一个release。
Helm的程序架构:
Helm主要由Helm客户端、Tiller服务器和Charts仓库组成:
helm:客户端,GO语言编写,实现管理本地的Chart仓库,可管理Chart,与Tiller服务进行交互,用于发送Chart,实例安装、查询、卸载等操作。
Tiller:服务端,通常运行在K8S集群之上。用于接收helm发来的Charts和Conifg,合并生成release,完成部署。
2 helm安装
[root@k8s-master ~]# wget https://get.helm.sh/helm-v2.14.1-linux-amd64.tar.gz
[root@k8s-master ~]# tar xf helm-v2.14.1-linux-amd64.tar.gz
[root@k8s-master ~]# mv linux-amd64/helm /usr/local/bin/
[root@k8s-master ~]# cat helm-service-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
[root@k8s-master ~]# kubectl apply -f helm-service-account.yaml
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
安装helm的bash命令补全脚本:
helm completion bash > .hermrc ;echo "source .helmrc" >> .bashrc
安装Tiller服务器
helm init --output yaml > tiller.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: helm
name: tiller
name: tiller-deploy
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: helm
name: tiller
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: helm
name: tiller
spec:
automountServiceAccountToken: true
containers:
- env:
- name: TILLER_NAMESPACE
value: kube-system
- name: TILLER_HISTORY_MAX
value: "0"
image: registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /liveness
port: 44135
initialDelaySeconds: 1
timeoutSeconds: 1
name: tiller
ports:
- containerPort: 44134
name: tiller
- containerPort: 44135
name: http
readinessProbe:
httpGet:
path: /readiness
port: 44135
initialDelaySeconds: 1
timeoutSeconds: 1
resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: helm
name: tiller
name: tiller-deploy
namespace: kube-system
spec:
ports:
- name: tiller
port: 44134
targetPort: tiller
selector:
app: helm
name: tiller
type: ClusterIP
status:
loadBalancer: {}
kubectl apply -f tiller.yaml
验证
[root@tech04 traefik]# kubectl get pods -n kube-system |grep tiller
tiller-deploy-5544466f89-t4vmz 1/1 Running 0 5m42s
[root@tech04 traefik]# helm version
Client: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
3 helm使用
helm常用命令:
- helm search: 搜索charts
- helm fetch: 下载charts到本地目录
- helm install: 安装charts
- helm list: 列出charts的所有版本
用法:
helm [command]
命令可用选项:
completion 为指定的shell生成自动补全脚本(bash或zsh)
create 创建一个新的charts
delete 删除指定版本的release
dependency 管理charts的依赖
fetch 下载charts并解压到本地目录
get 下载一个release
history release历史信息
home 显示helm的家目录
init 在客户端和服务端初始化helm
inspect 查看charts的详细信息
install 安装charts
lint 检测包的存在问题
list 列出release
package 将chart目录进行打包
plugin add(增加), list(列出), or remove(移除) Helm 插件
repo add(增加), list(列出), remove(移除), update(更新), and index(索引) chart仓库
reset 卸载tiller
rollback release版本回滚
search 关键字搜索chart
serve 启动一个本地的http server
status 查看release状态信息
template 本地模板
test release测试
upgrade release更新
verify 验证chart的签名和有效期
version 打印客户端和服务端的版本信息
4 helm仓库
Helm 安装时已经默认配置好了两个仓库:stable 和 local。stable 是官方仓库,local 是用户存放自己开发的chart的本地仓库。可以通过helm repo list进行查看。
[root@tech04 stable]# helm repo list
NAME URL
stable https://kubernetes-charts.storage.googleapis.com
local http://127.0.0.1:8879/charts
[root@tech04 stable]# helm repo remove stable
"stable" has been removed from your repositories
[root@tech04 stable]# helm repo list
NAME URL
local http://127.0.0.1:8879/charts
[root@tech04 stable]# helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"stable" has been added to your repositories
[root@tech04 stable]# helm repo list
NAME URL
local http://127.0.0.1:8879/charts
stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
[root@tech04 stable]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "stable" chart repository
Update Complete.
[root@tech04 stable]# helm search mysql
NAME CHART VERSION APP VERSION DESCRIPTION
stable/mysql 0.3.5 Fast, reliable, scalable, and easy to use open-source rel...
stable/percona 0.3.0 free, fully compatible, enhanced, open source drop-in rep...
stable/percona-xtradb-cluster 0.0.2 5.7.19 free, fully compatible, enhanced, open source drop-in rep...
stable/gcloud-sqlproxy 0.2.3 Google Cloud SQL Proxy
stable/mariadb 2.1.6 10.1.31 Fast, reliable, scalable, and easy to use open-source rel...
添加常用仓库
helm repo add elastic https://helm.elastic.co
helm repo add gitlab https://charts.gitlab.io
helm repo add harbor https://helm.goharbor.io
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com
helm repo add stable https://kubernetes-charts.storage.googleapis.com
使用国内仓库
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
5 安装chart
Helm 支持四种安装方法:
安装仓库中的 chart,例如:helm install stable/nginx
通过 tar 包安装,例如:helm install ./nginx-1.2.3.tgz
通过 chart 本地目录安装,例如:helm install ./nginx
通过 URL 安装,例如:helm install https://example.com/charts/nginx-1.2.3.tgz
自定义chart
[root@tech04 stable]# helm create mychart
[root@tech04 stable]# tree mychart
mychart
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml