helm3初体验

helm、kubernetes

安装

1: 配置kubectl认证文件
  Windows: $User/.kube/config
  Linux: $HOME/.kube/config
2: 下载二进制文件
  Windows: https://get.helm.sh/helm-v3.0.0-windows-amd64.zip
  Linux: https://get.helm.sh/helm-v3.0.0-linux-amd64.tar.gz
3: 解压拷贝到系统PATH路径下(helm2时代还需要初始化安装tiller-deploy作为服务端)
  Windows: C:\Windows\System32
  Linux: /usr/bin

help

The Kubernetes package manager

Common actions for Helm:

- helm search:    search for charts
- helm pull:      download a chart to your local directory to view
- helm install:   upload the chart to Kubernetes
- helm list:      list releases of charts

Environment variables:

+------------------+-----------------------------------------------------------------------------+
| Name             | Description                                                                 |
+------------------+-----------------------------------------------------------------------------+
| $XDG_CACHE_HOME  | set an alternative location for storing cached files.                       |
| $XDG_CONFIG_HOME | set an alternative location for storing Helm configuration.                 |
| $XDG_DATA_HOME   | set an alternative location for storing Helm data.                          |
| $HELM_DRIVER     | set the backend storage driver. Values are: configmap, secret, memory       |
| $HELM_NO_PLUGINS | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.                  |
| $KUBECONFIG      | set an alternative Kubernetes configuration file (default "~/.kube/config") |
+------------------+-----------------------------------------------------------------------------+

Helm stores configuration based on the XDG base directory specification, so

- cached files are stored in $XDG_CACHE_HOME/helm
- configuration is stored in $XDG_CONFIG_HOME/helm
- data is stored in $XDG_DATA_HOME/helm

By default, the default directories depend on the Operating System. The defaults are listed below:

+------------------+---------------------------+--------------------------------+-------------------------+
| Operating System | Cache Path                | Configuration Path             | Data Path               |
+------------------+---------------------------+--------------------------------+-------------------------+
| Linux            | $HOME/.cache/helm         | $HOME/.config/helm             | $HOME/.local/share/helm |
| macOS            | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm      |
| Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |
+------------------+---------------------------+--------------------------------+-------------------------+

Usage:
  helm [command]

Available Commands:
  completion  Generate autocompletions script for the specified shell (bash or zsh)
  create      create a new chart with the given name
  dependency  manage a chart's dependencies
  env         Helm client environment information
  get         download extended information of a named release
  help        Help about any command
  history     fetch release history
  install     install a chart
  lint        examines a chart for possible issues
  list        list releases
  package     package a chart directory into a chart archive
  plugin      install, list, or uninstall Helm plugins
  pull        download a chart from a repository and (optionally) unpack it in local directory
  repo        add, list, remove, update, and index chart repositories
  rollback    roll back a release to a previous revision
  search      search for a keyword in charts
  show        show information of a chart
  status      displays the status of the named release
  template    locally render templates
  test        run tests for a release
  uninstall   uninstall a release
  upgrade     upgrade a release
  verify      verify that a chart at the given path has been signed and is valid
  version     print the client version information

Flags:
      --add-dir-header                   If true, adds the file directory to the header
      --alsologtostderr                  log to standard error as well as files
      --debug                            enable verbose output
  -h, --help                             help for helm
      --kube-context string              name of the kubeconfig context to use
      --kubeconfig string                path to the kubeconfig file
      --log-backtrace-at traceLocation   when logging hits line file:N, emit a stack trace (default :0)
      --log-dir string                   If non-empty, write log files in this directory
      --log-file string                  If non-empty, use this log file
      --log-file-max-size uint           Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
      --logtostderr                      log to standard error instead of files (default true)
  -n, --namespace string                 namespace scope for this request
      --registry-config string           path to the registry config file (default "C:\\Users\\wangw\\AppData\\Roaming\\helm\\registry.json")
      --repository-cache string          path to the file containing cached repository indexes (default "C:\\Users\\wangw\\AppData\\Local\\Temp\\helm\\repository")
      --repository-config string         path to the file containing repository names and URLs (default "C:\\Users\\wangw\\AppData\\Roaming\\helm\\repositories.yaml")
      --skip-headers                     If true, avoid header prefixes in the log messages
      --skip-log-headers                 If true, avoid headers when opening log files
      --stderrthreshold severity         logs at or above this threshold go to stderr (default 2)
  -v, --v Level                          number for the log level verbosity
      --vmodule moduleSpec               comma-separated list of pattern=N settings for file-filtered logging

Use "helm [command] --help" for more information about a command.

仓库

# 查看仓库,默认是没有任何仓库的
PS C:\Users\wangw\helm> helm repo list
Error: no repositories to show
# 添加仓库
PS C:\Users\wangw\helm> helm repo add stable https://kubernetes-charts.storage.googleapis.com/
"stable" has been added to your repositories
PS C:\Users\wangw\helm> helm repo list
NAME    URL
stable  https://kubernetes-charts.storage.googleapis.com/
# 更新仓库
PS C:\Users\wangw\helm> 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!⎈

部署app(以mysql为例)

# 搜索mysql,并没有出现mysql应用列表,下面的说明表示要选择hub或者repo
PS C:\Users\wangw\helm> helm search mysql

Search provides the ability to search for Helm charts in the various places
they can be stored including the Helm Hub and repositories you have added. Use
search subcommands to search different locations for charts.

Usage:
  helm search [command]

Available Commands:
  hub         search for charts in the Helm Hub or an instance of Monocular
  repo        search repositories for a keyword in charts

Flags:
  -h, --help   help for search
Global Flags:
...
# 在hub上搜索,相当于helm提供的一个公共仓库,类似docker hub
PS C:\Users\wangw\helm> helm search hub mysql
URL                                                     CHART VERSION   APP VERSION     DESCRIPTION
https://hub.helm.sh/charts/rimusz/gcloud-sqlproxy       0.19.1          1.16            Google Cloud SQL Proxy
https://hub.helm.sh/charts/bitnami/phpmyadmin           4.2.3           4.9.1           phpMyAdmin is an mysql administration frontend
https://hub.helm.sh/charts/bitnami/mariadb              7.0.1           10.3.20         Fast, reliable, scalable, and easy to use open-...
https://hub.helm.sh/charts/bitnami/mysql                6.5.1           8.0.18          Chart to create a Highly available MySQL cluster
https://hub.helm.sh/charts/bitnami/mariadb-galera       0.5.1           10.3.20         MariaDB Galera is a multi-master database clust...
https://hub.helm.sh/charts/bitnami/mariadb-cluster      1.0.1           10.2.14         Chart to create a Highly available MariaDB cluster
https://hub.helm.sh/charts/ibm-charts/ibm-maria...      1.1.2                           MariaDB is developed as open source software an...
https://hub.helm.sh/charts/ibm-charts/ibm-galer...      1.1.0                           Galera Cluster is a multi-master solution for M...
https://hub.helm.sh/charts/incubator/mysqlha            1.0.0           5.7.13          MySQL cluster with a single master and zero or ...
https://hub.helm.sh/charts/banzaicloud-stable/p...      0.2.4           v0.11.0         A Helm chart for prometheus mysql exporter with...
https://hub.helm.sh/charts/banzaicloud-stable/m...      0.1.0           0.2.0           A Helm chart for deploying the Oracle MySQL Ope...
https://hub.helm.sh/charts/banzaicloud-stable/tidb      0.0.2                           A TiDB Helm chart for Kubernetes
https://hub.helm.sh/charts/presslabs/mysql-cluster      0.1.2           1.0             A Helm chart for easy deployment of a MySQL clu...
https://hub.helm.sh/charts/presslabs/mysql-oper...      0.3.4           v0.3.4          A Helm chart for mysql operator
https://hub.helm.sh/charts/presslabs/orchestrator       0.1.7           3.0.14          A Helm chart for github's mysql orchestrator
https://hub.helm.sh/charts/kanister/kanister-mysql      0.22.0          5.7.14          MySQL w/ Kanister support based on stable/mysql
https://hub.helm.sh/charts/appscode/stash-mysql         8.0.14          8.0.14          stash-mysql - MySQL database backup and restore...
https://hub.helm.sh/charts/t3n/mysql-backup             1.1.0
https://hub.helm.sh/charts/t3n/cloudsql-proxy           1.0.0           1.15            Google Cloud SQL Proxy
https://hub.helm.sh/charts/stable/percona-xtrad...      1.0.3           5.7.19          free, fully compatible, enhanced, open source d...
https://hub.helm.sh/charts/stable/sapho                 0.2.2                           A micro application development and integration...
https://hub.helm.sh/charts/stable/mysqldump             2.6.0           2.4.1           A Helm chart to help backup MySQL databases usi...
https://hub.helm.sh/charts/stable/prometheus-my...      0.5.2           v0.11.0         A Helm chart for prometheus mysql exporter with...
https://hub.helm.sh/charts/stable/mysql                 1.4.0           5.7.27          Fast, reliable, scalable, and easy to use open-...
https://hub.helm.sh/charts/stable/percona               1.2.0           5.7.17          free, fully compatible, enhanced, open source d...
https://hub.helm.sh/charts/choerodon/mysqld-exp...      0.1.0           1.0             A Helm chart for Kubernetes
https://hub.helm.sh/charts/choerodon/create-mys...      0.1.0           1.0             A Helm chart for Kubernetes
https://hub.helm.sh/charts/choerodon/mysql              0.1.1           0.1.1           A Helm chart for Kubernetes
https://hub.helm.sh/charts/choerodon/mysql-client       0.1.0           0.1.0           mysql Ver 15.1 Distrib 10.1.32-MariaDB, for Lin...
# 在repo上搜索,共有仓库之外,用户自己添加的helm仓库
PS C:\Users\wangw\helm> helm search repo mysql
NAME                                    CHART VERSION   APP VERSION     DESCRIPTION
stable/mysql                            1.4.0           5.7.27          Fast, reliable, scalable, and easy to use open-...
stable/mysqldump                        2.6.0           2.4.1           A Helm chart to help backup MySQL databases usi...
stable/prometheus-mysql-exporter        0.5.2           v0.11.0         A Helm chart for prometheus mysql exporter with...
stable/percona                          1.2.0           5.7.17          free, fully compatible, enhanced, open source d...
stable/percona-xtradb-cluster           1.0.3           5.7.19          free, fully compatible, enhanced, open source d...
stable/phpmyadmin                       4.2.3           4.9.1           phpMyAdmin is an mysql administration frontend
stable/gcloud-sqlproxy                  0.6.1           1.11            DEPRECATED Google Cloud SQL Proxy
stable/mariadb                          7.0.1           10.3.20         Fast, reliable, scalable, and easy to use open-...
# 部署stable/mysql
PS C:\Users\wangw\helm> helm install stable/mysql -n default
Error: must either provide a name or specify --generate-name
# 提示必须指定名称或者--generate-name自动生成(helm2时代不指定自动生成)
PS C:\Users\wangw\helm> helm install -name mysql stable/mysql -n default
NAME: mysql
LAST DEPLOYED: Mon Nov 18 14:47:09 2019
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
# 应用列表
PS C:\Users\wangw\helm> helm list
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mysql   default         1               2019-11-18 14:47:09.0835497 +0800 CST   deployed        mysql-1.4.0     5.7.27
# 查看应用当前状态
PS C:\Users\wangw\helm> helm status mysql
NAME: mysql
LAST DEPLOYED: Mon Nov 18 14:47:09 2019
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/mysql 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
# kubectl查看资源
PS C:\Users\wangw\helm> kubectl get all -n default
NAME                         READY   STATUS    RESTARTS   AGE
pod/mysql-57f6d6fbbb-pbrn8   1/1     Running   0          5m27s


NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    3h10m
service/mysql        ClusterIP   10.110.118.69   <none>        3306/TCP   5m27s


NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysql   1/1     1            1           5m27s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/mysql-57f6d6fbbb   1         1         1       5m27s

删除

PS C:\Users\wangw\helm> helm uninstall mysql
release "mysql" uninstalled
# 无需指定--purge选项,默认彻底删除
PS C:\Users\wangw\helm> helm list --all
NAME    NAMESPACE       REVISION        UPDATED STATUS  CHART   APP VERSION

拉取Chart

# 从helm hub
PS C:\Users\wangw\helm> helm pull https://hub.helm.sh/charts/incubator/mysqlha
# 从添加的仓库
PS C:\Users\wangw\helm> helm pull stable/mysql
PS C:\Users\wangw\helm> ls
    目录: C:\Users\wangw\helm

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/11/18     14:56          10657 mysql-1.4.0.tgz
-a----       2019/11/18     14:55           3299 mysqlha

创建模板

PS C:\Users\wangw\helm> helm create gistack
Creating gistack
PS C:\Users\wangw\helm> ls
    目录: C:\Users\wangw\helm
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2019/11/18     14:57                gistack
-a----       2019/11/18     14:56          10657 mysql-1.4.0.tgz
-a----       2019/11/18     14:55           3299 mysqlha

PS C:\Users\wangw\helm> ls .\gistack\
    目录: C:\Users\wangw\helm\gistack
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2019/11/18     14:57                charts
d-----       2019/11/18     14:57                templates
-a----       2019/11/18     14:57            342 .helmignore
-a----       2019/11/18     14:57            905 Chart.yaml
-a----       2019/11/18     14:57           1490 values.yaml

转化Chart为kubernetes yaml

PS C:\Users\wangw\helm> helm template stable/mysql
---
# Source: mysql/templates/secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: RELEASE-NAME-mysql
  namespace: default
  labels:
    app: RELEASE-NAME-mysql
    chart: "mysql-1.4.0"
    release: "RELEASE-NAME"
    heritage: "Helm"
type: Opaque
data:

  mysql-root-password: "ajgwdGVtTUwydw=="


  mysql-password: "MXI3eEVVQ0w2NQ=="
---
# Source: mysql/templates/tests/test-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: RELEASE-NAME-mysql-test
  namespace: default
  labels:
    app: RELEASE-NAME-mysql
    chart: "mysql-1.4.0"
    heritage: "Helm"
    release: "RELEASE-NAME"
data:
  run.sh: |-
---
# Source: mysql/templates/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: RELEASE-NAME-mysql
  namespace: default
  labels:
    app: RELEASE-NAME-mysql
    chart: "mysql-1.4.0"
    release: "RELEASE-NAME"
    heritage: "Helm"
spec:
  accessModes:
    - "ReadWriteOnce"
  resources:
    requests:
      storage: "8Gi"
---
# Source: mysql/templates/svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: RELEASE-NAME-mysql
  namespace: default
  labels:
    app: RELEASE-NAME-mysql
    chart: "mysql-1.4.0"
    release: "RELEASE-NAME"
    heritage: "Helm"
  annotations:
spec:
  type: ClusterIP
  ports:
  - name: mysql
    port: 3306
    targetPort: mysql
  selector:
    app: RELEASE-NAME-mysql
---
# Source: mysql/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: RELEASE-NAME-mysql
  namespace: default
  labels:
    app: RELEASE-NAME-mysql
    chart: "mysql-1.4.0"
    release: "RELEASE-NAME"
    heritage: "Helm"

spec:
  selector:
    matchLabels:
      app: RELEASE-NAME-mysql
      release: RELEASE-NAME
  template:
    metadata:
      labels:
        app: RELEASE-NAME-mysql
        release: RELEASE-NAME
    spec:
      initContainers:
      - name: "remove-lost-found"
        image: "busybox:1.29.3"
        imagePullPolicy: "IfNotPresent"
        resources:
          requests:
            cpu: 10m
            memory: 10Mi
        command:  ["rm", "-fr", "/var/lib/mysql/lost+found"]
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
      # - name: do-something
      #   image: busybox
      #   command: ['do', 'something']

      containers:
      - name: RELEASE-NAME-mysql
        image: "mysql:5.7.14"
        imagePullPolicy: "IfNotPresent"
        resources:
          requests:
            cpu: 100m
            memory: 256Mi
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: RELEASE-NAME-mysql
              key: mysql-root-password
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: RELEASE-NAME-mysql
              key: mysql-password
              optional: true
        - name: MYSQL_USER
          value: ""
        - name: MYSQL_DATABASE
          value: ""
        ports:
        - name: mysql
          containerPort: 3306
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 1
          successThreshold: 1
          failureThreshold: 3
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
        # - name: extras
        #   mountPath: /usr/share/extras
        #   readOnly: true

      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: RELEASE-NAME-mysql
      # - name: extras
      #   emptyDir: {}
---
# Source: mysql/templates/tests/test.yaml
apiVersion: v1
kind: Pod
metadata:
  name: RELEASE-NAME-mysql-test
  namespace: default
  labels:
    app: RELEASE-NAME-mysql
    chart: "mysql-1.4.0"
    heritage: "Helm"
    release: "RELEASE-NAME"
  annotations:
    "helm.sh/hook": test-success
spec:
  initContainers:
    - name: test-framework
      image: "dduportal/bats:0.4.0"
      command:
      - "bash"
      - "-c"
      - |
        set -ex
        # copy bats to tools dir
        cp -R /usr/local/libexec/ /tools/bats/
      volumeMounts:
      - mountPath: /tools
        name: tools
  containers:
    - name: RELEASE-NAME-test
      image: "mysql:5.7.14"
      command: ["/tools/bats/bats", "-t", "/tests/run.sh"]
      volumeMounts:
      - mountPath: /tests
        name: tests
        readOnly: true
      - mountPath: /tools
        name: tools
  volumes:
  - name: tests
    configMap:
      name: RELEASE-NAME-mysql-test
  - name: tools
    emptyDir: {}
  restartPolicy: Never
posted @ 2020-10-19 19:26  longtds  阅读(579)  评论(0编辑  收藏  举报