由yaml文件部署的kubernetes资源对象,如何纳入helm chart的管理,同时能随着chart中对象的升级而升级?
一. 方法
首先,将资源对象纳入helm chart的管理:
# 修改对象的annotation 和 label
kubectl annotate --overwrite {kind} {obj} meta.helm.sh/release-name={release}
kubectl annotate --overwrite {kind} {obj} meta.helm.sh/release-namespace={namespace}
kubectl label --overwrite {kind} {obj} app.kubernetes.io/managed-by=Helm
然后,执行helm命令:
helm upgrade --install {release} .
二. demo
- 首先,yaml创建一个deploy对象,其image=nginx:1.21;
- 然后,修改deploy对象的annotation和label,将其纳入Helm chart的管理;
- 再后,创建helm chart,chart中部署deploy,其image=nginx:1.23;
最后:
- 执行helm upgrade命令,确认deploy的image已升级至1.23;
- 执行helm uninstall命令,确认deploy被随之删除;
yaml创建deploy对象
apiVersion: apps/v1
kind: Deployment
metadata:
name: v1-nginx
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: nginx
app.kubernetes.io/instance: v1
template:
metadata:
labels:
app.kubernetes.io/name: nginx
app.kubernetes.io/instance: v1
spec:
containers:
- name: nginx
image: "nginx:1.21" # image = nginx:1.21
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
将deploy纳入helm chart的管理
kubectl annotate --overwrite deploy v1-nginx meta.helm.sh/release-name=v1
kubectl annotate --overwrite deploy v1-nginx meta.helm.sh/release-namespace=default
kubectl label --overwrite deploy v1-nginx app.kubernetes.io/managed-by=Helm
创建helm chart
# template/deploy.yaml
...
spec:
containers:
- name: nginx
image: "nginx:1.23"
imagePullPolicy: IfNotPresent
...
执行helm upgrade:
# helm upgrade --install v1 .
Release "v1" does not exist. Installing it now.
NAME: v1
LAST DEPLOYED: Tue Jan 31 15:52:03 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
验证deploy的image是否升级:
# kubectl get deploy v1-nginx -ojsonpath="{.spec.template.spec.containers[0].image}"
nginx:1.23
验证helm uninstall 后deploy是否删除:
# helm uninstall v1
release "v1" uninstalled
# kubectl get deploy