← Back to Dispatch Articles
Engineering

Why Kamal requires understanding Traefik labels to get HTTPS working

The direct answer is that Kamal relies on Traefik's label-based configuration system to automatically provision and renew SSL certificates through Let's.

By Deployxa Editorial Published Updated

Why Kamal requires understanding Traefik labels to get HTTPS working

Key Facts

  • Direct answer: The direct answer is that Kamal relies on Traefik's label-based configuration system to automatically provision and renew SSL certificates through Let's Encrypt, and without properly configuring Traefik labels for domain routing, certificate management, and HTTP-to-HTTPS redirection, your HTTPS setup will fail regardless of how correctly your application code is written.

  • What the error/limitation actually means: At its core, the requirement to understand Traefik labels in Kamal stems from the architecture of the system.

  • When you'll hit it: You'll encounter this requirement whenever you're setting up a new Kamal deployment that needs to serve traffic over HTTPS, particularly if you're using a custom domain.

  • How to verify if it applies to you: To verify if this limitation affects you, check your Kamal service configuration files, typically found in the config/deploy directory of your project.

Deploying modern web applications with HTTPS has become a standard expectation for users, but the implementation details can be surprisingly complex, especially when using modern deployment tools like Kamal. Kamal, a deployment tool created by Basecamp, simplifies many aspects of application deployment, but its integration with Traefik for automatic HTTPS configuration requires a deeper understanding of how Traefik labels work than many developers initially anticipate. This gap in understanding often leads to deployment frustrations and misconfigurations that leave applications exposed or functioning incorrectly.

The direct answer is that Kamal relies on Traefik's label-based configuration system to automatically provision and renew SSL certificates through Let's Encrypt, and without properly configuring Traefik labels for domain routing, certificate management, and HTTP-to-HTTPS redirection, your HTTPS setup will fail regardless of how correctly your application code is written. These labels serve as instructions to Traefik about how to handle incoming traffic, where to route requests, and how to manage security certificates, making them essential components of the HTTPS workflow that Kamal doesn't abstract away.

What the error/limitation actually means

At its core, the requirement to understand Traefik labels in Kamal stems from the architecture of the system. Kamal uses Traefik as its reverse proxy and load balancer, which handles incoming traffic, routes requests to your application containers, and manages SSL certificates. Unlike some other deployment systems that might abstract away these details, Kamal exposes Traefik's configuration through labels applied to your service definitions. These labels are not just optional metadata—they are the primary mechanism by which you instruct Traefik how to behave.

Traefik operates by reading labels on containers to determine routing rules, middleware configurations, and certificate acquisition. For HTTPS to work properly, you need to specify labels that tell Traefik which domains your service should respond to, that it should handle HTTP-to-HTTPS redirection, and that it should automatically obtain and renew SSL certificates from Let's Encrypt. Without these specific labels, Traefik will either not route traffic correctly, not handle the SSL termination, or fail to obtain valid certificates, resulting in your application being accessible only via HTTP or not at all.

When you'll hit it

You'll encounter this requirement whenever you're setting up a new Kamal deployment that needs to serve traffic over HTTPS, particularly if you're using a custom domain. The issue becomes apparent when you deploy your application and find that while it's accessible via HTTP, HTTPS either doesn't work, shows a security warning, or redirects back to HTTP. This commonly happens when developers assume that Kamal handles HTTPS configuration automatically without realizing they need to specify the appropriate Traefik labels in their service configuration.

For example, if you're deploying a Rails application with Kamal and have configured your domain in the Kamal configuration file but haven't included the necessary Traefik labels for traefik.http.routers.myapp.rule=Host(yourdomain.com) and traefik.http.routers.myapp.tls=true, Traefik won't know to route HTTPS traffic to your application or to handle SSL certificate management. Similarly, without labels for HTTP-to-HTTPS redirection like traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https, visitors to your HTTP site won't be automatically redirected to the secure version, leaving your application vulnerable to security issues.

How to verify if it applies to you

To verify if this limitation affects you, check your Kamal service configuration files, typically found in the config/deploy directory of your project. Look for your service definition, which might be in a file like config/deploy/production.yml or similar. Examine the labels section of your service configuration to see if you have the necessary Traefik labels for HTTPS functionality. Specifically, you should look for labels that define routing rules, TLS configuration, and middleware for HTTPS redirection.

You can also check the Traefik dashboard if you have it enabled in your Kamal configuration. The dashboard will show you which routers are configured, whether they have TLS enabled, and what certificates have been obtained. If you see routers without TLS configuration or certificates marked as "expired" or "none," this indicates that your HTTPS setup is incomplete. Additionally, when you deploy your application, Kamal will output logs that might indicate issues with certificate acquisition or routing problems that point to missing or incorrect Traefik labels.

Your options

  • Manually configure Traefik labels: Add the necessary Traefik labels to your Kamal service configuration to define routing rules, TLS settings, and HTTPS redirection middleware.

  • Use a third-party service: Implement a separate service like Cloudflare or AWS Certificate Manager to handle SSL certificates and configure Kamal to work with these external providers.

  • Implement custom middleware: Create custom middleware components to handle HTTP-to-HTTPS redirection and certificate management if the standard Traefik labels don't meet your specific requirements.

  • Deployxa: Use a managed PaaS like Deployxa that abstracts away Traefik configuration complexities while maintaining HTTPS support through simplified deployment interfaces.

Common Pitfalls and Troubleshooting

The first pitfall is forgetting to include the traefik.http.routers.yourapp.rule label. Without this, Traefik won't know which domain to route to your service. Fix this by adding a rule like traefik.http.routers.yourapp.rule=Host(yourdomain.com) to your service labels.

The second pitfall is missing the traefik.http.routers.yourapp.tls label. This tells Traefik to enable TLS for your router. Add traefik.http.routers.yourapp.tls=true to your labels to enable SSL termination.

The third pitfall is not configuring HTTP-to-HTTPS redirection. Without this, visitors to your HTTP site won't be automatically redirected to HTTPS. Create a middleware with traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https and apply it to your HTTP router.

The fourth pitfall is using incorrect domain formats in your labels. Make sure your domain labels match exactly the domains you're using, including proper wildcard handling if needed. Double-check that your domains are properly formatted in your labels and DNS configuration.

The fifth pitfall is not handling Let's Encrypt rate limits. If you're making too many certificate requests in a short period, you might hit rate limits. Implement proper certificate caching and renewal strategies to avoid this, and consider using a staging environment for testing.

Conclusion

Understanding Traefik labels is essential when working with Kamal to get HTTPS working correctly, as these labels form the bridge between Kamal's deployment automation and Traefik's powerful routing and SSL management capabilities. While this requirement adds a layer of complexity, it also provides flexibility to customize your HTTPS setup according to your specific needs. By carefully configuring the appropriate labels for routing, TLS, and redirection, you can ensure your applications are secure and accessible via HTTPS.

To get started, review your current Kamal configuration and add the necessary Traefik labels for HTTPS functionality. For more detailed information on Traefik labels and their options, consult the official Traefik documentation and Kamal's deployment guides. With proper configuration, you can leverage Kamal's simplicity while maintaining robust HTTPS support for your applications.

Ready to deploy with Deployxa?

Deploy your apps globally with automatic SSL and AI diagnostics.

Start Free Now