Shiru99
Shiru99

Reputation: 459

Kubernetes (K8s) Minikubes : how to use service URL in ConfigMap so other pods can use it

database-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: postgres
  name: postgres-db
spec:
  replicas:
  selector:
    matchLabels:  
      app: postgres-db
  template:
    metadata:
      labels:
        app: postgres-db
    spec:
      containers:
      - name: postgres-db
        image: postgres:latest
        ports:
        - protocol: TCP
          containerPort: 1234
        env:
        - name: POSTGRES_DB
          value: "classroom"
        - name: POSTGRES_USER
          value: temp
        - name: POSTGRES_PASSWORD
          value: temp

database-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: database-service
spec:
  selector: 
    app: postgres-db
  ports:
  - protocol: TCP
    port: 1234
    targetPort: 1234

I want to use this database-service url for other deployment so i tried to add it in configMap

my-configMap.yaml

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

[Not Working] Expected - database_url : database-service (will be replaced with corresponding service URL)

ERROR - Driver org.postgresql.Driver claims to not accept jdbcUrl, database-service

$ kubectl describe configmaps classroom-configmap

Output :

Name:         classroom-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
database_url:
----
database-service

BinaryData
====

Events:  <none>

Upvotes: 1

Views: 917

Answers (2)

Shiru99
Shiru99

Reputation: 459

updated my-configMap.yaml (database_url)

apiVersion: v1
kind: ConfigMap
metadata:
  name: classroom-configmap
data:
  database_url: jdbc:postgresql://database-service.default.svc.cluster.local:5432/classroom

expected URL - jdbc:{DATABASE}://{DATABASE_SERVICE with NAMESPACE}:{DATABASE_PORT}/{DATABASE_NAME}

DATABASE_SERVICE - database-service

NAMESPACE - default

DATABASE_SERVICE with NAMESPACE - database-service.default.svc.cluster.local

Upvotes: 1

Pepe T.
Pepe T.

Reputation: 184

According to the error you are having:

Driver org.postgresql.Driver claims to not accept jdbcUrl

It seems that there are a few issues with that URL, and a latest PSQL driver may complain.

  1. jdbc:postgres: isn't right, use jdbc:postgresql:instead
  2. Do not use jdbc:postgresql://<username>:<passwor>..., user parameters instead: jdbc:postgresql://<host>:<port>/<dbname>?user=<username>&password=<password>
  3. In some cases you have to force SSL connection by adding sslmode=require parameter

Upvotes: 1

Related Questions