When your business requires to know the sources of service requests, the backend server must be able to accurately obtain the real client source IP of the request client. Possible scenarios:
In TKE, the default external load balancer is Tencent Cloud Load Balancer, which serves as the first access entry for incoming traffic. The CLB forwards request traffic loads to Kubernetes Service (default) of Kubernetes worker nodes. During this load-balancing process, the real client source IP is preservec (pass-through forwarded). However, in Kubernetes Service forwarding scenarios, data packets will go through SNAT during forwarding no matter whether the CLB forwarding mode is iptables or ipvs, which means that the real client source IP will not be preserved. For your reference, this document provides the following four methods for obtaining the real client source IP in TKE use cases. You can choose an appropriate method based on your actual needs.
The advantage and disadvantage of this method are as follows:
To enable the feature of preserving the client source IP, you can configure the Service.spec.externalTrafficPolicy
field in Service resources. This field has two possible values, Cluster
(default) and Local
, which respectively indicate whether to route external traffic to the local or cluster endpoints of nodes, as shown in the figure below:
Cluster
: hides the client source IP. Service traffic of the LoadBalancer
and NodePort
types may be forwarded to the pods of other nodes.Local
: preserves the client source IP and prevents service traffic of the LoadBalancer
and NodePort
types from being forwarded to the pods of other nodes. For more information, see Create an External Load Balancer. The sample YAML configuration is as follows:apiVersion: v1
kind: Service
metadata:
name: example-Service
spec:
selector:
app: example-Service
ports:
- port: 8765
targetPort: 9376
externalTrafficPolicy: Local
type: LoadBalancer
The advantage and disadvantage of this method are as follows:
The CLB-to-pod direct connection forwarding is a TKE native feature, which is actually CLB pass-through forwarding and bypasses Kubernetes Service traffic forwarding) is used, the source IP address of a request received by backend pods is the real source IP address of the client. This method applies to layer-4 and layer-7 service forwarding scenarios. The following figure shows how the forwarding works:
For more information and configuration details, see Using CLB-to-Pod Direct Connection on TKE.
The advantage and disadvantage of this method are as follows:
In layer-7 (HTTP/HTTPS) service forwarding scenarios, the real source IP address of a client can be obtained from the X-Forwarded-For
and X-Real-IP
fields in the HTTP header. There are two use cases in TKE, as shown in the figure below:
CLB (CLB layer-7) stores the real source IP address of a client in the X-Forwarded-For
and X-Real-IP
fields of the HTTP header by default. When service traffic goes through Service layer-4 forwarding, both fields are retained, and the backend can obtain the real source IP address of the client through web server proxy configuration or application code. For more information, see Obtain Acutual IP for Layer 7 Load Balancing. The process for obtaining the source IP address in the TKE console is as follows:
X-Forwarded-For
or X-Real-IP
field of the HTTP header on the backend. The following figure shows the packet capture test results on the backend:Nginx Ingress service deployment requires Nginx Ingress to be able to perceive the real source IP address of a client. You can preserve the client source IP by create an external load balancer or using CLB-Pod direct connection on TKE. When forwarding requests, Nginx Ingress uses the X-Forwarded-For
and X-Real-IP
fields to store the client source IP, and the backend can obtain the real client source IP from these fields. The configuration process is as follows:
externalTrafficPolicy
field of Nginx Ingress Controller Service to Local
.apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx # ingressClass is "nginx".
name: example
namespace: default
spec:
rules: # Configure service forwarding rules
- http:
paths:
- backend:
serviceName: nginx
servicePort: 80
path: /
X-Forwarded-For
or X-Real-IP
field of the HTTP header on the backend. The following figure shows the packet capture test results on the backend:The advantage and disadvantages of this method are as follows:
For the principles and loading method of the TOA kernel component, see Obtaining the Real IPs of Access Users.
Apakah halaman ini membantu?