【问题标题】:How can I use the port of a server running on localhost in kubernetes running spring boot app如何在运行 spring boot 应用程序的 kubernetes 中使用在 localhost 上运行的服务器的端口
【发布时间】:2020-01-27 23:34:20
【问题描述】:

我是 Kubernetes 和 kubectl 的新手。我基本上在我的本地主机中运行 GRPC 服务器。我想在我的 mac 上使用 kubectl 在 kubernetes 上运行的 spring boot 应用程序中使用这个端点。如果我在 application.yml 中设置以下配置并在 kubernetes 中运行,它不起作用。如果我在 IDE 中运行,相同的配置也可以工作。

grpc:
  client:
    local-server:
      address: static://localhost:6565
      negotiationType: PLAINTEXT

我看到有些人建议端口转发,但它是相反的(当我想使用本地主机中已经存在于 kubernetes 中的端口时,它可以工作,就像从本地主机上的浏览器在 kubernetes 中运行的 tomcat 服务器一样)

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: testspringconfigvol
  labels:
    app: testspring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: testspringconfigvol
  template:
    metadata:
      labels:
        app: testspringconfigvol
    spec:
      initContainers:
      # taken from https://gist.github.com/tallclair/849601a16cebeee581ef2be50c351841
      # This container clones the desired git repo to the EmptyDir volume.
      - name: git-config
        image: alpine/git # Any image with git will do
        args:
          - clone
          - --single-branch
          - --
          - https://github.com/username/fakeconfig
          - /repo # Put it in the volume
        securityContext:
          runAsUser: 1 # Any non-root user will do. Match to the workload.
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
        volumeMounts:
        - mountPath: /repo
          name: git-config
      containers:
      - name: testspringconfigvol-cont
        image: username/testspring
        ports:
        - containerPort: 8080
        volumeMounts:  
        - mountPath: /usr/local/lib/config/
          name: git-config
      volumes:
        - name: git-config
          emptyDir: {}

简单来说我需要什么:

在我的本地主机中有一些服务器的端口: localhost:6565、localhost:6566,我需要在我的 kubernetes 中以某种方式访问​​这些端口。那么我应该在 application.yml 配置中设置什么?会不会是一样的localhost:6565、localhost:6566 或how-to-get-this-ip:6565、how-to-get-this-ip:6566。

【问题讨论】:

  • 抱歉,哪个localhost?物理主机本身、Mac 或 Minikube VM 的 Docker 和 pod 各自单独路由 localhost 到它们自己,所以说某些东西在“本地主机上”运行是非常含糊的。 (相应地,localhost 上的 pod 外调用将返回到 pod,而不是其他任何地方。)
  • grpc 服务器位于物理主机上,可在端口 6565 和 6566 上使用。我正在尝试从 minikube 中的 pod 访问它们。上面配置中的地址值我没有改过。
  • 所以基本上我试图从 minikube 内部访问在主机操作系统上运行的服务。 @大卫迷宫

标签: spring-boot docker kubernetes localhost portforwarding


【解决方案1】:

我们可以通过这个命令minikube ssh "route -n | grep ^0.0.0.0 | awk '{ print \$2 }'" 使用 minikube 获取 vmware 主机 ip。对我来说,在 Mac 上是 10.0.2.2。如果在 Docker for mac 上使用 Kubernetes,则为 host.docker.internal

通过使用这些命令,我​​设法从 kubernetes 连接到主机上运行的服务。

【讨论】:

  • Windows (PowerShell) 用户注意事项。引号应该不同并且被转义(否则你会得到 'awk: cmd.line:1: Unexpected token' 错误):minikube ssh 'route -n | grep ^0.0.0.0 | awk \"{ print \$2 }\"'
【解决方案2】:

1) 在你的 application.properties 中定义

 server.port=8000

2) 创建 Dockerfile

# Start with a base image containing Java runtime (mine java 8)
FROM openjdk:8u212-jdk-slim
# Add Maintainer Info
LABEL maintainer="vaquar.khan@gmail.com"
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 8080 available to the world outside this container
EXPOSE 8080
# The application's jar file (when packaged)
ARG JAR_FILE=target/codestatebkend-0.0.1-SNAPSHOT.jar
# Add the application's jar to the container
ADD ${JAR_FILE} codestatebkend.jar
# Run the jar file 
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/codestatebkend.jar"]

3) 确保 docker 工作正常

       docker run --rm -p 8080:8080 

4)

使用以下命令查找 pod 名称

 kubectl get pods 

然后

  kubectl port-forward <pod-name> 8080:8080

有用的链接:

【讨论】:

  • 感谢您的回答,但正如我在问题中所说,我并没有尝试从主机操作系统访问 kubernetes 中的服务。事实上,我正试图反过来做。我想从 minikube 内部访问主机操作系统中的服务。
猜你喜欢
  • 2019-08-06
  • 2017-12-15
  • 2016-02-15
  • 2021-08-20
  • 2023-04-06
  • 2021-05-03
  • 2017-07-17
  • 2019-03-05
  • 1970-01-01
相关资源
最近更新 更多