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 !
CodePudding user response:
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/<service-name> <local-port>:<service-port>
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
CodePudding user response:
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 <none> 3306:30064/TCP 23h
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","<password>");
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","<password>");
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)