• 无法从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

  • 相关阅读:
    Web性能压力测试工具之WebBench详解
    Request Header Or Cookie Too Large
    手动删除SVCH0ST.EXE的方法
    微信非80端口网页授权
    微信公众号开发80端口映射解决方案 推荐
    解决微信访问 80 端口的限制问题
    Windows查看所有的端口及端口对应的程序
    Windows环境下,用netstat命令查看某个端口号是否占用
    价值观--我的总结
    价值是事物由人(基于自己的需要和获取的难度而)作出的评价而存在的属性
  • 原文地址:https://www.cnblogs.com/cheyunhua/p/15193430.html
Copyright © 2020-2023  润新知