Building Your Monitoring Stack: Automating Docker Installation with SaltStack

Sai Kiran Pikili
4 min readApr 28, 2024

--

In the world of DevOps and system administration, efficient monitoring is key to ensuring the health and performance of your infrastructure. One popular approach is to utilize a monitoring stack comprising Docker, Prometheus, Alertmanager, Node Exporter, and Blackbox Exporter.

In this series, we’ll dive into the process of setting up each component on Linux (ubuntu/debian) machines using SaltStack, a powerful configuration management tool.

Today, we’ll kick things off by automating the installation of Docker with SaltStack.

Docker, a containerization platform, provides a convenient way to package applications and their dependencies into isolated containers, making deployment and scalability a breeze. By leveraging SaltStack, we’ll streamline the installation process, making it repeatable and easily scalable across your infrastructure.

Step 1: Installing Dependencies

Before we begin installing Docker, we need to ensure that our system has the necessary dependencies. These include ca-certificates and curl, which are essential for securely fetching packages and verifying their authenticity. In our SaltStack state file, we’ll include a task to install these dependencies:

install_dependencies:
pkg.installed:
- names:
- ca-certificates
- curl

Step 2: Adding Docker GPG Key

To ensure the integrity of the Docker packages we’ll be installing, we need to add Docker’s GPG key to our system. This key is used to sign the Docker packages, allowing us to verify their authenticity. We’ll use SaltStack to manage this task:

install_docker_gpg_key:
file.managed:
- name: /etc/apt/keyrings/docker.asc
- source: https://download.docker.com/linux/debian/gpg
- makedirs: True
- user: root
- group: root
- mode: '0644'
- skip_verify: True
- require:
- pkg: install_dependencies

Step 3: Adding Docker Repository

Next, we’ll add the Docker repository to our system’s package manager. This repository contains the Docker packages we need to install. By adding it, we’ll be able to easily fetch and install Docker using apt. Here’s how we can do it with SaltStack:

add_docker_repository:
file.managed:
- name: /etc/apt/sources.list.d/docker.list
- contents: |
deb [arch={{ grains['osarch'] }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian {{ grains['lsb_distrib_codename'] }} stable
- user: root
- group: root
- mode: '0644'
- require:
- file: install_docker_gpg_key

Step 4: Updating Package Lists

After adding the Docker repository, we need to update our system’s package lists to reflect the changes. This ensures that our package manager knows about the newly added Docker packages. With SaltStack, we can automate this step:

update_package_lists:
pkgrepo.managed:
- name: deb [arch={{ grains['osarch'] }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian {{ grains['lsb_distrib_codename'] }} stable
- file: /etc/apt/sources.list.d/docker.list
- refresh: True
- require:
- file: add_docker_repository

Step 5: Installing Docker Packages

Finally, it’s time to install Docker itself along with its dependencies. We’ll use SaltStack to ensure that the necessary Docker packages are installed on our system:

install_docker_packages:
pkg.installed:
- names:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- require:
- pkgrepo: update_package_lists

Step 6: Module Execution in All Required Servers

Now you can place all the code into one .sls file like install.sls

install_dependencies:
pkg.installed:
- names:
- ca-certificates
- curl

install_docker_gpg_key:
file.managed:
- name: /etc/apt/keyrings/docker.asc
- source: https://download.docker.com/linux/debian/gpg
- makedirs: True
- user: root
- group: root
- mode: '0644'
- skip_verify: True
- require:
- pkg: install_dependencies

add_docker_repository:
file.managed:
- name: /etc/apt/sources.list.d/docker.list
- contents: |
deb [arch={{ grains['osarch'] }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian {{ grains['lsb_distrib_codename'] }} stable
- user: root
- group: root
- mode: '0644'
- require:
- file: install_docker_gpg_key

update_package_lists:
pkgrepo.managed:
- name: deb [arch={{ grains['osarch'] }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian {{ grains['lsb_distrib_codename'] }} stable
- file: /etc/apt/sources.list.d/docker.list
- refresh: True
- require:
- file: add_docker_repository

install_docker_packages:
pkg.installed:
- names:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- require:
- pkgrepo: update_package_lists

Now that we have our SaltStack state file prepared, we need to execute it on all the required servers, known as Salt minions. This ensures that Docker is installed consistently across our infrastructure. We can achieve this using the state.apply command in SaltStack.

sudo salt '*' state.apply install

Explanation:

  • sudo: This command may require administrative privileges, so we prefix it with sudo to ensure we have the necessary permissions.
  • salt '*': This targets all Salt minions in our infrastructure. The * is a wildcard character that matches all minions.
  • state.apply install: This command tells SaltStack to apply the state defined in the install.sls file to all targeted minions. The install argument refers to the name of the state file without the .sls extension.

By running this command, SaltStack will execute the state file on each minion, ensuring that Docker is installed following the steps outlined in our SaltStack state file. This allows for consistent and automated management of Docker across our infrastructure.

Conclusion:

Congratulations! With SaltStack, we’ve automated the installation of Docker on our Linux machines. This lays the foundation for setting up our monitoring stack, allowing us to efficiently manage and monitor our infrastructure. Stay tuned for the next installment, where we’ll tackle the installation of Prometheus. Happy monitoring!

--

--

Sai Kiran Pikili
Sai Kiran Pikili

Written by Sai Kiran Pikili

Architecting seamless DevOps solutions 🚀 Automating workflows, enhancing reliability, and empowering innovation to fuel the future of technology 🌟

No responses yet