面对配置文件不再恐慌,k8s的包管理神器helm简介
nanshan 2024-12-10 18:55 11 浏览 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的更多信息可以参考官方文档了解。
相关推荐
- 0722-6.2.0-如何在RedHat7.2使用rpm安装CDH(无CM)
-
文档编写目的在前面的文档中,介绍了在有CM和无CM两种情况下使用rpm方式安装CDH5.10.0,本文档将介绍如何在无CM的情况下使用rpm方式安装CDH6.2.0,与之前安装C5进行对比。环境介绍:...
- ARM64 平台基于 openEuler + iSula 环境部署 Kubernetes
-
为什么要在arm64平台上部署Kubernetes,而且还是鲲鹏920的架构。说来话长。。。此处省略5000字。介绍下系统信息;o架构:鲲鹏920(Kunpeng920)oOS:ope...
- 生产环境starrocks 3.1存算一体集群部署
-
集群规划FE:节点主要负责元数据管理、客户端连接管理、查询计划和查询调度。>3节点。BE:节点负责数据存储和SQL执行。>3节点。CN:无存储功能能的BE。环境准备CPU检查JDK...
- 在CentOS上添加swap虚拟内存并设置优先级
-
现如今很多云服务器都会自己配置好虚拟内存,当然也有很多没有配置虚拟内存的,虚拟内存可以让我们的低配服务器使用更多的内存,可以减少很多硬件成本,比如我们运行很多服务的时候,内存常常会满,当配置了虚拟内存...
- 国产深度(deepin)操作系统优化指南
-
1.升级内核随着deepin版本的更新,会自动升级系统内核,但是我们依旧可以通过命令行手动升级内核,以获取更好的性能和更多的硬件支持。具体操作:-添加PPAs使用以下命令添加PPAs:```...
- postgresql-15.4 多节点主从(读写分离)
-
1、下载软件[root@TX-CN-PostgreSQL01-252software]#wgethttps://ftp.postgresql.org/pub/source/v15.4/postg...
- Docker 容器 Java 服务内存与 GC 优化实施方案
-
一、设置Docker容器内存限制(生产环境建议)1.查看宿主机可用内存bashfree-h#示例输出(假设宿主机剩余16GB可用内存)#Mem:64G...
- 虚拟内存设置、解决linux内存不够问题
-
虚拟内存设置(解决linux内存不够情况)背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级。所以,程序和数据如果在内存的话,会有非常快的读写速度。但是,内存...
- Elasticsearch性能调优(5):服务器配置选择
-
在选择elasticsearch服务器时,要尽可能地选择与当前业务量相匹配的服务器。如果服务器配置太低,则意味着需要更多的节点来满足需求,一个集群的节点太多时会增加集群管理的成本。如果服务器配置太高,...
- Es如何落地
-
一、配置准备节点类型CPU内存硬盘网络机器数操作系统data节点16C64G2000G本地SSD所有es同一可用区3(ecs)Centos7master节点2C8G200G云SSD所有es同一可用区...
- 针对Linux内存管理知识学习总结
-
现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的。对于内存部分需要知道:地址映射内存管理的方式缺页异常先来看一些基本的知识,在进程看来,内存分为内...
- MySQL进阶之性能优化
-
概述MySQL的性能优化,包括了服务器硬件优化、操作系统的优化、MySQL数据库配置优化、数据库表设计的优化、SQL语句优化等5个方面的优化。在进行优化之前,需要先掌握性能分析的思路和方法,找出问题,...
- Linux Cgroups(Control Groups)原理
-
LinuxCgroups(ControlGroups)是内核提供的资源分配、限制和监控机制,通过层级化进程分组实现资源的精细化控制。以下从核心原理、操作示例和版本演进三方面详细分析:一、核心原理与...
- linux 常用性能优化参数及理解
-
1.优化内核相关参数配置文件/etc/sysctl.conf配置方法直接将参数添加进文件每条一行.sysctl-a可以查看默认配置sysctl-p执行并检测是否有错误例如设置错了参数:[roo...
- 如何在 Linux 中使用 Sysctl 命令?
-
sysctl是一个用于配置和查询Linux内核参数的命令行工具。它通过与/proc/sys虚拟文件系统交互,允许用户在运行时动态修改内核参数。这些参数控制着系统的各种行为,包括网络设置、文件...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)