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 


USAGE:
    configmap-reload [OPTIONS]

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

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

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

    -u, --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到自己的仓库中。

你可能感兴趣的:(rust,kubernetes,k8s,docker)