Если вы видите что-то необычное, просто сообщите мне. Skip to main content

Пример ansible для установки и настройки docker swarm

Подготовка серверов для работы

Приведенный пример ниже, устанавливает на 3 хоста glusterfs - создает папку и сервис который синхронизирует её между серверами, а так же устанавливает docker.

---
- name: Apply GlusterFS configuration
  hosts: 192.168.1.3,192.168.1.51,192.168.1.28
  become: true
  tasks:
    - name: Установка пакетов для поддержки HTTPS в apt
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - gnupg2
          - software-properties-common
        state: present

    - name: Добавление GPG-ключа Docker
      apt_key:
        url: https://download.docker.com/linux/debian/gpg
        state: present

    - name: Добавление репозитория Docker
      apt_repository:
        repo: deb https://download.docker.com/linux/debian buster stable
        state: present

    - name: Установка Docker Engine
      apt:
        name: docker-ce
        state: present

    - name: Добавление текущего пользователя в группу docker
      user:
        name: user
        append: yes
        groups: docker

    - name: Add GlusterFS repository
      apt_repository:
        repo: "ppa:gluster/glusterfs-10"
        state: present

    - name: Update apt cache
      apt:
        update_cache: true

    - name: Install GlusterFS server
      apt:
        name: glusterfs-server
        state: present

    - name: Start glusterd service
      systemd:
        name: glusterd
        state: started
        enabled: true
    - name: Gluster peer probe pi4lab01
      command: gluster peer probe pi4lab01
      ignore_errors: true

    - name: Gluster peer probe pi4lab02
      command: gluster peer probe pi4lab02
      ignore_errors: true

    - name: Create /gluster/volumes directory
      file:
        path: /gluster/volumes
        state: directory
        mode: '0755'
        owner: root
        group: root

    - name: Create gluster volume staging-gfs
      command: >
        gluster volume create staging-gfs replica 3
        elzim:/gluster/volumes
        pi4lab01:/gluster/volumes
        pi4lab02:/gluster/volumes
        force
      register: volume_create_result
      changed_when: "'Volume successfully created' in volume_create_result.stdout"

    - name: Start gluster volume staging-gfs
      command: gluster volume start staging-gfs
      when: volume_create_result is changed

Заведение серверов в dockekr swarm.

---
- name: Создание и настройка Swarm кластера
  hosts: swarm_manager
  gather_facts: false
  
  tasks:
    - name: Инициализация Swarm кластера
      command: docker swarm init
      ignore_errors: true
      register: swarm_init_output
      changed_when: false

    - debug:
        var: swarm_init_output.stdout

    - set_fact:
        join_command: "{{ swarm_init_output.stdout_lines[-2] }}"

- name: Присоединение узлов к кластеру
  hosts: swarm_nodes
  gather_facts: false
  
  tasks:
    - name: Присоединение к кластеру
      command: "{{ hostvars['swarm_manager'].join_command }}"
      changed_when: false
      ignore_errors: true
      register: swarm_join_output

    - debug:
        var: swarm_join_output.stdout

- name: Настройка меток для узлов кластера
  hosts: swarm_manager
  gather_facts: false
  
  tasks:
    - name: Добавление меток для узлов кластера
      command: docker node update --label-add {{ item.label }} {{ item.node }}
      with_items: "{{ node_labels }}"
      changed_when: false

    - name: Удаление меток для узлов кластера
      command: docker node update --label-rm {{ item.label }} {{ item.node }}
      with_items: "{{ node_labels }}"
      changed_when: false

  vars:
    node_labels:
      - { label: "env=prod", node: "node1" }
      - { label: "env=staging", node: "node2" }
      - { label: "env=dev", node: "node3" }

нужно будет заменить значения хостов в разделах hosts: swarm_manager и hosts: swarm_nodes соответственно, чтобы указать IP-адреса или имена хостов в вашей среде. Также можно настроить метки узлов, добавив или удалив соответствующие элементы в списке node_labels.