Post

Montando um cluster Kubernetes local com Minikube e deploy de Rails + PostgreSQL

Montando um cluster Kubernetes local com Minikube e deploy de Rails + PostgreSQL

Montando um cluster Kubernetes local com Minikube e deploy de Rails + PostgreSQL

Suba o Minikube, containerize um app Rails, faça deploy do Postgres e exponha o serviço localmente.

Pré-requisitos

  • 4GB RAM/20GB disco, Docker instalado.
  • kubectl (download abaixo).
  • App Rails pronto para containerizar.

Passo a passo

1. Instalar o Minikube

Minikube roda um cluster Kubernetes single-node local.

  1. Baixe e instale o 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. Inicie o Minikube:

    1
    
    minikube start --driver=docker
    

2. Instalar kubectl

kubectl é a CLI para interagir com clusters Kubernetes.

  1. Baixe e instale 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. Verifique a instalação:

    1
    
    kubectl version --client
    

3. Containerizar seu app Rails

  1. Crie um Dockerfile na raiz do app:

    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. Faça o build:

    1
    
    docker build -t myapp:latest .
    

4. Subir PostgreSQL no Kubernetes

  1. Crie o Deployment do Postgres:

    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. Crie o Service do Postgres:

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

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

5. Deploy do app Rails

  1. Crie o Deployment do Rails:

    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. Crie o Service do Rails:

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

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

6. Acessar o app

  1. Pegue a URL do app:

    1
    
    minikube service myapp --url
    
  2. Abra no navegador para acessar o app Rails.

Conclusão

Pronto: Minikube rodando Rails + Postgres. Use como ensaio de manifestos antes de subir para clusters gerenciados.


Esta postagem está licenciada sob CC BY 4.0 pelo autor.