Docker(https://www.docker.com)已经被大量使用,能够从dockerhub(https://www.dockerhub.com)或者私有的Harbor/Registry(http://www.javashuo.com/article/p-nlopvtnz-hb.html)来拉取镜像,快速建立一个虚拟的系统。但对于不少服务来讲,每每须要多个docker来共同完成,这能够经过docker compose文件,一次性启动多个docker container,能够运行于swarm模式或者Kubernetes模式,从而提供伸缩能力。mysql
Helm(https://docs.helm.sh/)是基于Kubernetes面向容器的包管理工具,包括客户端、服务端两个主要部分。Helm的包相似于docker compose文件(与Rancher的应用文件也基本相似)。Helm的包以一个目录方式存储,能够放在github.com的代码仓库中进行共享(如:https://github.com/openthings/zhelm-charts),也能够从已有的仓库服务中直接安装,https://kubernetes-charts.storage.googleapis.com/ 是Google维护的chart库,访问该地址能够看到全部的chart列表(须要科学访问),因为这个是Helm的默认主库,所以安装Helm也是要求可以访问这个地址的,不然helm init执行后运行时会异常。也能够搭建本身的Helm服务,而本地通常会在主目录下维护一个.helm的本地仓库。linux
curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > get_helm.sh chmod 700 get_helm.sh ./get_helm.sh
或者直接下载,拷贝到/usr/local/bin目录中(或其它目录)运行:git
wget -c https://storage.googleapis.com/kubernetes-helm/helm-v2.9.1-linux-amd64.tar.gz tar -vxf helm-v2.9.1-linux-amd64.tar.gz sudo cp linux-amd64/helm /usr/local/bin
查看版本信息:github
helm version
查看状态:web
kubectl cluster-info kubectl config current-context
对于2.9.0/1版本,须要打上补丁,操做以下:sql
kubectl -n kube-system patch deployment tiller-deploy -p '{"spec": {"template": {"spec": {"automountServiceAccountToken": true}}}}
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
将上面内容保存为helm-rbac-config.yaml,而后运行建立服务帐号和kubernetes的访问受权:mongodb
kubectl create -f helm-rbac-config.yaml
helm init --service-account tiller
检查安装是否成功:docker
kubectl -n kube-system get pods | grep tiller
或者:shell
kubectl get pods --namespace=kube-system
更新仓库:json
helm repo update
helm init
运行起来了,可是helm list
运行出错,以下。
helm list Error: configmaps is forbidden: User "system:serviceaccount:kube-system:default" cannot list configmaps in the namespace "kube-system"
这是RABC权限设置问题,参照以下解决(更好的方式时使用前面的rbac-config.yaml配置文件):
kubectl create serviceaccount --namespace kube-system tiller kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}' helm init --service-account tiller --upgrade
Kubernetes升级后问题解决:
Kubernetes升级到1.11.2后,helm list出现下面错误:
Error: configmaps is forbidden: User "system:serviceaccount:kube-system:default"
从新运行权限设置和初始化后,运行正常:
# 在kube-system命名空间中建立tiller帐户 kubectl create serviceaccount --namespace kube-system tiller # 建立角色并授予cluster-admin权限 kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller # 指定帐户进行初始化,别忘了还要指定tiller镜像哦 helm init --service-account tiller --upgrade # --tiller-image
在控制台输入helm,显示helm的主要命令,以下:
Available Commands: completion Generate autocompletions script for the specified shell (bash or zsh) create create a new chart with the given name delete given a release name, delete the release from Kubernetes dependency manage a chart's dependencies fetch download a chart from a repository and (optionally) unpack it in local directory get download a named release history fetch release history home displays the location of HELM_HOME init initialize Helm on both client and server inspect inspect a chart install install a chart archive lint examines a chart for possible issues list list releases package package a chart directory into a chart archive plugin add, list, or remove Helm plugins repo add, list, remove, update, and index chart repositories reset uninstalls Tiller from a cluster rollback roll back a release to a previous revision search search for a keyword in charts serve start a local http web server status displays the status of the named release template locally render templates test test 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/server version information Flags: --debug enable verbose output -h, --help help for helm --home string location of your Helm config. Overrides $HELM_HOME (default "/Users/wangerqi/.helm") --host string address of Tiller. Overrides $HELM_HOST --kube-context string name of the kubeconfig context to use --kubeconfig string path to kubeconfig file. Overrides $KUBECONFIG --tiller-namespace string namespace of Tiller (default "kube
Helm已经有很多内置的应用服务(地址为 https://kubernetes-charts.storage.googleapis.com/), 源文件在 https://github.com/kubernetes/charts ,建议本身Fork下来,修改后就能够直接使用,我本身作了个镜像( https://github.com/openthings/zhelm-charts ),有一些小的改进。
下面以安装MySQL为例说明Helm的使用:
能够经过docker run或docker compose文件来启动 MySQL容器。这里,咱们使用helm来安装。运行:
helm install stable/mysql
而后,检查容器是否运行:
kubectl get pod
获取MySQL的登陆密码:
MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default knotted-pronghorn-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo) echo $MYSQL_ROOT_PASSWORD
记录下来,后面登陆使用。
建立MySQL客户端,基于Ubuntu:
kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il
而后,进入了Ubuntu的控制台,安装mysql的客户端软件:
apt-get update && apt-get install mysql-client -y
登陆进mysql:
mysql -h knotted-pronghorn-mysql -p
helm里面的包描述文件叫chart,既可使用kubernetes内置的,也能够建立本身的chart,而后加到仓库里面、安装使用。
这里介绍建立建立本身chart的方法,在控制台输入:
helm create mongodb #目录结构以下: mongodb ├── Chart.yaml #Chart自己的版本和配置信息 ├── charts #依赖的chart ├── templates #配置模板目录 │ ├── NOTES.txt #helm提示信息 │ ├── _helpers.tpl #用于修改kubernetes objcet配置的模板 │ ├── deployment.yaml #kubernetes Deployment object │ └── service.yaml #kubernetes Serivce └── values.yaml #kubernetes object configuration
目录结构和文件是helm自动生成的,具体的文件内容能够进目录去查看。
先验证、测试一下:
helm install --dry-run --debug mongodb
安装到本地Kubernetes服务中:
helm install mongodb
查看下该服务在Kubernetes环境下的运行状态:
kubectl get pod
查看Helm仓库:
helm ls
删除仓库中的服务,如:
helm delete mongodb
对该服务打包:
helm package .
注意,由于Kubernetes面向的网络服务集群,helm的后台是经过tiller服务远程执行的(即使位于同一个机器),所以须要tiller服务可用,若是reset集群后,能够经过helm init从新安装。
正常状况下,Kubernetes服务运行状态以下:
kubectl get pod --namespace=kube-system NAME READY STATUS RESTARTS AGE etcd-docker-for-desktop 1/1 Running 0 56m kube-apiserver-docker-for-desktop 1/1 Running 0 56m kube-controller-manager-docker-for-desktop 1/1 Running 0 55m kube-dns-6f4fd4bdf-gv59d 3/3 Running 0 55m kube-proxy-r4xnw 1/1 Running 0 55m kube-scheduler-docker-for-desktop 1/1 Running 0 57m kubernetes-dashboard-845747bdd4-v8jg6 1/1 Running 0 25m tiller-deploy-7594bf7b76-dlxt2 1/1 Running 0 8m
其中的dashboard是一个Kubernetes管理Web可视化面板,能够参照这里安装:
参考:https://docs.bitnami.com/kubernetes/how-to/deploy-application-kubernetes-helm/