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"]
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"
Related
I have 2 Springboot microservices, one is the Eureka server and the other one is the Gateway.
I can't find the right configuration to make the Gateway register to the Eureka server.
This is the eureka.yml with the K8s configuration:
Eureka.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: eureka-cm
data:
eureka_service_address: http://eureka-0.eureka:8761/eureka
---
apiVersion: v1
kind: Service
metadata:
name: eureka
labels:
app: eureka
spec:
clusterIP: None
ports:
- port: 8761
name: eureka
selector:
app: eureka
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: eureka
spec:
serviceName: "eureka"
replicas: 1
selector:
matchLabels:
app: eureka
template:
metadata:
labels:
app: eureka
spec:
containers:
- name: eureka
image: myrepo/eureka1.0:eureka
imagePullPolicy: Always
ports:
- containerPort: 8761
env:
- name: EUREKA_SERVER_ADDRESS
valueFrom:
configMapKeyRef:
name: eureka-cm
key: eureka_service_address
---
apiVersion: v1
kind: Service
metadata:
name: eureka-lb
labels:
app: eureka
spec:
selector:
app: eureka
type: NodePort
ports:
- port: 80
targetPort: 8761
Eureka.application.yml
spring:
application:
name: eureka
server:
port: 8761
eureka:
instance:
hostname: "${HOSTNAME}.eureka"
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: ${EUREKA_SERVER_ADDRESS}
Gateway.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cloud-gateway-app
labels:
app: cloud-gateway-app
spec:
replicas: 1
selector:
matchLabels:
app: cloud-gateway-app
template:
metadata:
labels:
app: cloud-gateway-app
spec:
containers:
- name: cloud-gateway-app
image: myrepo/gateway1.0:gateway
imagePullPolicy: Always
ports:
- containerPort: 9191
---
apiVersion: v1
kind: Service
metadata:
name: cloud-gateway-svc
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 9191
protocol: TCP
selector:
app: cloud-gateway-app
Gateway.application.yml
eureka:
instance:
preferIpAddress: true
hostname: eureka-0
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka-0.eureka.default.svc.cluster.local:8761/eureka
This is the error I got when I check the logs of the Gateway's pod:
error on POST request for "http://eureka-0.eureka.default.svc.cluster.local:8761/eureka/apps/API-GATEWAY": eureka-0.eureka.default.svc.cluster.local; nested exception is java.net.UnknownHostException: eureka-0.eureka.default.svc.cluster.local
Following the documentation I've tried to set defaultZone property of the Gateway.application.properties file following this pattern:
172-17-0-3.default.pod.cluster.local:8761/eureka
But in this way too, I can't subscribe to the Eureka Server.
I resolved by modifying the Gateway.application.yml in this way:
eureka:
instance:
preferIpAddress: true
hostname: eureka-0
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka-0.eureka:8761/eureka/
EDIT:
I'm encountering some problems in registering other microservices to the Eureka Server.
I've tried by increasing the replicas of the Eureka Server and made each microservice register to a dedicated replica, but as now this is not working.
I'm trying to configure the Eureka port with Spring Cloud with a eureka server, and config server (which is also a eureka client). Eureka service is successfully deployed and got a ip address. My Goal is to get the instance under eureka server.
I'm getting below error
2022-05-24 12:26:10.914 ERROR 21280 --- [freshExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}
2022-05-24 12:03:09.673 WARN 21280 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator : There was a problem with the instance info replicator
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
This is my application.yml file from the test-service
spring:
datasource:
url: jdbc:oracle:thin:#//[ip address]/[address]
username: *******
password: *******
driver-class-name: oracle.jdbc.OracleDriver
profiles:
active:#activatedProperties#
jpa:
database-platform: org.hibernate.dialect.Oracle12cDialect
hibernate:
use-new-id-generator-mappings: false
ddl-auto: update
application:
name: test-service
eureka:
instance:
preferIpAddress: 'true'
client:
fetchRegistry: 'true'
registerWithEureka: 'true'
enabled: 'true'
service-url:
defaultZone: http://[username]:[password]#localhost:8761/eureka
server:
port: 8080
Debug: true
This is my eureka-serviece application.property file
server.port=8761
eureka.server.enable-self-preservation = false
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
spring.security.user.name=******
spring.security.user.password=*******
eureka.instance.preferIpAddress=true
I add new syntax in application.yml but failed.
Config file structure:
- resources
- application.yml
- application-dev.yml
- application-test.yml
In application.yml:
spring:
config:
activate:
on-profile: dev
In application-dev.yml:
server:
port: 9010
spring:
application:
name: account-service
datasource:
url: jdbc:mysql:///account_db
username: root
password: root
Runtime:
2022-04-20 14:16:02.354 INFO 13796 --- [ main] c.lingyuango.seckill.SeckillServiceMain : No active profile set, falling back to 1 default profile: "default"
I have a eureka server up and running fine and am trying to register my config client application with eureka server, but unable to register the instance with eureka server
Config Client Configuration in application.yml
eureka:
client:
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
Eureka server configuration - application.yml
server:
port: ${PORT:8761}
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
Eureka server configuration - bootstrap.yml
spring:
application:
name: eureka
cloud:
config:
uri: ${CONFIG_SERVER_URL:http://localhost:9898}
I have application.yaml configuration file with few profiles:
server:
address: 0.0.0.0
port: 9090
db:
url: 'jdbc:postgresql://localhost:5432/the_db'
driver: 'org.postgresql.Driver'
username: 'postgres'
password: ''
---
spring:
profiles: devArtem
db:
url: 'jdbc:postgresql://localhost:5432/my_db'
---
spring:
profiles: prod_1
db:
password: 'first_pass'
---
spring:
profiles: prod_2
db:
password: 'second_pass'
And I want to remove other profiles before build jar file. I don't want to give access for prod_1's password to prod_2 platform for example.
For prod_1 it must be something like this:
server:
address: 0.0.0.0
port: 9090
db:
url: 'jdbc:postgresql://localhost:5432/the_db'
driver: 'org.postgresql.Driver'
username: 'postgres'
password: 'first_pass'
or this:
server:
address: 0.0.0.0
port: 9090
db:
url: 'jdbc:postgresql://localhost:5432/the_db'
driver: 'org.postgresql.Driver'
username: 'postgres'
password: ''
---
spring:
profiles: prod_1
db:
password: 'first_pass'
You can use multiple application-{profile}.yml and give each team the right file.
for example :
application-devArtem.yml ---> to team devArtem
application-prod_1.yml ---> to team prod_1
application-prod_2.yml ---> to team prod_2