docker-mailserver

by Docker Mailserver

Helm chartv7.1.0App 17.0.0

A fullstack but simple mailserver (smtp, imap, antispam, antivirus, ssl...) using Docker.

About this chart

docker-mailserver Helm Chart

Contents

  • Introduction
  • Prerequisites
  • Getting Started
  • Configuration
    • Volume
    • ConfigMaps
    • Secrets
  • Values YAML
    • Environment Variables
    • Minimal Configuration
    • Certificate
  • Ports
    • Rspamd web interface and metrics
  • Persistence
    • Backing Storage
      • Generic / All
      • NFS
  • Upgrading to Version 7
  • Upgrading to Version 6
  • Upgrading to Version 5
  • Upgrading to Version 3.0.0
  • Development
    • Testing

(Created by gh-md-toc)

Introduction

This chart deploys docker-mailserver into a Kubernetes cluster. docker-mailserver is a production-ready, fullstack mail server that supports SMTP, IMAP, LDAP, Anti-spam, Anti-virus, etc.). Just as importantly, it is designed to be simple to install and configure.

!!WARNING!! - Version 3.0.0 is not backwards compatible with previous versions. Please refer to the upgrade section for more information.

Prerequisites

  • Helm
  • A Kubernetes cluster with persistent storage and access to email ports
  • A custom domain name (for example, example.com)
  • Correctly configured DNS
  • Cert Manager or a similar tool to create and renew TLS certificates

Getting Started

Setting up docker-mailserver requires generating a number of configuration files. To make this easier, docker-mailserver includes a setup command that can generate these files.

To get started, first configure the firewall on your cluster to allow connections to ports 25 (imap), 465 (submissions), 587 (submission) and 993 (imaps) from any IP address.

If you have a LoadBalancer service routing traffic to your ingress controller, configure it to pass through the mail ports.

Then, configure your ingress controller (or Gateway) to pass through the email ports.

Next, manually create a TLS Certificate, setting metadata.name and spec.secretName to the same value. Also set the fully-qualified domain name for your mail server in spec.dnsNames and spec.issuerRef.name to the name of an Issuer or ClusterIssuer, and spec.issuerRef.kind to Issuer or ClusterIssuer.

apiVersion: cert-manager.io/v1
kind: Certificate

metadata:
  name: mail-tls-certificate-rsa

spec:
  secretName: mail-tls-certificate-rsa
  isCA: false
  privateKey:
    algorithm: RSA
    encoding: PKCS1
    size: 2048
  dnsNames: [mail.example.com]
  issuerRef:
    name: letsencrypt-production
    kind: Issuer
kubectl apply -f certificate.yaml --namespace mail

Then add the helm repo:

helm repo add docker-mailserver https://docker-mailserver.github.io/docker-mailserver-helm

Create a Helm values file. See the comments in values.yaml to understand all the options, or create a minimal file like this (where mail-tls-certificate-rsa is the name of the certificate you previously created and example.com is the name of your domain):

## Specify the name of a TLS secret that contains a certificate and private key for your email domain.
## See https://kubernetes.io/docs/concepts/configuration/secret/#tls-secrets
certificate: mail-tls-certificate-rsa

deployment:
  env:
    OVERRIDE_HOSTNAME: example.com       # You must OVERRIDE this!

If you're using the HAProxy ingress controller, configure it to send PROXY Protocol to the docker-mailserver ports, by appending this to your values file:

service:
  annotations:
    haproxy.org/send-proxy-protocol: proxy-v2

Then install docker-mailserver using the values file:

helm upgrade --install docker-mailserver docker-mailserver/docker-mailserver --namespace mail --create-namespace -f values.yaml

Next open a command prompt to the running container.

kubectl exec -it --namespace mail deploy/docker-mailserver -- bash

And now create a new account for Postfix and Dovecot.

kubectl exec -it --namespace mail deploy/docker-mailserver -- bash

setup email add [email protected] password

Alternatively you can do it one step like this:

$kubectl exec -it --namespace mail deploy/docker-mailserver -- setup email add [email protected] password

Account information will be saved in a file postfix-accounts.cf in the container path:

cat /tmp/docker-mailserver/postfix-accounts.cf

This path is mapped to a Kubernetes Volume.

Optionally (but reccomended), create a NetworkPolicy that only allows appropriate pods to connect to the DMS pod.

Configuration

Assuming you still have a command prompt open in the running container, run the setup command to see additional configuration options:

setup

As you run various setup commands, additional files will be generated. At a minimum you will want to run:

setup email add [email protected] password
setup config dkim keysize 2048 domain 'example.com'

These paths are stored to the container path /tmp/docker-mailserver which is mapped to a Kubernetes Volume.

Volume

Configuration files are stored on a Kubernetes volume mounted at /tmp/docker-mailserver in the container. The PVC is named mail-config. You can of course add additional configuration files to the volume as needed.

ConfigMaps

Its is also possible to use ConfigMaps to mount configuration files in the container. This is done by adding to the configFiles key in a custom values.yaml file. For more information please see the documentation in values.yaml

Secrets

Secrets can also be used to mount configuration files in the container. For example, dkim keys could be stored in a secret as opposed to a file in the mail-config volume. Once again, for more information please see the documentation in values.yaml

Values YAML

In addition to the configuration files generated above, the values.yaml file contains a number of knobs for customizing the docker-mailserver installation. Please refer to the extensive comments in values.yaml for additional information.

Environment Variables

Included in the knobs are many environment variables which allow you to customize the behaviour of docker-mailserver. These environment variables are documented in the docker-mailserver's configuration page. Note that docker-mailserver expects any true/false values to be set as numbers (1/0) rather than boolean values (true/false).

By default, the Chart enables rspamd and disables opendkim, dmarc, policyd-spf and clamav. This is the setup recommended by the docker-mailserver project.

Once you have created your own values.yaml files, then redeploy the chart like this:

helm upgrade docker-mailserver docker-mailserver --namespace mail --values <path_to_values.yaml>

You can also override individual configuration setting with helm upgrade --set, specifying each parameter using the --set key=value[,key=value] argument to helm install. For example:

$helm upgrade docker-mailserver docker-mailserver --namespace mail --set pod.dockermailserver.image="your/image:1.0.0"

Minimal Configuration

There are various settings in values.yaml that you must override.

ParameterDescriptionDefault
deployment.env.OVERRIDE_HOSTNAMEThe hostname to be presented on SMTP bannersmail.example.com
certificateName of a Kubernetes secret that stores TLS certificate for your mail domain

Certificate

You will need to setup a TLS certificate for your email domain. The easiest way to do this is use cert-manager.

Once you acquire a certificate, you will need to store it in a TLS secret in the docker-mailserver namespace. Once you have done that, update the values.yaml file like this:

certificate: my-certificate-secret

The chart will then automatically copy the certificate and private key to the /tmp/dms/custom-certs directory in the container and correctly set the SSL_CERT_PATH and SSL_KEY_PATH environment variables.

Ports

If you are running on a bare-metal Kubernetes cluster, you will have to expose ports to the internet to receive and send emails. In addition, you need to make sure that docker-mailserver receives the correct client IP address so that spam filtering works.

This can get a bit complicated, as explained in the docker-mailserver documentation.

One approach to preserving the client IP address is to use the PROXY protocol.

The Helm chart supports the use of the proxy protocol via the proxyProtocol key. By default proxyProtocol.enable is true, and trustedNetworks is set to the private IP network ranges, as are typically used inside a cluster.

proxyProtocol:
  enabled: true
  # List of sources (in CIDR format, space-separated) to permit PROXY protocol from
  trustedNetworks: "10.0.0.0/8 192.168.0.0/16 172.16.0.0/12"

Additionally, you will need to enable proxyProtocol for your loadbalancer.

  • If you are using a cloud service they will most likely have documentation on how to do this for their loadbalancer.
  • If you are using k3s then this is currently impossible with the default components.

For security, you should narrow trustedNetworks to the actual range of IP addresses used by your ingress controller pods, and be certain to exclude any IP ranges gatewayed from IPv6 to v4 or vice versa. Also note that any compromised container in the cluster could use the PROXY protocol to evade some security measures, so set a NetworkPolicy that only allows the appropriate pods to connect to the DMS pod.

Enabling the PROXY protocol will create an additional port for each protocol (by adding 10,000 to the standard port value) that is configured to understand the PROXY protocol. Thus:

ProtocolRegular PortPROXY Protocol Port
smtp2512525
submissions46510465
submission58710587
imap14310143
imaps99310993
pop311010110
pop3s99510995

If you do not enable the PROXY protocol and your mail server is not exposed using a load-balancer service with an external traffic policy in "Local" mode, then all incoming mail traffic will look like it comes from a local Kubernetes cluster IP.

Rspamd web interface and metrics

The Rspamd web interface (port 11334) and the metrics exporter (monitoring.service.port, 9102 by default) are not on the main Service. They are on a separate ClusterIP Service named <fullname>-internal, so a main Service of type LoadBalancer or NodePort never publishes them outside the cluster. The Rspamd ingress (rspamd.ingress) and the ServiceMonitor (metrics.serviceMonitor) use the internal Service, and it carries the prometheus.io/* annotations. service.labels, service.ipFamilyPolicy and service.ipFamilies apply to both Services.

If you reached the Rspamd web interface or scraped the metrics through the main Service before (a port-forward, an annotation-based scrape, or an external address), use <fullname>-internal instead.

Persistence

Docker-mailserver assumes there are four mounted volumes:

  • mail-config
  • mail-data
  • mail-state
  • mail-log

Therefore the chart requests four PersistentVolumeClaims under the persistent_volume_claims key. Each PVC can be set to an existing claim by setting the `persistent_volume_claims.<vol

Deploy docker-mailserver with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has docker-mailserver on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartdocker-mailserver is pinned at v7.1.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply