我有一些微服务,如:

APIGateway:使用Zuul代理的所有请求的通用网关

ConfigService:类似于属性文件的配置服务器

RegistryService:具有eureka服务器的服务注册表

HomePageService:1个在eureka和config-service注册的服务

ProductService:1个在eureka和config-service注册的服务

当我按本地顺序运行时:
RegistryService-> ConfigService然后将所有服务APIGateway,HomePageService,ProductService正常工作。

现在,我通过提供配置来创建docker映像,并在docker容器中运行并推送到GCR。
我已经为Google Cloud创建了帐户(免费使用1年),并且能够查看回购中的图片。

很好,但是我该如何在GKE中部署这些映像。我单独运行并部署,但没有链接。部署这些服务的方式是什么?

kubectl run service-registry --image=gcr.io/salesstock/service-registry:v1 --port=7002
kubectl expose deployment service-registry --name=service-registry  --type=LoadBalancer --port=7002 --target-port=7002


我尝试了一些并共享了一些代码片段。

RegistryService属性:

spring:
  profiles:
    active: dev
  application:
    name: registry-service
server:
  port: 7002
eureka:
  instance:
    hostname: localhost
    port: 7002
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/

#======docker======
---
spring:
  profiles: docker

eureka:
  instance:
    hostname: 192.168.99.100
    port: 7002
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/


APIGateway属性:

spring:
  profiles:
    active: dev
  application:
    name: api-gateway
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        service-id: config-service
#      uri: http://localhost:8888
server:
  port: 7001
eureka:
  instance:
    hostname: localhost
    port: 7002
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/

#======docker======
---
spring:
  profiles: docker
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        service-id: config-service
#       uri: http://192.168.99.100:8888

eureka:
  instance:
    hostname: 192.168.99.100
    port: 7002
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/


ConfigService属性:

spring:
  profiles:
    active: dev
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri:  git url
          search-paths: ConfigFiles
server:
  port: 8888
management:
  security:
    enabled: false

eureka:
  instance:
    hostname: service-registry
    port: 7002
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/

#======docker======
---
spring:
  profiles: docker
  cloud:
    config:
      server:
        git:
          uri: git repo
          search-paths: ConfigFiles
eureka:
  instance:
    hostname: 192.168.99.100
    port: 7002
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/


HomePageService属性:

spring:
  profiles:
    active: dev
  application:
    name: homepage-service
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        service-id: config-service
#      uri: http://localhost:8888

server:
  port: 7003

#for dynamic port
#server:
#  port: 0
feign:
  client:
    config:
      default:
        connectTimeout: 160000000
        readTimeout: 160000000
management:
  security:
    enabled: false
##  endpoints:
##    web:
##      exposure:
##        include: *
eureka:
  instance:
    hostname: localhost
    port: 7002
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/

#======docker======
---
spring:
  profiles: docker
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        service-id: config-service
#       uri: http://192.168.99.100:8888

eureka:
  instance:
    hostname: 192.168.99.100
    port: 7002
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/


对于Docker映像样本:

  FROM openjdk:8
    EXPOSE 7003
    ADD /target/homepage-service.jar homepage-service.jar
    ENTRYPOINT ["java","-Dspring.profiles.active=docker", "-jar", "homepage-service.jar"]

最佳答案

您可以通过在podspec of the k8s resource中定义容器来部署Docker容器。您已经使用kubectl run命令执行了此操作。

然后,您expose the pods using services,这就是您使用kubectl暴露所要做的事情(此默认值是ClusterIP,非常适合容器间通信)

这对您不起作用的原因是每个容器尝试到达其他容器的方式。 Localhost无法正常工作。您正在使用的IP(192.168.x.x)可能也不起作用。

而是,将每个容器配置为以the FQDN of the corresponding service为目标。
例:

RegistryService-> ConfigService

使用ClusterIP服务公开ConfigService(如果使用kubectl暴露,则名称为ConfigService)。
配置RegistryService以使用“ ConfigService.default.svc.cluster.local”寻找ConfigService。

关于java - 如何在Google Cloud Kubernetes中部署Docker镜像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57054450/

10-10 10:10