kubernetes实战篇之helm使用技巧
使用压缩包安装chart
我们使用helm package
打包的时候,默认会在当前位置生成一个tgz压缩包,然后helm把它复制到到$HOME/.helm/repository
目录下,现在还没有讲解helm私服搭建,使用的是helm自带的一个本地服务器,有时候这个服务器不能正常工作,但是我们又想体验功能,这时候可以使用本地chart压缩包来安装
我在当前目录下有以下文件
[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可以看到它了
[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
来安装它
安装的时候指定不同的环境
我们在安装一个chart的时候,由于环境不同,配置可能也会有差别,我们知道,chart的配置都存在values.yaml文件里,我们可以编写多个存储配置的yaml文件,在安装的时候通过-f
指定 不同环境的values文件
[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
指定它的位置),根据不同环境进行修改.
这种方法同样也可以解决另外一个问题,那就是很多公共仓库的chart依赖gcr.io上的镜像导致国内无法安装成功,如果镜像地址做成了参数,则我们可以修改,如果没有做成参数,我们仍然有办法,那就是先通过
helm fetch
把它拉到本地,然后解压,然后修改其镜像文件重新打包,通过上面讲到过的使用本地压缩包安装的方式来安装.
让 Helm 连接到指定其它Kubernetes集群
Helm 默认使用和 kubectl 命令相同的配置访问 Kubernetes 集群,其配置默认在 ~/.kube/config
中。
部署时指定名称空间
我们前面的示例都默认是在的名称空间,实际生产环境中往往都不是按这种默认设置,而是安装在指定的名称空间中.如果想部署到指定的命令空间,执行helm install
命令时 可以加上 --namespace 参数,比如:
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运行起来