k8s的應用包管理工具helm的部署和使用


1、概述

我們一般是在k8s里面部署一些簡單的應用,比如用deployment,daemonset,statefuleset的方式來部署應用,但是如果要部署一些復雜的應用,那么整個配置的編寫、部署的過程就會變的更加的復雜,那么helm這個工具就可以來幫助解決這個復雜應用的部署問題。

helm可以類比與linux環境下yum部署應用的方式,只需要執行幾條命令,就可以將應用需要的配置、依賴都部署到環境中。同樣,helm也是通過幾條簡單的命令,將某個應用需要的配置、依賴都部署到了k8s集群上。

本文檔就通過helm的安裝和使用的角度來介紹下helm這個k8s的應用包管理工具。

2、helm中基本概念

在helm這個工具的使用中,主要有3個基本的概念需要了解:

Chart: chart就是helm中的包。類似yum安裝中使用的rpm包。chart安裝到k8s之后就是一個應用,那么chart中就包含了所有需要的資源的定義

Repository: chart存放的位置。類似yum源的概念。

Release: chart在k8s集群中運行起來之后形成的實例。就類似與部署一個deployment要給一個名字一樣。一個chart可以在同一個集群中被安裝很多次,每一次的安裝,都會創建一個新的release。就和拿一個redis的安裝包可以安裝很多次一樣的。每次安裝都有一個實例對應有自己的端口號,配置一樣的。

那么針對以上的概念的說明,針對helm可以有如下的表述:

helm在chart倉庫中獲得chart,然后將chart安裝到k8s集群中,每次安裝都創建一個release.

3、helm的安裝

可以通過預編譯的二進制文件來安裝helm的客戶端命令,具體的版本可以到helm的github上去下載:

https://github.com/helm/helm/releases

本文檔使用的版本:

https://get.helm.sh/helm-v3.7.2-linux-amd64.tar.gz

注意:安裝在包含kubeconfig的主機上,這樣helm就可以操作k8s集群來部署對應的應用,helm默認去"~/.kube/config"目錄下獲取kubeconfig來連接k8s集群。

安裝過程

(1)先上傳helm的壓縮包到服務器

(2)解壓並且復制helm命令

tar -zxf helm-v3.7.2-linux-amd64.tar.gz \
&& cd linux-amd64 \
cp helm /usr/local/bin/

(3)驗證helm命令

輸入helm命令,如果輸出如下,則表示helm安裝成功

[root@nccztsjb-node-11 linux-amd64]# helm
The Kubernetes package manager

Common actions for Helm:

- helm search:    search for charts
- helm pull:      download a chart to your local directory to view
- helm install:   upload the chart to Kubernetes
- helm list:      list releases of charts

Environment variables:

| Name                               | Description                                                                       |
|------------------------------------|-----------------------------------------------------------------------------------|
| $HELM_CACHE_HOME                   | set an alternative location for storing cached files.                             |
| $HELM_CONFIG_HOME                  | set an alternative location for storing Helm configuration.                       |
| $HELM_DATA_HOME                    | set an alternative location for storing Helm data.                                |
| $HELM_DEBUG                        | indicate whether or not Helm is running in Debug mode                             |
| $HELM_DRIVER                       | set the backend storage driver. Values are: configmap, secret, memory, sql.       |
| $HELM_DRIVER_SQL_CONNECTION_STRING | set the connection string the SQL storage driver should use.                      |
| $HELM_MAX_HISTORY                  | set the maximum number of helm release history.                                   |
| $HELM_NAMESPACE                    | set the namespace used for the helm operations.                                   |
| $HELM_NO_PLUGINS                   | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.                        |
| $HELM_PLUGINS                      | set the path to the plugins directory                                             |
| $HELM_REGISTRY_CONFIG              | set the path to the registry config file.                                         |
| $HELM_REPOSITORY_CACHE             | set the path to the repository cache directory                                    |
| $HELM_REPOSITORY_CONFIG            | set the path to the repositories file.                                            |
| $KUBECONFIG                        | set an alternative Kubernetes configuration file (default "~/.kube/config")       |
| $HELM_KUBEAPISERVER                | set the Kubernetes API Server Endpoint for authentication                         |
| $HELM_KUBECAFILE                   | set the Kubernetes certificate authority file.                                    |
| $HELM_KUBEASGROUPS                 | set the Groups to use for impersonation using a comma-separated list.             |
| $HELM_KUBEASUSER                   | set the Username to impersonate for the operation.                                |
| $HELM_KUBECONTEXT                  | set the name of the kubeconfig context.                                           |
| $HELM_KUBETOKEN                    | set the Bearer KubeToken used for authentication.                                 |

Helm stores cache, configuration, and data based on the following configuration order:

- If a HELM_*_HOME environment variable is set, it will be used
- Otherwise, on systems supporting the XDG base directory specification, the XDG variables will be used
- When no other location is set a default location will be used based on the operating system

By default, the default directories depend on the Operating System. The defaults are listed below:

| Operating System | Cache Path                | Configuration Path             | Data Path               |
|------------------|---------------------------|--------------------------------|-------------------------|
| Linux            | $HOME/.cache/helm         | $HOME/.config/helm             | $HOME/.local/share/helm |
| macOS            | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm      |
| Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |

Usage:
  helm [command]

Available Commands:
  completion  generate autocompletion scripts for the specified shell
  create      create a new chart with the given name
  dependency  manage a chart's dependencies
  env         helm client environment information
  get         download extended information of a named release
  help        Help about any command
  history     fetch release history
  install     install a chart
  lint        examine a chart for possible issues
  list        list releases
  package     package a chart directory into a chart archive
  plugin      install, list, or uninstall Helm plugins
  pull        download a chart from a repository and (optionally) unpack it in local directory
  repo        add, list, remove, update, and index chart repositories
  rollback    roll back a release to a previous revision
  search      search for a keyword in charts
  show        show information of a chart
  status      display the status of the named release
  template    locally render templates
  test        run tests for a release
  uninstall   uninstall a release
  upgrade     upgrade a release
  verify      verify that a chart at the given path has been signed and is valid
  version     print the client version information

Flags:
      --debug                       enable verbose output
  -h, --help                        help for helm
      --kube-apiserver string       the address and the port for the Kubernetes API server
      --kube-as-group stringArray   group to impersonate for the operation, this flag can be repeated to specify multiple groups.
      --kube-as-user string         username to impersonate for the operation
      --kube-ca-file string         the certificate authority file for the Kubernetes API server connection
      --kube-context string         name of the kubeconfig context to use
      --kube-token string           bearer token used for authentication
      --kubeconfig string           path to the kubeconfig file
  -n, --namespace string            namespace scope for this request
      --registry-config string      path to the registry config file (default "/root/.config/helm/registry.json")
      --repository-cache string     path to the file containing cached repository indexes (default "/root/.cache/helm/repository")
      --repository-config string    path to the file containing repository names and URLs (default "/root/.config/helm/repositories.yaml")

Use "helm [command] --help" for more information about a command.

4、helm的使用

那么這里就要介紹在如何使用helm來安裝和管理應用包了。

4.1、獲取應用的repository

比如我們要安裝一個nginx應用,那么我們首先要知道哪些庫,類似於哪些yum源中有這個包,那么方法就是登錄如下的地址:

https://artifacthub.io/

這個中包含了所有的有這個應用包的一個匯總

在其中搜索“nginx”

就會有各種組織發布的不同的功能的包含nginx的包。

點開第一個,就會有相關的使用的說明

那么這個https://charts.bitnami.com/bitnami URL就是包含這個chart的倉庫。

4.2、helm添加倉庫和刪除

4.2.1、添加

OK,基於4.1中找到了包含需要的nginx的倉庫,那么就要把他加入到helm的repo中,也可以按照上面的查找的nginx的repo頁面的描述來添加和安裝。

https://artifacthub.io/packages/helm/bitnami/nginx這個頁面就有關於這個chart如何安裝和使用的具體的說明。

helm repo add bitnami https://charts.bitnami.com/bitnami

其中bitnami是定義的倉庫的名字,由於是要加入到本地的,可自行的定義。

執行過程

[root@nccztsjb-node-11 ~]# helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories

查看倉庫列表

[root@nccztsjb-node-11 ~]# helm repo list
NAME   	URL                               
bitnami	https://charts.bitnami.com/bitnami
[root@nccztsjb-node-11 ~]# 

可以看到倉庫和對應的URL信息。

4.2.1、刪除

可以通過如下的命令進行倉庫的刪除

helm repo remove bitnami

執行過程

[root@nccztsjb-node-11 ~]# helm repo remove bitnami
"bitnami" has been removed from your repositories
[root@nccztsjb-node-11 ~]# helm repo list
Error: no repositories to show
[root@nccztsjb-node-11 ~]# 

可以看到在helm中沒有任何的倉庫的信息了。

通過以上的庫的增加和刪除可以看到,就是對helm進行配置將包含chart的url地址添加為helm的倉庫。后續helm命令就去這些倉庫對應的url中去找需要的chart,獲取chart的包,然后部署到k8s上。

4.3、chart的查找

在已經添加的本地庫中查找需需要的char即應用包

[root@nccztsjb-node-11 ~]# helm search repo nginx
NAME                            	CHART VERSION	APP VERSION	DESCRIPTION                                       
bitnami/nginx                   	9.7.1        	1.21.5     	Chart for the nginx server                        
bitnami/nginx-ingress-controller	9.1.2        	1.1.1      	Chart for the nginx Ingress controller            
bitnami/kong                    	5.0.1        	2.7.0      	Kong is a scalable, open source API layer (aka ...
[root@nccztsjb-node-11 ~]# 

從輸出中可以看到軟件包的名字,其中的應用的版本信息,chart的版本信息,具體的chart的描述

4.4、chart安裝和卸載

4.4.1、安裝

以安裝nginx的chart為例

helm install helm-nginx bitnami/nginx

install后面就是release的名字,可根據需要自行的定義。

安裝過程

[root@nccztsjb-node-11 ~]# helm install helm-nginx bitnami/nginx
NAME: helm-nginx
LAST DEPLOYED: Mon Jan 17 10:33:26 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 9.7.1
APP VERSION: 1.21.5

** Please be patient while the chart is being deployed **

NGINX can be accessed through the following DNS name from within your cluster:

    helm-nginx.default.svc.cluster.local (port 80)

To access NGINX from outside the cluster, follow the steps below:

1. Get the NGINX URL by running these commands:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace default -w helm-nginx'

    export SERVICE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].port}" services helm-nginx)
    export SERVICE_IP=$(kubectl get svc --namespace default helm-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    echo "http://${SERVICE_IP}:${SERVICE_PORT}"

安裝是異步的,也就是這里提示的只是成功的將chart發布到了k8s中,應用的部署還需要時間。

從其他的信息中也可以知道,如果要訪問其中的服務的方法是什么。

    export SERVICE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].port}" services helm-nginx)
    export SERVICE_IP=$(kubectl get svc --namespace default helm-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    echo "http://${SERVICE_IP}:${SERVICE_PORT}"

查看部署的狀態

[root@nccztsjb-node-11 ~]# helm status helm-nginx
NAME: helm-nginx
LAST DEPLOYED: Mon Jan 17 10:33:26 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 9.7.1
APP VERSION: 1.21.5

** Please be patient while the chart is being deployed **

NGINX can be accessed through the following DNS name from within your cluster:

    helm-nginx.default.svc.cluster.local (port 80)

To access NGINX from outside the cluster, follow the steps below:

1. Get the NGINX URL by running these commands:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace default -w helm-nginx'

    export SERVICE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].port}" services helm-nginx)
    export SERVICE_IP=$(kubectl get svc --namespace default helm-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    echo "http://${SERVICE_IP}:${SERVICE_PORT}"

查看具體的pod的運行狀態

[root@nccztsjb-node-11 ~]# kubectl get pod | grep helm-nginx
helm-nginx-5756488bdb-vvp9g   1/1     Running     0          3m1s
[root@nccztsjb-node-11 ~]# 

pod已經正常的處於Running的狀態了。

4.4.2、卸載

如果要卸載應用,方法如下

[root@nccztsjb-node-11 ~]# helm uninstall helm-nginx
release "helm-nginx" uninstalled
[root@nccztsjb-node-11 ~]# helm status helm-nginx
Error: release: not found
[root@nccztsjb-node-11 ~]# helm list
NAME	NAMESPACE	REVISION	UPDATED	STATUS	CHART	APP VERSION
[root@nccztsjb-node-11 ~]# 

OK,這樣就完成了應用的卸載。

4.5、查看chart的信息

和yum中的rpm包一樣,我們也可以查看chart中的描述信息

[root@nccztsjb-node-11 ~]# helm inspect 

This command consists of multiple subcommands to display information about a chart

Usage:
  helm show [command]

Aliases:
  show, inspect

Available Commands:
  all         show all information of the chart
  chart       show the chart's definition
  crds        show the chart's CRDs
  readme      show the chart's README
  values      show the chart's values

Flags:
  -h, --help   help for show

Global Flags:
      --debug                       enable verbose output
      --kube-apiserver string       the address and the port for the Kubernetes API server
      --kube-as-group stringArray   group to impersonate for the operation, this flag can be repeated to specify multiple groups.
      --kube-as-user string         username to impersonate for the operation
      --kube-ca-file string         the certificate authority file for the Kubernetes API server connection
      --kube-context string         name of the kubeconfig context to use
      --kube-token string           bearer token used for authentication
      --kubeconfig string           path to the kubeconfig file
  -n, --namespace string            namespace scope for this request
      --registry-config string      path to the registry config file (default "/root/.config/helm/registry.json")
      --repository-cache string     path to the file containing cached repository indexes (default "/root/.cache/helm/repository")
      --repository-config string    path to the file containing repository names and URLs (default "/root/.config/helm/repositories.yaml")

Use "helm show [command] --help" for more information about a command.

其中,all是所有的信息,配置的默認值使用value命令。

查看nginx chart的安裝使用的默認值

[root@nccztsjb-node-11 ~]# helm inspect values bitnami/nginx
## @section Global parameters
## Global Docker image parameters
## Please, note that this will override the image parameters, including dependencies, configured to use the global value
## Current available global Docker image parameters: imageRegistry, imagePullSecrets and storageClass

## @param global.imageRegistry Global Docker image registry
## @param global.imagePullSecrets Global Docker registry secret names as an array
##
global:
  imageRegistry: ""
  ## E.g.
  ## imagePullSecrets:
  ##   - myRegistryKeySecretName
  ##
  imagePullSecrets: []

## @section Common parameters

## @param nameOverride String to partially override nginx.fullname template (will maintain the release name)
##
nameOverride: ""
## @param fullnameOverride String to fully override nginx.fullname template
##
fullnameOverride: ""
## @param kubeVersion Force target Kubernetes version (using Helm capabilities if not set)
##
kubeVersion: ""
## @param clusterDomain Kubernetes Cluster Domain
##
clusterDomain: cluster.local
## @param extraDeploy Extra objects to deploy (value evaluated as a template)
##
extraDeploy: []
## @param commonLabels Add labels to all the deployed resources
##
commonLabels: {}
## @param commonAnnotations Add annotations to all the deployed resources
##
commonAnnotations: {}

## @section NGINX parameters

## Bitnami NGINX image version
## ref: https://hub.docker.com/r/bitnami/nginx/tags/
## @param image.registry NGINX image registry
## @param image.repository NGINX image repository
## @param image.tag NGINX image tag (immutable tags are recommended)
## @param image.pullPolicy NGINX image pull policy
## @param image.pullSecrets Specify docker-registry secret names as an array
## @param image.debug Set to true if you would like to see extra information on logs
##
image:
  registry: docker.io
  repository: bitnami/nginx
... 以下的內容省略...

這些就是一些默認的配置項,在安裝的時候可以通過yaml或者--set來進行指定修改默認的配置,比如使用的鏡像的信息,版本等。

4.6、chart導出為本地壓縮包

chart就是部署應用時需要的配置,那么可以將這些配置都拉取到本地,和rpm一樣。

使用如下的命令來獲取

[root@nccztsjb-node-11 ~]# helm pull bitnami/nginx .
Error: non-absolute URLs should be in form of repo_name/path_to_chart, got: .
[root@nccztsjb-node-11 ~]# ls -l
total 24376
-rw-------. 1 root root       1396 Jun 22  2018 anaconda-ks.cfg
-rw-r--r--  1 root root       3485 Aug 10  2013 cloud-set-guest-password
-rw-r--r--  1 root root   13870692 Jan 14 14:02 helm-v3.7.2-linux-amd64.tar.gz
-rw-r--r--  1 root root    9080681 Jan 14 14:55 kubectl-df-pv_v0.3.0_linux_amd64.tar.gz
-rw-r--r--  1 root root    1870468 Jan 14 14:45 kubectl-example_1.1.0_Linux_x86_64.tar.gz
-rw-r--r--  1 1001 docker    11343 Jul 27 22:05 LICENSE
drwxr-xr-x  2 3434   3434     4096 Dec  9 04:31 linux-amd64
-rw-r--r--  1 root root       1021 Jan 14 09:45 ncc-sysconfig-ing.yaml
-rw-r--r--  1 root root      38340 Jan 17 11:32 nginx-9.7.1.tgz
-rw-r--r--  1 root root      48556 Jan 13 16:47 rabbitmq-8.26.1.tgz
-rw-r--r--  1 1001 docker     6154 Jul 27 22:05 README.md
-rwxr-xr-x  1 root root       4220 Dec  6 15:33 update_image
[root@nccztsjb-node-11 ~]# ls -l nginx-9.7.1.tgz 
-rw-r--r-- 1 root root 38340 Jan 17 11:32 nginx-9.7.1.tgz

這樣就把nginx的char下載到了本地。其中9.7.1是chart的版本信息。

4.7、查看本地chart包

對於本地的chart來說就是一個目錄

[root@nccztsjb-node-11 ~]# tar -zxf nginx-9.7.1.tgz 
[root@nccztsjb-node-11 ~]# ls -ltr
total 60
-rw-r--r--  1 root root  3485 Aug 10  2013 cloud-set-guest-password
-rw-------. 1 root root  1396 Jun 22  2018 anaconda-ks.cfg
-rwxr-xr-x  1 root root  4220 Dec  6 15:33 update_image
-rw-r--r--  1 root root 38340 Jan 17 11:32 nginx-9.7.1.tgz
drwxr-xr-x  5 root root  4096 Jan 17 11:35 nginx

chart目錄結構如下:

[root@nccztsjb-node-11 ~]# tree nginx
nginx
├── Chart.lock
├── charts
│   └── common
│       ├── Chart.yaml
│       ├── README.md
│       ├── templates
│       │   ├── _affinities.tpl
│       │   ├── _capabilities.tpl
│       │   ├── _errors.tpl
│       │   ├── _images.tpl
│       │   ├── _ingress.tpl
│       │   ├── _labels.tpl
│       │   ├── _names.tpl
│       │   ├── _secrets.tpl
│       │   ├── _storage.tpl
│       │   ├── _tplvalues.tpl
│       │   ├── _utils.tpl
│       │   ├── validations
│       │   │   ├── _cassandra.tpl
│       │   │   ├── _mariadb.tpl
│       │   │   ├── _mongodb.tpl
│       │   │   ├── _postgresql.tpl
│       │   │   ├── _redis.tpl
│       │   │   └── _validations.tpl
│       │   └── _warnings.tpl
│       └── values.yaml
├── Chart.yaml
├── ci
│   ├── ct-values.yaml
│   └── values-with-ingress-metrics-and-serverblock.yaml
├── README.md
├── templates
│   ├── deployment.yaml
│   ├── extra-list.yaml
│   ├── health-ingress.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── ldap-daemon-secrets.yaml
│   ├── NOTES.txt
│   ├── pdb.yaml
│   ├── prometheusrules.yaml
│   ├── server-block-configmap.yaml
│   ├── serviceaccount.yaml
│   ├── servicemonitor.yaml
│   ├── svc.yaml
│   └── tls-secrets.yaml
├── values.schema.json
└── values.yaml

6 directories, 43 files
[root@nccztsjb-node-11 ~]# 

主要的目錄結構

[root@nccztsjb-node-11 ~]# tree -L 1 nginx
nginx
├── Chart.lock
├── charts
├── Chart.yaml
├── ci
├── README.md
├── templates
├── values.schema.json
└── values.yaml

3 directories, 5 files
[root@nccztsjb-node-11 ~]# 

4.8、chart安裝時使用自定義的配置

在安裝chart的時候,如果不進行指定,那么就會使用默認的配置,比如,默認的鏡像版本,鏡像倉庫,啟動命令等等,這個可以根據實際的需要進行修改。

安裝時使用自定義的配置的方式有2種,一種是在命令行中使用--set命令,一種是使用yaml配置文件。

4.8.1、--set的方式

--set 后面設置key1=value1,key2=value2,多個值之間用逗號隔開,再有,層級關系的用.(點)間隔

helm install helm-nginx bitnami/nginx --set resources.limits.cpu=2,resources.limits.memory=2048Mi,resources.requests.cpu=1,resources.requests.memory=1024Mi

安裝后,查看具體的pod的資源情況

[root@nccztsjb-node-11 ~]# kubectl get deployment helm-nginx -o yaml | grep -A 6 resources
                f:resources:
                  .: {}
                  f:limits:
                    .: {}
                    f:cpu: {}
                    f:memory: {}
                  f:requests:
--
        resources:
          limits:
            cpu: "2"
            memory: 2Gi
          requests:
            cpu: "1"
            memory: 1Gi
[root@nccztsjb-node-11 ~]#

可以看到resources部分,cpu和memory的值都已經被修改了。

如果不加任何的參數,默認的值:

[root@nccztsjb-node-11 ~]# kubectl get deployment helm-nginx -o yaml | grep -A 6 resources
                f:resources: {}
                f:terminationMessagePath: {}
                f:terminationMessagePolicy: {}
            f:dnsPolicy: {}
            f:restartPolicy: {}
            f:schedulerName: {}
            f:securityContext: {}
--
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
[root@nccztsjb-node-11 ~]# 

默認是 resources: {}即沒有資源的限制。

具體的怎么查找值,可以參考4.5中values的部分

resources:
  ## Example:
  ## limits:
  ##    cpu: 100m
  ##    memory: 128Mi
  limits: {}
  ## Examples:
  ## requests:
  ##    cpu: 100m
  ##    memory: 128Mi
  requests: {}
## NGINX containers' liveness probe.
## ref: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes
## @param livenessProbe.enabled Enable livenessProbe
## @param livenessProbe.initialDelaySeconds Initial delay seconds for livenessProbe
## @param livenessProbe.periodSeconds Period seconds for livenessProbe
## @param livenessProbe.timeoutSeconds Timeout seconds for livenessProbe
## @param livenessProbe.failureThreshold Failure threshold for livenessProbe
## @param livenessProbe.successThreshold Success threshold for livenessProbe
##

OK,以上就是通過--set在命令行中以key-value的方式進行設置。

4.8.2、-f config.yaml的方式

另外的一種方式就是通過yaml配置文件來進行設置,可以指定多個yaml文件

將配置都寫入到yaml文件中去

resource.yaml(對資源配置的修改)

[root@nccztsjb-node-11 ~]# cat resource.yaml 
resources:
  limits:
    cpu: 2
    memory: 2048Mi
  requests:
    cpu: 1
    memory: 1024Mi

健康檢查配置調整

[root@nccztsjb-node-11 ~]# cat healthCheck.yaml 
livenessProbe:
  enabled: true
  initialDelaySeconds: 20
  timeoutSeconds: 5
  periodSeconds: 10
  failureThreshold: 6
  successThreshold: 1
readinessProbe:
  enabled: true
  initialDelaySeconds: 15
  timeoutSeconds: 3
  periodSeconds: 5
  failureThreshold: 3
  successThreshold: 1
[root@nccztsjb-node-11 ~]# 

通過以上的配置文件進行安裝

helm install helm-nginx bitnami/nginx -f resource.yaml -f healthCheck.yaml

查看安裝之后的配置

[root@nccztsjb-node-11 ~]# kubectl get deployment helm-nginx -o yaml | grep -A 6 resources
                f:resources:
                  .: {}
                  f:limits:
                    .: {}
                    f:cpu: {}
                    f:memory: {}
                  f:requests:
--
        resources:
          limits:
            cpu: "2"
            memory: 2Gi
          requests:
            cpu: "1"
            memory: 1Gi
[root@nccztsjb-node-11 ~]# 
[root@nccztsjb-node-11 ~]# kubectl get deployment helm-nginx -o yaml | grep -A 6 livenessProbe
                f:livenessProbe:
                  .: {}
                  f:failureThreshold: {}
                  f:periodSeconds: {}
                  f:successThreshold: {}
                  f:tcpSocket:
                    .: {}
--
        livenessProbe:
          failureThreshold: 6
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: http
          timeoutSeconds: 5
[root@nccztsjb-node-11 ~]# kubectl get deployment helm-nginx -o yaml | grep -A 6 readinessProbe
                f:readinessProbe:
                  .: {}
                  f:failureThreshold: {}
                  f:initialDelaySeconds: {}
                  f:periodSeconds: {}
                  f:successThreshold: {}
                  f:tcpSocket:
--
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 15
          periodSeconds: 5
          successThreshold: 1
          tcpSocket:
            port: http
[root@nccztsjb-node-11 ~]# 

通過以上的輸出可以看到,通過在安裝時指定配置文件已經將pod的資源、健康檢查的配置進行了調整為非默認值。

4.9、創建本地的Chart倉庫

以上的chart都是通過遠程倉庫的URL中拉取使用的,那么對於helm來說,也可以使用類似於yum建立本地倉庫源一樣建立chart的自定義的本地倉庫。

方法如下。

4.9.1、部署nginx或者apache服務

此處的目的就是將倉庫暴露為http的服務。本例使用nginx作為映射,具體部署的過程不贅述。

4.9.2、映射目錄

該目錄將作為chart的本地目錄

        location / {
          alias /data/;
          autoindex on;
          autoindex_format html;
          autoindex_exact_size off;
          autoindex_localtime on;
        }

4.9.3、上傳chart包

將4.6中的chart包,或者自己修改的chart包上傳到該目錄中

[root@node142 repo]# pwd
/data/kubernetes/helm/repo
[root@node142 repo]# ls -l
total 44
-rw-r--r-- 1 root root 38340 Jan 17 11:32 nginx-9.7.1.tgz
-rw-r--r-- 1 root root  4011 Jan 13 16:22 tomcat-0.4.0.tgz
[root@node142 repo]# 

4.9.4、創建索引文件

為了能夠通過目錄找到需要的chart包就需要一個索引文件,通過helm命令創建這個索引文件(注意:文件服務器中也需要安裝helm命令)

創建索引文件:

helm repo index /data/kubernetes/helm/repo/ --url http://172.20.56.117/kubernetes/helm/repo

最后面的是具體http訪問的url,前面是要在哪個目錄中創建索引文件

[root@node142 repo]# helm repo index /data/kubernetes/helm/repo/ --url http://172.20.56.117/kubernetes/helm/repo
[root@node142 repo]# ls
index.yaml  nginx-9.7.1.tgz  tomcat-0.4.0.tgz
[root@node142 repo]# 

OK,索引文件已經創建完成。

4.9.4、使用本地倉庫部署chart

將剛建立的倉庫添加到helm中

[root@nccztsjb-node-11 ~]# helm repo add local http://172.20.56.117/kubernetes/helm/repo
"local" has been added to your repositories
[root@nccztsjb-node-11 ~]# helm repo list
NAME 	URL                                      
local	http://172.20.56.117/kubernetes/helm/repo
[root@nccztsjb-node-11 ~]# 

本地倉庫中查找chart

[root@nccztsjb-node-11 ~]# helm search repo nginx
NAME       	CHART VERSION	APP VERSION	DESCRIPTION               
local/nginx	9.7.1        	1.21.5     	Chart for the nginx server
[root@nccztsjb-node-11 ~]# 

使用本地的倉庫進行安裝

[root@nccztsjb-node-11 ~]# helm install local-nginx local/nginx
NAME: local-nginx
LAST DEPLOYED: Mon Jan 17 14:13:29 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 9.7.1
APP VERSION: 1.21.5

** Please be patient while the chart is being deployed **

NGINX can be accessed through the following DNS name from within your cluster:

    local-nginx.default.svc.cluster.local (port 80)

To access NGINX from outside the cluster, follow the steps below:

1. Get the NGINX URL by running these commands:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace default -w local-nginx'

    export SERVICE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].port}" services local-nginx)
    export SERVICE_IP=$(kubectl get svc --namespace default local-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    echo "http://${SERVICE_IP}:${SERVICE_PORT}"
[root@nccztsjb-node-11 ~]# 

OK,已經可以獲取到chart包進行安裝。

以上就是helm的安裝和基本使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM