13
votes

I am new to Kubernetes and Nginx Ingress tools and now i am trying to host MySql service using VHost in Nginx Ingress on AWS. I have created a file something like :

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
    - port: 3306
      protocol: TCP
  selector:
    app: mysql
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql
          imagePullPolicy: IfNotPresent
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: password
          ports:
            - name: http
              containerPort: 3306
              protocol: TCP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: mysql
  labels:
    app: mysql
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    - host: mysql.example.com
      http:
        paths:
          - path: /
            backend:
              serviceName: mysql
              servicePort: 3306

My LoadBalancer (created by Nginx Ingress) port configuration looks like :

80 (TCP) forwarding to 32078 (TCP)
Stickiness options not available for TCP protocols

443 (TCP) forwarding to 31480 (TCP)
Stickiness options not available for TCP protocols

mysql.example.com is pointing to my ELB.

I was expecting something like, from my local box i can connect to MySql if try something like :

mysql -h mysql.example.com -u root -P 80 -p

Which is not working out. Instead of NodePort if i try with LoadBalancer, its creating a new ELB for me which is working as expected.

I am not sure if this is right approach for what i want to achieve here. Please help me out if there is a way for achieving same using the Ingress with NodePort.

2
Don't know Kubernetes, but my guess is you have make MySQL port 3306 (default) accessible. So Ingress has to be able to route traffic from any external port of your choice to 3306. So publish another port aside from 80 and 443.emix

2 Answers

12
votes

Kubernetes Ingress as a generic concept does not solve the issue of exposing/routing TCP/UDP services, as stated in https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/exposing-tcp-udp-services.md you should use custom configmaps if you want that with ingress. And please mind that it will never use hostname for routing as that is a feature of HTTP, not TCP.

3
votes

I succeded to access MariaDB/MySQL hosted on Google Kubernetes Engine through ingress-nginx, using the hostname specified in the ingress created for the database Cluster IP.

As per the docs, simply create the config map and expose the port in the Service defined for the Ingress.

This helped me to figure how to set --tcp-services-configmap and --udp-services-configmap flags.