重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何为Kubernetes制作一个Helm图表

这篇文章将为大家详细讲解有关如何为Kubernetes制作一个Helm图表,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联成立与2013年,是专业互联网技术服务公司,拥有项目成都网站制作、网站设计、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元湾里做网站,已为上家服务,为湾里各地企业和个人服务,联系电话:028-86922220

创建Helm图表

首先确认我们已安装了先决条件。

$ which helm ## this can be in any folder as long as it returns in the path/usr/local/bin/helm$ minikube status ## if it shows Stopped, run `minikube start`host: Runningkubelet: Runningapiserver: Runningkubeconfig: Configured

启动新的Helm图表需要一个简单的命令:

$ helm create mychartname

就本教程而言,将图表命名为buildachart:

$ helm create buildachartCreating buildachart$ ls buildachart/Chart.yaml charts/ templates/ values.yaml

检查图表的结构

现在,您已经创建了图表,请查看其结构以查看内部内容。您看到的前两个文件Chart.yaml和 values.yaml定义了图表的内容以及部署时其中的值。查看Chart.yaml,您可以看到Helm图表结构的轮廓:

apiVersion: v2name: buildachartdescription: 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.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.appVersion: 1.16.0

第一部分包括图表正在使用的API版本(这是必需的),图表的名称以及图表的描述。下一部分描述了图表的类型(默认情况下为应用程序),将要部署的图表的版本以及应用程序的版本(在进行更改时应递增)。

图表中最重要的部分是模板目录。它包含将部署到群集中的应用程序的所有配置。如下所示,该应用程序具有基本的部署,入口,服务帐户和服务。该目录还包括一个测试目录,其中包括一个测试到应用程序的连接的测试。这些应用程序功能中的每一个都在templates /下具有其自己的模板文件:

$ ls templates/NOTES.txt _helpers.tpl deployment.yaml ingress.yaml service.yaml serviceaccount.yaml tests/

还有另一个目录,称为图表,该目录为空。它允许您添加部署应用程序所需的从属图表。一些针对应用程序的Helm图表最多需要与主应用程序一起部署四个额外的图表。发生这种情况时,将使用每个图表的值更新值文件,以便同时配置和部署应用程序。这是高级得多的配置(在本介绍性文章中我不会介绍),因此将图表/文件夹留空。

了解和编辑值

模板文件的设置格式可从values.yaml文件收集部署信息。因此,要自定义Helm图表,您需要编辑值文件。默认情况下,values.yaml文件如下所示:

# Default values for buildachart.# This is a YAML-formatted file.# Declare variables to be passed into your templates.replicaCount: 1image:repository: nginxpullPolicy: IfNotPresentimagePullSecrets: []nameOverride: ""fullnameOverride: ""serviceAccount: # Specifies whether a service account should be createdcreate: true # Annotations to add to the service accountannotations: {} # The name of the service account to use. # If not set and create is true, a name is generated using the fullname templatename:podSecurityContext: {} # fsGroup: 2000securityContext: {} # capabilities: # drop: # - ALL # readOnlyRootFilesystem: true # runAsNonRoot: true # runAsUser: 1000service:type: ClusterIPport: 80ingress:enabled: falseannotations: {} # http://kubernetes.io/ingress.class: nginx # http://kubernetes.io/tls-acme: "true"hosts:- host: chart-example.localpaths: []tls: [] # - secretName: chart-example-tls # hosts: # - chart-example.localresources: {} # 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: 128MinodeSelector: {}tolerations: []affinity: {}

基本配置

从顶部开始,您可以看到copyingCount自动设置为1,这意味着只会出现一个pod。在此示例中,您只需要一个Pod,但是您可以看到告诉Kubernetes运行多个Pod以实现冗余是多么容易。

该图像部分有你需要看两样东西:库你在哪里拉你的图像和pullPolicy。pullPolicy设置为IfNotPresent ; 这意味着如果群集中不存在该映像,则该映像将下载该映像的新版本。有两个其他选项:Always,这意味着它将在每次部署或重新启动时提取映像(在映像失败的情况下,我总是建议这样做)和Latest,它将始终提取最新版本的。可用的图像。如果您相信映像存储库与部署环境兼容,则最新消息可能会很有用,但并非总是如此。

将值更改为Always。

之前:

image:repository: nginxpullPolicy: IfNotPresent

后来:

image:repository: nginxpullPolicy: Always

命名和加密

接下来,查看图表中的替代。第一个替代是imagePullSecrets,它是提取秘密(例如,您已将其作为私有注册表的凭据生成的密码或API密钥)的设置。接下来是nameOverride和fullnameOverride。从您开始掌控create的那一刻起,它的名称(buildachart)就添加到了许多配置文件中-从上面的YAML文件到templates / helper.tpl文件。如果在创建图表后需要重命名图表,则此部分是执行此操作的最佳位置,因此您不会错过任何配置文件。

使用替代更改图表的名称。

之前:

imagePullSecrets: []nameOverride: ""fullnameOverride: ""

后来:

imagePullSecrets: []nameOverride: "cherry-awesome-app"fullnameOverride: "cherry-chart"

帐目设置

服务帐户提供了一个用户身份,可以在群集内的Pod中运行。如果保留为空白,则将使用helpers.tpl文件根据全名生成名称。我建议始终设置一个服务帐户,以便该应用程序将直接与图表中控制的用户相关联。

作为管理员,如果使用默认服务帐户,则权限将太少或太多,因此请更改此设置。

之前:

serviceAccount: # Specifies whether a service account should be createdcreate: true # Annotations to add to the service accountannotations: {} # The name of the service account to use. # If not set and create is true, a name is generated using the fullname templateName:

后来:

serviceAccount: # Specifies whether a service account should be createdcreate: true # Annotations to add to the service accountannotations: {} # The name of the service account to use. # If not set and create is true, a name is generated using the fullname templateName: cherrybomb

安全设置

您可以配置pod安全性,以设置要使用的文件系统组类型或可以使用和不能使用哪个用户的限制。了解这些选项对于保护Kubernetes吊舱很重要,但是对于本示例,我将不赘述。

podSecurityContext: {} # fsGroup: 2000securityContext: {} # capabilities: # drop: # - ALL # readOnlyRootFilesystem: true # runAsNonRoot: true # runAsUser: 1000

联网设置

此图表中有两种不同类型的网络选项。一个使用带有ClusterIP地址的本地服务网络,该网络将服务公开在群集内部的IP上。选择此值可使与您的应用程序关联的服务仅可从群集内部访问(并通过ingress,默认情况下设置为false)。另一个网络选项是NodePort,它在静态分配的端口上在每个Kubernetes节点的IP地址上公开服务。建议使用此选项来运行minikube,因此可将其用于此方法。

之前:

service:type: ClusterIPport: 80ingress:enabled: false

后来:

service:type: NodePortport: 80ingress:enabled: false

资源设置

Helm允许您显式分配硬件资源。您可以配置Helm图表可以请求的最大资源量以及可以接收的最大限制。由于我在笔记本电脑上使用Minikube,因此我将通过删除花括号和哈希值(将注释转换为命令)来设置一些限制。

之前:

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

后来:

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: 100mmemory: 128Mirequests:cpu: 100mmemory: 128Mi

公差,节点选择器和关联性

最后三个值基于节点配置。尽管我无法在本地配置中使用它们中的任何一个,但我仍将说明它们的用途。

想要将应用程序的一部分分配给Kubernetes集群中的特定节点时,nodeSelector会派上用场。如果您具有特定于基础架构的应用程序,则可以设置节点选择器名称,并在Helm图表中匹配该名称。然后,在部署应用程序时,它将与匹配选择器的节点关联。

容差,污点和亲和力一起工作,以确保Pod在单独的节点上运行。节点亲和力是的性质豆荚该吸引他们的一组节点(无论是作为一个偏好或硬要求)。污点是相反它们允许节点以排斥一套豆荚。

实际上,如果节点受到污染,则意味着该节点无法正常工作或可能没有足够的资源来容纳应用程序部署。容差设置为调度程序监视的键/值对,以确认节点将与部署一起使用。

节点相似性在概念上类似于nodeSelector:它使您可以根据节点上的标签来限制Pod可以调度哪些节点。但是,标签不同,因为它们与适用于schedule的规则匹配。

nodeSelector: {}tolerations: []affinity: {}

开始部署

现在,您已经对创建Helm图表进行了必要的修改,可以使用Helm命令对其进行部署,在图表上添加名称点,添加值文件并将其发送到命名空间:

$ helm install my-cherry-chart buildachart/ --values buildachart/values.yamlRelease “my-cherry-chart” has been upgraded. Happy Helming!

该命令的输出将为您提供连接到应用程序的后续步骤,包括设置端口转发,以便您可以从本地主机访问该应用程序。要遵循这些说明并连接到Nginx负载均衡器:

$ export POD_NAME=$(kubectl get pods -l "http://app.kubernetes.io/name=buildachart,app.kubernetes.io/instance=my-cherry-chart" -o jsonpath="{.items[0].metadata.name}")$ echo "Visit http://127.0.0.1:8080 to use your application"Visit http://127.0.0.1:8080 to use your application$ kubectl port-forward $POD_NAME 8080:80Forwarding from 127.0.0.1:8080 -> 80Forwarding from [::1]:8080 -> 80

查看已部署的应用程序

要查看您的应用程序,请打开Web浏览器:

如何为Kubernetes制作一个Helm图表

关于“如何为Kubernetes制作一个Helm图表”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


本文名称:如何为Kubernetes制作一个Helm图表
路径分享:http://cqcxhl.com/article/gjhdes.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP