在Kubernetes集群上安装和使用Helm 3

时间:2020-02-23 14:31:40  来源:igfitidea点击:

欢迎使用有关如何在Kubernetes环境中安装和使用Helm 3的今天教程。 Helm是Kubernetes的最终包管理器。它可以通过使用Helm Charts管理Kubernetes应用程序。通过它,我们可以定义,安装和升级基本的最复杂的Kubernetes应用程序。

Helm 3没有像Helm 2那样的服务器/客户端体系结构。没有分till服务器组件。因此,该安装仅适用于通过kubectl配置文件和默认的Kubernetes RBAC与Kubernetes交互的helm命令行组件。

在Linux macOS上安装Helm 3

下载Helm 3安装脚本。

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3

授予脚本执行权限。

chmod 700 get_helm.sh

运行安装程序。

$./get_helm.sh

这是示例安装输出:

Downloading https://get.helm.sh/helm-v3.4.0-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm

确认安装helm 3

$helm version
version.BuildInfo{Version:"v3.4.0", GitCommit:"7090a89efc8a18f3d8178bf47d2462450349a004", GitTreeState:"clean", GoVersion:"go1.14.10"}

添加Helm Chart存储库

安装Helm后,添加图表存储库。加上官方Helm稳定图表:

$helm repo add stable https://kubernetes-charts.storage.googleapis.com/

"stable" has been added to your repositories

然后,我们将能够列出可以安装的图表:

$helm search repo stable

在Helm Chart上安装应用程序

首先列出可用的上下文,以确认Kubernetes CLI使用的群集上下文正确。

$kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO         NAMESPACE
*         k3s        k3s        k3s-admin        kube-system
          prod       prod       prod-admin

切换到所需的上下文:

$kubectl config use-context k3s
Switched to context "k3s".

我们将确认是否可以使用Helm 3在Kubernetes集群上安装应用程序。在这种设置下,我将安装Nginx入口控制器。 NGINX Ingress控制器可以从官方Helm chart稳定版/nginx-ingress存储库中轻松安装。

$helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈ Happy Helming!⎈

获取图表功能:

$helm show chart stable/nginx-ingress

apiVersion: v1
appVersion: 0.26.1
description: An nginx Ingress controller that uses ConfigMap to store the nginx configuration.
home: https://github.com/kubernetes/ingress-nginx
icon: https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Nginx_logo.svg/500px-Nginx_logo.svg.png
keywords:
- ingress
- nginx
kubeVersion: '>=1.10.0-0'
maintainers:
- name: ChiefAlexander
- email: theitroad@localhost
  name: taharah
name: nginx-ingress
sources:
- https://github.com/kubernetes/ingress-nginx
version: 1.26.2

使用helm install命令安装图表。

$helm install nginx-ingress stable/nginx-ingress

我们也可以让安装程序生成图表发行版的名称(忽略NAME参数)。安装后,我们会收到如下消息。

NAME: nginx-ingress
LAST DEPLOYED: Mon Dec 16 00:08:12 2019
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The nginx-ingress controller has been installed.
It Jan take a few minutes for the LoadBalancer IP to be available.
You can watch the status by running 'kubectl --namespace kube-system get services -o wide -w nginx-ingress-controller'

An example Ingress that makes use of the controller:

  apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    annotations:
      kubernetes.io/ingress.class: nginx
    name: example
    namespace: foo
  spec:
    rules:
      - host: www.example.com
        http:
          paths:
            - backend:
                serviceName: exampleService
                servicePort: 80
              path: /
    # This section is only required if TLS is to be enabled for the Ingress
    tls:
        - hosts:
            - www.example.com
          secretName: example-tls

If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:

  apiVersion: v1
  kind: Secret
  metadata:
    name: example-tls
    namespace: foo
  data:
    tls.crt: <base64 encoded cert>
    tls.key: <base64 encoded key>
  type: kubernetes.io/tls

确认安装:

$helm ls
NAME         	NAMESPACE  	REVISION	UPDATED                                	STATUS  	CHART               	APP VERSION
nginx-ingress	kube-system	1       	2019-12-16 00:08:12.513438985 +0300 EAT	deployed	nginx-ingress-1.26.2	0.26.1

要卸载发行版,请使用" helm uninstall"命令:

$helm uninstall nginx-ingress
release "nginx-ingress" uninstalled

$helm list                   
NAME	NAMESPACE	REVISION	UPDATED	STATUS	CHART	APP VERSION

下面是使用Helm在Kubernetes上安装DokuWiki的示例。

$helm install dokuwiki stable/dokuwiki 
NAME: dokuwiki
LAST DEPLOYED: Mon Dec 23 13:32:52 2019
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
** Please be patient while the chart is being deployed **

1. Get the DokuWiki URL by running:

** Please ensure an external IP is associated to the dokuwiki service before proceeding **
** Watch the status using: kubectl get svc --namespace kube-system -w dokuwiki **

  export SERVICE_IP=$(kubectl get svc --namespace kube-system dokuwiki --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
  echo "URL: http://$SERVICE_IP/"

2. Login with the following credentials

  echo Username: user
  echo Password: $(kubectl get secret --namespace kube-system dokuwiki -o jsonpath="{.data.dokuwiki-password}" | base64 --decode)