Kubernetes/k8s之包管理器helm

helm

在没有helm之前,我们要部署一个服务,deployment、service ingress

的作用通过打包的方式。把deployment、service ingress打包在一块,一键式部署服务。类似于yum功能。是官方提供的类似安装仓库的功能,可以实现一键化部署应用

helm的概念

由三个部分组成

chart:helm的软件包,部署包,service ingress,是一些定义好的yaml资源,类似于yum的rpm包

Release:可以理解为版本,也可以理解为在安装过程中,给部署的应用起一个名字,

Repository:仓库,提供一个人服务器,这个服务器当中包含chart这些资源,yaml资源的保存地址。

一般使用的是helm3

将Helm安装在master01节点上 


tar -zxvf helm-v3.6.0-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm


添加helm的自动补全功能,方便后续使用
vim /etc/bashrc
source <(helm completion bash)

使用 helm 安装 Chart 
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add stable http://mirror.azure.cn/kubernetes/charts
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo add incubator https://charts.helm.sh/incubato

更新和查看 charts 列表

helm repo update          
helm repo list

查看 stable 仓库可用的 charts 列表
helm search repo stable

删除 incubator 仓库
helm repo remove incubator

查看 chart 信息

helm show chart stable/mysql

helm show all stable/mysql         #获取指定 chart 的所有信息

安装 chart 
helm install my-redis bitnami/nginx 
my-nginx: bitnami仓库名,nginx就是chart一些列yaml资源的集合。
helm uninstall my-nginx  #删除
helm install bitnami/nginx --generate-name
--generate-name #随机生成一个rmlist的名称
helm ls #查看安装的所有版本


helm自定义模板:根据自己需要,定义chart,然后部署到集群中


helm pull stable/mysql
tar -xf mysql-1.6.9.tgz
tree nginx
nginx/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

charts:用于存储依赖环境,如果这个chart依赖于其他的chart,依赖文件保存这个目录

chart.yaml:helm chart的元数据文件,包含了这个chart的名称,版本,维护者信息等等

templates:包含清单模板的目录

deployment.yaml:部署应用的模板文件

heplers.tpl:帮助文档,告诉用户如何来定义模板的值

hpa.yaml:定义了应用程序副本数的扩缩容行为

ingress.yaml:定义了外部流量如何转发到应用程序

HOSTs.txt:注意事项

serviceaccount.yaml:应用程序的服务账号

service.yaml:集群内部的访问

tests test-connection.yaml:测试的目录和文件,部署完chart之后,用测试的文件

values.yaml:核心文件,自定义的值,都是通过values.yaml。把我们的数据覆盖到安装的chart

vim nginx/Chart.yaml
apiVersion: v2
name: nginx 
chart名字
description: A Helm chart for Kubernetes
type: application
chart类型,application或library
version: 0.1.0
chart版本
appVersion: 1.16.0
application部署版本
vim values.yaml
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 #开启ingress
  # 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: www.lucky.com  #指定域名
      paths:
        - path: /
          pathType: Prefix
  #指定ingress路径类型

  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 lint nginx        #检查依赖和模版配置是否正确
 
helm package nginx     #打包 chart,会在当前目录下生成压缩包 nginx-0.1.0.tgz

helm install nginx ./nginx --dry-run --debug    #使用 --dry-run 参数验证 Chart 的配置,并不执行安装

helm install nginx ./nginx -n default           #部署 chart,release 版本默认为 1
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

wget https://gitee.com/mirrors/ingress-nginx/raw/nginx 0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml

kubectl apppy -f mandatory.yaml
kubectl apppy -f service-nodeport.yaml

访问域名

修改chart之后重新部署
vim values.yaml
service:
  type: NodePort
  port: 80
  nodePort: 30000

ingress:
  enabled: false
  className: ""
  annotations: {}


vim service.yaml


apiVersion: v1
kind: Service
metadata:
  name: {
  { include "nginx.fullname" . }}
  labels:
    {
  {- include "nginx.labels" . | nindent 4 }}
spec:
  type: {
  { .Values.service.type }}
  ports:
    - port: {
  { .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
      nodePort: {.Values.service.nodePort}
  selector:
    {
  {- include "nginx.selectorLabels" . | nindent 4 }}


helm upgrade nginx-22 nginx
自定义chart的版本回滚 

helm history nginx1-22              #查看 release 版本历史
 
helm rollback nginx1-22 1           #回滚 release 到版本1

Helm 的私有仓库-Habor

harbor端



vim harbor.yml
harbor_admin_password: 123456

chart:
  absolute_url: enabled
#在chart当中使用绝对路径的url,https://hub.test.com/charts 相对路径: /charts

./install.sh

master上

mkdir -p ~/.local/share/helm/plugins/helm-push

tar -xf helm-push_0.8.1_linux_amd64.tar.gz -C ~/.local/share/helm/plugins/helm-push

docker login -u admin -p 123456 https://hub.test.com

helm package nginx

docker push nginx-0.2.0.tgz oci://hub.test.com/charts --insecure-skip-tls-verify

拉取chart包测试: 

rm -rf nginx

helm pull oci://hub.test.com/charts/nginx --version 0.2.0 --insecure-skip-tls-verify

helm install nginx-22 ./nginx-0.2.0.tgz

helm就是一个部署微服务的工具,可以跳过繁琐的自定义yaml过程,一键式的拉取和部署所有自定义或者模板定义的服务

helm查用的命令

helm repo add 仓库名 url地址 添加仓库

helm repo update 不加仓库名 就是更新所有仓库

helm repo list 仓库列表

hele repo remove 仓库名称  删除

helm show chart stable/nginx 可以查看指定仓库chart信息

helm show all stable/nginx 查看所有信息

helm install nginx-11 stable/nginx -n lucky-cloud 指定命名空间 安装chart,安装官网默认版本

helm uninstall nginx-11 删除安装好的chart

helm list 查看已经安装的chart

自定义模板

helm create nginx 创建一个自定义的chart模板

values.yaml:这里的值回传给templates里面的yaml文件

helm install nginx-11 ./nginx

helm install nginx-11 ./nginx-0.1.0/tgz

如何打包创建好的chart

helm package nginx

回滚

helm history nginx-11

helm rollback nginx-11 1

相关推荐

  1. k8s笔记 | helm管理

    2024-01-22 16:30:00       14 阅读
  2. K8Shelm

    2024-01-22 16:30:00       23 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 16:30:00       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 16:30:00       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 16:30:00       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 16:30:00       20 阅读

热门阅读

  1. htb Analysis wp

    2024-01-22 16:30:00       24 阅读
  2. php.2安装Imagick扩展

    2024-01-22 16:30:00       40 阅读
  3. 计算机视觉未来的走向

    2024-01-22 16:30:00       34 阅读
  4. UI -- Vue3

    2024-01-22 16:30:00       36 阅读