helm简介
Helm是一个kubernetes的包管理工具,好比CentOs下的yum或者Ubuntu下的apt-get,这两者都是Linux系统下的包管理工具。应用开发者可以管理应用包之间的依赖关系。用户可以进行查找、安装、升级、卸载等操作。
可以将Helm看作是yum/apt-get,可以将之间打包好的yaml文件部署到kubernetes上。
Helm特点:
- Helm:一个命令行客户端工具,主要用于kubernetes应用chart的创建、打包、发布、管理等操作。
- chart:应用描述,描述K8S资源相关文件的集合。
- Release:基于chart的部署实体,一个chart被Helm执行后会生成对应的一个Release;会在K8S中创建出真实运行的资源对象。
Helm版本区别
- v2版本包含Tiller,Tiller主要是对接K8S和Helm客户端。所有操作都要经过Tiller,需要授权Tiller访问K8S集群。
- v3版本删除Tiller,Helm可以直接访问K8S集群,由kubeconfig提供凭据。
Helm安装
[root@k8s-master ~]# wget https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz
[root@k8s-master helm]# tar -zxf helm-v3.2.4-linux-amd64.tar.gz
[root@k8s-master helm]# mv linux-amd64/helm /usr/bin/
Helm 使用
Helm 部署生命周期
- helm create 创建Chart
- helm install 安装部署
- helm upgrade 更新
- helm rollback 版本回滚
- helm uninstall 卸载
安装部署
# 创建nginx.chart的目录,然后生成如下目录结构
[root@k8s-master nginx.chart]# tree .
.
├── Chart.yaml
├── templates
│ ├── _helpers.tpl
│ ├── nginx.yaml
│ ├── NOTES.txt
│ └── service-nginx.yaml
└── values.yaml
1 directory, 6 files
[root@k8s-master nginx.chart]# cat Chart.yaml
apiVersion: v2
name: nginx.chart
description: A Helm chart for Kubernetes
# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0 # chart的版本号
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: 1.17.0 # chart里应用默认的版本
[root@k8s-master nginx.chart]# cat values.yaml #暂时为空,这是定义变量,在后面的文件进行引用
[root@k8s-master nginx.chart]# ls templates/ # 存放部署应用的yaml文件
_helpers.tpl nginx.yaml NOTES.txt service-nginx.yaml
[root@k8s-master templates]# kubectl create deployment nginx-web --image=nginx --dry-run=client -o yaml > nginx.yaml #生成deployment yaml文件,配置如下
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-web
name: nginx-web
spec:
replicas: 1
selector:
matchLabels:
app: nginx-web
strategy: {}
template:
metadata:
labels:
app: nginx-web
spec:
containers:
- image: nginx
name: nginx
[root@k8s-master templates]# kubectl create service nodeport nginx-web --tcp=80:80 --dry-run=client -o yaml > service-nginx.yaml # 生成service yaml文件,配置如下
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-web
name: nginx-web
spec:
ports:
- name: 80-80
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-web
type: NodePort
[root@k8s-master nginx.chart]# helm install nginx . #执行chart,安装nginx
[root@k8s-master nginx.chart]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
nginx default 1 2022-05-30 16:44:18.210480475 +0800 CST deployed nginx.chart-0.1.0 1.17.0
[root@k8s-master nginx.chart]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-web-5855c9859f-6r4k2 1/1 Running 0 19m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20d
service/nginx-web NodePort 10.108.147.176 <none> 80:31641/TCP 19m
[root@k8s-master nginx.chart]# kubectl exec -it nginx-web-5855c9859f-6r4k2 -- bash
root@nginx-web-5855c9859f-6r4k2:/# echo "Hello" > /usr/share/nginx/html/index.html
helm 实现高效复用
[root@k8s-master nginx.chart]# vim templates/nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
spec:
replicas: {{ .Values.replicas }}
selector:
matchLabels:
project: {{ .Values.labels.project }}
app : {{ .Values.labels.app }}
strategy: {}
template:
metadata:
labels:
project: {{ .Values.labels.project }}
app : {{ .Values.labels.app }}
spec:
containers:
- image: {{ .Values.image.warehouse }}:{{ .Values.image.tag }}
name: nginx
[root@k8s-master nginx.chart]# vim templates/service-nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.name }}
spec:
ports:
- name: 80-80
port: {{ .Values.service.port }}
protocol: TCP
targetPort: {{ .Values.service.targetPort }}
selector:
project: {{ .Values.labels.project }}
app: {{ .Values.labels.app }}
type: NodePort
[root@k8s-master nginx.chart]# vim values.yaml
# 定义变量
name: nginx
replicas: 1
labels:
project: kangjia
app: nginx
image:
warehouse: nginx
tag: "latest"
service:
port: 80
targetPort: 80
type: ClusterIP
配置helm复用的有两种方式
利用set进行传参
[root@k8s-master nginx.chart]# helm install tomcat . --description="tomcat:9.0.63" --set name=tomcat --set replicas=1 --set labels.app=tomcat --set image.warehouse=tomcat --set image.tag=9.0.63 --set service.targetPort=8080 --set service.type=NodePort
# 用set进行传参,实现helm复用,description是添加自定义信息,方便后期版本维护或回滚
[root@k8s-master nginx.chart]# helm install tomcat . --description="tomcat:9.0.63" --set name=tomcat --set replicas=1 --set labels.app=tomcat --set image.warehouse=tomcat --set image.tag=9.0.63 --set service.targetPort=8080 --set service.type=NodePort --dry-run
#查看渲染情况
NAME: tomcat
LAST DEPLOYED: Mon May 30 18:14:25 2022
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: nginx.chart/templates/service-nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: tomcat
spec:
ports:
- name: 80-80
port: 80
protocol: TCP
targetPort: 8080
selector:
project: kangjia
app: tomcat
type: NodePort
---
# Source: nginx.chart/templates/nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
spec:
replicas: 2
selector:
matchLabels:
project: kangjia
app : tomcat
strategy: {}
template:
metadata:
labels:
project: kangjia
app : tomcat
spec:
containers:
- image: tomcat:9.0.63
name: tomcat
NOTES:
欢迎使用nginx应用部署模板
[root@k8s-master nginx.chart]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/tomcat-5fb8c9b7fc-b57nv 1/1 Running 0 5m58s
pod/tomcat-5fb8c9b7fc-dksnv 1/1 Running 0 5m58s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20d
service/tomcat NodePort 10.103.195.168 <none> 80:31257/TCP 5m59s
指定yaml文件去传参
[root@k8s-master nginx.chart]# cp values.yaml afferent.yaml
[root@k8s-master nginx.chart]# vim afferent.yaml
# 定义变量
name: redis
replicas: 3
labels:
project: rongyao
app: redis
image:
warehouse: redis
tag: "latest"
service:
port: 6666
targetPort: 6379
type: NodePort
[root@k8s-master nginx.chart]# helm install redis . -f afferent.yaml
NAME: redis
LAST DEPLOYED: Mon May 30 18:25:09 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
欢迎使用nginx应用部署模板
[root@k8s-master nginx.chart]# helm install redis . -f afferent.yaml --dry-run
NAME: redis
LAST DEPLOYED: Mon May 30 18:25:17 2022
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: nginx.chart/templates/service-nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
ports:
- name: 80-80
port: 6666
protocol: TCP
targetPort: 6379
selector:
project: rongyao
app: redis
type: NodePort
---
# Source: nginx.chart/templates/nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
replicas: 3
selector:
matchLabels:
project: rongyao
app : redis
strategy: {}
template:
metadata:
labels:
project: rongyao
app : redis
spec:
containers:
- image: redis:latest
name: redis
NOTES:
欢迎使用nginx应用部署模板
[root@k8s-master nginx.chart]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/redis-5d7dbddbfd-fqk8c 1/1 Running 0 2m40s
pod/redis-5d7dbddbfd-wfwzs 1/1 Running 0 2m40s
pod/redis-5d7dbddbfd-wwjww 1/1 Running 0 2m40s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20d
service/redis NodePort 10.104.73.97 <none> 6666:30353/TCP 2m40s