[英]How to debug ingress-controller connections with a single IP by ConfigMap
我們正在嘗試編輯我們的ingress-nginx.yml
以使 ingress-controllers pod 調試來自特定源 IP 的流量。 我們的設置是:
From NGINX and Kubernetes DOCs it appears there is no very easy way to debug traffic from a single ip (you cannot edit the nginx config directly). 所以,我們想添加debug_connection
指令,如下所示:
error_log /path/to/log;
...
events {
debug_connection 192.168.1.1;
}
正確的做法是通過 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(在此處仔細檢查您的入口控制器版本,該文件會發生巨大變化) 對於第 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/']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.