Rotate the Swarm CA with a custom certificate and key
In this step, we will learn how to rotate the Swarm CA using a custom certificate and key. This is useful if you want to use your own CA infrastructure or integrate Swarm with an existing PKI (Public Key Infrastructure).
To rotate the Swarm CA with a custom certificate and key, you need to provide the paths to your custom CA certificate and key files using the --cert
and --key
flags with the docker swarm ca --rotate
command.
First, let's create dummy certificate and key files for demonstration purposes. In a real-world scenario, you would use your actual CA certificate and key.
Make sure you are in your home directory ~/project
.
pwd
You should see /home/labex/project
.
Now, let's create the dummy certificate and key files. We will use openssl
to generate a self-signed certificate and a private key.
openssl req -x509 -nodes -newkey rsa:2048 -keyout ~/project/custom_ca.key -out ~/project/custom_ca.crt -days 365 -subj "/CN=custom-swarm-ca"
This command generates a private key (custom_ca.key
) and a self-signed certificate (custom_ca.crt
) valid for 365 days with the Common Name "custom-swarm-ca".
Now that we have our custom certificate and key, we can rotate the Swarm CA using these files.
sudo docker swarm ca --rotate --cert ~/project/custom_ca.crt --key ~/project/custom_ca.key
You will see output indicating that the CA is being rotated with the custom certificate and key. The output will be similar to this:
Swarm CA rotated.
After the rotation is complete, the Swarm will start using your custom CA certificate. The nodes in the Swarm will automatically update their certificates to be signed by your custom CA.
To verify that the CA has been rotated with your custom certificate, you can view the Swarm CA certificate again:
sudo docker swarm ca --cert-dir /var/lib/docker/swarm/certificates
Examine the output. The Issuer
field should now show the Common Name of your custom CA, which is "CN=custom-swarm-ca".