Exposing a TCP port out of cluster in Kubernetes using nginx-Ingress
So, in order to achieve this you can do this:
- First create the configMap that you added to the post.
apiVersion: v1kind: ConfigMapmetadata: name: tcp-configmap-exampledata: 9000: "default/my-service-name:7051
Then edit your nginx-ingress-controller deployment by adding this flag to container args like below:
...containers:- name: nginx-ingress-controller image: "quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.26.1" imagePullPolicy: "IfNotPresent" args: - /nginx-ingress-controller - --default-backend-service=nginx-ingress/nginx-ingress-default-backend - --election-id=ingress-controller-leader - --ingress-class=nginx - --configmap=nginx-ingress/nginx-ingress-controller - --tcp-services-configmap=default/tcp-configmap-example ...
Edit LoadBalancer service by adding port to your LoadBalancer
...ports:- name: http port: 80 protocol: TCP targetPort: http- name: https port: 443 protocol: TCP targetPort: https- name: some-service-port port: 7051 protocol: TCP
Hope it helps!
If you are installing with helm
there is a way to expose tcp ports by setting values.
# add helm repohelm repo add ingress-nginx https://kubernetes.github.io/ingress-nginxhelm repo update
helm show values ingress-nginx/ingress-nginx
will show the values.yaml
file for reference, there are two dictionaries for exposing ports: tcp
and udp
:
# TCP service key:value pairs# Ref: https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/exposing-tcp-udp-services.md##tcp: {}# 8080: "default/example-tcp-svc:9000"# UDP service key:value pairs# Ref: https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/exposing-tcp-udp-services.md##udp: {}# 53: "kube-system/kube-dns:53"
To set the values from command line:
# set `tcp` dictionary in values (other `helm install` options omitted, only left options regarding to exposing tcp ports)helm install ingress-nginx ingress-nginx/ingress-nginx --set tcp.12345=some-namespace/some-service:80