美文网首页k8sK8s
k8s ingress-nginx 安装配置和获取真实客户端ip

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

作者: 王宣成 | 来源:发表于2021-12-17 17:18 被阅读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证书


    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 image.png

    相关文章

      网友评论

        本文标题:k8s ingress-nginx 安装配置和获取真实客户端ip

        本文链接:https://www.haomeiwen.com/subject/vetifrtx.html