Docker Install with TLS Termination at Layer-7 NGINX Load Balancer

For development and testing environments that have a special requirement to terminate TLS/SSL at a load balancer instead of your Rancher Server container, deploy Rancher and configure a load balancer to work with it conjunction.

A layer-7 load balancer can be beneficial if you want to centralize your TLS termination in your infrastructure. Layer-7 load balancing also offers the capability for your load balancer to make decisions based on HTTP attributes such as cookies, etc. that a layer-4 load balancer is not able to concern itself with.

This install procedure walks you through deployment of Rancher using a single container, and then provides a sample configuration for a layer-7 NGINX load balancer.

Requirements for OS, Docker, Hardware, and Networking

Make sure that your node fulfills the general installation requirements.

Installation Outline

1. Provision Linux Host

Provision a single Linux host according to our Requirements to launch your Rancher Server.

2. Choose an SSL Option and Install Rancher

For security purposes, SSL (Secure Sockets Layer) is required when using Rancher. SSL secures all Rancher network communication, like when you login or interact with a cluster.

Docker Install with TLS Termination at Layer-7 NGINX Load Balancer - 图1Do you want to..

  • Complete an Air Gap Installation?
  • Record all transactions with the Rancher API?

See Advanced Options below before continuing.

Choose from the following options:

Option A-Bring Your Own Certificate: Self-Signed

If you elect to use a self-signed certificate to encrypt communication, you must install the certificate on your load balancer (which you’ll do later) and your Rancher container. Run the Docker command to deploy Rancher, pointing it toward your certificate.

Docker Install with TLS Termination at Layer-7 NGINX Load Balancer - 图2Prerequisites:

Create a self-signed certificate.

  • The certificate files must be in PEM format.

To Install Rancher Using a Self-Signed Cert:

  1. While running the Docker command to deploy Rancher, point Docker toward your CA certificate file.

    1. docker run -d --restart=unless-stopped \
    2. -p 80:80 -p 443:443 \
    3. -v /etc/your_certificate_directory/cacerts.pem:/etc/rancher/ssl/cacerts.pem \
    4. rancher/rancher:latest

    Option B-Bring Your Own Certificate: Signed by Recognized CA

If your cluster is public facing, it’s best to use a certificate signed by a recognized CA.

Docker Install with TLS Termination at Layer-7 NGINX Load Balancer - 图3Prerequisites:

  • The certificate files must be in PEM format.

To Install Rancher Using a Cert Signed by a Recognized CA:

If you use a certificate signed by a recognized CA, installing your certificate in the Rancher container isn’t necessary. We do have to make sure there is no default CA certificate generated and stored, you can do this by passing the --no-cacerts parameter to the container.

  1. Enter the following command.

    1. ```
    2. docker run -d --restart=unless-stopped \
    3. -p 80:80 -p 443:443 \
    4. rancher/rancher:latest --no-cacerts
    5. ```

3. Configure Load Balancer

When using a load balancer in front of your Rancher container, there’s no need for the container to redirect port communication from port 80 or port 443. By passing the header X-Forwarded-Proto: https header, this redirect is disabled.

The load balancer or proxy has to be configured to support the following:

  • WebSocket connections

  • SPDY / HTTP/2 protocols

  • Passing / setting the following headers:

    HeaderValueDescription
    HostHostname used to reach Rancher.To identify the server requested by the client.
    X-Forwarded-ProtohttpsTo identify the protocol that a client used to connect to the load balancer or proxy.

    Note: If this header is present, rancher/rancher does not redirect HTTP to HTTPS.
    X-Forwarded-PortPort used to reach Rancher.To identify the protocol that client used to connect to the load balancer or proxy.
    X-Forwarded-ForIP of the client connection.To identify the originating IP address of a client.

Example NGINX configuration

This NGINX configuration is tested on NGINX 1.14.

Docker Install with TLS Termination at Layer-7 NGINX Load Balancer - 图4note

This NGINX configuration is only an example and may not suit your environment. For complete documentation, see NGINX Load Balancing - HTTP Load Balancing.

  • Replace rancher-server with the IP address or hostname of the node running the Rancher container.
  • Replace both occurrences of FQDN to the DNS name for Rancher.
  • Replace /certs/fullchain.pem and /certs/privkey.pem to the location of the server certificate and the server certificate key respectively.
  1. worker_processes 4;
  2. worker_rlimit_nofile 40000;
  3. events {
  4. worker_connections 8192;
  5. }
  6. http {
  7. upstream rancher {
  8. server rancher-server:80;
  9. }
  10. map $http_upgrade $connection_upgrade {
  11. default Upgrade;
  12. '' close;
  13. }
  14. server {
  15. listen 443 ssl http2;
  16. server_name FQDN;
  17. ssl_certificate /certs/fullchain.pem;
  18. ssl_certificate_key /certs/privkey.pem;
  19. location / {
  20. proxy_set_header Host $host;
  21. proxy_set_header X-Forwarded-Proto $scheme;
  22. proxy_set_header X-Forwarded-Port $server_port;
  23. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24. proxy_pass http://rancher;
  25. proxy_http_version 1.1;
  26. proxy_set_header Upgrade $http_upgrade;
  27. proxy_set_header Connection $connection_upgrade;
  28. # This allows the ability for the execute shell window to remain open for up to 15 minutes. Without this parameter, the default is 1 minute and will automatically close.
  29. proxy_read_timeout 900s;
  30. proxy_buffering off;
  31. }
  32. }
  33. server {
  34. listen 80;
  35. server_name FQDN;
  36. return 301 https://$server_name$request_uri;
  37. }
  38. }

What’s Next?

  • Recommended: Review Single Node Backup and Restore. Although you don’t have any data you need to back up right now, we recommend creating backups after regular Rancher use.
  • Create a Kubernetes cluster: Provisioning Kubernetes Clusters.

FAQ and Troubleshooting

For help troubleshooting certificates, see this section.

Advanced Options

API Auditing

If you want to record all transactions with the Rancher API, enable the API Auditing feature by adding the flags below into your install command.

  1. -e AUDIT_LEVEL=1 \
  2. -e AUDIT_LOG_PATH=/var/log/auditlog/rancher-api-audit.log \
  3. -e AUDIT_LOG_MAXAGE=20 \
  4. -e AUDIT_LOG_MAXBACKUP=20 \
  5. -e AUDIT_LOG_MAXSIZE=100 \

Air Gap

If you are visiting this page to complete an Air Gap Installation, you must pre-pend your private registry URL to the server tag when running the installation command in the option that you choose. Add <REGISTRY.DOMAIN.COM:PORT> with your private registry URL in front of rancher/rancher:latest.

Example:

  1. <REGISTRY.DOMAIN.COM:PORT>/rancher/rancher:latest

Persistent Data

Rancher uses etcd as a datastore. When Rancher is installed with Docker, the embedded etcd is being used. The persistent data is at the following path in the container: /var/lib/rancher.

You can bind mount a host volume to this location to preserve data on the host it is running on:

  1. docker run -d --restart=unless-stopped \
  2. -p 80:80 -p 443:443 \
  3. -v /opt/rancher:/var/lib/rancher \
  4. --privileged \
  5. rancher/rancher:latest

This operation requires privileged access.

This layer 7 NGINX configuration is tested on NGINX version 1.13 (mainline) and 1.14 (stable).

Docker Install with TLS Termination at Layer-7 NGINX Load Balancer - 图5note

This NGINX configuration is only an example and may not suit your environment. For complete documentation, see NGINX Load Balancing - TCP and UDP Load Balancer.

  1. upstream rancher {
  2. server rancher-server:80;
  3. }
  4. map $http_upgrade $connection_upgrade {
  5. default Upgrade;
  6. '' close;
  7. }
  8. server {
  9. listen 443 ssl http2;
  10. server_name rancher.yourdomain.com;
  11. ssl_certificate /etc/your_certificate_directory/fullchain.pem;
  12. ssl_certificate_key /etc/your_certificate_directory/privkey.pem;
  13. location / {
  14. proxy_set_header Host $host;
  15. proxy_set_header X-Forwarded-Proto $scheme;
  16. proxy_set_header X-Forwarded-Port $server_port;
  17. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  18. proxy_pass http://rancher;
  19. proxy_http_version 1.1;
  20. proxy_set_header Upgrade $http_upgrade;
  21. proxy_set_header Connection $connection_upgrade;
  22. # This allows the ability for the execute shell window to remain open for up to 15 minutes. Without this parameter, the default is 1 minute and will automatically close.
  23. proxy_read_timeout 900s;
  24. proxy_buffering off;
  25. }
  26. }
  27. server {
  28. listen 80;
  29. server_name rancher.yourdomain.com;
  30. return 301 https://$server_name$request_uri;
  31. }