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版本:

[[email protected] 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/
[[email protected] 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 下

[[email protected] 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 自定义模板

[[email protected] helm]# mkdir test
[[email protected] helm]# cd test/
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] 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]#
[email protected] 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
[[email protected] test]# cd templates/
[[email protected] 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
[[email protected] templates]# vim deployment.yaml
[[email protected] templates]# cd ..
[[email protected] 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/
[[email protected] 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
[[email protected] templates]# mv values.yaml ../
[[email protected] 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 ..
[[email protected] 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
[[email protected] 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

原文地址:https://www.cnblogs.com/dalianpai/p/12158679.html

时间: 2024-08-30 17:53:50

k8s中helm的使用的相关文章

k8s中helm安装部署,升级和回滚(chart,helm,tiller,StorageClass)

一.Helm介绍 helm是基于kubernetes 的包管理器.它之于 kubernetes 就如 yum 之于 centos,pip 之于 python,npm 之于 javascript 那 helm 的引入对于管理集群有哪些帮助呢? 更方便地部署基础设施,如 gitlab,postgres,prometheus,grafana 等 更方便地部署自己的应用,为公司内部的项目配置 Chart,使用 helm 结合 CI,在 k8s 中部署应用一行命令般简单 1.Helm用途 Helm把Kub

k8s中Helm安装使用(15)

概念:helm把一系列复杂的有状态和无状态服务的部署封装起来(实际上就是对yaml文件的组织),然后你可以暴露出一些自定义参数信息供用户选择,这样部署就会变得简单很多.有点类似ansible,salt的yaml文件差不多.Helm相当于kubernetes环境下的yum包管理工具 组件:Helm :是一个命令行下的客户端工具 Tiller: 是 Helm 的服务端,部署在 Kubernetes 集群中 Chart Helm :的软件包,类似YUM 的 RPM 包 Repoistory Helm

【转】K8S中部署Helm

K8S中的包管理工具 1. 客户端Helm(即Helm)  通过脚本安装:curl https://raw.githubusercontent.com/helm/helm/master/scripts/get > helm.sh,赋权运行: 123456789101112 chmod +x helm.sh./helm.sh # 输出Downloading https://kubernetes-helm.storage.googleapis.com/helm-v2.13.1-linux-amd64

K8S中如何跨namespace 访问服务?为什么ping不通ClusterIP?

1.K8S中如何跨namespace 访问服务? 2.在Pod中为什么ping不通ClusterIP? 简述: ??????? Rancher2.0中的一个用户,在K8S环境中,创建两个namespace,对应用进行分割管理,在一个namespace的pod中,如何访问另一个namespace中的服务?--K8S使用kube-DNS实现服务发现功能的,可以通过DNS名称访问服务名. ??????? 在K8S中,部署一个带ClusterIP的服务,供集群内部网络访问.为什么这个ClusterIP无

查看k8s中etcd数据

#查看etcd pod kubectl get pod -n kube-system | grep etcd #进入etcd pod kubectl exec -it -n kube-system etcd-node1.com sh #设置etcdctl 使用的版本,k8s中使用v3版本export ETCDCTL_API=3 #查询etcd中所有的key etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernete

k8s中token过期重新生成

k8s中token过期重新生成 通过kubeadm初始化之后,都会提供node加入的token 默认的token的有效期是24小时,当过期了,如何新生成呢 重新生成token: [[email protected] ~]# kubeadm token create kk0ee6.nhvz5p85avmzyof3 [[email protected]-master ~]# kubeadm token list TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA G

k8s中部署基于nfs的StorageClass

k8s中部署基于nfs的StorageClass ? storageclass相当于是一个动态的存储,即每个pod需要多少容量,直接在配置资源清单中声明即可;但是nfs默认是不支持storageclass动态存储的. ? 总结一下就是: ? 1. 平时使用过程中,如果是静态的存储,那么过程是先准备好存储,然后基于存储创建PV;然后在创建PVC,根据容量他们会找对应的PV ? 2. 使用动态存储,那么就是先准备好存储,然后直接创建PVC,storageclass会根据要求的大小自动创建PV 首先安

K8S中的资源

一.什么是资源? K8S中所有的内容都抽象为资源,资源实例化之后,叫做对象 二.K8S中存在哪些资源? 1.工作负载型资源(workload) Pod , ReplicaSet , Deployment, StatefulSet, DaemonSet, Job, CronJob (ReplicationController 在v.11版本被废弃) 2.服务发现及负载均衡型资源( ServiceDiscovery  LoadBalance) Service, Ingress... 3.配置与存储型

9 个技巧,解决 K8s 中的日志输出问题

作者 | 元乙??阿里云存储服务技术专家 导读:近年来,越来越多的同学咨询如何为 Kubernetes 构建一个日志系统,或者是来求助在此过程中遇到一系列问题如何解决,授人以鱼不如授人以渔,于是作者想把这些年积累的经验以文章的形式发出来,让看到文章的同学少走弯路.K8s 日志系列文章内容偏向落地实操以及经验分享,且内容会随着技术的迭代而不定期更新,本文为该系列文章的第 3 篇. 第一篇:<6 个 K8s 日志系统建设中的典型问题,你遇到过几个?> 第二篇:<一文看懂 K8s 日志系统设计