<p>I want to expose my Mariadb pod using Nginx ingress TCP service by following this step <a href="https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/</a>.  Mariadb running in default name space, with mariadb service type as ClusterIP.  I am running Nginx Ingress controller in nginx-ingress namespace, also defined <code>tcp-services</code> cofigmap for <code>mariadb</code> service.  But I am unable to connect MariaDB database from outside of the cluster.</p>

<p>From Nginx controller log I can see its reading tcp-services.</p>

<p>Ingress configuration</p>

<pre><code>containers:
      - 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
        - --default-ssl-certificate=nginx-ingress/ingress-tls
        - --tcp-services-configmap=nginx-ingress/tcp-services
        - --udp-services-configmap=nginx-ingress/udp-services
</code></pre>

<p>ConfigMap:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: nginx-ingress
data:
  3306: "default/mariadb:3306"
</code></pre>

<p>Ingress controller nginx config for TCP Service</p>

<pre><code> # TCP services

        server {
                preread_by_lua_block {
                        ngx.var.proxy_upstream_name="tcp-default-mariadb-3306";
                }

                listen                  3306;

                proxy_timeout           600s;
                proxy_pass              upstream_balancer;

        }
</code></pre>

<p>when I connect from external server, getting this message:</p>

<pre><code>ERROR 2002 (HY000): Can't connect to MySQL server on 
</code></pre>

<p>any tips to troubleshoot this issue?</p>

<p>thanks</p>

<p>I was missing my service with TCP Port info, after adding it I was able to access the MySQL with my service Port Number. Thanks for <code>Emanuel Bennici</code> pointing this one out.</p>

<p>Here is my service:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: nginx-ingress-controller  
spec:
  externalTrafficPolicy: Cluster
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
  - name: https
    port: 443
    protocol: TCP
    targetPort: https
  - name: 3066-tcp
    port: 3066
    protocol: TCP
    targetPort: 3066-tcp
  selector:
    app: nginx-ingress
    component: controller
    release: nginx-ingress
  sessionAffinity: None
  type: NodePort
</code></pre>

sfgroups
<p>Please check if you have opened the MySQL port in the Pod 
So to open the port on the Kubernetes-Port you have to create a pod like this:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: mysql
  namespace: default
  labels:
    name: mysql
spec:
  containers:
  - name: mysql
    image: docker.io/bitnami/mariadb:10.3.20-debian-9-r19
    ports:
    - containerPort: 3306
      protocol: TCP
</code></pre>

<p>Then you have to create a <em>service</em> so you can <em>talk directly</em> to the MySQL Pod through the service:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: svc-mysql
  namespace: default
  labels:
    run: mysql
spec:
  ports:
  - port: 3306
    targetPort: 3306
    protocol: TCP
  selector:
    name: mysql
</code></pre>

<p>If the Nginx Ingress Controller is working correctly you can now add the following line to your <em>tcp-services-configmap</em>:</p>

<pre><code>3306: "default/svc-mysql:3306"
</code></pre>

<p><em>Please note</em> that you have to add the MySQL port to the <em>Nginx Ingress Service</em>, like this:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: nginx-ingress
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
    - name: proxie-tcp-mysql
      port: 3306
      targetPort: 3306
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
</code></pre>

<p>Now you can use the <em>external IP</em> of the <em>Nginx Ingress Controller</em> to connect to your MySQL Server.</p>

<hr>

<p>Please provide more information about your setup in future Questions :)</p>
