Setup a Rails app with Kubernates and Orchestrate a Redis, Postgres pods deployment

Handle multiples pod services as database, cache and api as separate instances

Posted on June 06, 2025
Kubernetes is a widely adopted and more complex solution designed to support versatile, scalable services across diverse environments with multiple services (API, Backend, Webhook), or a more robust solution for managing all your environments, with zero downtime and the capability of test on production your newest changes (ie: canary deployment) is a very attractive option so how we integrate this tool on our Rails app.

Set up your PostgreSQL database volume


The first step is to definecat  your volume, where your database is saved. if you have any experience with Kubernetes, you must know that a Volume and PersistentVolume are necessary. A persistent volume is a instance on K8 where you saved all your data.


Persistent Volume (local purpose). Here we are defining a local storage of 4Gb for write all database records.

apiVersion: v1
metadata:
  name: postgres-pv
  labels:
    type: local
spec:
  capacity:
    storage: 4Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/data"
Persistent Volume Claim. A claim is a request to access storage 

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi

Password and Secrets

Using any service, local or external, you must have the API keys and configuration variables saved. With K8 you have two options for handle that.

  • Secrets: secrets are the alternative to save sensitive data, like API keys or tokens
  • Config Maps: Information for your architecture, like local IPs or environment variables.
For this purpose with use Config Map (config-map.yaml)

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-settings
  labels:
    app: app-settings
data:
  POSTGRES_DB: app_development
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: password
  DATABASE_URL: postgres://postgres:password@postgres:5432/app_development
  REDIS_URL: redis://redis:6380/0
Now we have the PostgreSQL credentials for out local environment


Redis and PostgreSQL instances


Now we have a storage for our database, so for this architecture we have a Redis instance and a Postgres database engine, but in case you have other services or DB engines, you can customize your solution. 

Deployments are a way in K8 of indicating that we are creating a new instance of a specific service; there are many many options customized for deployments.

Redis  

In the containers section set up the version image name (docker image)

kind: Deployment
metadata:
  name: redis-deploy
  labels:
    app: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      name: redis
      labels:
        app: web
        tier: redis
    spec:
      containers:
      - name: redis
        image: redis:6
        ports:
        - containerPort: 6379

Postgres  


Creating the persistent volume is a required previous step.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment-rails
  labels:
    app: web
spec:
  replicas: 1
  minReadySeconds: 10
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      name: postgres-rails
      labels:
        app: web
        tier: postgres-rails
        #track: stable
    spec:
      terminationGracePeriodSeconds: 1
      volumes:
       - name: postgres-pv
         persistentVolumeClaim:
           claimName: postgres-pvc
      containers:
      - name: postgres
        image: postgres:12.10
        envFrom:
        - configMapRef:
            name: app-settings
        env:
        - name: PGDATA
          value: /var/lib/postgresql/data
        ports:
        - containerPort: 5432
        volumeMounts:
        - mountPath: "/var/lib/postgresql/data"
          name: postgres-pv


On postgres deployment we must use the persistent volume and config map.

  1. On volumes tag indicated the named persistent volume claim previously created.
    volumes:
       - name: postgres-pv
         persistentVolumeClaim:
           claimName: postgres-pvc
   2. The persistent volume on volume mount tag.
    volumeMounts:
        - mountPath: "/var/lib/postgresql/data"
          name: postgres-pv
   3. The local environment variables loaded from our config map.
      envFrom:
        - configMapRef:
            name: app-settings

Postgres and Redis services

Now that we have our app running Redis and PostgreSQL, we must open access to both services using a Kubernetes service. One indicator decisive to our services is indicated by our selector tag, in both cases is web.

Redis Service

apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    app: web
spec:
  ports:
    - port: 6379
  selector:
    app: web
    tier: redis
Postgres Service

apiVersion: v1
kind: Service
metadata:
  name: postgres-rails
  labels:
    app: web
spec:
  #type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
    app: web
    tier: postgres-rails


 

App deployment

We have our PostgreSQL database instance, Redis instance for cache, the local volumes. Now we are ready to deploy our Rails app, using the config map for our environment variables. For this step, we must have a Docker image of our app on our local or submitted to Docker repository.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-rails
  labels:
    app: web
spec:
  replicas: 1
  minReadySeconds: 10
  selector:
    matchLabels:
      app: web
      #track: stable
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: web
        #track: stable
    spec:
      terminationGracePeriodSeconds: 1
      volumes:
      - name: html
        emptyDir: {}
      - name: app-config-vol
        configMap:
          name: app-settings
      containers:
      - name: appname
        image: dockerusername/imagename:latest
        #imagePullPolicy: Never
        ports:
        - containerPort: 3000
          hostPort: 3000
        env:
        - name: RAILS_ENV
          value: development
        - name: DATABASE_URL
          valueFrom:
            configMapKeyRef:
              name: app-settings
              key: DATABASE_URL
        - name: REDIS_URL
          valueFrom:
            configMapKeyRef:
              name: app-settings
              key: REDIS_URL

Service and Load Balancer

Final steps setup our app service and the load balancer to access locally
 
apiVersion: v1
kind: Service
metadata:
  name: web-service
  labels:
    app: web
spec:
  type: NodePort
  ports:
  - port: 3000
    targetPort: 3000
    protocol: TCP
  selector:
    app: web

Our ports open are 3000 both in container and listener port, now we can open our service to the local machine


apiVersion: v1
kind: Service
metadata:
  name: web-lb
  labels:
    app: web
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 81
    targetPort: 3000
    #nodePort: 30200
    name: http
  selector:
    app: web
Here we have our services running

Test the access to the app