kubernetes中ingress控制器traefik获取真实客户源IP

news2025/7/19 16:51:12

一.现象

公司kubenetes生产环境使用的ingress控制器是traefik,默认是通过deployment部署的,现在研发上反馈不能获取客户的真实源IP地址,通过x_forward_for获取的IP地址都是kubernetes集群内部的IP地址。

二.解决思路

通过查找traefik的官方文档

Traefik EntryPoints Documentation - Traefik

traefik传输客户源地址到后端需要配置Forwarded Headers参数,用于x_forwarded_for保存客户源IP地址

具体配置如下

 yaml格式,用于启用的时候指定配置文件

## Static configuration
entryPoints:
  web:
    address: ":80"
    forwardedHeaders:
      insecure: true

TOML格式,用于traefik启动的时候指定配置文件,这个文件在kubernetes中可以以comfigmap存在

## Static configuration
[entryPoints]
  [entryPoints.web]
    address = ":80"

    [entryPoints.web.forwardedHeaders]
      insecure = true

命名行格式

--entryPoints.web.address=:80
--entryPoints.web.forwardedHeaders.insecure

三.问题处理

1.公司现有环境配置

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    meta.helm.sh/release-name: traefik
    meta.helm.sh/release-namespace: default
  labels:
    app.kubernetes.io/instance: traefik
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: traefik
    helm.sh/chart: traefik-9.11.0
  name: traefik
  namespace: default
  resourceVersion: '505763774'
spec:
  progressDeadlineSeconds: 600
  replicas: 6
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/instance: traefik
      app.kubernetes.io/name: traefik
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app.kubernetes.io/instance: traefik
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/name: traefik
        helm.sh/chart: traefik-9.11.0
    spec:
      containers:
        - args:
            - '--global.checknewversion'
            - '--global.sendanonymoususage'
            - '--entryPoints.traefik.address=:9000/tcp'
            - '--entryPoints.web.address=:8000/tcp'
            - '--entryPoints.websecure.address=:8443/tcp'
            - '--api.dashboard=true'
            - '--ping=true'
            - '--providers.kubernetescrd'
            - '--providers.kubernetesingress'
          image: 'traefik:2.3.3'
          imagePullPolicy: IfNotPresent
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /ping
              port: 9000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 2
          name: traefik
          ports:
            - containerPort: 9000
              name: traefik
              protocol: TCP
            - containerPort: 8000
              name: web
              protocol: TCP
            - containerPort: 8443
              name: websecure
              protocol: TCP
          readinessProbe:
            failureThreshold: 1
            httpGet:
              path: /ping
              port: 9000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 2
          resources: {}
          securityContext:
            capabilities:
              drop:
                - ALL
            readOnlyRootFilesystem: true
            runAsGroup: 65532
            runAsNonRoot: true
            runAsUser: 65532
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /data
              name: data
            - mountPath: /tmp
              name: tmp
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        fsGroup: 65532
      serviceAccount: traefik
      serviceAccountName: traefik
      terminationGracePeriodSeconds: 60
      volumes:
        - emptyDir: {}
          name: data
        - emptyDir: {}
          name: tmp
status:
  availableReplicas: 6
  conditions:
    - lastTransitionTime: '2022-10-10T07:58:50Z'
      lastUpdateTime: '2022-10-10T07:58:50Z'
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: 'True'
      type: Available
    - lastTransitionTime: '2020-11-25T22:53:59Z'
      lastUpdateTime: '2022-11-17T10:44:40Z'
      message: ReplicaSet "traefik-54bf67c74d" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: 'True'
      type: Progressing
  observedGeneration: 13
  readyReplicas: 6
  replicas: 6
  updatedReplicas: 6

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    meta.helm.sh/release-name: traefik
    meta.helm.sh/release-namespace: default
  labels:
    app.kubernetes.io/instance: traefik
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: traefik
    helm.sh/chart: traefik-9.11.0
  name: traefik
  namespace: default
  resourceVersion: '505762379'
spec:
  clusterIP: 10.96.252.108
  externalTrafficPolicy: Cluster
  ports:
    - name: web
      nodePort: 30079
      port: 80
      protocol: TCP
      targetPort: web
  selector:
    app.kubernetes.io/instance: traefik
    app.kubernetes.io/name: traefik
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

发现trafik启动的参数里面没有指定配置文件,也没有以comfigmap形式存在,所以只能考虑通过添加启动参数的方式(即命令行方式)解决问题

2.解决问题

在deployment部署中的traefik启动参数中添加 - '--entrypoints.web.forwardedHeaders.insecure'和- '--entrypoints.websecure.forwardedHeaders.insecure'启动参数,其中标红的web和websecure根据--entryPoints.web.address=:8000/tcp和--entryPoints.websecure.address=:8443/tcp要保持一致,有可能不一样。

修改后的derloyment的yaml文件如下:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    meta.helm.sh/release-name: traefik
    meta.helm.sh/release-namespace: default
  labels:
    app.kubernetes.io/instance: traefik
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: traefik
    helm.sh/chart: traefik-9.11.0
  name: traefik
  namespace: default
  resourceVersion: '505763774'
spec:
  progressDeadlineSeconds: 600
  replicas: 6
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/instance: traefik
      app.kubernetes.io/name: traefik
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app.kubernetes.io/instance: traefik
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/name: traefik
        helm.sh/chart: traefik-9.11.0
    spec:
      containers:
        - args:
            - '--global.checknewversion'
            - '--global.sendanonymoususage'
            - '--entryPoints.traefik.address=:9000/tcp'
            - '--entryPoints.web.address=:8000/tcp'
            - '--entryPoints.websecure.address=:8443/tcp'
            - '--api.dashboard=true'
            - '--ping=true'
            - '--providers.kubernetescrd'
            - '--providers.kubernetesingress'
            - '--entrypoints.web.forwardedHeaders.insecure'
            - '--entrypoints.websecure.forwardedHeaders.insecure'
          image: 'traefik:2.3.3'
          imagePullPolicy: IfNotPresent
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /ping
              port: 9000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 2
          name: traefik
          ports:
            - containerPort: 9000
              name: traefik
              protocol: TCP
            - containerPort: 8000
              name: web
              protocol: TCP
            - containerPort: 8443
              name: websecure
              protocol: TCP
          readinessProbe:
            failureThreshold: 1
            httpGet:
              path: /ping
              port: 9000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 2
          resources: {}
          securityContext:
            capabilities:
              drop:
                - ALL
            readOnlyRootFilesystem: true
            runAsGroup: 65532
            runAsNonRoot: true
            runAsUser: 65532
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /data
              name: data
            - mountPath: /tmp
              name: tmp
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        fsGroup: 65532
      serviceAccount: traefik
      serviceAccountName: traefik
      terminationGracePeriodSeconds: 60
      volumes:
        - emptyDir: {}
          name: data
        - emptyDir: {}
          name: tmp
status:
  availableReplicas: 6
  conditions:
    - lastTransitionTime: '2022-10-10T07:58:50Z'
      lastUpdateTime: '2022-10-10T07:58:50Z'
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: 'True'
      type: Available
    - lastTransitionTime: '2020-11-25T22:53:59Z'
      lastUpdateTime: '2022-11-17T10:44:40Z'
      message: ReplicaSet "traefik-54bf67c74d" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: 'True'
      type: Progressing
  observedGeneration: 13
  readyReplicas: 6
  replicas: 6
  updatedReplicas: 6

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    meta.helm.sh/release-name: traefik
    meta.helm.sh/release-namespace: default
  labels:
    app.kubernetes.io/instance: traefik
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: traefik
    helm.sh/chart: traefik-9.11.0
  name: traefik
  namespace: default
  resourceVersion: '505762379'
spec:
  clusterIP: 10.96.252.108
  externalTrafficPolicy: Cluster
  ports:
    - name: web
      nodePort: 30079
      port: 80
      protocol: TCP
      targetPort: web
  selector:
    app.kubernetes.io/instance: traefik
    app.kubernetes.io/name: traefik
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

3.验证测试

通过和研发和业务验证测试,通过x_forwarded_for获取的客户端源Ip地址格式如下

 其中第一个公网IP地址是客户的真实IP地址,其他几个是经过的k8s节点和pod的IP地址,至此解决业务上获取真实客户IP地址的需求。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/17411.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【雨夜】业务中 自定义异常用 Exception 还是 RuntimeException? 为什么?

今天和同事 聊了下异常 相关的事,整理在此 目前公司中使用的 自定义异常是 extend RuntimeException 目录 思维导图 继承异常 我们在业务开发中 继承异常是extend RuntimeException 还是 Exception呢 一想 这肯定是 RuntimeException 啊,但是这是为什…

kingdee漏洞金蝶云星空存在弱口令漏洞

kingdee漏洞简介 金蝶云星是基于云计算、大数据、社交、人工智能、物联网等前沿技术研发的新一代战略性企业管理软件。金蝶云星空存在弱口令漏洞,攻击者利用该漏洞登录系统后台,获取敏感信息。 CNNVD编号:CNVD-2022-15854危害等级&#xff…

【C++笔试强训】第二十七天

🎇C笔试强训 博客主页:一起去看日落吗分享博主的C刷题日常,大家一起学习博主的能力有限,出现错误希望大家不吝赐教分享给大家一句我很喜欢的话:夜色难免微凉,前方必有曙光 🌞。 💦&a…

使用PostMan测试WebService接口

在浏览器中输入 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 查看是否启动服务,确保WS地址可用。通过地址获取获取WSDL地址相关参数,并找空间命名,这个位置是固定的,下面会用到,这里是 http://WebXml.c…

62岁腾格尔要开线上演唱会,直播间能否唱《遥远的地方》成焦点

人生六十岁一花甲,著名草原歌手腾格尔,在经历了人生的风风雨雨后,如今已经步入了六十二岁的年龄。虽然已经年过花甲,但是腾格尔老师依旧意气风发,他的个人线上演唱会,也准备在11月19日与大家见面。 在网友的…

【C++布隆过滤器和哈希切分】

目录 1.布隆过滤器概念 2.布隆过滤器优点 3.哈希切分 位图储存的类型只能是整形,有没有储存自定义类型或者是字符串类型的“位图”呢? 1.布隆过滤器概念 步隆过滤器是由布隆(Burton Howard Bloom)在1970年提出的 一种紧凑型的…

从3D ToF到智能座舱系统方案,英飞凌如何赋能未来出行?

当前,全球汽车产业正处于大变革和市场重构的关键时期,智能汽车新时代的大幕已然拉开。 “智能座舱、人机交互已经成为车载信息娱乐系统‘智能化’的新亮点。”英飞凌相关负责人表示。在汽车智能化、网联化、电动化等大趋势下,越来越多的创新…

【C++进阶】map和set——下篇(红黑树的学习以及封装map和set)

🎇C学习历程:入门 博客主页:一起去看日落吗持续分享博主的C学习历程博主的能力有限,出现错误希望大家不吝赐教分享给大家一句我很喜欢的话: 也许你现在做的事情,暂时看不到成果,但不要忘记&…

ggplot2图形简单绘制

文章目录一、所用函数1. sort、rank、order用法2. arrange 用法3. reorder用法4. cumsum 用法5. interaction用法二、散点图三、折线图、直方图、箱线图四、柱状图1. 单一变量 (统计单一变量的属性值分布)2. 单一变量fill (列联表&#xff09…

mybatis详解

学习到现在,作为我们的java萌新来说,是时候来学习一点偷懒的武林秘籍了,今天我给大家介绍的就是在无上秘宝--mybatis持久型框架. 学习一个东西之前,我们得首先了解他的前世今生... 前世:原是Apache的一个开源项目iBatis, 2010年6月这个项目由ApacheSoftware Foundation 迁移到…

SQL注入靶机练习:BUU SQL COURSE 1

SQL注入靶机练习:BUU SQL COURSE 1一、SQL注入知识点二、前置知识三、SQL注入测试的一般步骤四、解题过程一、SQL注入知识点 可参考SQL注入详解 二、前置知识 参考来源:渗透攻防Web篇-深入浅出SQL注入 mysql5.0以上版本中存在一个重要的系统数据…

无敌,全面对标字节跳动2-2:算法与数据结构突击手册(leetcode)

学习是一种基础性的能力。然而,“吾生也有涯,而知也无涯。”,如果学习不注意方法,则会“以有涯随无涯,殆矣”。 学习就像吃饭睡觉一样,是人的一种本能,人人都有学习的能力。我们在刚出生的时候…

某城市道路桥梁设计计算书+cad图纸

第一章 工程概述 41 1.1设计题目 41 1.2 设计资料 42 1.3 桥梁设计的基本要求 42 1.3.1 使用上的要求: 42 1.3.2 经济上的要求: 42 1.3.3 结构和尺寸上的要求: 42 1.3.4 施工上的要求: 42 1.3.5美观上的要求&#xff1…

Aspose.PDF for Java Crack by Xacker

Aspose.PDF for Java 是一个本地库,使开发人员能够将 PDF 处理功能添加到他们的应用程序中。API 可用于构建任何类型的 32 位和 64 位应用程序,以在不使用 Adob​​e Acrobat 的情况下生成或读取、转换和操作 PDF 文件。 Aspose.PDF for Java API 允许执…

嵌入式(驱动开发)(内核内存管理)

一、内核内存管理框架 内核将物理内存等分成N块4KB,称之为一页,每页都用一个struct page来表示,采用伙伴关系算法维护 内核地址空间划分图: 3G~3G896M:低端内存,直接映射 虚拟地址 3G 物理地址 ​ 细…

Disk Drill for Mac v5.0.1043 苹果数据恢复软件

Disk Drill Enterprise for Mac,数据恢复软件,Disk Drill for Mac 解锁钥匙,Disk Drill for Mac序列号,顶尖的Mac数据恢复软件,你需要恢复在Mac OS X中已经删除的文件吗?比如重要商业文档、音乐、图片或者视…

Linux开发工具(5)——git

文章目录git版本控制器git是什么git的操作clone仓库到本地上传本地文件到gitgit版本控制器 git是什么 标题也说了git就是一个版本控制器,版本控制器是用来保存一个文件的历史版本,如果有需要可以进行回溯,也就是取得以前编辑完成的版本。 比…

Flutter基础知识

const 常量构造函数,如果控件是,则前面加;如果常量构造函数包含有非常量构造函数,则不加,内部是的控件单独加 1.Flutter学习网址 Flutter 中文官网 https://flutter.cn/ Flutter 官网:https://flutter.de…

移动WEB开发之流式布局--移动端基础

浏览器现状 PC端常见浏览器 360浏览器、谷歌浏览器、火狐浏览器、QQ浏览器、百度浏览器、搜狗浏览器、IE浏览器。 移动端常见浏览器 UC浏览器,QQ浏览器,欧朋浏览器, 百度手机浏览器,360安全浏览器,谷歌 浏览器&…

数学建模笔记

文章目录层次分析法——评价类问题原理代码插值算法原理代码拟合算法原理代码相关系数person 相关系数spearman 相关系数二分类和多分类二分类逻辑回归费希尔判别多分类Fisher判别聚类模型分析时间序列分析层次分析法——评价类问题 原理 1. 首先确定评价的目标,可…