Skip to content

Instantly share code, notes, and snippets.

@draganHR
Created February 16, 2023 10:57
Show Gist options
  • Save draganHR/6510c48aa346e2b3bbb32497ace40c43 to your computer and use it in GitHub Desktop.
Save draganHR/6510c48aa346e2b3bbb32497ace40c43 to your computer and use it in GitHub Desktop.
Exposing TCP and UDP services trough kubernetes ingress

https://www.haproxy.com/documentation/kubernetes/latest/configuration/controller/#--configmap-tcp-services

Install HAProxy Kubernetes Ingress Controller:

helm repo add haproxytech https://haproxytech.github.io/helm-charts
helm repo update
helm install kubernetes-ingress haproxytech/kubernetes-ingress \
    --create-namespace \
    --namespace haproxy-controller

Create tcp-services ConfigMap that maps some port (e.g. 11587) to port 587 of connector-smtp service:

kubectl create configmap tcp-services -n haproxy-controller
kubectl patch configmap tcp-services -n haproxy-controller -p '{"data":{"11587":"my-namespace/connector-smtp:587"}}'

Edit ingress deployment (kubectl edit deployment.apps kubernetes-ingress -n haproxy-controller startup argument:

    spec:
      containers:
      - args:
        # ...
        - --configmap-tcp-services=haproxy-controller/tcp-services

Edit kubernetes-ingress service (kubectl edit svc kubernetes-ingress -n haproxy-controller) and add port is mapped in ConfigMap:

  ports:
    # ...
  - name: smtp
    protocol: TCP
    port: 11587
    targetPort: 11587

https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/

Install nginx ingress controller:

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install nginx-ingress ingress-nginx/ingress-nginx

Create tcp-services ConfigMap that maps some port (e.g. 11587) to port 587 of connector-smtp service:

kubectl create configmap tcp-services
kubectl patch configmap tcp-services -p '{"data":{"11587":"my-namespace/connector-smtp:587"}}'

Edit ingress deployment (kubectl edit deploy nginx-ingress-ingress-nginx-controller) and add --tcp-services-configmap startup argument:

containers:
- args:
  - /nginx-ingress-controller
  # ...
  - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services

Edit ingress service (kubectl edit svc nginx-ingress-ingress-nginx-controller) and add port is mapped in ConfigMap:

  ports:
    # ...
  - name: smtp
    protocol: TCP
    port: 11587
    targetPort: 11587
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: my-service
port:
number: 587
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment