Get client IP address in GRPC service behind Kubernetes nginx ingress

6/17/2020

I am still struggling with kubernetes.

I have issue with preserving request IP address on service for logging purposes. Logging is done with GRPC server. This code is working outside kubernetes as intended.

Service is defined similar to this.

apiVersion: v1
kind: Service
metadata:
  annotations: {}
  labels:
    name: grpc-api
  name: grpc-api
  namespace: myns
spec:
  ports:
  - name: ext-5000
    port: 5000
    targetPort: 5000
  - name: grpc-5050
    port: 5050
    targetPort: 5050
  selector:
    name: grpc-api
  type: ClusterIP

Ingress is:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    certmanager.k8s.io/cluster-issuer: letsencrypt-myns
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/backend-protocol: GRPC
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
  labels:
    name: api-grpc
  name: api-grpc
  namespace: myns
spec:
  rules:
  - host: api.example.org
    http:
      paths:
      - backend:
          serviceName: grpc-api
          servicePort: 5000
        path: /
  tls:
  - hosts:
    - api.example.org
    secretName: grpc-api-ingress-cert

Documentation mentions externalTrafficPolicy: Local in service, where type is LoadBalancer. Would it be enough to add parameter above to ClusterIP type service or do I have to change it to something else?

Thank you in advance.

-- EnterSB
kubernetes

0 Answers