Spring boot 在运行spring boot应用程序的kubernetes中,如何使用在localhost上运行的服务器的端口

Spring boot 在运行spring boot应用程序的kubernetes中,如何使用在localhost上运行的服务器的端口,spring-boot,docker,kubernetes,localhost,portforwarding,Spring Boot,Docker,Kubernetes,Localhost,Portforwarding,我对库伯内特斯和库贝特尔都是新手。我基本上是在本地主机上运行GRPC服务器。我想在我的mac上使用kubectl在kubernetes上运行的spring boot应用程序中使用此端点。如果我在application.yml中设置以下配置并在kubernetes中运行,它将不起作用。如果我在IDE中运行,同样的配置也可以工作 grpc: client: local-server: address: static://localhost:6565 negotia

我对库伯内特斯和库贝特尔都是新手。我基本上是在本地主机上运行GRPC服务器。我想在我的mac上使用kubectl在kubernetes上运行的spring boot应用程序中使用此端点。如果我在application.yml中设置以下配置并在kubernetes中运行,它将不起作用。如果我在IDE中运行,同样的配置也可以工作

grpc:
  client:
    local-server:
      address: static://localhost:6565
      negotiationType: PLAINTEXT
我看到一些人建议使用port forward,但当我想使用本地主机的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 config中设置什么呢?是同一个localhost:6565、localhost:6566还是如何获取此ip:6565、如何获取此ip:6566。

1在您的应用程序中。属性定义

 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 
四,

使用以下命令查找pod名称

 kubectl get pods 
然后

有用链接:


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


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

对不起,哪个本地主机?物理主机本身、Mac或Minikube VM的Docker和pod各自将localhost路由到它们自己,因此说localhost上正在运行某些东西是相当模糊的。相应地,本地主机上从pod发出的呼叫将返回到pod,而不是其他任何地方。grpc服务器位于物理主机上,在端口6565和6566上可用。我正试图从minikube的豆荚中获取它们。我没有更改上面配置中的地址值配置,所以我基本上是试图从minikube内部访问运行在主机操作系统上的服务@david mazeThanks寻求答案,但正如我在问题中所说的,我并没有试图从主机操作系统访问kubernetes中的服务。事实上,我正试图反过来做。我想从minikube内部访问主机操作系统中的服务。请注意windows PowerShell用户。引号应该不同并转义,否则您将得到'awk:cmd'。行:1:意外令牌“错误:minikube ssh”路由-n | grep^0.0.0.0 | awk\{print\$2}
  kubectl port-forward <pod-name> 8080:8080