zoukankan      html  css  js  c++  java
  • k8s中helm的使用

    什么是 Helm
    在没使用 helm 之前,向 kubernetes 部署应用,我们要依次部署 deployment、svc 等,步骤较繁琐。况且随
    着很多项目微服务化,复杂的应用在容器中部署以及管理显得较为复杂,helm 通过打包的方式,支持发布的版本
    管理和控制,很大程度上简化了 Kubernetes 应用的部署和管理
    Helm 本质就是让 K8s 的应用管理(Deployment,Service 等 ) 可配置,能动态生成。通过动态生成 K8s 资源清
    单文件(deployment.yaml,service.yaml)。然后调用 Kubectl 自动执行 K8s 资源部署
    Helm 是官方提供的类似于 YUM 的包管理器,是部署环境的流程封装。Helm 有两个重要的概念:chart 和
    release
    chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板、参数定义、依赖关系、文档说
    明等。chart 是应用部署的自包含逻辑单元。可以将 chart 想象成 apt、yum 中的软件安装包
    release 是 chart 的运行实例,代表了一个正在运行的应用。当 chart 被安装到 Kubernetes 集群,就生成
    一个 release。chart 能够多次安装到同一个集群,每次安装都是一个 release
    Helm 包含两个组件:Helm 客户端和 Tiller 服务器,如下图所示

    Helm 客户端负责 chart 和 release 的创建和管理以及和 Tiller 的交互。Tiller 服务器运行在 Kubernetes 集群
    中,它会处理 Helm 客户端的请求,与 Kubernetes API Server 交互

    Helm 部署
    越来越多的公司和团队开始使用 Helm 这个 Kubernetes 的包管理器,我们也将使用 Helm 安装 Kubernetes 的常用
    组件。 Helm 由客户端命 helm 令行工具和服务端 tiller 组成,Helm 的安装十分简单。 下载 helm 命令行工具到
    master 节点 node1 的 /usr/local/bin 下,这里下载的 2.15. 2版本:

    [root@k8s-master helm]# ll
    总用量 58188
    -rw-r--r-- 1 root root 22949819 1月   5 20:58 helm-v2.13.1-linux-amd64.tar.gz
    -rw-r--r-- 1 root root 24525846 1月   5 22:46 helm-v2.15.2-linux-amd64.tar.gz
    -rw-r--r-- 1 root root 12101232 1月   5 22:10 helm-v3.0.2-linux-amd64.tar.gz
    drwxr-xr-x 2 root root       64 10月 30 04:53 linux-amd64
    -rw-r--r-- 1 root root      354 1月   5 22:39 rbac.yaml
    drwxr-xr-x 3 root root       60 1月   6 17:44 test
    [root@k8s-master helm]#
    tar -zxvf helm-v2.15.2-linux-amd64.tar.gz
    cd linux-amd64/
    cp helm /usr/local/bin/
    [root@k8s-master helm]# cat rbac.yaml
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: tiller
      namespace: kube-system
    ---
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: tiller
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: cluster-admin
    subjects:
      - kind: ServiceAccount
        name: tiller
        namespace: kube-system
    [root@k8s-master helm]#

    为了安装服务端 tiller,还需要在这台机器上配置好 kubectl 工具和 kubeconfig 文件,确保 kubectl 工具可以
    在这台机器上访问 apiserver 且正常使用。 这里的 node1 节点以及配置好了 kubectl
    因为 Kubernetes APIServer 开启了 RBAC 访问控制,所以需要创建 tiller 使用的 service account: tiller 并分
    配合适的角色给它。 详细内容可以查看helm文档中的 Role-based Access Control。 这里简单起见直接分配
    cluster- admin 这个集群内置的 ClusterRole 给它。创建 rbac.yaml 文件.

    kubectl create -f rbac.yaml
    helm init --service-account tiller --skip-refresh

    https://www.cnblogs.com/dalianpai/p/12154410.html,初始化遇到问题,参照我这篇博客。

    tiller 默认被部署在 k8s 集群中的 kube-system 这个namespace 下

    [root@k8s-master helm]# kubectl get pod -n kube-system
    NAME                                 READY   STATUS    RESTARTS   AGE
    coredns-58cc8c89f4-9gn5g             1/1     Running   12         17d
    coredns-58cc8c89f4-xxzx7             1/1     Running   12         17d
    etcd-k8s-master                      1/1     Running   14         17d
    kube-apiserver-k8s-master            1/1     Running   14         17d
    kube-controller-manager-k8s-master   1/1     Running   24         17d
    kube-flannel-ds-amd64-4bc88          1/1     Running   16         17d
    kube-flannel-ds-amd64-lzwd6          1/1     Running   18         17d
    kube-flannel-ds-amd64-vw4vn          1/1     Running   16         17d
    kube-proxy-bs8sd                     1/1     Running   13         17d
    kube-proxy-nfvtt                     1/1     Running   12         17d
    kube-proxy-rn98b                     1/1     Running   14         17d
    kube-scheduler-k8s-master            1/1     Running   21         17d
    tiller-deploy-7476769959-pns9q       1/1     Running   1          21h
    [root@k8s-master helm]# helm version
    Client: &version.Version{SemVer:"v2.15.2", GitCommit:"8dce272473e5f2a7bf58ce79bb5c3691db54c96b", GitTreeState:"clean"}
    Server: &version.Version{SemVer:"v2.15.2", GitCommit:"8dce272473e5f2a7bf58ce79bb5c3691db54c96b", GitTreeState:"clean"}
    [root@k8s-master helm]#

    Helm 自定义模板

    [root@k8s-master helm]# mkdir test
    [root@k8s-master helm]# cd test/
    [root@k8s-master test]# ll
    总用量 0
    [root@k8s-master test]# cat <<'EOF' > ./Chart.yaml
    > name: hello-world
    > version: 1.0.0
    > EOF
    [root@k8s-master test]# ll
    总用量 4
    -rw-r--r-- 1 root root 33 1月   6 16:43 Chart.yaml
    [root@k8s-master test]# mkdir ./templates
    [root@k8s-master test]# ll
    总用量 4
    -rw-r--r-- 1 root root 33 1月   6 16:43 Chart.yaml
    drwxr-xr-x 2 root root  6 1月   6 16:44 templates
    [root@k8s-master helm]# cd test
    [root@k8s-master test]# ll
    总用量 8
    -rw-r--r-- 1 root root 33 1月   6 16:43 Chart.yaml
    drwxr-xr-x 2 root root 49 1月   6 17:44 templates
    -rw-r--r-- 1 root root 53 1月   6 17:29 values.yaml
    [root@k8s-master test]# cd templates
    [root@k8s-master templates]# ll
    总用量 8
    -rw-r--r-- 1 root root 407 1月   6 17:42 deployment.yaml
    -rw-r--r-- 1 root root 174 1月   6 16:53 service.yaml
    [root@k8s-master templates]# cat deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-world
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: hello-world
      template:
        metadata:
          labels:
            app: hello-world
        spec:
          containers:
            - name: hello-world
              image: wangyanglinux/myapp:v1
              ports:
               - containerPort: 80
                 protocol: TCP
    [root@k8s-master templates]# cat service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: hello-world
    spec:
      type: NodePort
      ports:
      - port: 80
        targetPort: 80
        protocol: TCP
      selector:
        app: hello-world
    [root@k8s-master templates]#
    root@k8s-master test]# helm install .
    NAME:   callous-duck
    LAST DEPLOYED: Mon Jan  6 16:57:05 2020
    NAMESPACE: default
    STATUS: DEPLOYED
    
    RESOURCES:
    ==> v1/Deployment
    NAME         READY  UP-TO-DATE  AVAILABLE  AGE
    hello-world  0/1    1           0          0s
    
    ==> v1/Pod(related)
    NAME                          READY  STATUS             RESTARTS  AGE
    hello-world-64f7589d8c-jthnw  0/1    ContainerCreating  0         0s
    
    ==> v1/Service
    NAME         TYPE      CLUSTER-IP    EXTERNAL-IP  PORT(S)       AGE
    hello-world  NodePort  10.104.65.50  <none>       80:30644/TCP  0s
    
    
    [root@k8s-master test]# helm list
    NAME            REVISION        UPDATED                         STATUS          CHART                   APP VERSION     NAMESPACE
    callous-duck    1               Mon Jan  6 16:57:05 2020        DEPLOYED        hello-world-1.0.0                       default
    [root@k8s-master test]# kubectl get pod
    NAME                           READY   STATUS    RESTARTS   AGE
    hello-world-64f7589d8c-jthnw   1/1     Running   0          97s
    [root@k8s-master test]# helm list
    NAME            REVISION        UPDATED                         STATUS          CHART                   APP VERSION     NAMESPACE
    callous-duck    1               Mon Jan  6 16:57:05 2020        DEPLOYED        hello-world-1.0.0                       default
    # 列出已经部署的 Release
    $ helm ls
    # 查询一个特定的 Release 的状态
    $ helm status RELEASE_NAME
    # 移除所有与这个 Release 相关的 Kubernetes 资源
    $ helm delete cautious-shrimp
    # helm rollback RELEASE_NAME REVISION_NUMBER
    $ helm rollback cautious-shrimp 1
    # 使用 helm delete --purge RELEASE_NAME 移除所有与指定 Release 相关的 Kubernetes 资源和所有这个
    Release 的记录
    $ helm delete --purge cautious-shrimp
    $ helm ls --deleted
    [root@k8s-master test]# cd templates/
    [root@k8s-master templates]# ll
    总用量 12
    -rw-r--r-- 1 root root 432 1月   6 17:25 deployment.yaml
    -rw-r--r-- 1 root root 174 1月   6 16:53 service.yaml
    -rw-r--r-- 1 root root  54 1月   6 17:26 values.yaml
    [root@k8s-master templates]# vim values.yaml
    [root@k8s-master templates]# vim deployment.yaml
    [root@k8s-master templates]# cd ..
    [root@k8s-master test]# ll
    总用量 4
    -rw-r--r-- 1 root root 33 1月   6 16:43 Chart.yaml
    drwxr-xr-x 2 root root 68 1月   6 17:31 templates
    [root@k8s-master test]# helm upgrade callous-duck .
    UPGRADE FAILED
    Error: render error in "hello-world/templates/deployment.yaml": template: hello-world/templates/deployment.yaml:17:27: executing "hello-world/templates/deployment.yaml" at <.Values.image.repository>: nil pointer evaluating interface {}.repository
    Error: UPGRADE FAILED: render error in "hello-world/templates/deployment.yaml": template: hello-world/templates/deployment.yaml:17:27: executing "hello-world/templates/deployment.yaml" at <.Values.image.repository>: nil pointer evaluating interface {}.repository
    [root@k8s-master test]# cat templates/values.yaml
    image:
      repository: wangyanglinux/myapp
      tag: 'v2'
    [root@k8s-master test]# cat templates/deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-world
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: hello-world
      template:
        metadata:
          labels:
            app: hello-world
        spec:
          containers:
            - name: hello-world
              image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
              ports:
               - containerPort: 80
                 protocol: TCP
    [root@k8s-master test]#  helm lint
    ==> Linting .
    [ERROR] Chart.yaml: directory name (test) and chart name (hello-world) must be the same
    [ERROR] Chart.yaml: apiVersion is required
    [INFO] Chart.yaml: icon is recommended
    [INFO] values.yaml: file does not exist
    [ERROR] templates/: render error in "hello-world/templates/deployment.yaml": template: hello-world/templates/deployment.yaml:17:27: executing "hello-world/templates/deployment.yaml" at <.Values.image.repository>: nil pointer evaluating interface {}.repository
    
    Error: 1 chart(s) linted, 1 chart(s) failed
    [root@k8s-master test]# cd templates/
    [root@k8s-master templates]# ll
    总用量 12
    -rw-r--r-- 1 root root 407 1月   6 17:31 deployment.yaml
    -rw-r--r-- 1 root root 174 1月   6 16:53 service.yaml
    -rw-r--r-- 1 root root  53 1月   6 17:29 values.yaml
    [root@k8s-master templates]# vim deployment.yaml
    [root@k8s-master templates]# mv values.yaml ../
    [root@k8s-master templates]# ll
    总用量 8
    -rw-r--r-- 1 root root 407 1月   6 17:42 deployment.yaml
    -rw-r--r-- 1 root root 174 1月   6 16:53 service.yaml
    [root@k8s-master templates]# cd ..
    [root@k8s-master test]# ll
    总用量 8
    -rw-r--r-- 1 root root 33 1月   6 16:43 Chart.yaml
    drwxr-xr-x 2 root root 49 1月   6 17:44 templates
    -rw-r--r-- 1 root root 53 1月   6 17:29 values.yaml
    [root@k8s-master test]# history
    [root@k8s-master test]# helm upgrade callous-duck .
    Release "callous-duck" has been upgraded.
    LAST DEPLOYED: Mon Jan  6 17:53:41 2020
    NAMESPACE: default
    STATUS: DEPLOYED
    
    RESOURCES:
    ==> v1/Deployment
    NAME         READY  UP-TO-DATE  AVAILABLE  AGE
    hello-world  1/1    0           1          56m
    
    ==> v1/Pod(related)
    NAME                          READY  STATUS             RESTARTS  AGE
    hello-world-64f7589d8c-jthnw  1/1    Running            0         56m
    hello-world-77cbb5cd7d-zljd7  0/1    ContainerCreating  0         1s
    
    ==> v1/Service
    NAME         TYPE      CLUSTER-IP    EXTERNAL-IP  PORT(S)       AGE
    hello-world  NodePort  10.104.65.50  <none>       80:30644/TCP  56m
    
    
    [root@k8s-master test]# helm upgrade callous-duck --set image.tag='v3' .
    Release "callous-duck" has been upgraded.
    LAST DEPLOYED: Mon Jan  6 17:54:56 2020
    NAMESPACE: default
    STATUS: DEPLOYED
    
    RESOURCES:
    ==> v1/Deployment
    NAME         READY  UP-TO-DATE  AVAILABLE  AGE
    hello-world  1/1    1           1          57m
    
    ==> v1/Pod(related)
    NAME                          READY  STATUS             RESTARTS  AGE
    hello-world-76d75cfc8f-7hq2j  0/1    ContainerCreating  0         0s
    hello-world-77cbb5cd7d-zljd7  1/1    Running            0         75s
    
    ==> v1/Service
    NAME         TYPE      CLUSTER-IP    EXTERNAL-IP  PORT(S)       AGE
    hello-world  NodePort  10.104.65.50  <none>       80:30644/TCP  57m
  • 相关阅读:
    20175311 2018-2019-2 《Java程序设计》第7周学习总结
    20175311胡济栋 2018-2019-2《Java程序设计》结对编程项目-四则运算 第一周 阶段性总结
    20175314 《信息安全系统设计基础》课程总结
    USCOSII
    改进ls的实现
    cat userlist
    实现ls
    stat命令的实现-mysate
    实现mypwd
    2019-2020-1 20175314_20175316 实验五 通讯协议设计
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12158679.html
Copyright © 2011-2022 走看看