从Kubernetes中的其他服务访问服务

时间:2020-08-06 23:36:58

标签: mongodb spring-boot kubernetes

我有一个Spring Boot服务,我想连接到Kubernetes中的mongodb。到目前为止,我已经构建了应用程序的docker映像并创建了Kubernetes部署,并在同一部署YAML文件中同时指定了应用程序和mongodb的映像。另外,我已经为该应用创建了服务YAML,并且运行正常。

deployment.yml

apiVersion: v1
kind: Service
metadata:
  name: springbootmongodb
  labels:
    app: springbootmongodb
spec:
  type: ClusterIP
  ports:
  - name: 8080-8080
    port: 8080
    targetPort: 8080
    protocol: TCP
  selector:
    app: springbootmongodb

-service.yaml

kubectl port-forward svc/springbootmongodb 8080:8080

端口转发命令:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springbootmongodb
  labels:
    app: springbootmongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springbootmongodb
  template:
    metadata:
      labels:
        app: springbootmongodb
    spec:
      containers:
      - name: springbootmongodb
        image: mytest/springbootmongodb

一切都适用于上述配置。

现在,我想为该应用创建单独的部署,并创建了两个部署和服务YAML,但是它不起作用。有人可以帮我吗?

app-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb
  labels:
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
        - name: mongo
          image: mongo
          ports:
            - containerPort: 27017

mongo-deployment.yml

apiVersion: v1
kind: Service
metadata:
  name: springbootmongodb
  labels:
    app: springbootmongodb
spec:
  type: NodePort
  ports:
  - name: 8080-8080
    port: 8080
    targetPort: 8080
    protocol: TCP
  selector:
    app: springbootmongodb

appservice.yml

apiVersion: v1
kind: Service
metadata:
  name: mongodb
  labels:
    app: mongo
spec:
  type: ClusterIP
  ports:
    - port: 27017
      targetPort: 27017
      protocol: TCP
  selector:
    app: mongo

mongoservice.yml

NAME                                     READY   STATUS    RESTARTS   AGE
pod/mongodb-686dd5cb7f-dr9hq             1/1     Running   0          3m16s
pod/springbootmongodb-7ccbc488fb-vtgw5   1/1     Running   0          119s

NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/kubernetes          ClusterIP   10.96.0.1       <none>        443/TCP          59d
service/mongodb             ClusterIP   10.111.83.192   <none>        27017/TCP        2m35s
service/springbootmongodb   NodePort    10.103.18.137   <none>        8080:31015/TCP   8s

NAME                                READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mongodb             1/1     1            1           3m16s
deployment.apps/springbootmongodb   1/1     1            1           119s

NAME                                           DESIRED   CURRENT   READY   AGE
replicaset.apps/mongodb-686dd5cb7f             1         1         1       3m16s
replicaset.apps/springbootmongodb-7ccbc488fb   1         1         1       119s

我看到所有创建的对象:

kubectl获得豆荚 名称就绪状态重启年龄 mongodb-686dd5cb7f-dr9hq 1/1正在运行0 3m12s springbootmongodb-7ccbc488fb-vtgw5 1/1运行0 115秒

kubectl全部获得

2020-08-06 23:19:01.526  INFO 1 --- [nio-8080-exec-8] org.mongodb.driver.cluster               : Cluster description not yet available. Waiting for 30000 ms before timing out
2020-08-06 23:19:07.048 ERROR 1 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]] with root cause

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
        at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:179) ~[mongodb-driver-core-3.8.2.jar!/:na]
        at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41) ~[mongodb-driver-core-3.8.2.jar!/:na]
        at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136) ~[mongodb-driver-3.8.2.jar!/:na]
        at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94) ~[mongodb-driver-3.8.2.jar!/:na]
        at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249) ~[mongodb-driver-3.8.2.jar!/:na]
        at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172) ~[mongodb-driver-3.8.2.jar!/:na]
        at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132) ~[mongodb-driver-3.8.2.jar!/:na]
        at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86) ~[mongodb-driver-3.8.2.jar!/:na]
        at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2643) ~[spring-data-mongodb-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2380) ~[spring-data-mongodb-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2363) ~[spring-data-mongodb-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:820) ~[spring-data-mongodb-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:360) ~[spring-data-mongodb-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:194) ~[spring-data-mongodb-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:51) ~[spring-data-mongodb-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
        at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar!/:5.1.2.RELEASE]
        at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]

服务错误日志:

<!DOCTYPE html>
<html>
<head>
<title>Email Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

2 个答案:

答案 0 :(得分:3)

您似乎需要更改Springboot应用程序的配置以使用mongodb端点:mongodb:27017而不是localhost:27017❓?。由于mongo不再在同一容器/ pod上运行。

In the application.properties file something like this

spring.data.mongodb.uri=mongodb://mongodb:27017/<dbname>

✌️<​​/ p>

答案 1 :(得分:0)

就像 Rico 已经提到你在应用程序配置 'localhost' 中的地址错误

但还有其他问题:)

而不是部署,您应该使用 StateFulSet,因为它旨在具有状态和卷。 当您创建超过 1 个副本时会出现主要问题,所有部署都将具有相同的持久卷,因此可能会导致数据不一致和数据库崩溃。 StateFulSet 将为每个副本创建每个卷。 Ofc 那么你将有 2 个不同数据的数据库,所以它可能会导致另一个问题。

如果您正在构建企业应用程序,那么最好了解指定的数据库和 kubernetes 配置,该配置提供种类:MongoDB。

https://docs.mongodb.com/kubernetes-operator/master/reference/k8s-operator-specification/

另外值得一提的是,您没有使用环境变量将凭据/地址传递到您的数据库。很高兴知道这个东西存在并且应该在配置更改时使用它而不是重建应用程序。