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-config
labels:
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-js
labels:
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-home
emptyDir: {}
- name: html
configMap:
name: alertmanager-script-js
items:
- key: script.js
mode: 438
path: script.js
- name: alertmanager-nginx
configMap:
name: alertmanager-nginx-proxy-config
items:
- key: nginx.conf
mode: 438
path: nginx.conf
containers:
# 增加 NGINX sidecar
- name: alertmanager-proxy
args:
- nginx
- -g
- daemon off;
- -c
- /nginx/nginx.conf
image: "nginx:stable"
ports:
- containerPort: 8080
name: nginx-http
protocol: TCP
volumeMounts:
- mountPath: /nginx
name: alertmanager-nginx
- mountPath: /var/cache/nginx
name: nginx-home
- mountPath: /usr/share/nginx/html
name: html
securityContext:
runAsUser: 101
runAsGroup: 101

修改 Service 端口

如下:

apiVersion: v1
kind: Service
metadata:
name: monitor-alertmanager
labels:
app.kubernetes.io/name: alertmanager
spec:
ports:
- name: nginx-http
protocol: TCP
# 修改以下 2 项
port: 8080
targetPort: 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 的实用之处。

️参考文档

K8S Pod Sidecar 应用场景之一-加入 NGINX Sidecar 做反代和 web 服务器的更多相关文章

  1. [阿里云部署] Ubuntu+Flask+Nginx+uWSGI+Mysql搭建阿里云Web服务器

    部署地址:123.56.7.181 Ubuntu+Flask+Nginx+uWSGI+Mysql搭建阿里云Web服务器 这个标题就比之前的"ECS服务器配置Web环境的全过程及参考资料&qu ...

  2. nginx+uWSGI+django+virtualenv+supervisor发布web服务器

    nginx+uWSGI+django+virtualenv+supervisor发布web服务器   导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以 ...

  3. nginx+uwsgi+django+virtualenv+supervisor部署web服务器

    wsgi 全称web server gateway interface,wsgi不是服务器,也不是python模块,只是一种协议,描述web server如何和web application通信的规则 ...

  4. Linux - nginx+uWSGI+django+virtualenv+supervisor发布web服务器

    目录 Linux - nginx+uWSGI+django+virtualenv+supervisor发布web服务器 crm django项目部署流程 使用supervisro启动uwsgi,退出虚 ...

  5. 使用nginx做反代时遇到413 Request Entity Too Large的解决方法

    在使用nginx做反向代理的时候,被反代的系统在上传文件的时候遇到413 错误 :Request Entity Too Large 原因是nginx限制了上传文件的大小,在nginx中可以配置最大允许 ...

  6. 12,nginx+uWSGI+django+virtualenv+supervisor发布web服务器

    导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的框架 ...

  7. nginx+uWSGI+django+virtualenv+supervisor发布web服务器流程

    导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求)基于wsgi运行的框架有 ...

  8. 三 nginx+uWSGI+django+virtualenv+supervisor发布web服务器

    https://www.cnblogs.com/pyyu/p/9481344.html?tdsourcetag=s_pcqq_aiomsg 一 uwsgi安装 1.安装uwsgi,进入到一个虚拟机环境 ...

  9. Nginx和Squid配合搭建的Web服务器前端系统

    这个架构是目前我个人觉得比较稳妥并且最方便的架构,易于多数人接受: 前端的lvs和squid,按照安装方法,把epoll打开,配置文件照搬,基本上问题不多. 这个架构和app_squid架构的区别,也 ...

  10. nginx+uWSGI+django+virtualenv+superviso发布web服务器

    1.环境依赖 yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel pcre-dev ...

随机推荐

  1. “XZ”格式文件解压

    1.下载xz 官网:https://tukaani.org/xz/ 例:wget https://nchc.dl.sourceforge.net/project/lzmautils/xz-5.2.6. ...

  2. Zabbix技术分享——docker组件编译使用教程

    docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源,它可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的Linux机器上,还可以实现 ...

  3. [.NET学习] EFCore学习之旅 -3 一些其他的迁移命令

    1.Update-DataBase  xxx 概述:将数据库回滚到某个版本. 1.首先创建一个表 Dog 2.生成迁移 Add-Migration CreateDogTable 并更新到数据库 Upd ...

  4. 《MySQL必知必会》知识汇总二

    六.用通配符进行过滤 本章介绍什么是通配符.如何使用通配符以及怎样使用LIKE操作符进行通配搜索 LIKE操作符 百分号(%)通配符 select prod_id,prod_name from pro ...

  5. Idea中Git的常用操作及可能存在的问题

    一.使用 1.从git上下载项目 (1)默认branch下载(pull) (2)指定branch下载 (3)克隆远程仓库到本地(git clone) git clone https://github. ...

  6. 3、mysql着重号解决关键字冲突

    1.着重号(`  `): 使用着重号(` `)将字段名或表名括起来解决冲突:保证表中的字段.表名等没有和保留字.数据库系统名或常用方法名冲突

  7. 【转载】MSSQL汉字首字母查询处理自定义函数

    -- 汉字首字母查询处理用户定义函数 CREATE FUNCTION f_GetPY(@str nvarchar(4000)) RETURNS nvarchar(4000) AS BEGIN DECL ...

  8. Jupyter Notebook入门指南

    作者:京东科技隐私计算产品部 孙晓军 1. Jupyter Notebook介绍 图1 Jupter项目整体架构 [https://docs.jupyter.org/en/latest/project ...

  9. 发布个工具,一键恢复Win8/8.1中的微软拼音长句模式(新体验模式)

    (cnBeta:http://www.cnbeta.com/articles/277936.htm) 首先贴个图,大家来一起念台词~ 念完了木有?很激情澎湃义愤填膺有木有? 这事情最早追溯到前年 8 ...

  10. [C++]default constructor默认构造函数

    例子: class A{ public: int a; char b; } A temp; cout<<temp.a<<endl; 问题1:什么时候会合成出一个default ...