安装helm wget https://get.helm.sh/helm-v3.14.3-darwin-amd64.tar.gz wget https://get.helm.sh/helm-v3.14.3-linux-amd64.tar.gz tar zxvf helm-v3.14.3-darwin-amd64.tar.gz sudo mv darwin-amd64/helm /usr/local /bin/helm helm version version.BuildInfo{Version:"v3.14.3" , GitCommit:"f03cc04caaa8f6d7c3e67cf918929150cf6f3f12" , GitTreeState:"clean" ,GoVersion:"go1.21.7" }
mac需要去 系统设置>隐私与安全性>安全性
允许helm运行
helm命令 helm create 该命令用于初始化一个新的 Helm Chart。Helm Chart 是 Kubernetes 应用的包,包含了运行应用所需的所有资源定义。
helm create nmk-app ➜ ~ tree nmk-app nmk-app ├── Chart.yaml ├── charts ├── templates │ ├── NOTES.txt │ ├── _helpers.tpl │ ├── deployment.yaml │ ├── hpa.yaml │ ├── ingress.yaml │ ├── service.yaml │ ├── serviceaccount.yaml │ └── tests │ └── test -connection.yaml └── values.yaml
helm install 用于将 Helm Chart 安装到 Kubernetes 集群中。它会创建所有定义在 Chart 中的资源。
➜ ~ helm install nmk-app nmk-app NAME: nmk-app LAST DEPLOYED: Mon Aug 23 16:06:14 2024 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None
helm upgrade 用于升级已安装的 Helm Chart 到新版本。可以修改 Chart 的配置和资源。
将已有的 release 升级到一个新版本的 chart
➜ ~ helm upgrade nmk-app nmk-app Release "nmk-app" has been upgraded. Happy Helming! NAME: nmk-app LAST DEPLOYED: Mon Aug 26 16:07:57 2024 NAMESPACE: default STATUS: deployed REVISION: 2 TEST SUITE: None
helm rollback 当 Helm Chart 升级后出现问题时,可以使用该命令回滚到之前的版本。
➜ ~ helm rollback nmk-app 1 Rollback was a success! Happy Helming! ➜ ~ kubectl get pod -n tke-docker-test -l app=nmk-app NAME READY STATUS RESTARTS AGE nmk-app 1/1 Running 0 8m49s
helm lint ➜ ~ helm lint nmk-app ==> Linting nmk-app [INFO] Chart.yaml: icon is recommended 1 chart(s) linted, 0 chart(s) failed
helm show 用于查看 Helm Chart 的内容,包括模板、配置文件和其他资源的定义。
显示Chart的信息和元数据 ➜ ~ helm show chart nmk-app apiVersion: v2 appVersion: 1.16.0 description: A Helm chart for Kubernetes name: nmk-app type : applicationversion: 0.1.0
helm uninstall 移除已安装的 Helm Chart。
➜ ~ helm uninstall nmk-app release "nmk-app" uninstalled
helm pull 从远程仓库下载或拉取一个 Helm Chart 到本地。
➜ ~ helm repo add stable https://charts.helm.sh/stable ➜ ~ helm pull stable/mysql --version 1.6.9 常用参数: --version:允许指定一个版本约束,用于下载特定版本的 Helm 图表。默认使用latest版本; --untar:下载Chart后是否自动解压缩。如果设置为true ,则Helm会在下载后自动解压缩 --untardir:指定图表提取到的目录; --repo:指定从中下载图表的存储库的 URL; --username和--password:提供访问私人存储库的凭证; --verify:可以在使用图表之前对其进行验证,确保图表的完整性和安全性。
制作helm chart helm create nmk-app ➜ Downloads tree nmk-app nmk-test ├── Chart.yaml ├── charts ├── templates │ ├── NOTES.txt │ ├── _helpers.tpl │ ├── deployment.yaml │ ├── hpa.yaml │ ├── ingress.yaml │ ├── service.yaml │ ├── serviceaccount.yaml │ └── tests │ └── test -connection.yaml └── values.yaml
删除没用的配置文件,只保留以下文件
deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Values.appname }} namespace: {{ .Values.namespace }} labels: app: {{ .Values.appname }} spec: selector: matchLabels: app: {{ .Values.appname }} strategy: rollingUpdate: maxSurge: 25 % maxUnavailable: 25 % type: RollingUpdate template: metadata: labels: app: {{ .Values.appname }} spec: imagePullSecrets: - name: {{ .Values.imagepullsecrets }} containers: - name: {{ .Values.appname }} env: - name: JAVA_OPTS value: {{ .Values.env.javaoptions }} image: "{{ .Values.image.repository }} {{ .Values.appname }} :{{ .Values.image.tag }} " imagePullPolicy: {{ .Values.image.pullPolicy }} lifecycle: preStop: exec: command: - sleep - 20s readinessProbe: failureThreshold: 10 httpGet: path: /actuator/ port: {{ .Values.service.port }} scheme: HTTP initialDelaySeconds: 10 periodSeconds: 15 successThreshold: 1 timeoutSeconds: 5 startupProbe: failureThreshold: 10 httpGet: path: /actuator/ port: {{ .Values.service.port }} scheme: HTTP initialDelaySeconds: 10 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 5 resources: limits: cpu: {{ .Values.resource.limitCpu }} memory: {{ .Values.resource.limitMemory }} requests: cpu: {{ .Values.resource.requestCpu }} memory: {{ .Values.resource.requestMemory }}
service.yaml apiVersion: v1 kind: Service metadata: name: {{ .Values.appname }} namespace: {{ .Values.namespace }} spec: type: {{ .Values.service.type }} ports: - port: {{ .Values.service.port }} targetPort: {{ .Values.service.port }} protocol: TCP name: {{ .Values.appname }} selector: app: {{ .Values.appname }} sessionAffinity: None
ingress.yaml {{- if .Values.ingress.enabled -}} {{- $fullName := .Values.appName -}} {{- $svcPort := .Values.service.port -}} {{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class" ) }} {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} {{- end }} {{- end }} {{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} apiVersion: networking.k8s.io/v1 {{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} apiVersion: networking.k8s.io/v1beta1 {{- else -}} apiVersion: extensions/v1beta1 {{- end }} kind: Ingress metadata: name: {{ $fullName }} namespace: {{ .Values.namespace }} spec: {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} ingressClassName: {{ .Values.ingress.className }} {{- end }} {{- if .Values.ingress.tls }} tls: {{- range .Values.ingress.tls }} - hosts: {{- range .hosts }} - {{ . | quote }} {{- end }} secretName: {{ .secretName }} {{- end }} {{- end }} rules: {{- range .Values.ingress.hosts }} - host: {{ .host | quote }} http: paths: {{- range .paths }} - path: {{ .path }} {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} pathType: {{ .pathType }} {{- end }} backend: {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} service: name: {{ $fullName }} port: number: {{ $svcPort }} {{- else }} serviceName: {{ $fullName }} servicePort: {{ $svcPort }} {{- end }} {{- end }} {{- end }} {{- end }}
values.yaml appname: nmk-app namespace: tke-docker-test replicaCount: 1 image: repository: helm-hub.com/nmk/ pullPolicy: IfNotPresent tag: app-20240806110148 imagepullsecrets: qcloudregistrykey env: javaoptions: -Xms2g -Xmx2g -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=512M -Dspring.profiles.active=test -Dspring.cloud.consul.enabled=false -Dtsf.discovery.ribbon.enabled=false service: type: ClusterIP port: 8080 resource: limitCpu: '2' limitMemory: 2Gi requestCpu: '1' requestMemory: 1Gi ingress: enabled: true className: "nginx" annotations: {} hosts: - host: nmk.com paths: - path: / pathType: ImplementationSpecific tls: - secretName: ssl-2024 hosts: - nmk.com
Chart.yaml apiVersion: v2 name: nmk-app description: A Helm chart for Kubernetes type: application version: 0.1 .0 appVersion: "1.16.0"
安装后查看 ➜ ~ Downloads helm list nmk-app default 1 2024-08-26 16:06:14.735267 +0800 CST deployed nmk-app-0.1.0 1.16.0 ➜ ~ Downloads kubectl get pods -n tke-docker-test nmk-app-6fdc57bd55-gx6mn 1/1 Running 0 117s ➜ ~ Downloads kubectl get service -n tke-docker-test nmk-app ClusterIP 172.31.159.244 <none> 8080/TCP 4m14s ➜ ~ Downloads kubectl get ingress -n tke-docker-test nmk-app nginx nmk.com 10.30.150.3 80, 443 4m24s
可使用以下参数指定变量进行多应用部署使用
--set appName=demo --set version=v1.1
参考文献:https://blog.csdn.net/dhf880913/article/details/138125561 https://blog.csdn.net/zhuganlai168/article/details/131483308