1

I have a kubernetes cluster with front end as C# console application written in .NET Core and a backend Mysql db. Both of these applications are deployed as deployments in kubernetes in different pods. I have also created a mysql service to be able to connect with Mysql db. But, I can't seem to connect to mysql server from .NET core console app to kubernetes Mysql service. However, I can pretty much connect with Mysql pod using the IP address from the console app.

I will try my best to describe the situation here.

$ kubectl -n radiomicsapp get pods -o wide
NAME                                                          READY     STATUS    RESTARTS   AGE       IP             NODE
anonymizer-pod-586548ddd9-kv8cj                               2/2       Running   0          21s       10.244.0.187   aks-agentpool-35971152-1
mysql-744bfb878c-scwz9                                        1/1       Running   0          19h       10.244.1.244   aks-agentpool-35971152-2

$ kubectl -n abcapp get svc
NAME                                         TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)                      AGE
mysql                                        ClusterIP      10.0.54.120    <none>           3306/TCP                     104d

If I use IP address from the pod (10.244.1.244) to connect with db, it works. However, if I use IP address (10.0.54.120) from mysql service, it throws

Error: Unable to connect to any of the specified MySQL hosts.

Since, the IP address of the mysql pod changes whenever the pod is restarted/recreated, I will have to change the db connection string in my anonymizer console app, I created a mysql service with type ClusterIP as given below:

apiVersion: v1
kind: Service
metadata:
   name: mysql
   namespace: abcapp
   labels:
    app: abc
spec:
   type: ClusterIP
   ports:
     - name: mysql
       port: 3306
       protocol: TCP
       targetPort: 3306
   selector:
     app: abc

Also, my anonymizer pod, mysql pod as well as mysql service are all in the same namespace.

Any help would be much appreciated. Thanks for taking your time.

Binay
  • 33
  • 4

1 Answers1

0

You have to use the service name on your code and leave kube-dns to handle the IP resolution for you to be sure your code works no matter if the pod is recreated or moved and it takes a different IP address during the process.

From another answer is easy to extrapolate an approximate example

How to connect to MySQL Database?

string connstring = string.Format("Server=mysql; database={0}; UID=UserName; password=your password", databaseName);
connection = new MySqlConnection(connstring);

Notice that only need to put on the Server mysql

wolmi
  • 1,659
  • 12
  • 25
  • Could you elaborate it how? I have tried using it several times without success. In my console app, I have `var serviceProvider = new ServiceCollection() .AddDbContext(o => o.UseMySql($"Server={mysql};Database=abc;UID=root;Password=abc321;")); – Binay Jul 12 '19 at 10:44
  • I tried to use var serviceProvider = new ServiceCollection() .AddDbContext(o => o.UseMySql($"Server={Environment.GetEnvironmentVariable("MYSQL_SERVICE_HOST")};Database=abc;UID=root;Password=abc321;")); MYSQL_SERVICE_HOST returns the IP address of mysql service. However, this is not working for me. – Binay Jul 12 '19 at 11:01
  • you have to use the service name "mysql" has it is a domain, internally Kubernetes will resolve to the correct IP address using the services kube-dns that know what IP is assigned to each service. – wolmi Jul 12 '19 at 11:02
  • How do I use the service name "mysql" as a domain in my connection string? Can you please elaborate with some code? – Binay Jul 12 '19 at 11:05
  • here you have an example of how to connect to mysql https://stackoverflow.com/questions/21618015/how-to-connect-to-mysql-database you only need to replace `localhost` on the connection by `mysql` – wolmi Jul 12 '19 at 11:17
  • Okay, now I used var serviceProvider = new ServiceCollection() .AddDbContext(o => o.UseMySql($"Server=mysql;Database=abc;UID=root;Password=abc321")); And it still didn't work. Error: Unable to connect to any of the specified MySQL hosts. – Binay Jul 12 '19 at 11:25
  • To discard you have another problems try to change the service to NodePort then you can connect with a mysql client on your desktop to the mysql server on K8S – wolmi Jul 12 '19 at 11:32
  • I created another mysql service with NodePort type. And tried to connect with it using Mysql client on my desktop. I still can't connect. – Binay Jul 12 '19 at 12:26
  • It seems your mysql is not properly deployed, you need to review the logs and check first if you can connect directly, are you using K8S on local or are you using some cloud provider? – wolmi Jul 12 '19 at 12:31
  • I am using Azure kubernetes service (AKS). – Binay Jul 12 '19 at 12:32