Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access MySql hosted with Nginx Ingress+Kubernetes from client

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.

like image 397
Vivek Kumar Avatar asked Feb 14 '18 12:02

Vivek Kumar


People also ask

How do I access MySQL on Kubernetes?

To connect to a MySQL instance from outside of your Kubernetes cluster, you must configure the Kubernetes service for the instance to be of type LoadBalancer . To access the MySQL server from an external IP address: Create a database user to use for the external connection.

How do I access Nginx in Kubernetes?

With this service-type, Kubernetes will assign this service on ports on the 30000+ range. Run the get svc command to see a summary of the service and the ports exposed. Now you can verify that the Nginx page is reachable on all nodes using the curl command. As you can see, the “WELCOME TO NGINX!” page can be reached.


2 Answers

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.

like image 174
Radek 'Goblin' Pieczonka Avatar answered Oct 19 '22 04:10

Radek 'Goblin' Pieczonka


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.

like image 34
aleric Avatar answered Oct 19 '22 03:10

aleric