Joseph Hwang
Joseph Hwang

Reputation: 1421

What is the external connection IP address of Kubernetes service?

I try to make simple Kubernetes Pods and Services with using minikube dashboard. First, I generate Kubernetes mysql Service with the following yaml.

apiVersion: v1
kind: Pod
metadata:
  name: blog-db
  labels:
    app: blog-mysql
spec:
  containers:
  - name: blog-mysql
    image: mysql:latest
    env:
      - name: MYSQL_ROOT_PASSWORD
        value: password
      - name: MYSQL_PASSWORD
        value: password
      - name: MYSQL_DATABASE
        value: test
    ports:
      - containerPort: 3306

---
apiVersion: v1
kind: Service
metadata:
  name: blog-db-svc
spec:
  selector:
    app: blog-mysql
  ports:
  - name: mysql
    port: 3306
    protocol: TCP
    targetPort: 3306
  externalIPs:
  - 10.96.10.10

The mysql service is generated successfully. But my front-end application docker image is built with Spring Boot, so I have to assign the correct mysql connection url into application.properties file like below,

# ==============================================================
# = MySQL DataSource properties
# ==============================================================
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://10.96.10.10:3306/test?characterEncoding=utf8&serverTimezone=Asia/Seoul
spring.datasource.username = root
spring.datasource.password = password

I type the ip address, 10.96.10.10 of the external ip of service resource into spring boot application.properties file. But the connection is not successful. I checked the mysql service properties with the kubectl cli command,

> kubectl get svc
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
blog-db-svc   ClusterIP   10.104.29.31   10.96.10.10   3306/TCP   30m
kubernetes    ClusterIP   10.96.0.1      <none>        443/TCP    2d

And I insert every ip addresses into application.properties of my docker container. But every effort of mysql connection is failed. How can the correct external ip be set on kubernetes service resource for mysql connection?

Upvotes: 0

Views: 1517

Answers (1)

Arghya Sadhu
Arghya Sadhu

Reputation: 44569

If both the mysql and spring boot app running inside the kubernetes cluster then you don't use external IP to connect to mysql from spring boot app.External IP is for accessing something running inside kubernetes from outside kubernetes. You can use servicename:port i.e blog-db-svc:3306 to refer to mysql from spring boot app if they are both running in same namespace. If mysql and spring boot app are in different namespace then you can create a local service in the spring boot app namespace to refer to mysql service located in different namespace.

kind: Service
apiVersion: v1
metadata:
  name: service-y
  namespace: namespace-a
spec:
  type: ExternalName
  externalName: service-x.namespace-b.svc.cluster.local
  ports:
  - port: 3306

Here is a guide on how to use mysql with spring boot in minikube.

Upvotes: 3

Related Questions