https://github.com/RitikaxG/configmaps-secrets

image.png

Configmaps & Secrets

API object / resource in K8s that are used to inject env variables. Configmaps are used to store non confidential data in Key-Value Pairs

Step 1 : Start a K8s Cluster

eksctl create cluster --name ingress-cluster --region ap-south-1 --nodes 3 --node-type t2.medium

Step 2 : Write simple express server without .env file ( inject it later via configmaps and secrets )

require("dotenv").config({
    path:"./secret/.env"
}) // Get the env variable from this dir

import express from "express"
const app = express()

console.log(process.env.PORT);
console.log(process.env.DATABASE_URL);
app.listen(3000,()=>{
    console.log("Listening on port 3000")
})

Step 3 : Write Dockerfile

FROM oven/bun:1

WORKDIR /app

COPY ./package.json ./package.json
COPY ./bun.lock ./bun.lock

RUN bun install

COPY . .

CMD ["bun","run","index.ts"]

docker build --platform=linux/amd64 -t ritikaxg/configmaps-secrets:1 .
docker push ritikaxg/configmaps-secrets:1
docker run -e PORT=3000 -e DATABASE_URL=postgres  -p 3000:3000 ritikaxg/configmaps-secrets:1

Step 4 : Write cm.yml to inject non-confidential data to your pod

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-backend
data:
  port: "3000"