目录

K8S获取客户端ip[client ip]方法

一种测试验证K8S获取客户端ip[client ip]的方法

测试方法

部署source-ip-app

source-ip-deploy-svc.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: source-ip
  name: source-ip-controller
spec:
  replicas: 1
  selector:
    matchLabels:
      name: source-ip
  template:
    metadata:
      labels:
        name: source-ip
    spec:
      containers:
      # - image: gcr.io/google_containers/echoserver:1.4
      # - image: cilium/echoserver:latest
      - image: digiboy2008/echoserver:1.4
        name: source-ip
        imagePullPolicy: IfNotPresent


---
apiVersion: v1
kind: Service
metadata:
  name: source-ip
  labels:
    name: source-ip
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 31999
      targetPort: 8080
      protocol: TCP
      name: http
  selector:
    name: source-ip
  externalTrafficPolicy: Local
  sessionAffinity: ClientIP

create source-ip-deploy-svc

1
kubectl create -f source-ip-deploy-svc.yaml

查看source-ip-svc服务

1
kubectl describe svc source-ip

获取客户端ip,在终端浏览器输入

1
2
3
# http://node_ip:node_port/

http://192.168.1.111:31999/

显示内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Hostname: source-ip-controller-548bd49748-pmb6j

Pod Information:
        -no pod information available-

Server values:
        server_version=nginx: 1.13.3 - lua: 10008

Request Information:
        client_address=::ffff:192.168.1.111
        method=GET
        real path=/
        query=
        request_version=1.1
        request_scheme=http
        request_uri=http://192.168.1.111:8080/

Request Headers:
        accept=*/*
        host=192.168.1.111:31999
        user-agent=curl/7.65.2

Request Body:
        -no body in request-




删除source-ip-app

1
kubectl delete -f source-ip-deploy-svc.yaml

参考资料