K8S Pod Sidecar 应用场景之一

编程入门 行业动态 更新时间:2024-10-24 16:31:39

K8S Pod Sidecar 应用<a href=https://www.elefans.com/category/jswz/34/1770727.html style=场景之一"/>

K8S Pod Sidecar 应用场景之一

Kubernetes Pod Sidecar 简介

Sidecar 是一个独立的容器,与 Kubernetes pod 中的应用容器一起运行,是一种辅助性的应用。

Sidecar 的常见辅助性功能有这么几种:

  1. 服务网格 (service mesh) 代理
  2. 监控 Exporter(如 redis exporter)
  3. ConfigMap 或/和 Secret Reloader(如 Prometheus 的 Config Reloader)
  4. Auth Proxy(如 OAuth Proxy 等)
  5. 7 层反向代理和 Web 服务器
  6. 日志整合(审计日志单独发到某个日志渠道。..)
  7. Demo 或 AllInOne 应用(如 nextcloud 或 Jaeger AllInOne 等示例应用)
  8. ...

这里选几个场景细说一下,在服务网格的情况下,sidecar 负责从应用程序本身卸载服务网格中所有应用程序所需的功能--SSL/mTLS、流量路由、高可用性等,并实施部署各种高级发布模式,如断路器、金丝雀和蓝绿等。

作为数据平面组件,sidecar 通常由服务网格中的某种类型的控制平面管理。当 sidecar 路由应用流量并提供其他数据平面服务时,控制平面在必要时将 sidecars 注入 pod 并执行管理任务,例如更新 mTLS 证书并在需要时将其推送到适当的 sidecars。

日志整合场景下,Sidecar 被用来将多个应用实例的日志信息汇总并格式化为一个文件。

接下来进入本次的正题:将 NGINX (或 Caddy 等)作为 Sidecar 使用,主要用做反代和 web 服务器

场景假设

假设有这么一个场景:

我在使用原生的 Prometheus AlertManager, 我已经有 Ingress. 我现在想要做 2 件事:

  1. 提升 AlertManager UI 的并发能力(增加 buffer, cache; 启用 gzip 等)
  2. AlertManager 的某个 js(假设是 script.js), 我做了一点修改,但不希望侵入式地修改 原生 AlertManager 二进制文件,而是把修改后 js 放到 nginx 的 www 目录,让 nginx 来用不同的 location 进行处理。

这种场景下,显然 Ingress 是无法同时满足的。这时候就可以在 AlertManager Pod 里加个 NGINX 的 sidecar 来实现。

具体如下

NGINX Sidecar 典型使用步骤

  1. 创建 NGINX Conf 的 configmap; (监听 8080, 反向代理到后端的 9093)
  2. 创建 alertmanager script.js 的 configmap;
  3. 修改原 AlertManager 的 StatefulSets, 增加:
    1. NGINX Sidecar
    2. 3 个 volumes: 其中 2 个就是用于挂载上面的 ConfigMap, 另外一个 EmptyDir 用于挂载 nginx cache
  4. 修改 AlertManager Service 的端口,从 9093 改为 8080, name 从http 改为 nginx-http
  5. (可选)修改其他部分,如 Ingress 等,调整端口。

NGINX Conf 的 ConfigMap

具体如下:

apiVersion: v1
kind: ConfigMap
metadata:name: alertmanager-nginx-proxy-configlabels:app.kubernetes.io/name: alertmanager
data:nginx.conf: |-worker_processes      auto;error_log             /dev/stdout warn;pid                   /var/cache/nginx/nginx.pid;events {worker_connections 1024;}http {include       /etc/nginx/mime.types;log_format    main '[$time_local - $status] $remote_addr - $remote_user $request ($http_referer)';proxy_connect_timeout       10;proxy_read_timeout          180;proxy_send_timeout          5;proxy_buffering             off;proxy_cache_path            /var/cache/nginx/cache levels=1:2 keys_zone=my_zone:100m inactive=1d max_size=10g;server {listen          8080;access_log      off;gzip            on;gzip_min_length 1k;gzip_comp_level 2;gzip_types      text/plain application/javascript application/x-javascript text/css application/xml text/javascript image/jpeg image/gif image/png;gzip_vary       on;gzip_disable    "MSIE [1-6]\.";proxy_set_header Host $host;location = /script.js {root /usr/share/nginx/html;expires             90d;}location / {proxy_cache         my_zone;proxy_cache_valid   200 302 1d;proxy_cache_valid   301 30d;proxy_cache_valid   any 5m;proxy_cache_bypass  $http_cache_control;add_header          X-Proxy-Cache $upstream_cache_status;add_header          Cache-Control "public";proxy_pass     http://localhost:9093/;if ($request_filename ~ .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) {expires             90d;}}}}

AlertManager script.js ConfigMap

详细内容略。

先通过浏览器将script.js 下载下来。然后按需修改:

apiVersion: v1
kind: ConfigMap
metadata:name: alertmanager-script-jslabels:app.kubernetes.io/name: alertmanager
data:script.js: >-...

修改 StatefulSets

修改的部分内容如下:

apiVersion: apps/v1
kind: StatefulSet
metadata:name: monitor-alertmanager
spec:template:spec:volumes:# 增加 3 个 volumes- name: nginx-homeemptyDir: {}- name: htmlconfigMap:name: alertmanager-script-jsitems:- key: script.jsmode: 438path: script.js- name: alertmanager-nginxconfigMap:name: alertmanager-nginx-proxy-configitems:- key: nginx.confmode: 438path: nginx.confcontainers:# 增加 NGINX sidecar- name: alertmanager-proxyargs:- nginx- -g- daemon off;- -c- /nginx/nginx.confimage: "nginx:stable"ports:- containerPort: 8080name: nginx-httpprotocol: TCPvolumeMounts:- mountPath: /nginxname: alertmanager-nginx- mountPath: /var/cache/nginxname: nginx-home- mountPath: /usr/share/nginx/htmlname: htmlsecurityContext:runAsUser: 101runAsGroup: 101

修改 Service 端口

如下:

apiVersion: v1
kind: Service
metadata:name: monitor-alertmanagerlabels:app.kubernetes.io/name: alertmanager
spec:ports:- name: nginx-httpprotocol: TCP# 修改以下 2 项port: 8080targetPort: nginx-http

最终效果

以这次的 AlertManager 为例,修改前:

修改后:(matcher 的例子更符合实际场景,并增加了多个示例。确实是很小的改动)

总结

Kubernetes 的 Pod 设计之初就定义为:一个 Pod 可以包含多个 Containers, 这为 Kubernetes 中 Pod 的 Sidecar 使用留下了无尽的想象空间。

Sidecar 一般是用来做辅助功能的,比如:

  1. 服务网格 (service mesh) 代理
  2. 监控 Exporter(如 redis exporter)
  3. ConfigMap 或/和 Secret Reloader(如 Prometheus 的 Config Reloader)
  4. Auth Proxy(如 OAuth Proxy 等)
  5. 7 层反向代理和 Web 服务器
  6. 日志整合(审计日志单独发到某个日志渠道。..)
  7. Demo 或 AllInOne 应用(如 nextcloud 或 Jaeger AllInOne 等示例应用)
  8. ...

我们这次通过加入 NGINX 作为 7 层反向代理和 Web 服务器用途的 Sidecar 来进行演示,生动地说明了 Sidecar 的实用之处。

🎉🎉🎉

📚️参考文档

  • Pod | Kubernetes

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper 编写.

更多推荐

K8S Pod Sidecar 应用场景之一

本文发布于:2024-03-13 09:42:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1733690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:场景   K8S   Pod   Sidecar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!