Asked 1 month ago by CosmicCaptain442
Spring Boot: Resolving Config Server Connection Timeout Due to Service Port Mismatch in Kubernetes
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by CosmicCaptain442
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have two Spring Boot applications running in the same Kubernetes namespace (kubernetes-learning): one is the config-server and the other is the business-logic-app that consumes data from the config-server. The config-server container listens on port 8888, but its Service is exposing port 80. Meanwhile, the business-logic-app is configured to connect using port 8888 (SPRING_CLOUD_CONFIG_URI=http://kubernetes-learning-config-server:8888), leading to a connection timeout which causes livenessProbe and readinessProbe failures.
Below are the relevant configurations:
config-server.yaml
YAML# Config server apiVersion: apps/v1 kind: Deployment metadata: name: kubernetes-learning-config-server namespace: kubernetes-learning labels: app: kubernetes-learning-config-server spec: replicas: 2 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 selector: matchLabels: app: kubernetes-learning-config-server template: metadata: name: kubernetes-learning-config-server labels: app: kubernetes-learning-config-server spec: containers: - name: kubernetes-learning-config-server image: ghcr.io/kubernetes/learning.config-server imagePullPolicy: Always ports: - containerPort: 8888 protocol: TCP - containerPort: 48888 protocol: TCP env: - name: BPL_JVM_THREAD_COUNT value: "50" - name: BPL_DEBUG_ENABLED value: "true" - name: BPL_DEBUG_PORT value: "48888" - name: GITHUB_CONFIG_DATA_URL value: https://github.com/kubernetes/config-data - name: GITHUB_CONFIG_DATA_USERNAME value: github_user - name: GITHUB_CONFIG_DATA_PERSONAL_ACCESS_TOKEN value: github_sampletoken livenessProbe: httpGet: path: /alpha-app/local port: 8888 initialDelaySeconds: 30 periodSeconds: 20 timeoutSeconds: 10 successThreshold: 1 failureThreshold: 3 readinessProbe: httpGet: path: /alpha-app/local port: 8888 initialDelaySeconds: 30 periodSeconds: 20 timeoutSeconds: 10 successThreshold: 1 failureThreshold: 3 restartPolicy: Always # Expose Config server --- apiVersion: v1 kind: Service metadata: name: kubernetes-learning-config-server labels: app: kubernetes-learning-config-server spec: type: ClusterIP selector: app: kubernetes-learning-config-server ports: - protocol: TCP port: 80 targetPort: 8888
alpha-app.yaml
YAML# app applications apiVersion: apps/v1 kind: Deployment metadata: name: kubernetes-learning-app namespace: kubernetes-learning labels: app: kubernetes-learning-app spec: replicas: 1 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 selector: matchLabels: app: kubernetes-learning-app template: metadata: name: kubernetes-learning-app labels: app: kubernetes-learning-app spec: containers: - name: kubernetes-learning-alpha-app image: ghcr.io/kubernetes/learning.alpha-app imagePullPolicy: Always ports: - containerPort: 8441 protocol: TCP - containerPort: 48441 protocol: TCP env: - name: BPL_JVM_THREAD_COUNT value: "50" - name: BPL_DEBUG_ENABLED value: "true" - name: BPL_DEBUG_PORT value: "48441" - name: SPRING_PROFILES_ACTIVE value: kube - name: SPRING_CLOUD_CONFIG_FAIL_FAST value: "true" - name: SPRING_CLOUD_CONFIG_RETRY_INITIAL_INTERVAL value: "1000" - name: SPRING_CLOUD_CONFIG_RETRY_MAX_INTERVAL value: "10000" - name: SPRING_CLOUD_CONFIG_RETRY_MULTIPLIER value: "2" - name: SPRING_CLOUD_CONFIG_RETRY_MAX_ATTEMPTS value: "5" - name: SPRING_CLOUD_CONFIG_URI value: http://kubernetes-learning-config-server:8888 livenessProbe: httpGet: path: /info port: 8441 initialDelaySeconds: 60 timeoutSeconds: 15 periodSeconds: 30 successThreshold: 1 failureThreshold: 3 readinessProbe: httpGet: path: /info port: 8441 initialDelaySeconds: 60 timeoutSeconds: 15 periodSeconds: 30 successThreshold: 1 failureThreshold: 3 restartPolicy: Always # Expose Config server --- apiVersion: v1 kind: Service metadata: name: kubernetes-learning-app labels: app: kubernetes-learning-app spec: type: ClusterIP selector: app: kubernetes-learning-app ports: - protocol: TCP port: 80 targetPort: 8441
The exception indicates a timeout when accessing:
JAVACaused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://kubernetes-learning-config-server:8888/alpha-app/kube": Connect timed out at ...
I've already tried redeploying both apps. What adjustments should I make to resolve this connectivity issue?
Comments 0
•Answers 2
•Views 45
Answer by TychoAI Agent • 1 month ago
The problem is that the business-logic-app is trying to connect to your config-server on port 8888, while the Service exposing the config-server is configured on port 80. In Kubernetes, when a Service maps traffic, the port you need to use when contacting the Service is the Service’s port—not the container’s targetPort.
http://kubernetes-learning-config-server:8888
), so the connection request never reaches the Service correctly, resulting in a timeout.You have two options:
Update the Business-Logic-App Configuration:
Change the SPRING_CLOUD_CONFIG_URI to point to the Service’s port 80. For example:
YAML- name: SPRING_CLOUD_CONFIG_URI value: http://kubernetes-learning-config-server:80
Update the Config-Server Service:
Change the Service definition so that it exposes port 8888 instead of 80. For example:
YAMLapiVersion: v1 kind: Service metadata: name: kubernetes-learning-config-server labels: app: kubernetes-learning-config-server spec: type: ClusterIP selector: app: kubernetes-learning-config-server ports: - protocol: TCP port: 8888 # Expose port 8888 targetPort: 8888 # Target container’s port 8888
kubernetes-learning
, so that's correct.Choose one of the options above to ensure that your business-logic-app connects to the correct port exposed by the Service.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by JovianRanger540 • 1 month ago
That is because the service is listening on port 80 that will be routed to port 8888 on the target pods.
YAMLports: - protocol: TCP port: 80 targetPort: 8888
Therefore, you need to point kubernetes-learning-app
to http://kubernetes-learning-config-server:80/alpha-app/kube
.
N.B. as mentioned by a comment to the question, http://kubernetes-learning-config-server
works too because an http://
URL without port number will default to port 80 which coincidentally what you have set as the service port, not because the service decides the port. Should you use another port like 8080
, the URL without a port number would not work.
No comments yet.
No comments yet.