简体   繁体   English

Kubernetes服务的对外连接IP地址是多少?

[英]What is the external connection IP address of Kubernetes service?

I try to make simple Kubernetes Pods and Services with using minikube dashboard.我尝试使用 minikube 仪表板制作简单的 Kubernetes Pod 和服务。 First, I generate Kubernetes mysql Service with the following yaml.首先,我使用以下 yaml 生成 Kubernetes mysql 服务。

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. mysql服务生成成功。 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,但是我的前端应用 docker 镜像是用 Spring Boot 构建的,所以我必须将正确的 mysql 连接 url 分配到 application.properties 文件中,如下所示,

# ==============================================================
# = 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.我将服务资源的外部ip的ip地址,10.96.10.10输入到spring boot application.properties文件中。 But the connection is not successful.但是连接不成功。 I checked the mysql service properties with the kubectl cli command,我使用 kubectl cli 命令检查了 mysql 服务属性,

> 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.我将每个 ip 地址插入到我的 docker 容器的 application.properties 中。 But every effort of mysql connection is failed.但是mysql连接的一切努力都失败了。 How can the correct external ip be set on kubernetes service resource for mysql connection?如何在 mysql 连接的 kubernetes 服务资源上设置正确的外部 ip?

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.如果 mysql 和 spring boot 应用程序都在 kubernetes 集群内运行,那么您不要使用外部 IP 从 spring boot 应用程序连接到 mysql。 External IP is for accessing something running inside kubernetes from outside kubernetes .外部 IP 用于从 kubernetes 外部访问在 kubernetes 内部运行的东西 You can use servicename:port ie blog-db-svc:3306 to refer to mysql from spring boot app if they are both running in same namespace.如果它们都在相同的命名空间中运行,您可以使用servicename:portblog-db-svc:3306从 spring boot 应用程序引用 mysql。 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.如果 mysql 和 spring boot app 在不同的命名空间,那么你可以在 spring boot app 命名空间中创建一个本地服务来引用位于不同命名空间的 mysql 服务。

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.这是有关如何在 minikube 中使用带有 spring boot 的 mysql 的指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM