目录
前提:我已经部署好了k8s集群环境(如何部署k8s集群,可以翻看作者之前的文章)
Helm简介
Helm分为Helm2.*和Helm3.*;其主要区别是Helm2.*需要安装tiller(helm server);helm3最大的变化就是helm3移除了tiller组件,所有功能都通过helm client直接跟K8S APIserver交互通信,不需要helm init安装了。
Helm2有两个主要的组件:
Tiller: helm的服务端,部署在k8s里面的一个pod,通常在kube-system这个系统空间里。主要负责部署helm charts,管理release,跟k8s API通信。
Helm Client: 主要负责从共有或者私有helm charts仓库拉取chart包,修改变量值,然后直接扔给tiller。
安装Helm3
1、下载Helm二进制文件:https://github.com/helm/helm/releases
2、解压文件:tar -zxvf helm-v3.9.0-linux-amd64.tar.gz
3、mv linux-amd64/helm /usr/local/bin/helmhelm version
version.BuildInfo{Version:"v3.9.0", GitCommit:"7ceeda6c585217a19a1131663d8cd1f7d641b2a7", GitTreeState:"clean", GoVersion:"go1.17.5"}
部署私有Helm仓库
# 运行nginx容器,用于提供helm仓库的http服务
docker run -d --name=nginx -p 80:80 -v /opt/helm/charts:/usr/share/nginx/html/charts nginx
添加常用helm repo,验证helm
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add stable http://mirror.azure.cn/kubernetes/charts
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
#BitNami是一个开源项目,该项目产生的开源软件包括安装 Web应用程序和解决方案堆栈,以及虚拟设备。
helm repo add bitnami https://charts.bitnami.com/bitnami
#搜索nginx app
helm search repo nginx
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/nginx 15.4.3 1.25.3 NGINX Open Source is a web server that can be a...
#安装nginx app
helm install bitnami/nginx --generate-name
#根据提示访问nginx
http://nodeip:nodeport
打包Chart Package并发布(以发布machine应用为例)
### 创建工作目录,用户打包
mkdir /opt/helm/workstation
cd /opt/helm/workstation### 创建chart 目录(注意创建的chart目录名:必须由“字母、数字和-”组成)
helm create chart目录名例如:helm create machine
### 查看chart目录结构
ll machine/*
-rw-r--r-- machine/Chart.yaml
-rw-r--r-- machine/values.yaml
machine/charts:machine/templates:
-rw-r--r-- deployment.yaml
-rw-r--r-- _helpers.tpl
-rw-r--r-- hpa.yaml
-rw-r--r-- ingress.yaml
-rw-r--r-- NOTES.txt
-rw-r--r-- serviceaccount.yaml
-rw-r--r-- service.yaml
drwxr-xr-x tests
自定义chart配置信息
### 编辑values.yaml
vi values.yaml
……
image:
repository: 192.168.11.101/library/machine
tag: "1.0" # --- 如果不在tag这里指定镜像tag,就要编辑Chart.yaml
……
app:
env:
ftp_ipaddr: 192.168.14.237
ftp_username: 用户
ftp_password: 密码
ftp_port: 21
……
service:
type: NodePort
port: 31778
nodePort: 31778
……
resources:
limits:
cpu: 1
memory: 4Gi
requests:
cpu: 1
memory: 2Gi### 编辑templates/deployment.yaml
### 这里我们不需要livenessProbe和readinessProbe探针,且需要修改containerPort,追加环境变量
……
ports:
- containerPort: {{ .Values.service.port }}
env:
- name: FTP_IP
value: "{{ .Values.app.env.ftp_ipaddr }}"
- name: FTP_USERNAME
value: "{{ .Values.app.env.ftp_username }}"
- name: FTP_PASSWORD
value: "{{ .Values.app.env.ftp_password }}"
- name: FTP_PORT
value: "{{ .Values.app.env.ftp_port }}"
- name: PYTHON_ANALYSIS_SERVER_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: PYTHON_ANALYSIS_SERVER_PORT
value: "{{ .Values.service.port }}"
volumeMounts:
- name: temp-data
mountPath: /home/machine_account_mining/analytical_model/data
volumes:
- name: temp-data
hostPath:
path: /machine_account_mining/analytical_model/data
……
### 编辑templates/service.yaml
ports:
- port: {{ .Values.service.port }}
nodePort: {{ .Values.service.port }}
……
### 修改Chart.yaml,定义appVersion(如果values.yaml中的tag未定义,就要在这里定义)
cat Chart.yaml |egrep -v '^#|^$'
apiVersion: v2
name: machine
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.0"
进行打包
helm package machine
mv myrepo/machine-0.1.0.tgz /opt/helm/charts/
发布chart包
### 执行helm repo index生成库的index文件
### 如果同名的chart package已经加入到仓库,就不用再次执行helm repo index
helm repo index /opt/helm/charts/ --url http://192.168.11.190/charts
helm通过repo启动新发布的chart包
### 通过helm repo add 将新仓库添加到helm
helm repo add newrepo http://192.168.11.190/charts
### 如果repo已经被add,则需要helm repo update
### 才能通过helm search repo发现chart包### 查看是否添加成功
helm repo list
NAME URL
bitnami https://charts.bitnami.com/bitnami
newrepo http://192.168.11.190/charts### 搜索发布的 machine chart包
helm search repo machine
NAME CHART VERSION APP VERSION DESCRIPTION
newrepo/machine 0.1.0 1.16.0 A Helm chart for Kubernetes### 通过helm安装 machine
helm install newrepo/machine --generate-name
或
helm install machine newrepo/machine### 查看已启动的chart
helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
machine default 1 2022-07-13 18:10:51.064242534 +0800 CST deployed machine-0.1.0 1.16.0### 也可以通过kubectl get po查看
kubectl get po
NAME READY STATUS RESTARTS AGE
machine-1657265630 1/1 Running 0 104m### 删除启动的应用
helm uninstall machine-1657265630### 删除repo
helm repo remove newrepo### 拉取应用的chart package
helm pull newrepo/machine该操作会拉取machine应用,在当前目录生成一个对应版本的machine-0.1.0.tgz
FAQ
Error: INSTALLATION FAILED: YAML parse error on machine-v1.0/templates/deployment.yaml: error converting YAML to JSON: yaml: line 58: did not find expected '-' indicator
问题一:helm install machine newrepo/machine失败,报上述错误
解决:这是因为templates/deployment.yaml中YAML语法错误,根据提示进行修改即可。Error: INSTALLATION FAILED: Service "machine-machine-v1.0" is invalid: spec.ports[0].nodePort: Invalid value: 33778: provided port is not in the valid range. The range of valid ports is 30000-32767
问题二:helm install machine newrepo/machine失败,报上述错误
解决:service指定的端口超出了默认范围。Error: INSTALLATION FAILED: Service "machine-machine-v1.0" is invalid: metadata.name: Invalid value: "machine-machine-v1.0": a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')
问题三:helm install machine newrepo/machine失败,报上述错误
解决:在只用helm create 命令创建chart环境时,目录名:必须由“字母、数字和-”组成,开头和结尾必须是字母或数字。Error: INSTALLATION FAILED: Deployment in version "v1" cannot be handled as a Deployment: json: cannot unmarshal number into Go struct field EnvVar.spec.template.spec.containers.env.value of type string
问题四:helm install machine newrepo/machine失败,报上述错误
解决:templates/deployment.yaml中定义环境变量时出现错误:
env:
- name: FTP_IP
value: {{ .Values.app.env.ftp_ipaddr }}
正确格式为:
env:
- name: FTP_IP
value: "{{ .Values.app.env.ftp_ipaddr }}"Error: INSTALLATION FAILED: Deployment.apps "machine" is invalid: [spec.template.spec.volumes[0].name: Invalid value: "tempData": a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?'), spec.template.spec.containers[0].volumeMounts[0].name: Not found: "tempData"]
问题五:helm install machine newrepo/machine失败,报上述错误
解决:deploy中定义的值只能是小写字母、数字和-经历九九八十一难,终于成功了:
helm install machine newrepo/machine
NAME: machine
LAST DEPLOYED: Wed Jul 13 16:55:03 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services machine)
export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT