乐趣区

k8s与configmap–安利configmap-reload组件

前言
在 kubernetes 集群内,当 ConfigMap 以 volume 形式挂载到 pod 内时,更新 ConfigMap,kubernetes 会自动同步被挂载到 pod 内的文件内容。当然并不是更改立即生效的,大约是需要 10S 钟后,才会生效。实际生产使用案例中,假如你的应用具备 hot reload 功能, 这时可以增加一些监测配置文件变更的脚本,然后 reload 对应服务。比如 prometheus。今天就给大家介绍一个 configmap-reload 组件。
configmap-reload
configmap-reload 采用 rust 语言实现,作为主业务容器的 sidercar,主要用于 k8s 当中监听 configmap 的变化,待变化后通过 http 接口的方式通知主业务。在资源消耗上,更小。具体如下:
[root@ip-172-xx-xx-10 src]# kubectl top pods
NAME CPU(cores) MEMORY(bytes)
configmap-reload-6bbbb8b45b-7zg2x 0m 1Mi
输入参数可以通过 configmap-reload -h 获取:

configmap-reload 0.1.0
gaohj <gaohj2015@yeah.net>

USAGE:
configmap-reload [OPTIONS]

FLAGS:
-h, –help Prints help information
-V, –version Prints version information

OPTIONS:
-l, –log_level <LOG_LEVEL> log level: error|warn|info|debug|trace [default: info]
-p, –path <VOLUME_PATH> the config map volume directory to watch for updates [default:]
-m, –webhook_method <WEBHOOK_METHOD>
the HTTP method url to use to send the webhook: GET|POST [default: POST]

-c, –webhook_status_code <WEBHOOK_STATUS_CODE>
the HTTP status code indicating successful triggering of reload [default: 200]

-u, –webhook_url <WEBHOOK_URL> the HTTP method url to use to send the webhook [default:]

示例使用:

apiVersion: v1
kind: ConfigMap
metadata:
labels:
app: configmap-reload
name: configmap-reload-cm
data:
test.ini: |-
key: a


kind: Deployment
apiVersion: apps/v1
metadata:
name: configmap-reload
labels:
app: configmap-reload
spec:
replicas: 1
selector:
matchLabels:
app: configmap-reload
template:
metadata:
labels:
app: configmap-reload
spec:
volumes:
– name: config
configMap:
name: configmap-reload-cm
containers:
– name: configmap-reload
image: ‘iyacontrol/configmap-reload:v0.1’
command:
– configmap-reload
args:
– -l
– debug
– -p
– /etc/test/
– -c
– ‘200’
– -u
– https://www.baidu.com
volumeMounts:
– name: config
mountPath: /etc/test/
imagePullPolicy: Always

总结
大家直接可以拉取 dockerhub 中的镜像。
当然仓库中已经提供了 Dockerfile 文件,
FROM clux/muslrust:stable as builder

WORKDIR /configmap-reload
COPY ./ ./

ARG use_mirror
RUN if [$use_mirror]; then \
mkdir -p $HOME/.cargo; \
mv -f ./docker/cargo_config $HOME/.cargo/config; \
fi
RUN cargo build –release

#####################################

FROM alpine:latest as prod

RUN apk add –no-cache ca-certificates

COPY –from=0 /configmap-reload/target/x86_64-unknown-linux-musl/release/configmap-reload /usr/bin/configmap-reload
RUN chmod +x /usr/bin/configmap-reload
ENTRYPOINT [“configmap-reload”]
大家可以自己打镜像,然后 push 到自己的仓库中。

退出移动版