Simple script to automate adding services to Traefik dynamic config and add CNAME records to Cloudflare DNS
This repository contains a scripts and configuration files to set up Traefik to share services externally using Cloudflare as the DNS provider. Follow the steps below to configure and run the automation scripts.
- Python 3.6 or higher
- Traefik installed and configured
- Cloudflare account
cd /mnt/user/appdata/traefik
cd /opt/appdata/traefik
git clone https://github.com/jdepew88/update_traefik_and_cloudflare.git
cd update_traefik_and_cloudflare
Modify the .env
file in the ./traefik/update_traefik_and_cloudlfare folder
# Cloudflare configuration
CLOUDFLARE_API_TOKEN=your_cloudflare_api_token
CLOUDFLARE_ZONE_ID=your_cloudflare_zone_id
# Your main domain
DOMAIN_NAME=your-domain.com
# Config file path
CONFIG_FILE_PATH=/mnt/user/appdata/traefik-ext/config.yml
#CONFIG_FILE_PATH=/opt/appdata/traefik/config.yml
Replace your_cloudflare_api_token
, your_cloudflare_zone_id
, and your-domain.com
with your actual Cloudflare API token, zone ID, and domain name, respectively. Set the CONFIG_FILE_PATH
to the path of your Traefik dynamic configuration file.
Make the setup_and_run.sh
script executable and run it:
chmod +x setup_and_run.sh
./setup_and_run.sh
When you run the script, it will ask you three questions:
-
Enter the name of your service:
- This is the name you want to use for your new service.
- Example:
plex
-
Enter the IP address of
<service_name>
:- This is the IP address and port where your service is running.
- Example:
10.10.0.100:32800
-
Enter the scheme (http or https) for the service:
- This specifies whether your service uses HTTP or HTTPS.
- Example:
http
Based on your inputs, the script will:
- Create a new router entry in the Traefik configuration for the service.
- Create a new service entry in the Traefik configuration pointing to the specified IP address and scheme.
- Update the configuration file and create a backup.
- Add a CNAME record to Cloudflare to point to your main domain.
- Log in to your Cloudflare account.
- Navigate to the "My Profile" page by clicking on your profile icon in the top right corner and selecting "My Profile".
- Click on the "API Tokens" tab.
- Click on the "Create Token" button.
- Use the "Edit zone DNS" template under the "API Token Templates" section.
- Configure the token with the necessary permissions:
- Zone: DNS: Edit
- Click "Continue to summary", review the token settings, and then click "Create Token".
- Copy the generated API token and store it securely. You'll need it for the
.env
file.
- Log in to your Cloudflare account and go to the "Dashboard".
- Select your domain from the list of zones.
- Navigate to the "DNS" tab.
- Ensure that the domain is using Cloudflare's nameservers.
- Make sure your domain's DNS settings are properly configured to use Cloudflare's DNS services.
The purpose of this repository is to automate the setup and management of Traefik for external services, using Cloudflare as the DNS provider. This helps in managing the DNS records efficiently and ensures that your services are correctly exposed through Traefik.
- The
setup_and_run.sh
script creates a Python virtual environment, installs the required packages, and runs theupdate_traefik_and_cloudflare.py
script. - The
update_traefik_and_cloudflare.py
script updates the Traefik dynamic configuration file with the new service and router entries, creates a backup of the original configuration, and adds a CNAME record to Cloudflare for the new service.
By following these steps, you can efficiently manage your Traefik configuration and DNS settings with Cloudflare.
Feel free to fork this repository, make changes, and submit pull requests. Any contributions are welcome!
This project is licensed under the GNU General Public License - see the LICENSE file for details.