Techdots
Blog
All You Need to Know About Kamal
Learn about Kamal, a tool that simplifies Kubernetes deployment. Kamal automates scaling, integrates with CI/CD, and enhances efficiency in DevOps workflows.

Kubernetes has become the backbone of containerized application management—but its complexity often presents a steep challenge. 

Kamal, an open-source deployment tool is built to simplify and automate the intricate tasks of Kubernetes environments. With its robust CI/CD integration and emphasis on seamless scaling, Kamal equips DevOps teams to streamline deployments and enhance workflow efficiency. 

In this guide, we’ll explore how Kamal transforms Kubernetes management, making it an essential tool for modern tech teams. So, let’s get into the topic. 

An Overview of Kamal

Kamal is an open-source deployment tool designed to streamline the process of deploying applications in a Kubernetes environment. 

As Kubernetes has become a cornerstone for managing containerized applications, Kamal addresses many complexities by simplifying deployment, scaling, and management tasks. 

Here are the top reasons why Kamal matters:

Kubernetes Simplicity: While powerful, Kubernetes has a steep learning curve. Kamal abstracts many complexities, allowing teams to deploy applications with fewer manual configurations.

DevOps Integration: Kamal’s tight integration with CI/CD pipelines and tools like Docker enhances deployment automation and consistency.

Scalability and Reliability: Kamal’s features make scaling microservices more manageable and improve the reliability of production environments, especially in large-scale operations.

Features of Kamal:

Kamal offers several powerful features that make it stand out in the Kubernetes ecosystem:

CI/CD Integration: Kamal integrates seamlessly with tools like GitHub Actions, Jenkins, and CircleCI to ensure that deployments are automated from code commit to production.

Container Orchestration: Managing Docker containers is a breeze with Kamal, as it helps in building, tagging, and pushing Docker images into your Kubernetes clusters.

Kubernetes Management: Kamal takes care of managing Kubernetes resources such as pods, services, and ingress, allowing developers to focus on coding rather than infrastructure.

Declarative Configuration: Kamal employs a declarative approach, where the desired state of your application is defined in YAML files. This allows for easy versioning and rollback.

Rollback Automation: One of Kamal's standout features is its automated rollback mechanism, which ensures that any failed deployments can revert to a stable state with minimal manual intervention.

Advanced Monitoring & Logging: Kamal integrates with tools like Prometheus and Grafana to provide detailed metrics and logs for your applications.

Kamal Vs. Other Deployment Tools:

 Here is a quick comparison of Kamal and other deployment tools that are used for scalability and microservice:

Feature

Kamal

Helm

Skaffold

Kustomize

CI/CD Integration

Deep integration with CI/CD workflows, streamlining deployment and automation

Limited integration with CI/CD workflows

Basic CI/CD integration, primarily for local development

No CI/CD integration, focused on configuration overlay

Automated Rollbacks

Supports automated rollbacks for smoother error recovery

Limited support for rollbacks, requires manual configuration

Lacks native rollback mechanisms

Lacks rollback functionality

Kubernetes Resource Management

Simplifies resource management with automated controls

Complex resource management due to templating system

Focuses on build and development stages, limited production management

Emphasizes configuration overlays without resource management

Production-Level Features

Supports container orchestration, continuous deployment, and monitoring for production environments

Primarily focuses on templating for application packaging

Optimized for local development workflows, lacks robust production capabilities

Configuration overlays without production-level management tools

Real-Time Monitoring

Includes real-time monitoring for proactive issue resolution

No real-time monitoring, requiring external tools

No real-time monitoring, requires external integration

Lacks monitoring and automation features

Primary Use Case

Production-grade deployments with full CI/CD, scaling, and automation

Packaging and deploying Kubernetes applications as charts

Local development workflows and build automation

Overlaying and managing Kubernetes configurations without templates

How Kamal Fits into a DevOps Workflow

Kamal is designed with a DevOps-first approach, making it an integral part of a CI/CD pipeline. Here's how Kamal fits into a typical workflow:

Code Changes and Commit: A developer pushes code changes to a Git repository (e.g., GitHub).

CI Pipeline: A CI tool (e.g., Jenkins) builds the application and triggers Kamal’s deployment process. Kamal automatically builds and tags Docker images.

Kubernetes Resource Management: Kamal defines Kubernetes manifests (for services, deployments, etc.) and pushes them to the cluster.

Deployment and Monitoring: The deployment is executed, and real-time monitoring is available via integrated services like Prometheus. In case of failure, Kamal's rollback feature automatically restores the previous version of the app.

Kamal’s ability to manage Docker and Kubernetes resources simplifies end-to-end deployments, making it indispensable in DevOps workflows that rely on automation.

System Design Considerations

Kamal introduces several advantages when considering system design in microservices architectures:

Scalability: Kubernetes natively supports scaling, and Kamal simplifies the process with automated updates to your deployment configurations.

Reliability: Automated rollbacks and health checks ensure that applications remain stable, even in case of errors during deployment.

Resource Optimization: Kamal helps optimize resource usage by automatically scaling applications based on real-time load and usage data, ensuring cost-efficiency.

Security: Through integration with Kubernetes RBAC (Role-Based Access Control) and secret management, Kamal helps enforce security policies across deployments.

Learn how to test Ruby on Rails with Capybara and RSpec. 

Step-by-step Guide to Deploy a Ruby-Based Application with Kamal

Now that you know all the basics of Kamla and how it stands out for microservice and sclabaility, let’s walk through deploying a Ruby application using Kamal. But first, let’s talk about the prerequisites of cloud infrastructure: 

Prerequisites

To deploy the ruby-based application you need:

  • Docker installed
  • A Kubernetes cluster (can be set up with Minikube (See Setup) or any cloud provider like GKE or EKS)
  • Kamal installed (gem install kamal)
  • A Ruby-based app with a Dockerfile

Step 1: Build the Docker Image & configure Kamal

Define your Dockerfile:

FROM ruby:3.0
WORKDIR /app
COPY . .
RUN bundle install
CMD ["rails", "server", "-b", "0.0.0.0"]

Generate Kamal configuration

kamal init

This will create a file in config/deploy.yml:

# Name of your application. Used to uniquely configure containers.
service: my-app

# Name of the container image.
image: my-user/my-app

# Deploy to these servers.
servers:
  web:
    - 192.168.0.1
  # job:
  #   hosts:
  #     - 192.168.0.1
  #   cmd: bin/jobs

# Enable SSL auto certification via Let's Encrypt (and allow for multiple apps on one server).
# If using something like Cloudflare, it is recommended to set encryption mode
# in Cloudflare's SSL/TLS setting to "Full" to enable end-to-end encryption.
proxy:
  ssl: true
  host: app.example.com
  # kamal-proxy connects to your container over port 80, use `app_port` to specify a different port.
  # app_port: 3000

# Credentials for your image host.
registry:
  # Specify the registry server, if you're not using Docker Hub
  # server: registry.digitalocean.com / ghcr.io / ...
  username: my-user

  # Always use an access token rather than real password (pulled from .kamal/secrets).
  password:
    - KAMAL_REGISTRY_PASSWORD

# Configure builder setup.
builder:
  arch: amd64

# Inject ENV variables into containers (secrets come from .kamal/secrets).
#
# env:
#   clear:
#     DB_HOST: 192.168.0.2
#   secret:
#     - RAILS_MASTER_KEY

# Aliases are triggered with "bin/kamal <alias>". You can overwrite arguments on invocation:
# "bin/kamal logs -r job" will tail logs from the first server in the job section.
#
# aliases:
#   shell: app exec --interactive --reuse "bash"

# Use a different ssh user than root
#
# ssh:
#   user: app

# Use a persistent storage volume.
#
# volumes:
#   - "app_storage:/app/storage"

# Bridge fingerprinted assets, like JS and CSS, between versions to avoid
# hitting 404 on in-flight requests. Combines all files from new and old
# version inside the asset_path.
#
# asset_path: /app/public/assets

# Configure rolling deploys by setting a wait time between batches of restarts.
#
# boot:
#   limit: 10 # Can also specify as a percentage of total hosts, such as "25%"
#   wait: 2

# Use accessory services (secrets come from .kamal/secrets).
#
# accessories:
#   db:
#     image: mysql:8.0
#     host: 192.168.0.2
#     port: 3306
#     env:
#       clear:
#         MYSQL_ROOT_HOST: '%'
#       secret:
#         - MYSQL_ROOT_PASSWORD
#     files:
#       - config/mysql/production.cnf:/etc/mysql/my.cnf
#       - db/production.sql:/docker-entrypoint-initdb.d/setup.sql
#     directories:
#       - data:/var/lib/mysql
#   redis:
#     image: redis:7.0
#     host: 192.168.0.2
#     port: 6379
#     directories:
#       - data:/

Step 2: Update Kamal configuration

After generating kamal configuration, the next step is to update it. Edit the config/deploy.yml file that was created. Define the service, deployment, and environment settings for the Rails app.

Registry: Change server to your docker registry host(e.g registry.digitalocean.com ) or leave it blank for default Docker Hub. Replace my-user with registry username. Set KAMAL_REGISTRY_PASSWORD value in env.

Server: Add array of IP addresses or hashes. E.g

servers:
  web:
    - 192.168.0.1

OR

servers:
    - 192.168.0.1
    - 192.168.0.2

Make sure you have ssh access to these servers.

SSH: You can configure your ssh user/pass/key in this section.

Envs: Add your app environments.

env:
  clear:
    DB_HOST: Host name of your db

    DB_USER: User of your db

    …
  secret:
    - RAILS_MASTER_KEY

    …

Volume: You can persists storage volumes by define array of paths e.g:

volumes:
  - "app_storage:/app/storage"

Step 3: Setup & Deploy the Application

Deploy the Ruby app to your Kubernetes cluster:

kamal deploy

Kamal will handle the deployment process, including setting up Kubernetes resources.

Step 4: Monitor the Deployment

Kamal integrates with monitoring tools like Prometheus. Set up monitoring for your application to track performance and deployment health:

kamal monitor

Step 5: Automating Rollbacks

If something goes wrong with your deployment, Kamal can automatically trigger a rollback:

kamal rollback

Optimizing Deployment Efficiency:

To maximize deployment efficiency with Kamal, follow these best practices:

  • Use Efficient Docker Builds: Employ multi-stage builds in Docker to minimize image sizes, which reduces deployment time.
  • Parallel Deployments: Kamal supports rolling updates, allowing you to deploy updates without downtime.
  • Automated Rollbacks: Make sure you configure health checks and error monitoring so that Kamal can trigger rollbacks automatically when needed.

Kamal in Production Environments:

In production environments, Kamal shines by offering advanced scaling and monitoring capabilities:

  • Auto-Scaling: Configure Horizontal Pod Autoscalers (HPA) with Kamal to automatically scale your application based on CPU and memory usage.
  • Logging and Monitoring: Integrate Kamal with centralized logging (e.g., ELK Stack) and monitoring tools like Prometheus to ensure you have real-time insights into your application’s performance.
  • Security Considerations: Use Kubernetes secrets and configure network policies to ensure your production environment remains secure.

Setup Minikube

To set up Minikube, you need: 

Installation

Download the Binary file and install it:

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

sudo install minikube-linux-amd64 /usr/local/bin/minikube && rm minikube-linux-amd64

Step 1: Start your cluster:

minikube start

This will set up a local Kubernetes cluster. If you're using Docker as your container runtime, Minikube will automatically configure it.

 

 You can check the status of your Minikube cluster with:

minikube status

Step 2: Configure Docker to Use Minikube's Docker Daemon

Since Kamal works with Docker to build and manage images, you'll need to configure your Docker CLI to point to Minikube’s Docker environment:

eval $(minikube -p minikube docker-env)

This command will set your Docker environment variables to the Minikube cluster's daemon. Now, any Docker commands (such as build or push) will affect the Minikube cluster’s internal Docker registry rather than your local Docker environment.

You can verify this by running:

docker ps

To switch back to docker daemon when done with minikube::

eval $(minikube -p minikube docker-env --unset)

Step 3: Access Your Application

Once deployed, you can access your application using Minikube’s service functionality. To get the URL for your service, run:

minikube service <your-service-name> --url

Replace <your-service-name> with the name of your Kubernetes service (defined in your YAML file, typically ruby-app).

minikube service ruby-app --url

Step 4: Verify Kubernetes Resources

To verify that Kamal has correctly deployed the app, you can check the Kubernetes resources using kubectl:

kubectl get pods
kubectl get services

This will show the running pods and services in your Minikube cluster.

Conclusion

That’s all you need to know about Kamal. It offers a streamlined, powerful approach to deploying applications in Kubernetes environments. Its tight integration with CI/CD workflows, automated rollbacks, and easy-to-use Kubernetes resource management make it a game-changer for DevOps teams. 

By simplifying the complexities of Kubernetes, Kamal empowers teams to focus on building features while ensuring that their deployments are reliable, scalable, and secure. 

Contact Techdots for assistance in microservices architecture or deploying a monolithic app via Kamal.

 

 

Contact
Us
Our Technology Stack

Work with future-proof technologies

Typescript
React JS
Node JS
Angular
Vue JS
Rails
Ruby on Rails
Go
Next JS
AWS
SASS