Step 1 : Create a simple express backend application with database

Step 2 : Containerise your application

# Dockerfile
FROM oven/bun:latest

WORKDIR /app
COPY ./package.json ./package.json
COPY ./bun.lock ./bun.lock

RUN bun install
COPY . .

EXPOSE 3000

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

# Build docker image
docker build --platform=linux/amd64 -t ritikaxg/backend-ingress-k8:1 .

# Push to docker.hub
docker push ritikaxg/backend-ingress-k8:1

Step 3 : Create manifest.yml ( deployment ) file for backend application

# Create namespace for backend
apiVersion: v1
kind: Namespace
metadata:
  name: backend-team

---
# Create deployment 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployment
  namespace: backend-team # backend-deployment will be part of backend-team namespace
spec:
  replicas: 2 # 2 Pods will be started
  selector:
    matchLabels:
      app: backend # Deplyment Pods will have this as label and selector will match it
  template: 
    labels:
      app: backend
    spec:
      containers:
        - name: backend
          image: ritikaxg/backend-ingress-k8:1
          ports:
            - containerPort: 3000 # Containers exposes this port internally

---

apiVersion: v1
kind: Service
metadata:
  name: backend-service
  namespace: backend-team # backend-service Service will be part of backend-team namespace
spec:
  selector:
    app: backend # Service routes traffic to pods matching this label
  ports:
    - protocol: TCP
      port: 3000 # backend-service will be exposed at 3000 inside cluster
      targetPort: 3000 # Service forwards traffic to targetPort 3000 on pods
  type: ClusterIP
kubectl apply -f manifest.yml # In dir where manifest file exists
postgres://postgres:[email protected]:5432/postgres

Right now, the backend pod cannot find db.default.svc.cluster.local, so the app crashes → CrashLoopBackOff.

How will a pod in a cluster talk to another pod ?

Different pods can access internal service through ClusterIP service using connection string.

eg backend service pods are accessible via [email protected]:3000

db service pods will be accessible via [email protected]:5432


const pgClient = new pg.Pool({
		# Self-hosting ( Local DB URL )
    connectionString : "postgres://postgres:[email protected]:5432/postgres"
                                          # PASSWORD
      # db : name of service 
      # default : name of namespace where it started
      # 5432 : port where service is exposed                                                       
})

# DB ( Database ) Pod will be exposed via ClusterIP Service ( for internal pod-to-pod communication only )

Step 4 : Creating db deployment