kubernetes實戰篇之helm使用技巧

系列目錄html

使用壓縮包安裝chart

咱們使用helm package打包的時候,默認會在當前位置生成一個tgz壓縮包,而後helm把它複製到到$HOME/.helm/repository目錄下,如今尚未講解helm私服搭建,使用的是helm自帶的一個本地服務器,有時候這個服務器不能正常工做,可是咱們又想體驗功能,這時候可使用本地chart壓縮包來安裝node

我在當前目錄下有如下文件nginx

[centos@k8s-master helm]$ ls
get_helm.sh  mychart  mychart-0.1.0.tgz  mychart-0.2.0.tgz
[centos@k8s-master helm]$ helm install --name tylertest mychart-0.1.0.tgz
NAME:   tylertest
LAST DEPLOYED: Thu Apr 25 09:37:32 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Deployment
NAME               READY  UP-TO-DATE  AVAILABLE  AGE
tylertest-mychart  0/1    1           0          0s

==> v1/Pod(related)
NAME                                READY  STATUS             RESTARTS  AGE
tylertest-mychart-545479dd4b-wbdm4  0/1    ContainerCreating  0         0s

==> v1/Service
NAME               TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)  AGE
tylertest-mychart  ClusterIP  10.107.107.190  <none>       80/TCP   0s


NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=tylertest" -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

而且經過helm list能夠看到它了json

[centos@k8s-master helm]$ helm list
NAME                    REVISION        UPDATED                         STATUS          CHART                           APP VERSION     NAMESPACE
kubernetes-dashboard    1               Thu Mar 14 09:06:25 2019        DEPLOYED        kubernetes-dashboard-1.2.0      1.10.1          kube-system
tylertest               1               Thu Apr 25 09:37:32 2019        DEPLOYED        mychart-0.1.0                   1.0             default

固然也能夠進入到$HOME/.helm/repository來安裝它centos

安裝的時候指定不一樣的環境

咱們在安裝一個chart的時候,因爲環境不一樣,配置可能也會有差異,咱們知道,chart的配置都存在values.yaml文件裏,咱們能夠編寫多個存儲配置的yaml文件,在安裝的時候經過-f指定 不一樣環境的values文件api

[centos@k8s-master helm]$ helm install --name=tylertest -f mychart/values.yaml mychart
NAME:   tylertest
LAST DEPLOYED: Thu Apr 25 09:44:00 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Deployment
NAME               READY  UP-TO-DATE  AVAILABLE  AGE
tylertest-mychart  0/1    0           0          0s

==> v1/Pod(related)
NAME                                READY  STATUS   RESTARTS  AGE
tylertest-mychart-545479dd4b-m5wj5  0/1    Pending  0         0s

==> v1/Service
NAME               TYPE       CLUSTER-IP     EXTERNAL-IP  PORT(S)  AGE
tylertest-mychart  ClusterIP  10.108.206.51  <none>       80/TCP   0s


NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=tylertest" -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

注意,values.yaml是安裝時缺省狀況下使用的文件,咱們能夠能夠把它複製若干(名字能夠是任意名稱,只須要在install的時候經過-f指定它的位置),根據不一樣環境進行修改.bash

這種方法一樣也能夠解決另一個問題,那就是不少公共倉庫的chart依賴gcr.io上的鏡像致使國內沒法安裝成功,若是鏡像地址作成了參數,則咱們能夠修改,若是沒有作成參數,咱們仍然有辦法,那就是先經過helm fetch把它拉到本地,而後解壓,而後修改其鏡像文件從新打包,經過上面講到過的使用本地壓縮包安裝的方式來安裝.服務器

讓 Helm 鏈接到指定其它Kubernetes集羣

Helm 默認使用和 kubectl 命令相同的配置訪問 Kubernetes 集羣,其配置默認在 ~/.kube/config 中。app

部署時指定名稱空間

咱們前面的示例都默認是在的名稱空間,實際生產環境中每每都不是按這種默認設置,而是安裝在指定的名稱空間中.若是想部署到指定的命令空間,執行helm install命令時 能夠加上 --namespace 參數,好比:ide

helm install local/mychart --name tylertest --namespace mynamespace

查看某一部署應用的詳細信息

其它跟kubectl get相似,helm也有get命令,能夠經過它來查看部署應用詳細信息

[centos@k8s-master helm]$ helm get tylertest
REVISION: 1
RELEASED: Thu Apr 25 09:44:00 2019
CHART: mychart-0.2.0
USER-SUPPLIED VALUES:
affinity: {}
fullnameOverride: ""
image:
  pullPolicy: IfNotPresent
  repository: nginx
  tag: stable
ingress:
  annotations: {}
  enabled: false
  hosts:
  - host: chart-example.local
    paths: []
  tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
  port: 80
  type: ClusterIP
tolerations: []

COMPUTED VALUES:
affinity: {}
fullnameOverride: ""
image:
  pullPolicy: IfNotPresent
  repository: nginx
  tag: stable
ingress:
  annotations: {}
  enabled: false
  hosts:
  - host: chart-example.local
    paths: []
  tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
  port: 80
  type: ClusterIP
tolerations: []

HOOKS:
---
# tylertest-mychart-test-connection
apiVersion: v1
kind: Pod
metadata:
  name: "tylertest-mychart-test-connection"
  labels:
    app.kubernetes.io/name: mychart
    helm.sh/chart: mychart-0.2.0
    app.kubernetes.io/instance: tylertest
    app.kubernetes.io/managed-by: Tiller
  annotations:
    "helm.sh/hook": test-success
spec:
  containers:
    - name: wget
      image: busybox
      command: ['wget']
      args:  ['tylertest-mychart:80']
  restartPolicy: Never
MANIFEST:

---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: tylertest-mychart
  labels:
    app.kubernetes.io/name: mychart
    helm.sh/chart: mychart-0.2.0
    app.kubernetes.io/instance: tylertest
    app.kubernetes.io/managed-by: Tiller
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: mychart
    app.kubernetes.io/instance: tylertest
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tylertest-mychart
  labels:
    app.kubernetes.io/name: mychart
    helm.sh/chart: mychart-0.2.0
    app.kubernetes.io/instance: tylertest
    app.kubernetes.io/managed-by: Tiller
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: mychart
      app.kubernetes.io/instance: tylertest
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mychart
        app.kubernetes.io/instance: tylertest
    spec:
      containers:
        - name: mychart
          image: "nginx:stable"
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {}

默認狀況下會顯示最新的版本的相關信息,若是想要查看指定發佈版本的信息可加上 --revision 參數。

$ helm get  --revision 1  tylertest

chart中定義依賴

能夠在要打包的chart目錄中建立一個requirements.yaml文件定義該chart的依賴

$ cat > ./mychart/requirements.yaml <<EOF
dependencies:
- name: mariadb
  version: 0.6.0
  repository: https://kubernetes-charts.storage.googleapis.com
EOF

經過helm命令更新和下載cahrt的依賴

helm dep update ./mychart

在次安裝運行chart時會把依賴中定義的chart運行起來

相關文章
相關標籤/搜索