traefik 2.2.1 expose mysql service in kubernetes cluster

8/28/2020

I am now using traefik 2.2.1 in kubernetes cluster v1.18, now I want to expose mysql port out of kubernetes cluster,and follow by official doc , add start args:

- '--entryPoints.tcpep.address=:3308'

and add port define:

         ports:
            - name: tcpep
              hostPort: 3308
              containerPort: 3308
              protocol: TCP

this is the tcp ingress route:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRouteTCP
metadata:
    name: report-mysql
spec:
    entryPoints:
        - tcpep
    routes:
        - match: HostSNI(`*`)
    services:
        - name: report-mysqlha
          port: 3306

then check the port listening in host machine:

[dolphiin@K8SSlave3 production]$ sudo lsof -i:3308
[sudo] password for miaoyou:
lsof: no pwd entry for UID 65532
COMMAND   PID     USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
lsof: no pwd entry for UID 65532
traefik 28956    65532    8u  IPv6 2902156338      0t0  TCP *:tns-server (LISTEN)

scan port:

[dolphin@K8SSlave3 production]$ sudo nmap -sS -p 3308 -v 192.168.64.251

Starting Nmap 6.40 ( http://nmap.org ) at 2020-08-28 19:19 CST
Initiating SYN Stealth Scan at 19:19
Scanning MeowK8SSlave3 (192.168.64.251) [1 port]
Discovered open port 3308/tcp on 192.168.64.251
Completed SYN Stealth Scan at 19:19, 0.21s elapsed (1 total ports)
Nmap scan report for MeowK8SSlave3 (192.168.64.251)
Host is up (0.000071s latency).
PORT     STATE SERVICE
3308/tcp open  unknown

but when I login MySQL:

mycli -h 192.168.64.251 -P 3308 -u root -p OgHeee8bfw6jcadewu

it is stuck and no response, is the mapping port success? why I could not loggin mysql? PS:when I am using kubernetes service ip to login, it could login success.

What I having tried:

I found when I access to traefik dashboard, it give me tips(the name is different because I am trying in different kubernetes cluster in my home machine):

the service "infrastructure-mysql-673acf455cb2dab0b43a@kubernetescrd" does not exist

enter image description here

and I find the traefik dashboard did not having TCP services. I am trying to create a TraefikService like this:

[root@k8smaster traefik]# cat mysql-tcp-services.yaml 
apiVersion: traefik.containo.us/v1alpha1
kind: TraefikService
metadata:
    name: mysql-service
spec:
    tcp:
        services:
           mysql:
            loadBalancer:
              servers:
              - address: "10.20.58.239:3306"

it seems not work. the dashboard still not found a TCP Service, so what should I do to fix?

-- Dolphin
kubernetes

1 Answer

8/30/2020

config IngressRouteTCP like this:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRouteTCP
metadata:
    name: report-mysql
spec:
    entryPoints:
        - tcpep
    routes:
        - match: HostSNI(`*`)
          services:
             - name: report-mysqlha
               port: 3306

works.

-- Dolphin
Source: StackOverflow