阅读 103

k8s ingress-nginx 安装配置和获取真实客户端ip

官方文档地址: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证书


image.png

进入容器可查看配置


image.png

cd /etc/nginx
cat nginx.conf

image.png

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

image.png


image.png


  externalTrafficPolicy: Local

image.png

访问 http://ip:nodePort

image.png


通过代理到是访问拿不到客户端ip的,需要再配置

  compute-full-forwarded-for: 'true'
  forwarded-for-header: X-Forwarded-For  use-forwarded-headers: 'true'

ingress-nginx 配置字典加上


image.png


image.png

pod容器 nginx配置加上

real_ip_header   X-Forwarded-For

image.png



作者:王宣成
链接:https://www.jianshu.com/p/be0c723b8d96

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐