How to connect to mysql server pod from external standalone application?
How to connect to mysql server pod from external standalone application?
https://stackoverflow.com/questions/73360213/how-to-connect-to-mysql-server-pod-from-external-standalone-application
Asked 3 years, 5 months ago Modified 3 years, 5 months ago Viewed 4k times 0
I’m newbie to k8s and trying to understand how we can connect the external application(i.e outside kubernetes cluster) to db pod. Please consider external application as my standalone java program(JDBC program)
Eg : I have myserver server pod in my k8s cluster and I can access it using ‘kubectl exec…’.
Now, If I want to use mysql server and connect to my external application like standalone java jdbc application to perform simple CRUD operation, how do we connect to mysql pod ? What could be the connection string for the same?
Do I need to first expose mysql server pod as nodeport service and use that IP:port in my connection string? Or is there any other way. Any hint or help would be appreciated.
Thanks in advance !
mysqlkuberneteskubernetes-pod Share Improve this question Follow asked Aug 15, 2022 at 11:13 Newbie’s user avatar Newbie 47644 silver badges1111 bronze badges Add a comment 2 Answers Sorted by:
Highest score (default) 2
1st, you need to create a service in K8s which routes traffic from client to your mysql pods.
apiVersion: v1 kind: Service metadata: name: mysql-service spec: selector: app: mysql # labels should be the same as the ones used in the Pod’s definition. ports: - protocol: TCP port: 3306 # port of the service. targetPort: 3306 # Port that the pods have exposed. Now, if your external app is running on local machine & you want it to communicate with the database, use kubectl port-forward command which will create a local session between your machine & K8s pods
kubectl port-forward svc/ :
kubectl port-forward svc/mysql-service 3306:3306 For production, you will need to expose your mysql service over HTTP using Ingress. For this, you will need an Ingress Controller e.g Nginx and an Ingress resource.
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: db-ingress spec: rules:
- http:
paths:
- path: /db # path at which you want to expose your service pathType: Prefix backend: service: name: mysql-service # name of the service port: number: 3306 # port of the service For reference, check out these links
https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service https://kubernetes.io/docs/concepts/services-networking/ingress/#the-ingress-resource https://kubernetes.github.io/ingress-nginx/ https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/#forward-a-local-port-to-a-port-on-the-pod Share Improve this answer Follow answered Aug 15, 2022 at 11:34 Taimoor Mirza’s user avatar Taimoor Mirza 1,11777 silver badges1919 bronze badges Sign up to request clarification or add additional context in comments.
4 Comments
Newbie Over a year ago thank you for the details explanation. So, what would be my connection string to connect to the mysql pod. Is it podIP:port ? FYI; I’m trying the below JDBC code to connect to mysql pod and want to perform select, create operation using program. Connection con=DriverManager.getConnection( “jdbc:mysql://localhost:3306/sonoo”,”root”,”root”); javatpoint.com/example-to-connect-to-the-mysql-database
Taimoor Mirza Over a year ago If your app running within the k8s cluster then you will use service to communicate with the MySQL pods. For example, if your MySQL service is called mysql-svc, has port 3306 & present in the namespace default, then your connection string will be like this: jdbc:mysql://default.mysql-service:3306/database-1 If the app running on you local machine then using port-forwarding, you will expose your service & its port on localhost and port and use it in your JDBC connection string. kubectl port-forward svc/mysql-svc 3307:3306 jdbc:mysql://localhost:3307/database-1
Taimoor Mirza Over a year ago Using Pod’s IP directly isn’t recommended because Pods are ephermeral in nature, and everytime they’re recreated, they get new IP address. So it’s better not to hard-code the Pod IP and use service instead.
Newbie Over a year ago Hello @TAM, Due to character limitation I can not reply to this thread so, I’ve shared my findings separately on this post. Thanks ! Add a comment 0
Yes, it’s my local cluster.
I have tried the way you have mentioned and unfortunately, it’s failing and I’ve tried 2 scenario. Please see below :
a) my svc details :
kubectl get svc | grep mysql
mysql NodePort 10.43.92.516
b) my pod details :
kubectl get pods -A –show-labels | grep -i mysql default mysql-6d86c78b54-p7srr 1/1 Running 0 6h26m app=mysql,pod-template-hash=6d86c78b54
C) Pod endpoint :
kubectl get ep mysql NAME ENDPOINTS AGE mysql 10.42.0.42:3306 23h
D) Tried to get the DNS record value :
root@mysql-6d86c78b54-p7srr:/# busybox nslookup mysql.default.svc.cluster.local Server: 10.43.0.14 Address 1: 10.43.0.14 kube-dns.kube-system.svc.cluster.local
Name: mysql.default.svc.cluster.local Address 1: 10.43.92.516 mysql.default.svc.cluster.local So, as per you suggestion, I’ve tried with namespace.service_name but it failed with UnknownHostException :
Scenario 1)
Connection con=DriverManager.getConnection(“jdbc:mysql://default.mysql:30064/mysql”,”test_user”,”
Error :
java.net.UnknownHostException MESSAGE: default.mysql: Name or service not known
STACKTRACE:
java.net.UnknownHostException: default.mysql: Name or service not known at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929) Scenaio 2) I followed the as per official documentation (https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/) and given actual DNS entry but still it’s an same issue.
Connection con=DriverManager.getConnection(“jdbc:mysql://mysql.default.svc.cluster.local:30064/mysql”,”test_user”,”
error :
java.net.UnknownHostException MESSAGE: mysql.default.svc.cluster.local: Temporary failure in name resolution
STACKTRACE:
java.net.UnknownHostException: mysql.default.svc.cluster.local: Temporary failure in name resolution at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) Share Improve this answer Follow answered Aug 16, 2022 at 9:56 Newbie’s user avatar Newbie 47644 silver badges1111 bronze badges 2 Comments
Taimoor Mirza Over a year ago For scenario # 1: Replace default.mysql with node’s IP address.
Newbie
Over a year ago
Hello TAM, Thanks a lot for your help. As suggested I’ve exported the service via nodeport and in my jdbc connections I’ve given the IP address of pod and its working fine. I studied that the connection will not work simply with “jdbc:mysql://