How to deploy docker images in google cloud kubernates

7/16/2019

I have few microservices as :

APIGateway: common gateway for all requestwith zuul proxy

ConfigService: like config server for properties files

RegistryService: service registry with eureka server

HomePageService: 1 service registered with eureka and config-service

ProductService: 1 service registered with eureka and config-service

When I ran in local like order: RegistryService->ConfigService THEN all services APIGateway, HomePageService, ProductService, its working fine.

Now i created docker images by providing config and run in docker container and pushed to GCR. I have created account for google cloud(free for 1 Yr) and able to see images in repo.

All fine BUT how can I deploy those images in GKE. I ran individually and deploying but no linking. What is the way to deploy those services?

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 

I tried something and shared some code snipet.

RegistryService properties:

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 properties:

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 properties:

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 properties:

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/    

for Docker image sample:

  FROM openjdk:8
    EXPOSE 7003
    ADD /target/homepage-service.jar homepage-service.jar
    ENTRYPOINT ["java","-Dspring.profiles.active=docker", "-jar", "homepage-service.jar"]
-- Joe
docker
google-kubernetes-engine
java
microservices
spring-boot

1 Answer

7/16/2019

You deploy Docker containers by defining the container in the podspec of the k8s resource. You are already doing this with the kubectl run commands.

You then expose the pods using services, this is what you are doing with kubectl expose (the default value for this is ClusterIP, which is perfect for inter pod communication)

The reason this is not working for you is the way each container is trying to reach other containers. Localhost won't work. the IP you are using (192.168.x.x) likely isn't working either.

Instead, configure each container to target the FQDN of the corresponding service. Example:

RegistryService->ConfigService

Expose the ConfigService using a ClusterIP service (if you use kubectl expose, the name will be ConfigService).
Configure the RegistryService to look for the ConfigService using "ConfigService.default.svc.cluster.local"

-- Patrick W
Source: StackOverflow