经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
K8S Pod Sidecar 应用场景之一-加入 NGINX Sidecar 做反代和 web 服务器
来源:cnblogs  作者:东风微鸣  时间:2023/2/27 9:42:54  对本文有异议

Kubernetes Pod Sidecar 简介

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 服务器

Web Server Sidecar

场景假设

假设有这么一个场景:

我在使用原生的 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

具体如下:

  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. proxy_pass http://localhost:9093/;
  46. if ($request_filename ~ .*\.(?:js|css|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) {
  47. expires 90d;
  48. }
  49. }
  50. }
  51. }

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 为例,修改前:

AlertManager UI - matcher

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

AlertManager UI - 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 的实用之处。

??????

???参考文档

原文链接:https://www.cnblogs.com/east4ming/p/17158451.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号