使用helm部署应用
Helm 是一个 Kubernetes 的包管理工具,用于简化应用的部署和管理。下面是用helm在k8s上部署应用的一般流程。
- 添加helm仓库
shell
# 添加仓库
root@master1:~# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
root@master1:~# helm repo add bitnami https://charts.bitnami.com/bitnami
# 更新
root@master1:~# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "aliyun" chart repository
...Successfully got an update from the "harbor" chart repository
...Successfully got an update from the "gitlab-jh" chart repository
...
# 仓库列表
root@master1:~# helm repo list
NAME URL
ingress-nginx https://kubernetes.github.io/ingress-nginx
openebs https://openebs.github.io/openebs
harbor https://helm.goharbor.io
jenkins https://charts.jenkins.io
gitlab https://charts.gitlab.io/
gitlab-jh https://charts.gitlab.cn
bitnami https://charts.bitnami.com/bitnami
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
# 删除
helm repo remove xxx
- 搜索应用
shell
root@master1:~# helm search repo nginx
NAME CHART VERSION APP VERSION DESCRIPTION
aliyun/nginx-ingress 0.9.5 0.10.2 An nginx Ingress controller that uses ConfigMap...
aliyun/nginx-lego 0.3.1 Chart for nginx-ingress-controller and kube-lego
bitnami/nginx 18.1.4 1.27.0 NGINX Open Source is a web server that can be a...
bitnami/nginx-ingress-controller 11.3.12 1.11.0 NGINX Ingress Controller is an Ingress controll...
bitnami/nginx-intel 2.1.15 0.4.9 DEPRECATED NGINX Open Source for Intel is a lig...
ingress-nginx/ingress-nginx 4.11.0 1.11.0 Ingress controller for Kubernetes using NGINX a...
aliyun/gcloud-endpoints 0.1.0 Develop, deploy, protect and monitor your APIs ...
默认情况下可以直接部署官方发布的chart,也可以下拉取到本地后进行修改。
- 修改chart
shell
# 拉取chart
root@master1:~# helm pull bitnami/nginx --version 18.1.4
# 解压缩chart
root@master1:~# tar xf nginx-18.1.4.tgz
root@master1:~# cd nginx/
root@master1:~/nginx# ls
Chart.lock charts Chart.yaml README.md templates values.schema.json values.yaml
root@master1:~/nginx# vim values.yaml
global:
imageRegistry: "harbor.test.com" # 改为私有的helm仓库,此处为本地的harbor服务器
## E.g.
## imagePullSecrets:
## - myRegistryKeySecretName
...
image:
registry: docker.io
repository: bitnami/nginx
tag: 1.27.0-debian-12-r3 # 根据本地harbor仓库的nginx镜像,修改repository和tag。registry可以不用修改,使用global配置。
digest: ""
## Specify a imagePullPolicy
...
service:
## @param service.type Service type
##
type: LoadBalancer # 默认为LoadBalancer,如果没有LB可以修改为NodePort或ClusterIP等。
## @param service.ports.http Service HTTP port
## @param service.ports.https Service HTTPS port
##
ports:
备注
- Helm chart 通常包含一个
values.yaml
文件,用于配置应用的参数。- 也可以使用
--values
参数指定一个自定义的values.yaml
文件。- 也可以通过
--set
参数在命令行中设置参数:
helm install my-nginx stable/nginx --set service.type=LoadBalancer
- 将nginx镜像下载后推送到本地harbor仓库
shell
# 从docker hub下载nginx镜像
nerdctl pull bitnami/nginx:1.27.0-debian-12-r3
# 重新打tag推送到本地的harbor
nerdctl tag bitnami/nginx:1.27.0-debian-12-r3 harbor.test.com/bitnami/nginx:1.27.0-debian-12-r3
# 推送之前需要在harbor上创建bitnami的项目
nerdctl push harbor.test.com/bitnami/nginx:1.27.0-debian-12-r3
- 此时部署chart,镜像默认会从本地的harbor去拉取
shell
root@master1:~/nginx/nginx# ls
Chart.lock charts Chart.yaml README.md templates values.schema.json values.yaml
root@master1:~/nginx/nginx# helm install webapp .
root@master1:~/nginx/nginx# helm status webapp
- 检查部署的nginx
shell
root@master1:~/nginx/nginx# kubectl get pod
NAME READY STATUS RESTARTS AGE
webapp-nginx-7958699dc-45stm 1/1 Running 0 10m
root@master1:~/nginx/nginx# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21d
webapp-nginx LoadBalancer 10.98.11.69 192.168.0.242 80:32079/TCP,443:32506/TCP 17m
本文使用的环境有LB,访问http://192.168.0.242
即可看到nginx页面。
- 升级部署的应用
helm upgrade webapp . --values values.yaml
- 删除部署的应用
helm uninstall webapp
默认为default,删除指定命名空间需要指定-n <ns-name>
。