原创-K8S INGRESS配置分析 并总结一次配置事故

摘要:
上周,在调整K8S域名中一段PATH的入口白名单时。

上周在调整K8S中某域名其中一段PATH的ingress白名单问题时,由于对ingress的白名单策略理解不充分导致错误配置,使白名单应用到全域名中造成整个域名403。

特此花时间研究一下整个ingress相关的配置。

参考文档:https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/annotations.md

问题配置:

nginx.ingress.kubernetes.io/server-snippet: |
set_real_ip_from 0.0.0.0/0;
real_ip_header RemoteIp;
real_ip_recursive on;
allow 1.2.3.4;
deny all;

语句分析:

先看下原文解释:Using the annotation nginx.ingress.kubernetes.io/server-snippet it is possible to add custom configuration in the server configuration block.

attention This annotation can be used only once per host.

意思使server-snippet的配置是添加在server的配置块的,而且只可以使用一次。而我在配置时,误以为将该配置应用于单个path的ingress即可局部生效,导致ingress起来后,该配置应用于域名全局,造成几乎100%的403错误。可谓危险。

-----

server-snippet其他配置分析:

例子一:apiVersion: networking.k8s.io/v1beta1

kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
        set $agentflag 0;

        if ($http_user_agent ~* "(Mobile)" ){
          set $agentflag 1;
        }

        if ( $agentflag = 1 ) {
          return 301 https://m.example.com;
        }
在server全局块设置变量默认0,如果是移动请求,将请求变为1,同时跳转301至移动页面。


 从整体上看,server-snippet里面的配置与常规的nginx无异,不过即使在单个ingress中应用,也会使域名全局生效。正确的配置应该是

nginx.ingress.kubernetes.io/configuration-snippet
Using this annotation you can add additional configuration to the NGINX location.

特别注意!!!在ingress中还有一个
nginx.ingress.kubernetes.io/whitelist-source-range
的配置,同样是白名单的设置,
Adding an annotation to an Ingress rule overrides any global restriction.
意思是这个设置也是全局性的,要千万注意。

-----

nginx.ingress.kubernetes.io其他配置分析:
nginx.ingress.kubernetes.io/app-root:根路径重定向
If the Application Root is exposed in a different path and needs to be redirected, set the annotation nginx.ingress.kubernetes.io/app-root to redirect requests for /.
如果应用程序根在不同的路径中公开并且需要重定向,请设置注释重定向/的请求。


nginx.ingress.kubernetes.io/affinity:会话亲和性
The annotation nginx.ingre
ss.kubernetes.io/affinity
 enables and sets the affinity type in all Upstreams of an Ingress
The only affinity type available for NGINX is cookie. nginx-ingress的会话类型只能选择cookie。

nginx.ingress.kubernetes.io/affinity-mode:会话亲和性模式
The annotation nginx.ingress.kubernetes.io/affinity-mode defines the stickyness of a session. Setting this to balanced (default) will redistribute some sessions if a deployment gets scaled up, therefore rebalancing the load on the servers. Setting this to persistent will not rebalance sessions to new servers, therefore providing maximum stickyness.
俩种模式:balanced和persistent,balanced可以在pod扩展时将请求发送至新pod,persistent则保持最大的粘性,不会在有新扩展时发送给新pod。

nginx.ingress.kubernetes.io/auth-type: [basic|digest]
nginx.ingress.kubernetes.io/auth-secret: secretName 添加验证
It is possible to add authentication by adding additional annotations in the Ingress rule. The source of the authentication is a secret that contains usernames and passwords.

nginx.ingress.kubernetes.io/auth-tls-secret: secretName
nginx.ingress.kubernetes.io/auth-tls-verify-depth
nginx.ingress.kubernetes.io/auth-tls-verify-client
nginx.ingress.kubernetes.io/auth-tls-error-page
nginx.ingress.kubernetes.io/auth-tls-pass-certificate-to-upstream
添加证书认证

nginx.ingress.kubernetes.io/backend-protocol

Using backend-protocol annotations is possible to indicate how NGINX should communicate with the backend service. (Replaces secure-backends in older versions) Valid Values: HTTP, HTTPS, GRPC, GRPCS, AJP and FCGI

By default NGINX uses HTTP.  默认情况下后端默认使用http协议通信,也可以指定其他。

添加后端协议

nginx.ingress.kubernetes.io/canary
In some cases, you may want to "canary" a new set of changes by sending a small number of requests to a different service than the production service. The canary annotation enables the Ingress spec to act as an alternative service for requests to route to depending on the rules applied. The following annotations to configure canary can be enabled after nginx.ingress.kubernetes.io/canary: "true" is set:
可以使用canary模式将小部分流量导入canary的新服务用于测试,canary金丝雀指灰度版本的意思

nginx.ingress.kubernetes.io/client-body-buffer-size
Sets buffer size for reading client request body per location. In case the request body is larger than the buffer, the whole body or only its part is written to a temporary file. By default, buffer size is equal to two memory pages. This is 8K on x86, other 32-bit platforms, and x86-64. It is usually 16K on other 64-bit platforms. This annotation is applied to each location provided in the ingress rule.
设置读取客户端请求正文的缓冲区大小,如客户端post或上传文件。

nginx.ingress.kubernetes.io/enable-cors: "true"
开启跨域支持

nginx.ingress.kubernetes.io/permanent-redirect
永久重定向至某域名

-----
还有更多的配置请见官方文档。


 

免责声明:文章转载自《原创-K8S INGRESS配置分析 并总结一次配置事故》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Nginx简介及环境搭建CDK安装下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Apache Tomcat 7 安装与配置

下载 首先需要下载tomcat7的安装文件,地址如下: http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz 安装与配置 安装之前需要确保已经安装JDK,若没有安装JDK请参考上一篇blog先正确安装JDK: Linux CentOS...

ES集群Master节点配置问题

ES集群的主节点发现机制采用单播形式,主要配置有三行,如下: discovery.zen.minimum_master_nodes: 2 discovery.zen.ping.multicast.enabled: false discovery.zen.ping.unicast.hosts: ["localhost:9001","localhost:9...

Linux清空文件内容方法大全

Linux系统中一切皆文件。 所以在清空或删除文件之前一定要确保该文件不是系统文件或者其他重要配置文件,否则可能引发系统错误。 一、通过重定向来清空文件内容 该方法是最简单的,通过shell重定向null到指定文件即可 $ > system.log 还有两个重定向方法: 重定向:或者true到指定文件 $ :> system.log 或者...

学习bash——数据流重定向

一、概述 1. 数据流 定义:以规定顺序被读取一次的数据序列。 分类:标准输入(stdin)、标准输出(stdout)和标准错误输出(stderr)。 标准输出:指的是命令执行所回传的正确信息。 标准错误输出:指的是命令执行失败后,所回传的错误信息。 2. 文件描述符 定义:在形式上是一个非负整数。实际上,它是一个索引值,所有打开的文件都通过文件描述符...

[原创]ASP.NET中Response.Redirect()方法深度剖析[转]

本文中,我们将借助http分析工具Fiddler,来对Response.Redirect()方法的整个流程进行剖析      http://www.cnblogs.com/ybwang/archive/2010/07/20/1781800.html 首先在Visual Studio中新建一个网站,在Default页面的Page_Load事件中写下Resp...

flask请求钩子、HTTP响应、响应报文、重定向、手动返回错误码、修改MIME类型、jsonify()方法 --

请求钩子: 当我们需要对请求进行预处理和后处理时,就可以用Flask提供的回调函数(钩子),他们可用来注册在请求处理的不同阶段执行的处理函数。这些请求钩子使用装饰器实现,通过程序实例app调用,以 before_request钩子为例(请求之前),当你对一个函数附加了app.before_request装饰器后,就会将这个函数注册为before_reque...