百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

面对配置文件不再恐慌,k8s的包管理神器helm简介

nanshan 2024-12-10 18:55 9 浏览 0 评论

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的更多信息可以参考官方文档了解。

相关推荐

服务器温度监控--lm-sensors(服务器温度怎么看)

lm-sensors是一款linux的硬件监控的软件,可以帮助我们来监控主板,CPU的工作电压,风扇转速、温度等数据。这些数据我们通常在主板的BIOS也可以看到。当我们可以在机器运行的时候通过...

MySQL版本区别及管理(mysql版本最新版本)

MySQL版本区别及管理一.MySQL5.6与MySQL5.7安装的区别1、cmake的时候加入了bostorg2、初始化时使用mysqld--initialize替代mysql_install...

Linux技术问答系列-NO4(linux必知必会)

一.绝对路径用什么符号表示?当前目录、上层目录用什么表示?主目录用什么表示?切换目录用什么命令?绝对路径:如/etc/init.d当前目录和上层目录:./../主目录:~/切换目录:cd二...

猫盘原版系统开启ssh教程(猫盘原版系统怎么样)

猫盘是之前网上流传许久的矿渣,默认其系统不支持SSH功能,为了能打开其SSH功能,我特意制作操作教程如下:1、到网盘下载相关软件,利用猫盘系统自带功能,将assets放入个人存储目录下,并牢记对应的...

一探究竟——天融信网闸TopRules7000

网闸即:安全隔离与信息交换系统,常用作企业内外网隔离与业务互访用途。相比给服务器加多块网卡跨多个网段来说,网闸提供了更加安全的方式。探究背景:某次,网闸配置新业务,重启设备查看是否生效,结果发现刚重启...

操作系统加固通用Linux篇(linux系统加固常见操作)

1检查是否配置登陆超时时间设置编辑vi/etc/profile文件,配置TMOUT将值设置为低于300.TMOUT=3002检查是否禁止root用户登录FTP设置如下将对应配置文件中,设置roo...

zabbix agent的安装与配置(zabbix-agent安装)

Agent安装rpm-ivhzabbix-agent-3.2.4-1.el6.x86_64.rpm安装完成后,zabbixagent端已经安装完成了,zabbixagent端的配置目录位于/e...

Linux基础命令之计划任务(linux计划任务crontab)

一、计划任务1、at只能执行一次语法:at时间服务:atd必须开启123[root@xuegod163~]#/etc/init.d/atdstatus#查看服务状态atd(pid2...

Secure Delivery Center (SDC)安装指南二:Delivery Hub

免费下载SecureDeliveryCenter2015>7月23日软件分发管理神器SecureDeliveryCenter免费技术交流会,MyEclipse原厂商倾力主讲,敬请关注!...

OpenWrt 常用命令及用法!!(openwrt常用功能)

OpenWrt是一个高度可定制的嵌入式Linux操作系统,常用于路由器等网络设备。以下是一些常见的OpenWrt命令及其详细解释和示例操作:一、系统信息相关命令1.`uname-a``u...

Linux 设置定时任务crontab命令(linux定时任务cron表达式)

看了同事的脚本,发现他用了cron来自检自身的那个程序是否崩溃了,这是有多大的不自信才用这种机制的?点击(此处)折叠或打开$sudocat/var/spool/cron/crontabs/ro...

vCenter纳管ESXI主机出错(vsphere esxi)

vCenter纳管主机的大致步骤为:(1)vc和esxi交换证书,确立信任;(2)esxi把自己的资源信息同步到VC,VC建立清单。(3)VC在esxi建立几个操作用户;(4)然后下发...

从选购到安装 小白也能看懂的超全NAS经验分享

0.篇首语Hello大家好,我是KC,上一篇器材和工作流分享的文章里,有小伙伴问我怎么没有提到NAS?其实是因为前段时间碰巧更换了一台新NAS,折腾了一段时间很多内容还没来及整理和汇总,今天就...

手把手教你!如何在 Linux 服务器中搭建 Sentinel 环境?

你在Linux服务器上搭建Sentinel环境时,是不是也遇到过各种报错,要么是启动失败,要么是配置后无法正常访问控制台?看着同事顺利搭建好,自己却一头雾水,别提多着急了!其实,很多互联网大厂...

服务器被暴力破解的解决办法(二)(服务器被攻破严重吗)

上一次,我们说到小王公司服务器遭遇暴力破解,拿到解决方案回公司就开始部署。部署完成后的确起到了一定的效果,不过接下来的一个问题让他很头疼,原来黑客虽然攻入不进系统,但是依旧不依不饶的进行暴力破解。...

取消回复欢迎 发表评论: