如何通过 ConfigMap 调试具有单个 IP 的入口控制器连接
How to debug ingress-controller connections with a single IP by ConfigMap
我们正在尝试编辑 ingress-nginx.yml
以制作入口控制器 pods debug traffic coming from a specific source IP。
我们的设置是:
- Kubernetes v1.13
- 入口控制器 v0.24.1
从 NGINX 和 Kubernetes DOC 来看,似乎没有非常简单的方法来调试来自单个 ip 的流量(您不能直接编辑 nginx 配置)。所以,我们想添加 debug_connection
指令,使其看起来像这样:
error_log /path/to/log;
...
events {
debug_connection 192.168.1.1;
}
The correct way to do it 应该通过 ConfigMap 中的 CustomAnnotations + 一个新的入口来启用 CustomAnnotation,所以我们尝试了这个:
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app: ingress-nginx
data:
ingress-template: |
#Creating the custom annotation to make debug_connection on/off
{if index $.Ingress.Annotations "custom.nginx.org/debug_connection"}
{$ip := index $.Ingress.Annotations "custom.nginx.org/ip"}
{end}
{range $events := .Events}
events {
# handling custom.nginx.org/debug_connection
{if index $.Ingress.Annotations "custom.nginx.org/debug_connection"}
{end}
并且:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: debugenabler
annotations:
kubernetes.io/ingress.class: "nginx"
custom.nginx.org/debug_connection: "on"
custom.nginx.org/ip: "192.168.1.1"
spec:
rules:
- host: "ourhostname"
http:
paths:
- path: /tea
backend:
serviceName: tea-svc
servicePort: 80
- path: /coffee
backend:
serviceName: coffee-svc
servicePort: 80
我们应用 ingress-nginx.yml
没有错误。我们在 nginx conf 中看到新行:
location /coffee {
set $namespace "test";
set $ingress_name "debugenabler";
set $service_name "coffee-svc";
set $service_port "80";
set $location_path "/coffee";
rewrite_by_lua_block {
lua_ingress.rewrite({
force_ssl_redirect = true,
use_port_in_redirects = false,
})
balancer.rewrite()
但是 events
块中的 debug_connection 仍然一无所获:
events {
multi_accept on;
worker_connections 16384;
use epoll;
}
如何在事件上下文中插入 debug_connection?
对于那些可能面临类似挑战的人,我实际上是通过以下方式做到的:
- 使用包含
debug_connection
行的新入口控制器模板文件 (nginx.tmpl
) 创建 ConfigMap(在此处仔细检查您的入口控制器版本,文件变化很大)
- 创建链接到 Configmap 的卷(指定卷和卷挂载)
- 创建一个 InitContainer,它在容器启动之前复制 /etc/nginx/template 中的卷内容(这需要克服可能与权限相关的问题)。
对于第2点和第3点你可以在deployment
或pod
代码末尾添加相关代码,我分享一个例子:
volumes:
- name: nginxconf2
configMap:
name: nginxconf2
items:
- key: nginx.tmpl
path: nginx.tmpl
initContainers:
- name: copy-configs
image: {{ kubernetes.ingress_nginx.image }}
volumeMounts:
- mountPath: /nginx
name: nginxconf2
command: ['sh', '-c', 'cp -R /nginx/ /etc/nginx/template/']
我们正在尝试编辑 ingress-nginx.yml
以制作入口控制器 pods debug traffic coming from a specific source IP。
我们的设置是:
- Kubernetes v1.13
- 入口控制器 v0.24.1
从 NGINX 和 Kubernetes DOC 来看,似乎没有非常简单的方法来调试来自单个 ip 的流量(您不能直接编辑 nginx 配置)。所以,我们想添加 debug_connection
指令,使其看起来像这样:
error_log /path/to/log;
...
events {
debug_connection 192.168.1.1;
}
The correct way to do it 应该通过 ConfigMap 中的 CustomAnnotations + 一个新的入口来启用 CustomAnnotation,所以我们尝试了这个:
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app: ingress-nginx
data:
ingress-template: |
#Creating the custom annotation to make debug_connection on/off
{if index $.Ingress.Annotations "custom.nginx.org/debug_connection"}
{$ip := index $.Ingress.Annotations "custom.nginx.org/ip"}
{end}
{range $events := .Events}
events {
# handling custom.nginx.org/debug_connection
{if index $.Ingress.Annotations "custom.nginx.org/debug_connection"}
{end}
并且:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: debugenabler
annotations:
kubernetes.io/ingress.class: "nginx"
custom.nginx.org/debug_connection: "on"
custom.nginx.org/ip: "192.168.1.1"
spec:
rules:
- host: "ourhostname"
http:
paths:
- path: /tea
backend:
serviceName: tea-svc
servicePort: 80
- path: /coffee
backend:
serviceName: coffee-svc
servicePort: 80
我们应用 ingress-nginx.yml
没有错误。我们在 nginx conf 中看到新行:
location /coffee {
set $namespace "test";
set $ingress_name "debugenabler";
set $service_name "coffee-svc";
set $service_port "80";
set $location_path "/coffee";
rewrite_by_lua_block {
lua_ingress.rewrite({
force_ssl_redirect = true,
use_port_in_redirects = false,
})
balancer.rewrite()
但是 events
块中的 debug_connection 仍然一无所获:
events {
multi_accept on;
worker_connections 16384;
use epoll;
}
如何在事件上下文中插入 debug_connection?
对于那些可能面临类似挑战的人,我实际上是通过以下方式做到的:
- 使用包含
debug_connection
行的新入口控制器模板文件 (nginx.tmpl
) 创建 ConfigMap(在此处仔细检查您的入口控制器版本,文件变化很大) - 创建链接到 Configmap 的卷(指定卷和卷挂载)
- 创建一个 InitContainer,它在容器启动之前复制 /etc/nginx/template 中的卷内容(这需要克服可能与权限相关的问题)。
对于第2点和第3点你可以在deployment
或pod
代码末尾添加相关代码,我分享一个例子:
volumes:
- name: nginxconf2
configMap:
name: nginxconf2
items:
- key: nginx.tmpl
path: nginx.tmpl
initContainers:
- name: copy-configs
image: {{ kubernetes.ingress_nginx.image }}
volumeMounts:
- mountPath: /nginx
name: nginxconf2
command: ['sh', '-c', 'cp -R /nginx/ /etc/nginx/template/']