K8S Pod Sidecar 应用场景之一-加入 NGINX Sidecar 做反代和 web 服务器 ...

金歌  金牌会员 | 2022-11-23 04:03:24 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 581|帖子 581|积分 1743

Kubernetes Pod Sidecar 简介


Sidecar 是一个独立的容器,与 Kubernetes pod 中的应用容器一起运行,是一种辅助性的应用。
Sidecar 的常见辅助性功能有这么几种:

  • 服务网格 (service mesh) 代理
  • 监控 Exporter(如 redis exporter)
  • ConfigMap 或/和 Secret Reloader(如 Prometheus 的 Config Reloader)
  • Auth Proxy(如 OAuth Proxy 等)
  • 7 层反向代理和 Web 服务器
  • 日志整合(审计日志单独发到某个日志渠道。..)
  • Demo 或 AllInOne 应用(如 nextcloud 或 Jaeger AllInOne 等示例应用)
  • ...
这里选几个场景细说一下,在服务网格的情况下,sidecar 负责从应用程序本身卸载服务网格中所有应用程序所需的功能--SSL/mTLS、流量路由、高可用性等,并实施部署各种高级发布模式,如断路器、金丝雀和蓝绿等。
作为数据平面组件,sidecar 通常由服务网格中的某种类型的控制平面管理。当 sidecar 路由应用流量并提供其他数据平面服务时,控制平面在必要时将 sidecars 注入 pod 并执行管理任务,例如更新 mTLS 证书并在需要时将其推送到适当的 sidecars。
日志整合场景下,Sidecar 被用来将多个应用实例的日志信息汇总并格式化为一个文件。
接下来进入本次的正题:将 NGINX (或 Caddy 等)作为 Sidecar 使用,主要用做反代和 web 服务器

场景假设

假设有这么一个场景:
我在使用原生的 Prometheus AlertManager, 我已经有 Ingress.
我现在想要做 2 件事:

  • 提升 AlertManager UI 的并发能力(增加 buffer, cache; 启用 gzip 等)
  • AlertManager 的某个 js(假设是 script.js), 我做了一点修改,但不希望侵入式地修改 原生 AlertManager 二进制文件,而是把修改后 js 放到 nginx 的 www 目录,让 nginx 来用不同的 location 进行处理。
这种场景下,显然 Ingress 是无法同时满足的。这时候就可以在 AlertManager Pod 里加个 NGINX 的 sidecar 来实现。
具体如下
NGINX Sidecar 典型使用步骤


  • 创建 NGINX Conf 的 configmap; (监听 8080, 反向代理到后端的 9093)
  • 创建 alertmanager script.js 的 configmap;
  • 修改原 AlertManager 的 StatefulSets, 增加:

    • NGINX Sidecar
    • 3 个 volumes: 其中 2 个就是用于挂载上面的 ConfigMap, 另外一个 EmptyDir 用于挂载 nginx cache

  • 修改 AlertManager Service 的端口,从 9093 改为 8080, name 从http 改为 nginx-http
  • (可选)修改其他部分,如 Ingress 等,调整端口。
NGINX Conf 的 ConfigMap

具体如下:
  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4.   name: alertmanager-nginx-proxy-config
  5.   labels:
  6.     app.kubernetes.io/name: alertmanager
  7. data:
  8.   nginx.conf: |-
  9.     worker_processes      auto;
  10.     error_log             /dev/stdout warn;
  11.     pid                   /var/cache/nginx/nginx.pid;
  12.     events {
  13.        worker_connections 1024;
  14.     }
  15.     http {
  16.       include       /etc/nginx/mime.types;
  17.       log_format    main '[$time_local - $status] $remote_addr - $remote_user $request ($http_referer)';
  18.       proxy_connect_timeout       10;
  19.       proxy_read_timeout          180;
  20.       proxy_send_timeout          5;
  21.       proxy_buffering             off;
  22.       proxy_cache_path            /var/cache/nginx/cache levels=1:2 keys_zone=my_zone:100m inactive=1d max_size=10g;
  23.       server {
  24.         listen          8080;
  25.         access_log      off;
  26.         gzip            on;
  27.         gzip_min_length 1k;
  28.         gzip_comp_level 2;
  29.         gzip_types      text/plain application/javascript application/x-javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
  30.         gzip_vary       on;
  31.         gzip_disable    "MSIE [1-6]\.";
  32.         proxy_set_header Host $host;
  33.         location = /script.js {
  34.           root /usr/share/nginx/html;
  35.           expires             90d;
  36.         }
  37.         location / {
  38.           proxy_cache         my_zone;
  39.           proxy_cache_valid   200 302 1d;
  40.           proxy_cache_valid   301 30d;
  41.           proxy_cache_valid   any 5m;
  42.           proxy_cache_bypass  $http_cache_control;
  43.           add_header          X-Proxy-Cache $upstream_cache_status;
  44.           add_header          Cache-Control "public";
  45.          
  46.           proxy_pass     http://localhost:9093/;
  47.           if ($request_filename ~ .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) {
  48.             expires             90d;
  49.           }
  50.         }
  51.       }
  52.     }
复制代码
AlertManager script.js ConfigMap

详细内容略。
先通过浏览器将script.js 下载下来。然后按需修改:
  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4.   name: alertmanager-script-js
  5.   labels:
  6.     app.kubernetes.io/name: alertmanager
  7. data:
  8.   script.js: >-
  9.     ...
复制代码
修改 StatefulSets

修改的部分内容如下:
  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4.   name: monitor-alertmanager
  5. spec:
  6.   template:
  7.     spec:
  8.       volumes:
  9.         # 增加 3 个 volumes
  10.         - name: nginx-home
  11.           emptyDir: {}
  12.         - name: html
  13.           configMap:
  14.             name: alertmanager-script-js
  15.             items:
  16.             - key: script.js
  17.               mode: 438
  18.               path: script.js
  19.         - name: alertmanager-nginx
  20.           configMap:
  21.             name: alertmanager-nginx-proxy-config
  22.             items:
  23.             - key: nginx.conf
  24.               mode: 438
  25.               path: nginx.conf
  26.       containers:
  27.         # 增加 NGINX sidecar
  28.         - name: alertmanager-proxy
  29.           args:
  30.           - nginx
  31.           - -g
  32.           - daemon off;
  33.           - -c
  34.           - /nginx/nginx.conf
  35.           image: "nginx:stable"
  36.           ports:
  37.           - containerPort: 8080
  38.             name: nginx-http
  39.             protocol: TCP
  40.           volumeMounts:
  41.           - mountPath: /nginx
  42.             name: alertmanager-nginx
  43.           - mountPath: /var/cache/nginx
  44.             name: nginx-home
  45.           - mountPath: /usr/share/nginx/html
  46.             name: html
  47.           securityContext:
  48.             runAsUser: 101
  49.             runAsGroup: 101
复制代码
修改 Service 端口

如下:
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4.   name: monitor-alertmanager
  5.   labels:
  6.     app.kubernetes.io/name: alertmanager
  7. spec:
  8.   ports:
  9.     - name: nginx-http
  10.       protocol: TCP
  11.       # 修改以下 2 项
  12.       port: 8080
  13.       targetPort: nginx-http
复制代码
最终效果

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

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

总结

Kubernetes 的 Pod 设计之初就定义为:一个 Pod 可以包含多个 Containers, 这为 Kubernetes 中 Pod 的 Sidecar 使用留下了无尽的想象空间。
Sidecar 一般是用来做辅助功能的,比如:

  • 服务网格 (service mesh) 代理
  • 监控 Exporter(如 redis exporter)
  • ConfigMap 或/和 Secret Reloader(如 Prometheus 的 Config Reloader)
  • Auth Proxy(如 OAuth Proxy 等)
  • 7 层反向代理和 Web 服务器
  • 日志整合(审计日志单独发到某个日志渠道。..)
  • Demo 或 AllInOne 应用(如 nextcloud 或 Jaeger AllInOne 等示例应用)
  • ...
我们这次通过加入 NGINX 作为 7 层反向代理和 Web 服务器用途的 Sidecar 来进行演示,生动地说明了 Sidecar 的实用之处。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表