如何在Google Cloud Kubernates中部署Docker映像

时间:2019-07-16 09:46:25

标签: java spring-boot docker microservices google-kubernetes-engine

我的微服务很少:

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

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

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

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

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

当我按照本地顺序运行时: RegistryService-> ConfigService然后再使用API​​Gateway,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"]

1 个答案:

答案 0 :(得分:0)

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

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

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

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

RegistryService-> ConfigService

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