通过Http API的方式更改k8s中的configmap

1、在PUT中直接输入json串会因为格式原因报出各种各样的错误,因此
将修改后的configmap存到configmap_core.json文件中。

curl -X PUT -u k8s_usrname:k8s _password -H 'Content-Type: application/json' --data-binary @configmap_core.json https://10.4.**.**:6443/api/v1/namespaces/monitoring/configmaps/prometheus-rules

API参考:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#replace-195

PUT理解:https://blog.csdn.net/weixin_38070561/article/details/83272480

注1:这里必须加-u参数指定k8s的用户名和密码,否则连接会被拒绝。

注2:-H "Content-Type:application/json"表示使用JSON形式post数据。

注3:-X指定请求方式是GET还是POST。
GET 方法相对简单,只要把数据附在网址后面就行。

curl example.com/form.cgi?data=xxx

POST 方法则必须把数据和网址分开,curl 就要用到 --data 或者 -d 参数。-d后面的是post的数据。

curl -X POST --data "data=xxx" example.com/form.cgi
curl -X POST -d"data=123&key=456" http://localhost:8080/search

2、prometheus热加载
curl -X POST http://10.4.**.**:31391/-/reload

注1:二进制文件启动时,如果要热加载prometheus,端口为9090。如果要热加载altermanager,则需要将端口改成9093。

注2:直接执行会提示API不可用。需要修改prometheus的Dockerfile文件,重新打镜像。默认的peometheus是以nobody用户启动,需要增加--web.enable-lifecycle参数,才能使上述API可用。

注3:在热加载的过程中,如果Prometheus监控的是k8s集群,那么需要在prometheus被分配的到的那个Node节点上执行热加载指令,才可以生效。在主节点执行不生效。

Dockerfile文件修改为:

FROM        quay.io/prometheus/busybox:latest
LABEL maintainer "保持不变"

COPY prometheus                             /bin/prometheus
COPY promtool                               /bin/promtool
COPY prometheus.yml                         /etc/prometheus/prometheus.yml
COPY console_libraries/                     /usr/share/prometheus/console_libraries/
COPY consoles/                              /usr/share/prometheus/consoles/

RUN ln -s /usr/share/prometheus/console_libraries /usr/share/prometheus/consoles/ /etc/prometheus/
RUN mkdir -p /prometheus 

EXPOSE     9090
VOLUME     [ "/prometheus" ]
WORKDIR    /prometheus
ENTRYPOINT [ "/bin/prometheus" ]
CMD        [ "--config.file=/etc/prometheus/prometheus.yml", \
             "--storage.tsdb.path=/prometheus", \
             "--web.console.libraries=/usr/share/prometheus/console_libraries", \
             "--web.enable-lifecycle", \
             "--web.console.templates=/usr/share/prometheus/consoles" ]

接着执行(不要遗漏最后的点):docker build -t prom/prometheus:v.0.0.0 .
在起版本号是不要起latest,可能会造成拉取失败。

将镜像拷贝到目标节点,docker load < *.tar

接着修改deployment.yaml,修改内容有两方面。一方面是修改image:镜像的名字。一方面是在args:加上--web.enable-lifecycle参数。

最后执行ps -ef | grep prometheus查看更改是否生效,成功的标志为

prometheus --config.file /etc/prometheus/prometheus.yml --web.enable-lifecycl

参考:
https://blog.csdn.net/chenleiking/article/details/80182086

猜你喜欢

转载自blog.csdn.net/weixin_38645718/article/details/85116282