無法從k8s pod訪問外部數據庫


無法從Kubernetes Pod內部連接到外部數據庫(Cannot connecto to external database from inside kubernetes pod)

 1101 2020-08-11 IT屋

Google Fac戳這里《

 

 

百度翻譯此文 有道翻譯此文

 

我正在嘗試將kubernetes pod中的spring-boot api連接到外部數據庫(單獨的機器,但是在本地網絡中),但是在運行時,我得到SQLNonTransientConnectionException,UknownHostException:mysql-svc,該api無法連接到數據庫,但是每當我使用kubectl exec -it從pod內部ping主機中的數據庫地址時,我都能成功ping到任何遇到與我相同的錯誤的人嗎?

 

這是我的application.properties連接uri和kubernetes對象:

 

spring.datasource.url=jdbc:mysql://mysql-svc/test?autoReconnect=true

 

---

apiVersion: v1

kind: Service

metadata:

 name: mysql-svc

spec:

 type: ClusterIP

 ports:

 - port: 3306

   targetPort: 3306

---

apiVersion: v1

kind: Endpoints

metadata:

 name: mysql-svc

subsets:

 - addresses:

   - ip: 10.0.123.28

   ports:

   - port: 3306

     name: mysql-svc

解決方案

 

 

經過數小時的挖掘和重新配置,終於可以正常工作了,這要歸功於kubernetes組的Xenwar指出了externalIP屬性,事實證明我需要將我的應用程序指向外部ip(在本例中為數據庫),這是服務對象:

 

apiVersion: v1

kind: Service

metadata:

  name: db-con

spec:

  selector:

    app: api2

  ports:

    - name: http

      protocol: TCP

      port: 3306

      targetPort: 3306

  externalIPs:

    - 10.0.123.28

原文

I am trying to connect my spring-boot api inside a kubernetes pod to an external database (a separate machine but within the local network), however when running, I get SQLNonTransientConnectionException, UknownHostException: mysql-svc, the api cannot connect to the database but whenever I ping the database address from inside the pod using kubectl exec -it, I can ping it successfully, anybody who has experienced the same error as mine?

 

here is my application.properties connection uri and kubernetes objects:

 

spring.datasource.url=jdbc:mysql://mysql-svc/test?autoReconnect=true

 

---

apiVersion: v1

kind: Service

metadata:

 name: mysql-svc

spec:

 type: ClusterIP

 ports:

 - port: 3306

   targetPort: 3306

---

apiVersion: v1

kind: Endpoints

metadata:

 name: mysql-svc

subsets:

 - addresses:

   - ip: 10.0.123.28

   ports:

   - port: 3306

     name: mysql-svc

解決方案

After hours of digging and reconfiguring, I finally get it to work, thanks to Xenwar from kubernetes group for pointing out the externalIP property, it turns out that i need to point my app to an external ip, in this case the database, here is the service object:

 

apiVersion: v1

kind: Service

metadata:

  name: db-con

spec:

  selector:

    app: api2

  ports:

    - name: http

      protocol: TCP

      port: 3306

      targetPort: 3306

  externalIPs:

    - 10.0.123.28


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM