Protecting web applications with TLS/SSL used to be considered necessary only for applications handling sensitive information, since getting an official certificate had a cost and required extra setup. Let’s Encrypt lets us create official certificates in an automated way without any cost, meaning we can add this layer of security to any website without trade-offs.
Rancher manages Docker containers in an intuitive way with an easy-to-use dashboard. Rancher has a catalog of popular applications that we can deploy instantly, including a Let’s Encrypt service that can generate certificates, and will also take care of renewal when necessary. Once created, the certificates are stored within Rancher and are available for use without any complications.
The process to set up Let’s Encrypt in Rancher consists of three major steps: We deploy the Let’s Encrypt service, we apply the certificate it generates to the load balancer, and we set up HTTP to HTTPS redirection. This tutorial walks you through the entire process.
Note: As of December 15, 2022, DigitalOcean no longer supports the creation of new RancherOS Droplets through the Control Panel or API. However, any existing RancherOS Droplets created prior to December 15, 2022, will be functional despite the change in offerings. Additionally, you can still spin up RancherOS Droplets using a custom image. Learn how to import a custom image to DigitalOcean by following our product documentation.
To complete this tutorial, you will need:
your_domain
pointed at the public IP address of your host that runs the Rancher Load Balancer service. This is required because of how Let’s Encrypt validates that you own the domain it is issuing a certificate for. You can follow the tutorial How To Set Up a Host Name with DigitalOcean to configure this record. Ensure that you can view your deployed application at http://your_domain
before you begin this tutorial.We are going to deploy the Let’s Encrypt service as a Docker container, which is going to be hosted on one of our Rancher hosts. The process consists of selecting the Let’s Encrypt service from the Rancher catalog and filling in the required information. Once you finish this step, you will have a certificate available in Rancher. Best of all, the service will automatically renew the certificate when it is close to expiration, without any further action on your part.
To start, go to the Rancher Catalog by clicking the Catalog menu at the top of the Rancher user interface. Then search for the Let’s Encrypt service. Once you find it, click the View Details button and follow these steps to configure the service:
Next, we need to tell Racher’s load balancer service to forward requests for /.well-known/acme-challenge
to our new Certificate service. Without this, Let’s Encrypt won’t be able to verify that we are the owner of the domain. Follow these steps to complete the process:
80
./.well-known/acme-challenge
.With the new rule in place, start the Let’s Encrypt service:
At this point, the Let’s Encrypt service should be running and a certificate will be created. The process can take anywhere from 5 to 15 minutes. Select the Infrastructure menu and choose Certificates to view the certificates. In a short while, you’ll see the new certificate appear, although you may need to refresh the page. Once you see the certificate, you can use it with your application.
Once the Let’s Encrypt certificate is available in Rancher, you can select it for use in the Rancher Load Balancer service. To do that, you’ll change the rule in your Load Balancer to use HTTPS and apply the certificate. Follow these steps to make those configuration changes:
If you access the website with the HTTPS protocol (https://your_domain
) you can see that the connection is now secure. But since you replaced port 80
with port 443
, any request via HTTP will no longer work. To solve this problem, we could just add back the rule for HTTP and port 80
that we had before, but instead, we will adjust our load balancer to redirect the traffic from HTTP to HTTPS. This ensures people always visit the site in a secure manner.
The Rancher Load Balancer service has support for custom HAProxy configuration settings. We are going to use that feature to include some configuration that will redirect all the traffic coming from HTTP to HTTPS. The approach in this section leverages the Let’s Encrypt service you configured previously, as it’s currently listening on port 80
to forward domain verification requests.
To set up the redirection, locate your load balancer service in Rancher and press the Upgrade/Edit button to access the settings as you did in the previous steps. Once the settings page appears, select the Custom haproxy.cfg tab at the bottom of the page.
Add the following piece of code to create the redirection:
frontend 80
acl lepath path_beg -i /.well-known/acme-challenge
redirect scheme https code 301 if !lepath !{ ssl_fc }
This creates a rule for the load balancer that redirects all traffic to HTTPS, but ignores requests for the /.well-known/acme-challenge
path we configured for Let’s Encrypt domain verification. We use code 301
to indicate that we want a permanent redirection for this domain. To learn more about redirection settings, you can look at the HAProxy documentation.
Click the Edit button at the bottom of the page to apply these changes.
At this point, every time your visitors access the website through HTTP, they will be redirected to HTTPS, making the website secure for everybody. Now we can proceed to test our website.
To test your website, open the address in a web browser, using the HTTP protocol (http://your_domain
) and then look for the secure indicator in the address bar. You can also test it using the curl
utility by executing the following command, which sends a request to the server, follows any redirects, and returns only the response headers:
- curl -I -L http://your_domain
You should see a result like the following:
OutputHTTP/1.1 301 Found
Cache-Control: no-cache
Content-length: 0
Location: https://your_domain/
Connection: close
HTTP/1.1 200 OK
Cache-Control: public, max-age=0
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Date: Sun, 19 Feb 2017 03:42:47 GMT
The first block of output shows the response when first requesting the website through HTTP, saying that it was found but the location is now in another address. Note the 301 Found
section, which tells you that the HAProxy rule we added has worked. The Location
section shows the new location of the requested resource. The second block of output shows that curl
followed the redirect to the new location. It also shows that the website has been found at the new location, as indicated by the 200 OK
response.
In this tutorial, you set up HTTPS on a website using Rancher and the Let’s Encrypt service. Getting a secure website is now easier than ever and you don’t have to worry about constantly renewing your certificates or setting up other tools for the task. And with Rancher, you can scale up your infrastructure to meet future demand.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
My tip: Set up some external monitoring to check that your ssl renewal happens reliably.
I wrote https://IsItWorking.info which is one option to do this (hosted on DO and signed by Let’s Encrypt)
It checks your certificate(s) regularly and notifies you if they get too close to expiry. There are other services which do the same.
Thanks Brian. I’ve just started trying to do this via custom dockerfile commands. And failed. Should have checked DO first!
Great tutorial, saved hours of time.