k8s ingress-nginx 安装配置和获取真实客户端ip
2021-12-17 本文已影响0人
王宣成
官方文档地址:https://kubernetes.github.io/ingress-nginx/deploy/
安装
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/cloud/deploy.yaml
检查pod 应该在ingress-nginx命名空间中启动
kubectl get pods --namespace=ingress-nginx
等待入口控制器 pod 启动、运行并准备就绪
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=120s
让我们创建一个简单的 Web 服务器和关联的服务
kubectl create deployment demo --image=httpd --port=80
kubectl expose deployment demo
查看该 IP 地址或 FQDN
kubectl get service ingress-nginx-controller --namespace=ingress-nginx
创建入口资源映射到主机,解析域名到服务器
kubectl create ingress demo-localhost --class=nginx \
--rule=demo.localdev.me/*=demo:80
此时,如果您访问 http://demo.localdev.me,您应该会看到一个HTML页面告诉您"它有效!
将本地端口转发到入口控制器
kubectl port-forward --namespace=ingress-nginx service/ingress-nginx-controller 8080:80
此时,如果您访问 http://demo.localdev.me:8080/,您应该会看到一个HTML页面告诉您"它有效!
开启ssl证书

进入容器可查看配置

cd /etc/nginx
cat nginx.conf

获取客户端真实ip配置 当前我的环境是 ingress-nginx > pod nginx 部署的 nginx+php


externalTrafficPolicy: Local


通过代理到是访问拿不到客户端ip的,需要再配置
compute-full-forwarded-for: 'true'
forwarded-for-header: X-Forwarded-For
use-forwarded-headers: 'true'
ingress-nginx 配置字典加上


pod容器 nginx配置加上
real_ip_header X-Forwarded-For

