Posts Setting Up a Local Kubernetes Cluster with Minikube and Deploying a Ruby on Rails Application with PostgreSQL
Post
Cancel

Setting Up a Local Kubernetes Cluster with Minikube and Deploying a Ruby on Rails Application with PostgreSQL

Setting Up a Local Kubernetes Cluster with Minikube and Deploying a Ruby on Rails Application with PostgreSQL

This tutorial will guide you through setting up a local Kubernetes cluster using Minikube and deploying a Ruby on Rails application with a PostgreSQL database.

Prerequisites

  • A system with at least 4GB of RAM and 20GB of free disk space.
  • Docker installed on your system.
  • Basic knowledge of Kubernetes and Docker.
  • A Ruby on Rails application ready to be containerized.

Step-by-Step Guide

1. Install Minikube

Minikube is a tool that runs a single-node Kubernetes cluster locally.

  1. Download and install Minikube:

    1
    2
    3
    
    curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    chmod +x minikube
    sudo mv minikube /usr/local/bin/
    
  2. Start Minikube:

    1
    
    minikube start --driver=docker
    

2. Install kubectl

kubectl is a command-line tool for interacting with Kubernetes clusters.

  1. Download and install kubectl:

    1
    2
    3
    
    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    chmod +x kubectl
    sudo mv kubectl /usr/local/bin/
    
  2. Verify kubectl installation:

    1
    
    kubectl version --client
    

3. Containerize Your Ruby on Rails Application

  1. Create a Dockerfile in the root directory of your Rails application:

    1
    2
    3
    4
    5
    6
    7
    8
    
    FROM ruby:2.7
    RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
    WORKDIR /myapp
    COPY Gemfile /myapp/Gemfile
    COPY Gemfile.lock /myapp/Gemfile.lock
    RUN bundle install
    COPY . /myapp
    CMD ["rails", "server", "-b", "0.0.0.0"]
    
  2. Build the Docker image:

    1
    
    docker build -t myapp:latest .
    

4. Set Up PostgreSQL on Kubernetes

  1. Create a PostgreSQL Deployment:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: postgres
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: postgres
      template:
        metadata:
          labels:
            app: postgres
        spec:
          containers:
          - name: postgres
            image: postgres:latest
            env:
            - name: POSTGRES_USER
              value: postgres
            - name: POSTGRES_PASSWORD
              value: password
            ports:
            - containerPort: 5432
    
  2. Create a PostgreSQL Service:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    apiVersion: v1
    kind: Service
    metadata:
      name: postgres
    spec:
      ports:
      - port: 5432
      selector:
        app: postgres
    
  3. Apply the configurations:

    1
    2
    
    kubectl apply -f postgres-deployment.yaml
    kubectl apply -f postgres-service.yaml
    

5. Deploy Your Rails Application

  1. Create a Deployment for your Rails application:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myapp
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: myapp
      template:
        metadata:
          labels:
            app: myapp
        spec:
          containers:
          - name: myapp
            image: myapp:latest
            env:
            - name: DATABASE_HOST
              value: postgres
            - name: DATABASE_USER
              value: postgres
            - name: DATABASE_PASSWORD
              value: password
            ports:
            - containerPort: 3000
    
  2. Create a Service for your Rails application:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    apiVersion: v1
    kind: Service
    metadata:
      name: myapp
    spec:
      ports:
      - port: 3000
      selector:
        app: myapp
    
  3. Apply the configurations:

    1
    2
    
    kubectl apply -f myapp-deployment.yaml
    kubectl apply -f myapp-service.yaml
    

6. Access Your Application

  1. Get the URL of your Rails application:

    1
    
    minikube service myapp --url
    
  2. Open the URL in your browser to access your Rails application.

Conclusion

You have successfully set up a local Kubernetes cluster using Minikube and deployed a Ruby on Rails application with a PostgreSQL database. This setup allows you to develop and test your applications in an isolated environment similar to production.

If you encounter any issues, refer to the official Kubernetes documentation or seek help from communities like Stack Overflow.


This post is licensed under CC BY 4.0 by the author.