Helm安裝和項目使用


整體架構

 

1.為什么要用?

首先在原來項目中都是基於yaml文件來進行部署發布的,而目前項目大部分微服務化或者模塊化,會分成很多個組件來部署,每個組件可能對應一個deployment.yaml,一個service.yaml,一個Ingress.yaml還可能存在各種依賴關系,這樣一個項目如果有5個組件,很可能就有15個不同的yaml文件,這些yaml分散存放,如果某天進行項目恢復的話,很難知道部署順序,依賴關系等,而所有這些包括

  • 基於yaml配置的集中存放
  • 基於項目的打包
  • 組件間的依賴

都可以通過helm來進行解決。

 

2.Helm 基本概念

Helm 可以理解為 Kubernetes 的包管理工具,可以方便地發現、共享和使用為Kubernetes構建的應用,它包含幾個基本概念

  • Chart:一個 Helm 包,其中包含了運行一個應用所需要的鏡像、依賴和資源定義等,還可能包含 Kubernetes 集群中的服務定義
  • Release: 在 Kubernetes 集群上運行的 Chart 的一個實例。在同一個集群上,一個 Chart 可以安裝很多次。每次安裝都會創建一個新的 release。例如一個 MySQL Chart,如果想在服務器上運行兩個數據庫,就可以把這個 Chart 安裝兩次。每次安裝都會生成自己的 Release,會有自己的 Release 名稱。
  • Repository:用於發布和存儲 Chart 的倉庫。

 

3.Helm 組件及架構

Helm 采用客戶端/服務器架構,有如下組件組成:

  • Helm CLI 是 Helm 客戶端,可以在本地執行
  • Tiller 是服務器端組件,在 Kubernetes 群集上運行,並管理 Kubernetes 應用程序的生命周期
  • Repository 是 Chart 倉庫,Helm客戶端通過HTTP協議來訪問倉庫中Chart的索引文件和壓縮包。

 

15019325767895

4.Helm的安裝

在下列網站下載helm的相關版本

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


tar -xvzf  $HELM.tar.gz 

mv linux-amd64/helm /usr/local/bin/helm

 在K8s節點上配置.kube/config(我是因為之前沒搞這些,大家如果搞過就直接忽略)

[root@k8s-node-1 ~]# kubectl config set-cluster kubernetes --server=http://192.168.0.104:8080
Cluster "kubernetes" set.


[root@k8s-node-1 ~]# kubectl config set-credentials admin
User "admin" set.

[root@k8s-node-1 ~]# kubectl config set-context kubernetes \
> --cluster=kubernetes \
> --user=admin
Context "kubernetes" set.


[root@k8s-node-1 ~]# kubectl config use-context kubernetes
Switched to context "kubernetes".

 

  • 運行安裝

 先創建用戶和角色。

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-node-1 ~]# helm init --service-account tiller --skip-refresh
$HELM_HOME has been configured at /root/.helm.

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.
Happy Helming!

然后去查看

[root@k8s-node-1 ~]# kubectl get pod -n kube-system -l app=helm
NAME                             READY     STATUS              RESTARTS   AGE
tiller-deploy-3018021184-dxpfj   0/1       ContainerCreating   0          26s
[root@k8s-node-1 ~]# kubectl describe pods tiller-deploy-3018021184-dxpfj -n kube-system
Name:        tiller-deploy-3018021184-dxpfj
Namespace:    kube-system
Node:        k8s-node-1/192.168.0.105
Start Time:    Fri, 23 Feb 2018 18:41:28 +0800
Labels:        app=helm
        name=tiller
        pod-template-hash=3018021184
Status:        Pending
IP:        
Controllers:    ReplicaSet/tiller-deploy-3018021184
Containers:
  tiller:
    Container ID:    
    Image:        gcr.io/kubernetes-helm/tiller:v2.8.1
    Image ID:        
    Ports:        44134/TCP, 44135/TCP
    State:        Waiting
      Reason:        ContainerCreating
    Ready:        False
    Restart Count:    0
    Liveness:        http-get http://:44135/liveness delay=1s timeout=1s period=10s #success=1 #failure=3
    Readiness:        http-get http://:44135/readiness delay=1s timeout=1s period=10s #success=1 #failure=3
    Volume Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from tiller-token-xng0h (ro)
    Environment Variables:
      TILLER_NAMESPACE:        kube-system
      TILLER_HISTORY_MAX:    0
Conditions:
  Type        Status
  Initialized     True 
  Ready     False 
  PodScheduled     True 
Volumes:
  tiller-token-xng0h:
    Type:    Secret (a volume populated by a Secret)
    SecretName:    tiller-token-xng0h
QoS Class:    BestEffort
Tolerations:    <none>
Events:
  FirstSeen    LastSeen    Count    From            SubObjectPath        Type        Reason        Message
  ---------    --------    -----    ----            -------------        --------    ------        -------
  1m        1m        1    {default-scheduler }                Normal        Scheduled    Successfully assigned tiller-deploy-3018021184-dxpfj to k8s-node-1
  1m        1m        1    {kubelet k8s-node-1}    spec.containers{tiller}    Normal        Pulling        pulling image "gcr.io/kubernetes-helm/tiller:v2.8.1"

發現需要gcr.io/kubernetes-helm/tiller:v2.8.1

一番折騰后運行后完成,因為找不到2.8.1版本,所以最后搞了個2.6.0版本

[root@k8s-master helm]# helm init --service-account tiller --upgrade -i index.tenxcloud.com/kubernetes-helm/tiller:v2.6.0  --skip-refresh
Creating /root/.helm 
Creating /root/.helm/repository 
Creating /root/.helm/repository/cache 
Creating /root/.helm/repository/local 
Creating /root/.helm/plugins 
Creating /root/.helm/starters 
Creating /root/.helm/cache/archive 
Creating /root/.helm/repository/repositories.yaml 
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com 
Adding local repo with URL: http://127.0.0.1:8879/charts 
$HELM_HOME has been configured at /root/.helm.

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.
Happy Helming!

 

  • 問題解決

先解決兩個錯誤:

  • unable to do port forwarding: socat not found.
[root@k8s-master hello-svc]# helm version
Client: &version.Version{SemVer:"v2.8.1", GitCommit:"6af75a8fd72e2aa18a2b278cfe5c7a1c5feca7f2", GitTreeState:"clean"}
E0224 14:13:16.077226    7416 portforward.go:331] an error occurred forwarding 37271 -> 44134: error forwarding port 44134 to pod 76a7312e49220a229e443546a4b32d3e0406f09fd9b3646b3d30f6833e121375, uid : unable to do port forwarding: socat not found.
Error: cannot connect to Tiller

解決辦法在node節點安裝socat

yum install socat

 

  • 版本不一致

重新下載一致的版本包,和images的版本保持一致

 

5.項目中如何使用

  • 針對每個項目形成一個chart,最后形成一個Chart Package

比如下面針對hello-svc這個基於tomcat的項目,先生成一個chart的結構

  • 創建chart及部署
[root@k8s-master ~]# helm create hello-svc
Creating hello-svc

 

按照我們自己的需求修改模板中的deployment.yaml,service.yaml和values.yaml文件

[root@k8s-master templates]# cat deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tomcatjmx
spec:
  replicas: {{.Values.replicas}}
  template:
    metadata:
      labels:
        tomcat-app: "tomcatjmx"
        version: "1"
    spec:
      containers:
      - name: tomcatjmx
        image: tomcat:{{.Values.images.dockerTag}}
        ports:
        - containerPort: {{.Values.images.Port}}
          name: tomcatport
        - containerPort: 35135
          name: jmx

 

[root@k8s-master templates]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: {{.Values.service.name}} 
  labels:
    tomcat-app: tomcatjmx
spec:
  ports:
  - port: {{.Values.service.Port}} 
    protocol: TCP
    targetPort: 8080
    name: http
  - name: jmx
    protocol: TCP
    port: 35135
    targetPort: {{.Values.service.targetPort}}
  type: NodePort
  selector:
    tomcat-app: tomcatjmx

 

[root@k8s-master hello-svc]# cat values.yaml 
# Default values for hello-svc.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicas: 1

images:
  dockerTag: jmxv4 
  Port: 8080

service:
  name: tomcatjmxsvc
  Port: 80
  targetPort: 35135

 

相應的NOTES.txt也進行調整直到驗證沒有問題,驗證完成通過install安裝

 

helm install --dry-run --debug ./
[root@k8s-master hello-svc]# helm install ./
NAME:   kindly-worm
LAST DEPLOYED: Sat Feb 24 14:45:58 2018
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Service
NAME          CLUSTER-IP     EXTERNAL-IP  PORT(S)                       AGE
tomcatjmxsvc  10.254.25.181  <nodes>      80:32733/TCP,35135:30714/TCP  1s

==> v1beta1/Deployment
NAME       DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE
tomcatjmx  1        1        1           0          1s


NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods -l "app=hello-svc,release=kindly-worm" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl port-forward $POD_NAME 8080:80

[root@k8s-master hello-svc]# helm list
NAME           REVISION    UPDATED                     STATUS      CHART              NAMESPACE
kindly-worm    1           Sat Feb 24 14:45:58 2018    DEPLOYED    hello-svc-0.1.0    default  

 

  • 形成一個chart Package

 打包形成一個tgz文件,估計是每個項目一個chart,對應一個tgz

helm package ./

 

  • Chart Package的集中管理和存放

上面我們是從本地的目錄結構中的chart去進行部署,如果要集中管理chart,就需要涉及到repository的問題,因為helm repository都是指到外面的地址,接下來我們可以通過minio建立一個企業私有的存放倉庫。

Minio提供對象存儲服務。它的應用場景被設定在了非結構化的數據的存儲之上了。眾所周知,非結構化對象諸如圖像/音頻/視頻/log文件/系統備份/鏡像文件…等等保存起來管理總是不那么方便,size變化很大,類型很多,再有雲端的結合會使得情況更加復雜,minio就是解決此種場景的一個解決方案。Minio號稱其能很好的適應非結構化的數據,支持AWS的S3,非結構化的文件從數KB到5TB都能很好的支持。

Minio的使用比較簡單,只有兩個文件,服務端minio,客戶訪問端mc,比較簡單。

在項目中,我們可以直接找一台虛擬機作為Minio Server,提供服務,當然minio也支持作為Pod部署。

1.安裝配置Minio

下載

在https://dl.minio.io/client/mc/release/linux-amd64/ 下載客戶端程序mc和服務端程序minio

啟動minio服務

minio server ./repository

 針對使用得客戶端加入security和token信息

./mc config host add myminio http://192.168.44.108:9000 B0SW3MZ00J7OCG2JCG5D nZrG2olrz+aDbhbzVhft6JivkkoPQe2gp5JaG+XO

創建一個bucket同時設置權限

mc mb myminio/minio-helm-repo
mc policy download myminio
/minio-helm-repo

  安裝完成后通過瀏覽器訪問minio的界面,http://192.168.44.108:9000/

這里寫圖片描述

 

2.將我們上面創建的chart Package傳入Minio的Repository

index.yaml為了讓helm對里面的包進行索引,找到各種entry,每個entry是一個項目,每個項目對應一個chart Package.

在本地建立一個index.yaml,然后上傳,將來在使用的時候可以通過程序實現自動添加。

[root@k8s-master minio-helm-repo]# cat index.yaml
apiVersion: v1
entries:
  hello-svc:
  - apiVersion: v1
    description: Distributed object storage server built for cloud applications and  devops.
    digest: 8440f6f064ed91a75194e77d4b2be99c491c1cb04fb34bca4a36a5be67e1ef2c
    name: hello-svc
    urls:
    - http://192.168.44.108:9000/minio-helm-repo/hello-svc-0.1.0.tgz
    version: 0.1.0

 將之前的tgz package和index文件都傳入。

./mc cp ./index.yaml myminio/minio-helm-repo
./index.yaml:       601 B / 601 B ┃▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┃ 100.00% 47.73 KB/s 0s

.
/mc cp /root/hello-svc/hello-svc-0.1.0.tgz myminio/minio-helm-repo ...-svc-0.1.0.tgz: 1.50 KB / 1.50 KB ┃▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┃ 100.00% 48.00 KB/s 0s

 

3.設置Helm

在helm中加入repository,加入完成后可以通過helm repo list進行羅列。修改了index.yaml后也需要用helm repo update進行更新。

[root@k8s-master helm]# helm repo add myrepo http://192.168.44.108:9000/minio-helm-repo
"myrepo" has been added to your repositories

[root@k8s
-master helm]# helm repo update Hang tight while we grab the latest from your chart repositories... ...Skip local chart repository ...Unable to get an update from the "stable" chart repository (https://kubernetes-charts.storage.googleapis.com): Get https://kubernetes-charts.storage.googleapis.com/index.yaml: dial tcp: lookup kubernetes-charts.storage.googleapis.com on [::1]:53: read udp [::1]:59458->[::1]:53: read: connection refused ...Successfully got an update from the "myrepo" chart repository Update Complete. ⎈ Happy Helming!⎈

 

  • 基於Minio的Repository進行Helm的部署

好了,一切就緒,先search一下

[root@k8s-master helm]# helm search hello
WARNING: Repo "stable" is corrupt or missing. Try 'helm repo update'.NAME                VERSION    DESCRIPTION                                       
local/hello-svc     0.1.0      A Helm chart for Kubernetes                       
myrepo/hello-svc    0.1.0      Distributed object storage server built for clo...

[root@k8s
-master helm]# helm install myrepo/hello-svc NAME: quelling-hound LAST DEPLOYED: Sat Feb 24 17:14:23 2018 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/Service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE tomcatjmxsvc 10.254.180.188 <nodes> 80:31790/TCP,35135:31132/TCP 2s ==> v1beta1/Deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE tomcatjmx 1 1 1 0 2s NOTES: 1. Get the application URL by running these commands: export POD_NAME=$(kubectl get pods -l "app=hello-svc,release=quelling-hound" -o jsonpath="{.items[0].metadata.name}") echo "Visit http://127.0.0.1:8080 to use your application" kubectl port-forward $POD_NAME 8080:80

 這里search能搜索到但是部署不上去的問題折騰了一下,最后原因是那個Index.yaml文件寫得有問題。再度更新后成功部署。


免責聲明!

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



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