Kubernetes 暴露pod源IP配置(2021-03-23)

前言

需求如下鼻种,Kuberneters 中部署的pod訪問Cluster集群外的server(后續(xù)成為remote server)時缕探,期望可以在remote server中查看到pod的源IP。

環(huán)境如下

  • kubernetes: v1.12.5
  • calico: v3.7.5
  • OS: Redhat7.6

工作原理

本次配置中Kubernetes集群搭建中選用的網(wǎng)絡插件為cailico搅轿。pod在網(wǎng)訪問remote server時助泽,會在node節(jié)點先匹配策略抖拦,將pod的源IP 經(jīng)過SNAT映射為node節(jié)點的物理IP 拗踢,此時remote server看到的請求IP則是node節(jié)點的IP券膀,無法獲取到pod的源IP芹彬。
所以需要在calico中添加配置,設置nat-outgoing參數(shù)為false,pod在對外訪問時不做nat映射译红,通過邊界路由實現(xiàn)訪問remote server刨沦。(注:需要在remote server上添加訪問pod的訪問路由梧田,否則remote server無法回包)

具體配置如下

calico配置文件calico.yaml中,kind: DaemonSet 的配置添加

            - name: CALICO_IPV4POOL_NAT_OUTGOING
              value: "false"

具體如下:

kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: calico-node
  namespace: kube-system
  labels:
    k8s-app: calico-node
spec:
  selector:
    matchLabels:
      k8s-app: calico-node
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      labels:
        k8s-app: calico-node
      annotations:
        # This, along with the CriticalAddonsOnly toleration below,
        # marks the pod as a critical add-on, ensuring it gets
        # priority scheduling and that its resources are reserved
        # if it ever gets evicted.
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      nodeSelector:
        beta.kubernetes.io/os: linux
      hostNetwork: true
      tolerations:
        # Make sure calico-node gets scheduled on all nodes.
        - effect: NoSchedule
          operator: Exists
        # Mark the pod as a critical add-on for rescheduling.
        - key: CriticalAddonsOnly
          operator: Exists
        - effect: NoExecute
          operator: Exists
      serviceAccountName: calico-node
      # Minimize downtime during a rolling upgrade or deletion; tell Kubernetes to do a "force
      # deletion": https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods.
      terminationGracePeriodSeconds: 0
      initContainers:
        # This container performs upgrade from host-local IPAM to calico-ipam.
        # It can be deleted if this is a fresh installation, or if you have already
        # upgraded to use calico-ipam.
        - name: upgrade-ipam
          image: calico/cni:v3.7.5
          command: ["/opt/cni/bin/calico-ipam", "-upgrade"]
          env:
            - name: KUBERNETES_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: CALICO_NETWORKING_BACKEND
              valueFrom:
                configMapKeyRef:
                  name: calico-config
                  key: calico_backend
          volumeMounts:
            - mountPath: /var/lib/cni/networks
              name: host-local-net-dir
            - mountPath: /host/opt/cni/bin
              name: cni-bin-dir
        # This container installs the CNI binaries
        # and CNI network config file on each node.
        - name: install-cni
          image: calico/cni:v3.7.5
          command: ["/install-cni.sh"]
          env:
            # Name of the CNI config file to create.
            - name: CNI_CONF_NAME
              value: "10-calico.conflist"
            # The CNI network config to install on each node.
            - name: CNI_NETWORK_CONFIG
              valueFrom:
                configMapKeyRef:
                  name: calico-config
                  key: cni_network_config
            # Set the hostname based on the k8s node name.
            - name: KUBERNETES_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            # CNI MTU Config variable
            - name: CNI_MTU
              valueFrom:
                configMapKeyRef:
                  name: calico-config
                  key: veth_mtu
            # Prevents the container from sleeping forever.
            - name: SLEEP
              value: "false"
          volumeMounts:
            - mountPath: /host/opt/cni/bin
              name: cni-bin-dir
            - mountPath: /host/etc/cni/net.d
              name: cni-net-dir
      containers:
        # Runs calico-node container on each Kubernetes node.  This
        # container programs network policy and routes on each
        # host.
        - name: calico-node
          image: calico/node:v3.7.5
          env:
            # Use Kubernetes API as the backing datastore.
            - name: DATASTORE_TYPE
              value: "kubernetes"
            # Wait for the datastore.
            - name: WAIT_FOR_DATASTORE
              value: "true"
            # Set based on the k8s node name.
            - name: NODENAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            # Choose the backend to use.
            - name: CALICO_NETWORKING_BACKEND
              valueFrom:
                configMapKeyRef:
                  name: calico-config
                  key: calico_backend
            # Cluster type to identify the deployment type
            - name: CLUSTER_TYPE
              value: "k8s,bgp"
            # Auto-detect the BGP IP address.
            - name: IP
              value: "autodetect"
            # Enable IPIP
            - name: CALICO_IPV4POOL_IPIP
              value: "off"
            # Set MTU for tunnel device used if ipip is enabled
            - name: FELIX_IPINIPMTU
              valueFrom:
                configMapKeyRef:
                  name: calico-config
                  key: veth_mtu
            # The default IPv4 pool to create on startup if none exists. Pod IPs will be
            # chosen from this range. Changing this value after installation will have
            # no effect. This should fall within `--cluster-cidr`.
            - name: CALICO_IPV4POOL_CIDR
              value: "10.96.0.0/16"
            - name: CALICO_IPV4POOL_NAT_OUTGOING
              value: "false"
            # Disable file logging so `kubectl logs` works.
            - name: CALICO_DISABLE_FILE_LOGGING
              value: "true"
            # Set Felix endpoint to host default action to ACCEPT.
            - name: FELIX_DEFAULTENDPOINTTOHOSTACTION
              value: "ACCEPT"
            # Disable IPv6 on Kubernetes.
            - name: FELIX_IPV6SUPPORT
              value: "false"
            # Set Felix logging to "info"
            - name: FELIX_LOGSEVERITYSCREEN
              value: "info"
            - name: FELIX_HEALTHENABLED
              value: "true"
          securityContext:
            privileged: true
          resources:
            requests:
              cpu: 250m
          livenessProbe:
            httpGet:
              path: /liveness
              port: 9099
              host: localhost
            periodSeconds: 10
            initialDelaySeconds: 10
            failureThreshold: 6
          readinessProbe:
            exec:
              command:
              - /bin/calico-node
              - -bird-ready
              - -felix-ready
            periodSeconds: 10
          volumeMounts:
            - mountPath: /lib/modules
              name: lib-modules
              readOnly: true
            - mountPath: /run/xtables.lock
              name: xtables-lock
              readOnly: false
            - mountPath: /var/run/calico
              name: var-run-calico
              readOnly: false
            - mountPath: /var/lib/calico
              name: var-lib-calico
              readOnly: false
      volumes:
        # Used by calico-node.
        - name: lib-modules
          hostPath:
            path: /lib/modules
        - name: var-run-calico
          hostPath:
            path: /var/run/calico
        - name: var-lib-calico
          hostPath:
            path: /var/lib/calico
        - name: xtables-lock
          hostPath:
            path: /run/xtables.lock
            type: FileOrCreate
        # Used to install CNI.
        - name: cni-bin-dir
          hostPath:
            path: /opt/cni/bin
        - name: cni-net-dir
          hostPath:
            path: /etc/cni/net.d
        # Mount in the directory for host-local IPAM allocations. This is
        # used when upgrading from host-local to calico-ipam, and can be removed
        # if not using the upgrade-ipam init container.
        - name: host-local-net-dir
          hostPath:
            path: /var/lib/cni/networks

啟動calico后用node訪問remote server可看到pod的源IP

實現(xiàn)效果

Pod IP 內網(wǎng)地址:10.96.164.131
node IP : 10.0.5.203
remote server: 10.0.5.204
remote server 未在kubernetes集群中

  1. pod訪問遠程服務器10.0.5.204的8081,地址不可達,因為remote server上未添加返回pod的路由


    pod訪問remote server
  2. remote server 10.0.5.204上添加路由
    含義為10.0.5.204訪問10.90.164.131這個pod時,由于網(wǎng)段不同信粮,使用10.0.5.203作為網(wǎng)關轉發(fā)访娶。


    添加路由
  3. 再次從pod發(fā)送訪問請求,可查看訪問成功


    pod訪問remote server
  4. remote server tcpdump查看pod源IP,可查看到源IP為10.90.164.131


    tcpdump抓包結果

Tips

  1. 路由策略可以添加直接主機的乱顾,也可以添加指向子網(wǎng)的
    route add -net 10.0.X.0/24 gw 10.0.X.1
  2. 啟動pod時間,默認dns策略會自動加載node節(jié)點的/etc/resolv.conf中的search域扭粱。由于nat已經(jīng)被禁用,原來node可訪問的dns search域則會在pod中不可達背伴。造成pod查詢域超時徐紧。建議在kubernetes搭建時配置kubelet不繼承node節(jié)點的/etc/resolv.conf
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末凯旋,一起剝皮案震驚了整個濱河市荒椭,隨后出現(xiàn)的幾起案子身害,更是在濱河造成了極大的恐慌庭瑰,老刑警劉巖福侈,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件就乓,死亡現(xiàn)場離奇詭異,居然都是意外死亡拱烁,警方通過查閱死者的電腦和手機生蚁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來戏自,“玉大人邦投,你說我怎么就攤上這事∩帽剩” “怎么了志衣?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長猛们。 經(jīng)常有香客問我念脯,道長,這世上最難降的妖魔是什么弯淘? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任绿店,我火速辦了婚禮,結果婚禮上耳胎,老公的妹妹穿的比我還像新娘惯吕。我一直安慰自己,他們只是感情好怕午,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布废登。 她就那樣靜靜地躺著,像睡著了一般郁惜。 火紅的嫁衣襯著肌膚如雪堡距。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天兆蕉,我揣著相機與錄音羽戒,去河邊找鬼。 笑死虎韵,一個胖子當著我的面吹牛易稠,可吹牛的內容都是我干的。 我是一名探鬼主播包蓝,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼驶社,長吁一口氣:“原來是場噩夢啊……” “哼企量!你這毒婦竟也來了?” 一聲冷哼從身側響起亡电,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤届巩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后份乒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體恕汇,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年或辖,在試婚紗的時候發(fā)現(xiàn)自己被綠了瘾英。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡孝凌,死狀恐怖方咆,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情蟀架,我是刑警寧澤瓣赂,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站片拍,受9級特大地震影響煌集,放射性物質發(fā)生泄漏。R本人自食惡果不足惜捌省,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一苫纤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧纲缓,春花似錦卷拘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至工闺,卻和暖如春乍赫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背陆蟆。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工雷厂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叠殷。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓改鲫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子钩杰,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內容