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

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

nanshan 2024-12-10 18:55 8 浏览 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的更多信息可以参考官方文档了解。

相关推荐

Let’s Encrypt免费搭建HTTPS网站

HTTPS(全称:HyperTextTransferProtocoloverSecureSocketLayer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入...

使用Nginx配置TCP负载均衡(nginx tcp负载)

假设Kubernetes集群已经配置好,我们将基于CentOS为Nginx创建一个虚拟机。以下是实验种设置的详细信息:Nginx(CenOS8Minimal)-192.168.1.50Kube...

Nginx负载均衡及支持HTTPS与申请免费SSL证书

背景有两台minio文件服务器已做好集群配置,一台是192.168.56.41:9000;另一台是192.168.56.42:9000。应用程序通过Nginx负载均衡调用这两台minio服务,减轻单点...

HTTPS配置实战(https配置文件)

原因现在网站使用HTTPS是规范操作之一,前些日子买了腾讯云服务,同时申请了域名http://www.asap2me.top/,目前该域名只支持HTTP,想升级为HTTPS。关于HTTPS的链接过程大...

只有IP地址没有域名实现HTTPS访问方法

一般来说,要实现HTTPS,得有个注册好的域名才行。但有时候呢,咱只有服务器的IP地址,没注册域名,这种特殊情况下,也能照样实现HTTPS安全访问,按下面这些步骤来就行:第一步,先确认公网...

超详解:HTTPS及配置Django+HTTPS开发环境

众所周知HTTP协议是以TCP协议为基石诞生的一个用于传输Web内容的一个网络协议,在“网络分层模型”中属于“应用层协议”的一种。在这里我们并不研究该协议标准本身,而是从安全角度去探究使用该协议传输数...

Godaddy购买SSL之后Nginx配置流程以及各种错误的解决

完整流程:参考地址:https://sg.godaddy.com/zh/help/nginx-generate-csrs-certificate-signing-requests-3601生成NGI...

Nginx从安装到高可用,一篇搞定(nginx安装与配置详解)

一、Nginx安装1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本2、上传nginx到linux系统3、安装依赖环境(1)安装gcc环境yuminstallgc...

阿里云免费证书申请,配置安装,使用tomcat,支持http/https访问

参数说明商品类型默认已选择云盾证书服务(无需修改)。云盾证书服务类型SSL证书服务的类型。默认已选择云盾SSL证书(无需修改),表示付费版SSL证书。如果您需要免费领取或付费扩容DV单域名证书【免费试...

你试过两步实现Nginx的规范配置吗?极速生成Nginx配置小工具

NGINX是一款轻量级的Web服务器,最强大的功能之一是能够有效地提供HTML和媒体文件等静态内容。NGINX使用异步事件驱动模型,在负载下提供可预测的性能。是当下最受欢迎的高性能的Web...

从零开始搭建HTTPS服务(搭建https网站)

搭建HTTPS服务的最初目的是为了开发微信小程序,因为wx.request只允许发起HTTPS请求,并且还必须和指定的域名进行网络通信。要从零开始搭建一个HTTPS的服务需要下面4...

群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问

安全第一步,群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问【新手导向】NAS本质还是一个可以随时随地访问的个人数据存储中心,我们在外网访问的时候,特别是在公网IP下,其实会面临着很多安...

让网站快速升级HTTPS协议提高安全性

为什么用HTTPS网络安全越来越受到重视,很多互联网服务网站,都已经升级改造为https协议。https协议下数据包是ssl/tcl加密的,而http包是明文传输。如果请求一旦被拦截,数据就会泄露产生...

用Https方式访问Harbor-1.9版本(https访问流程)

我上周在头条号写过一篇原创文章《Docker-Harbor&Docker-kitematic史上最详细双系统配置手册》,这篇算是它的姊妹篇吧。这篇文章也将用到我在头条写的另一篇原创文章的...

如何启用 HTTPS 并配置免费的 SSL 证书

在Linux服务器上启用HTTPS并配置免费的SSL证书(以Let'sEncrypt为例)可以通过以下步骤完成:---###**一、准备工作**1.**确保域名已解析**...

取消回复欢迎 发表评论: