PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

[FIXED] How to add protocol prefix in Kubernetes ConfigMap

 November 05, 2022     environment-variables, kubernetes, postgresql, spring-boot, yaml     No comments   

Issue

In my Kubernetes cluster, I have a ConfigMap object containing the address of my Postgres pod. It was created with the following YAML:

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-configmap
data:
  database_url: postgres-service

Now I reference this value in one of my Deployment's configuration:

env:
 - name: DB_ADDRESS
   valueFrom:
     configMapKeyRef:
       name: postgres-configmap
       key: database_url

This deployment is a Spring Boot application that intends to communicate with the database. Thus it reads the database's URL from the DB_ADDRESS environment variable. (ignore the default values, those are used only during development)

datasource:
    url: ${DB_ADDRESS:jdbc:postgresql://localhost:5432/users}
    username:  ${POSTGRES_USER:postgres}
    password:  ${POSTGRES_PASSWORD:mysecretpassword}

So, according to the logs, the problem is that the address has to have the jdbc:postgresql:// prefix. Either in the ConfigMap's YAML or in the application.yml I would need to concatenate the prefix protocol string with the variable. Any idea how to do it in yml or suggestion of some other workaround?


Solution

If you create a Service, that will provide you with a hostname (the name of the service) that you can then use in the ConfigMap. E.g., if you create a service named postgres, then your ConfigMap would look like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-configmap
data:
  database_url: jdbc:postgresql://postgres:5432/users


Answered By - larsks
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing