网站首页 > 技术文章 正文
helm 是 k8s 生态中的软件包管理工具,其作用可类比于 Ubuntu 系统中的 apt,或者是 CentOS 中的 yum,又或者是 python 中的 pip。都是为了应用或模块的统一打包、分发、依赖管理等工作。
同大多数包管理器一样,helm 也是爱偷懒的程序猿们为了解放双手解决低端劳动力而产生的。在 k8s 中,要部署一个应用,需要很多 k8s 的配置文件(yaml)协同工作,例如Deployment/Service/ReplicationController 等等。
编写这些配置文件是一项非常枯燥且乏味的事情。往往部署一个新服务,需要`Ctrl+C`/`Ctrl+V`一个已有的服务,然后修修改改,一不小心写错一个配置说不定还能引起雪崩效应。同时这些资源的配置非常松散,没有一个统一管理的有效途径,如果直接通过**kubectl**来管理集群,将会是一个灾难。
helm帮我们解决了这些问题
- 统一配置管理
- 统一分发配置模板
- 统一管理服务依赖(整合为软件包)
Helm 架构
helm 通过客户端工具从仓库拉取软件包,再通过kubeconfig配置文件链接到k8s集群,与k8s的ApiServer进行交互,以实现部署。
在helm3.X版本之前,还有一个helm Tiller作为helm部署在k8s集群中的服务器,负责接收来自客户端的请求再投递给k8s集群。从helm 3.X以后,helm客户端直连k8s ApiServer,进一步简化了架构。
Helm 关键词
Helm client,是一个命令行客户端工具,主要用于 k8s 应用程序 chart 的创建、打包、发布以及创建和管理本地和远程的 chart 仓库。
Chart,即helm的软件包,采用tar格式打包。chart包含了一组定义k8s资源的相关配置文件。
Release,使用helm install命令生成在k8s集群中的部署叫做**Release**。
Repository,helm的软件包仓库,就如同docker的hub。
Helm 客户端使用
如果你在本地使用了k3s,那么其自带的helm在使用时可能会遇到这样的问题:
$ helm list
Error: Kubernetes cluster unreachable: Get "http://localhost:8080/version?timeout=32s": dial tcp [::1]:8080: connect: connection refused
或者是
$ helm list
Error: INSTALLATION FAILED: Kubernetes cluster unreachable: Get "https://127.0.0.1:6443/version?timeout=32s": x509: certificate signed by unknown authority
第一个问题是因为上面说到的3.X版本的helm不再需要Tiller服务器了,直接访问了ApiServer,而第二种报错则是因为kubeconfig配置文件中的证书信息问题,但这两个问题都可以用同意的方法解决。
手动配置 KUBECONFIG 环境变量
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
其目的是让helm使用正确的kubeconfig配置文件,之后在此执行helm命令就恢复正常了。
$ helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
使用 Helm 部署服务
helm创建一个软件包非常容易,创建之后再使用install命令即可部署一个服务,创建好的软件包可保留下次创建时直接使用,或者直接通过Repository部署他人编写好的软件包。
创建软件包
$ helm create nginx-chart
我们来看一下生成的软件包目录下的结构
$ ls nginx-chart
charts Chart.yaml templates values.yaml
Chart.yaml
$ cat ./nginx-chart/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
# 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.16.0"
Chart.yaml 文件描述了软件包的基本信息,如名称、简介、版本号等内容。
templates/
templates/ 目录下保存了部署服务所需要用到的配置模板,这些模板统一使用`Go`语言内置的模板库实现,其语法格式也同内置模板库相同。
values.yaml
这个文件里面存储了 templates/ 模板文件中所需要用到的键值,例如以下默认的部署软件包的内容:
# Default values for nginx-chart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: nginx
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: ""
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
# Specifies whether a service account should be created
create: true
# Annotations to add to the service account
annotations: {}
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
name: ""
podAnnotations: {}
podSecurityContext: {}
# fsGroup: 2000
securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000
service:
type: ClusterIP
port: 80
ingress:
enabled: false
className: ""
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local
resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80
# targetMemoryUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {}
helm就是这样通过获取 templates/* 与 values.yaml 文件,联合渲染为k8s的配置文件,然后投递给k8s ApiServer进行服务部署的。
部署软件包
之后我们只需要简单地执行install命令即可将服务部署到k8s集群中去。
$ helm install mynginx ./nginx-chart
NAME: mynginx
LAST DEPLOYED: Fri Jan 14 14:12:20 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=nginx-chart,app.kubernetes.io/instance=mynginx" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT
执行list命令可查看部署包的状态。
$ helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mynginx default 1 2022-01-14 14:12:20.896406384 +0800 CST deployed nginx-chart-0.1.0 1.16.0
以上为helm简单入门,关于helm的更多信息可以参考官方文档了解。
猜你喜欢
- 2024-09-30 「技术干货」k8s学习-calico的默认配置
- 2024-09-30 Prometheus Operator 初体验 prometheus详解
- 2024-09-30 Java基础教程:k8s快速入门 java基础入门第二版黑马程序员内容
- 2024-09-30 k8s dashboard 集成heapster pod资源监控界面
- 2024-09-30 k8s暴露集群内和集群外服务的方法
- 2024-09-30 K8S最全详解(图文全面总结) k8s go
- 2024-09-30 用户数从 0 到亿,我的 K8s 踩坑血泪史
- 2024-09-30 使用operator部署Prometheus prometheus api调用
- 2024-09-30 K8s服务编排 k8s service selector
- 2024-09-30 k8s基础概念及术语 k8s重要概念
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)