以下内容均来自个人笔记并重新梳理,如有错误欢迎指正!如果对您有帮助,烦请点赞、关注、转发!欢迎扫码关注个人公众号!
目录
一、基本介绍
二、工作原理
三、部署方法
四、使用方法
一、基本介绍
Reloader 是一个用于 Kubernetes 的工具,能够监听 Kubernetes 中 ConfigMap 和 Secret 的变化,并通过自动触发 Deployment、StatefulSet、Daemonset 等对象重启,实现应用程序配置的热更新(重新加载)。
二、工作原理
- 监听配置:Reloader 会监听指定 ConfigMap 或 Secret 的配置文件
- 触发事件:当检测到配置文件内容发生变化时,Reloader 会触发一个事件
- 重新加载:Reloader 向控制器发送信号,请求重启 Deployment、StatefulSet、Daemonset 等对象
三、部署方法
1、资源清单
# reloader.yaml
## 定义 RBAC 对象
apiVersion: v1
kind: ServiceAccount
metadata:name: reloader---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:name: reloader-role
rules:- apiGroups:- ""resources:- secrets- configmapsverbs:- list- get- watch- apiGroups:- "apps"resources:- deployments- daemonsets- statefulsetsverbs:- list- get- update- patch- apiGroups:- "extensions"resources:- deployments- daemonsetsverbs:- list- get- update- patch- apiGroups:- ""resources:- eventsverbs:- create- patch---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:name: reloader-rolebinding
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: reloader-role
subjects:- kind: ServiceAccountname: reloader## 定义 Deployment 对象
---
apiVersion: apps/v1
kind: Deployment
metadata:name: reloader
spec:replicas: 1revisionHistoryLimit: 10strategy:type: RollingUpdaterollingUpdate:maxSurge: 25%maxUnavailable: 25%selector:matchLabels:app: reloadertemplate:metadata:labels:app: reloaderspec:containers:- name: reloaderimage: stakater/reloader:v1.0.12imagePullPolicy: IfNotPresentports:- name: httpcontainerPort: 9090env:- name: KUBERNETES_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespacestartupProbe:httpGet:path: /metricsport: httpinitialDelaySeconds: 5periodSeconds: 5timeoutSeconds: 5successThreshold: 1failureThreshold: 10readinessProbe:httpGet:path: /metricsport: httpinitialDelaySeconds: 5periodSeconds: 5timeoutSeconds: 5successThreshold: 1failureThreshold: 5livenessProbe:httpGet:path: /liveport: httpinitialDelaySeconds: 5periodSeconds: 5timeoutSeconds: 5successThreshold: 1failureThreshold: 5resources:limits:cpu: 200mmemory: 200Mirequests:cpu: 20mmemory: 20MirestartPolicy: AlwayssecurityContext: runAsNonRoot: truerunAsUser: 65534serviceAccountName: reloader
2、部署命令
kubectl apply -f reloader.yaml
四、使用方法
在 Deployment、StatefulSet、DaemonSet 等对象的资源清单中,添加相应的 Annotation 注解即可,示例如下:
apiVersion: apps/v1
kind: Deployment
metadata:nanme: demo-deploymentannotations:reloader.stakater.com/auto: "true"
...🔔 reloader.stakater.com/auto: "true":对所有 ConfigMap 和 Secret 对象生效
🔔 configmap.reloader.stakater.com/reload: "demo1,demo2,...":对指定的 ConfigMap 对象生效
🔔 secret.reloader.stakater.com/reload: "demo1,demo2,...":对指定的 Secret 对象生效