NGINX Kubernetes Ingress Controller 集成 NGINX App Protect WAF 实战:从部署到验证
发布时间:2026/10/14 7:05:10 锦皓数字建站

API网关云原生负载均衡可观测性微服务【免费下载链接】kubernetes-ingressNGINX and NGINX Plus Ingress Controllers for Kubernetes项目地址https://gitcode.com/gh_mirrors/ku/kubernetes-ingress点击查看免费下载本文基于 kubernetes-ingress 仓库中的 app-protect-waf 示例完整演示如何用 NGINX Plus Ingress Controller 与 NGINX App ProtectWAF保护一个运行在 Kubernetes 中的 Web 应用。你将掌握 App Protect 的 Policy、LogConf、User Defined Signature 三类 CRD 的创建方法学会通过 Ingress 注解启用 WAF 防护、把安全日志转发到 syslog并用 curl 实际验证拦截效果。示例概览本示例部署了以下组件NGINX Plus Ingress Controller已启用 NGINX App Protect 模块Cafe 示例应用coffee 与 tea 两个 Deployment 及对应 Service镜像为nginxdemos/hello:plain-textApp Protect 安全资源一个安全策略APPolicy、一个日志配置APLogConf和一个用户自定义签名APUserSigsyslog 服务用于接收并保存 App Protect 安全日志Ingress 资源通过注解把上述策略与日志配置绑定到路由上。从源码结构看App Protect 的 CRD 属于appprotect.f5.comAPI 组包含APPolicyappolicies、APLogConfaplogconfs、APUserSigapusersigs三类资源其 CRD 定义位于 config/crd/bases/appprotect.f5.com_appolicies.yaml、config/crd/bases/appprotect.f5.com_aplogconfs.yaml 和 config/crd/bases/appprotect.f5.com_apusersigs.yaml。第一步部署 Ingress Controller1. 生成示例所需 Secrets示例需要使用自签名证书等凭据仓库根目录的 Makefile 提供了make secrets目标它会调用 hack/secrets-gen 中的 Go 程序动态生成“just in time”的 TLS 证书与各类示例密钥make secrets执行该命令后会在示例目录下生成cafe-secret.yaml等文件包含证书与私钥供后续创建 TLS Secret 使用。2. 安装带 App Protect 的 Ingress Controller按照官方安装说明部署启用了 NGINX App Protect 的 NGINX Plus Ingress Controller。App Protect 功能依赖 NGINX Plus 订阅与对应的 WAF 模块镜像属于 NGINX Plus 的付费能力OSS 版 Ingress Controller 不包含该功能。安装完成后将 Controller 的公网 IP 和 HTTPS 端口保存到 shell 变量IC_IPXXX.YYY.ZZZ.III IC_HTTPS_PORTport number第二步部署 Cafe 示例应用创建 coffee 和 tea 两个 Deployment 及其 Servicekubectl create -f cafe.yamlcafe.yaml 的定义如下coffee 运行 2 个副本、tea 运行 3 个副本容器监听 80 端口并分别通过coffee-svc和tea-svc两个 Service 暴露端口 80。这是后续验证负载均衡与 WAF 拦截的后端目标。第三步配置 WAF 防护1. 创建 syslog 服务与 PodApp Protect 会把安全事件写入日志示例通过 syslog.yaml 部署一个基于balabit/syslog-ng:4.12.0镜像的 syslog 收集端暴露 514 端口kubectl create -f syslog.yamlIngress Controller 生成的 Nginx 配置会通过syslog:serversyslog-svc.default:514把安全日志转发到该 Pod随后我们可以进入 syslog Pod 查看被拦截请求的详细记录。2. 创建 TLS Secretkubectl create -f cafe-secret.yaml该 Secrettls-secret由make secrets生成包含自签名证书与私钥用于给cafe.example.com提供 HTTPS 访问。3. 创建 App Protect 策略、日志配置与用户自定义签名依次创建三类 CRDkubectl create -f ap-dataguard-alarm-policy.yaml kubectl create -f ap-logconf.yaml kubectl create -f ap-apple-uds.yamlAPPolicy安全策略ap-dataguard-alarm-policy.yaml 基于POLICY_TEMPLATE_NGINX_BASE模板关键配置项包括enforcementMode: blocking策略整体进入“阻断”模式命中的违规请求直接拒绝signature-requirementssignature-sets要求签名集合按Fruits标签筛选并启用名为apple_sigs的签名集且block: trueblocking-settings.violations对VIOL_DATA_GUARD违规仅告警alarm: true、block: falsedata-guard启用数据防泄漏对信用卡号creditCardNumbers与 US 社保号usSocialSecurityNumbers进行掩码处理maskData: true仅暴露后 4 位。从实现角度看Controller 在接收APPolicy后调用 pkg/apis/configuration/validation/appprotect.go 中的ValidateAppProtectPolicy进行校验要求spec.policy字段必须存在随后在 internal/k8s/appprotect/app_protect_configuration.go 中通过verifyPolicyAgainstUserSigs检查策略的signature-requirements是否被集群内的用户自定义签名APUserSig满足——这正是本示例中策略要求Fruits标签、而 UDS 提供该标签的原因。若存在未满足的签名需求策略会被标记为无效并从配置中剔除对应错误信息policy has unsatisfied signature requirements见 app_protect_configuration.go。APLogConf日志配置ap-logconf.yaml 定义安全日志的格式与过滤条件apiVersion: appprotect.f5.com/v1beta1 kind: APLogConf metadata: name: logconf spec: content: format: default # 使用默认日志格式 max_message_size: 64k max_request_size: any filter: request_type: all # 记录所有请求Controller 会对该对象调用ValidateAppProtectLogConf见 appprotect.go校验spec.content与spec.filter两个必填字段是否存在。APUserSig用户自定义签名ap-apple-uds.yaml 定义了一条自定义攻击签名规则content:apple; nocase;在请求内容中匹配字符串apple不区分大小写signatureType: request作用于请求attackType: Brute Force Attack、risk: medium、accuracy: mediumtag: Fruits打上Fruits标签与策略中的signature-requirements对应。UDS 会被ValidateAppProtectUserSig校验见 appprotect.go并且 Controller 会检测同一标签下的重复 UDS当多个 UDS 拥有相同tag时只保留创建时间最早的一个其余被拒绝错误信息duplicate tag set逻辑见 app_protect_configuration.go。4. 创建 Ingress 资源kubectl create -f cafe-ingress.yamlcafe-ingress.yaml 是 WAF 防护的“开关”所在注意其中的 App Protect 注解apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: cafe-ingress annotations: appprotect.f5.com/app-protect-policy: default/dataguard-alarm appprotect.f5.com/app-protect-enable: True appprotect.f5.com/app-protect-security-log-enable: True appprotect.f5.com/app-protect-security-log: default/logconf appprotect.f5.com/app-protect-security-log-destination: syslog:serversyslog-svc.default:514 spec: ingressClassName: nginx tls: - hosts: - cafe.example.com secretName: tls-secret rules: - host: cafe.example.com http: paths: - path: /tea pathType: Prefix backend: service: name: tea-svc port: number: 80 - path: /coffee pathType: Prefix backend: service: name: coffee-svc port: number: 80注解详解注解值示例作用appprotect.f5.com/app-protect-enableTrue/False是否对该 Ingress 启用 App Protect WAF 防护appprotect.f5.com/app-protect-policydefault/dataguard-alarm指定绑定的APPolicy格式为namespace/nameappprotect.f5.com/app-protect-security-log-enableTrue/False是否启用安全日志appprotect.f5.com/app-protect-security-logdefault/logconf指定绑定的APLogConfappprotect.f5.com/app-protect-security-log-destinationsyslog:serversyslog-svc.default:514安全日志的转发目的地从源码层面印证这些注解的常量定义位于 internal/configs/annotations.go并汇总在ingressWAFAnnotations列表中见 internal/configs/ingress.go。在 annotations.go 中Controller 会把app-protect-enable与app-protect-security-log-enable解析为布尔值并映射为 Nginx 配置中的app_protect_enableon/off与app_protect_log_enable参数。另外日志目的地有严格的格式校验ValidateAppProtectLogDestination见 pkg/apis/configuration/validation/appprotect_common.go要求目标必须是syslog:serverIP|localhost|FQDN:port、stderr或绝对文件路径且拒绝包含危险字符、相对路径或空白字符的写法。第四步验证 WAF 拦截效果1. 正常访问 coffee 与 tea使用curl的--insecure关闭自签名证书校验--resolve把cafe.example.com解析到 IC 的 IPcurl --resolve cafe.example.com:$IC_HTTPS_PORT:$IC_IP https://cafe.example.com:$IC_HTTPS_PORT/coffee --insecureServer address: 10.12.0.18:80 Server name: coffee-7586895968-r26zn ...curl --resolve cafe.example.com:$IC_HTTPS_PORT:$IC_IP https://cafe.example.com:$IC_HTTPS_PORT/tea --insecureServer address: 10.12.0.19:80 Server name: tea-7cd44fcb4d-xfw2x ...可以看到请求分别被负载均衡到 coffee 与 tea 的后端 Pod说明正常流量可以顺利通过。2. 触发攻击签名拦截发送一个带有script标签的可疑 URLcurl --resolve cafe.example.com:$IC_HTTPS_PORT:$IC_IP https://cafe.example.com:$IC_HTTPS_PORT/tea/script --insecurehtmlheadtitleRequest Rejected/title/headbody ...请求被 App Protect 内置的注入攻击签名拦截返回Request Rejected页面。3. 触发用户自定义签名拦截用 POST 发送匹配 UDS 规则的apple数据curl --resolve cafe.example.com:$IC_HTTPS_PORT:$IC_IP -X POST -d apple https://cafe.example.com:$IC_HTTPS_PORT/tea/ --insecurehtmlheadtitleRequest Rejected/title/headbody ...这条请求命中了我们在ap-apple-uds.yaml中定义的content:apple; nocase;规则同样被 App Protect 拦截。这正是用户自定义签名UDS的价值除了内置签名库WAF 还能按业务场景定制识别与拦截规则。4. 查看安全日志进入 syslog Pod检查被拦截请求的详细日志kubectl exec -it SYSLOG_POD -- cat /var/log/messages日志中会记录被拦截请求的请求行、触发签名、违规类型与策略名称等信息可用于后续安全分析与告警。底层原理小结从仓库源码可以梳理出 App Protect 资源的完整处理链路CRD 监听与校验Controller 监听appprotect.f5.com组的APPolicy、APLogConf、APUserSig资源通过 pkg/apis/configuration/validation 中的校验函数检查必填字段与非法引用如被弃用的外部引用字段会打印告警UDS 与策略的绑定解析在 internal/k8s/appprotect/app_protect_configuration.go 中解析策略的signature-requirements含可选的minRevisionDatetime/maxRevisionDatetime时间约束再通过verifyPolicyAgainstUserSigs确认集群内存在满足标签与版本要求的 UDS否则策略不生效并产生Rejected事件注解驱动的配置生成Ingress 上的appprotect.f5.com/*注解被解析后连同策略、日志配置一起写入 Nginx 配置最终由 App Protect 模块NGINX Plus 内嵌执行实时拦截与日志转发。整体架构可以概括为CRD 定义“防护规则” → Ingress 注解完成“规则绑定” → App Protect 模块执行“实时拦截” → syslog 承载“日志审计”。延伸阅读使用 VirtualServer 而不是 Ingress 启用 App Protect参考 app-protect-waf 自定义资源示例 及 app-protect-waf-v5App Protect 与 Ingress 结合的更多场景见 examples/ingress-resources/app-protect-waf-policy 与 app-protect-waf-policy-v5App Protect 的 DoS 防护能力参考 examples/custom-resources/app-protect-dosCRD 的完整字段说明见 docs/crd/appprotect.f5.com_appolicies.md、docs/crd/appprotect.f5.com_aplogconfs.md 与 docs/crd/appprotect.f5.com_apusersigs.md。赞分享API网关云原生负载均衡可观测性微服务【免费下载链接】kubernetes-ingressNGINX and NGINX Plus Ingress Controllers for Kubernetes项目地址https://gitcode.com/gh_mirrors/ku/kubernetes-ingress点击查看免费下载相关推荐NGINX Plus Ingress Controller 集成 App Protect WAF v5 实战从策略 Bundle 部署到 VirtualServer 防护配置NGINX Plus Ingress Controller 集成 App Protect WAF v5 实战从策略 Bundle 部署到 VirtualSerAPI网关云原生负载均衡可观测性微服务kubernetes-ingress 实战使用 NGINX Plus 与 NGINX App Protect 为 VirtualServer 配置 WAF 防护kubernetes ingress 实战使用 NGINX Plus 与 NGINX App Protect 为 VirtualServer 配置 WAF 防API网关云原生负载均衡可观测性微服务10分钟快速部署NGINX Ingress Controller到Kubernetes集群10分钟快速部署NGINX Ingress Controller到Kubernetes集群 想要在Kubernetes集群中高效管理外部流量吗NGINX InAPI网关云原生负载均衡可观测性微服务上一篇euler-copilot-rag架构设计如何构建企业级数据管理与检索增强服务下一篇大模型参数精度格式全解析从 FP32、BF16 到 FP8、INT4 与 1-bit 量化创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
锦
锦皓数字建站
深耕本土企业品牌数字化升级,专注原创端正雅致商务官网,从视觉设计到稳定运维全程保驾护航。