Docker Swarm Setup on Ansible

From Knowledge Center
Jump to: navigation, search

Install Ansible:

$ pip install ansible

hosts

Host *
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null

hosts

manager ansible_host=51.15.255.190 ansible_ssh_user=root ansible_ssh_private_key_file=/Users/ruan.bekker/.ssh/sw_main.pem
worker1 ansible_host=51.15.241.218 ansible_ssh_user=root ansible_ssh_private_key_file=/Users/ruan.bekker/.ssh/sw_main.pem
worker2 ansible_host=51.15.255.211 ansible_ssh_user=root ansible_ssh_private_key_file=/Users/ruan.bekker/.ssh/sw_main.pem

[masters]
manager

[master]
manager

[workers]
worker1
worker2

swarm_cluster.yml

---
  - hosts: all
    remote_user: root
    become: yes
    become_method: sudo
    tasks:
      - name: "Update all packages to the latest version"
        apt:
          upgrade: dist

      - name: Run the equivalent of "apt-get update" as a separate step
        apt:
          update_cache: yes

      - name: "get fix gpg script"
        get_url:
          url=https://download.docker.com/linux/ubuntu/gpg
          dest=/tmp/gpg
          mode=0440

      - name: "install pgp"
        shell: cat /tmp/gpg | apt-key add -

      - name: "install dep apt-transport-https"
        action: apt  name=apt-transport-https state=present install_recommends=yes allow_unauthenticated=yes

      - name: "install dep software-common"
        action: apt name=software-properties-common state=present install_recommends=yes allow_unauthenticated=yes

      - name: "add docker repository"
        apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable' state=present

      - name: "ensure old versions of docker are purged 2"
        apt: name=docker state=absent purge=yes

      - name: "ensure old versions of docker are purged 3"
        apt: name=docker-engine state=absent purge=yes

      - name: "ensure old versions of docker are purged 4"
        apt: name=docker.io state=absent purge=yes

      - name: "get kernel version"
        shell: uname -r
        register: kernel

      - name: "install docker"
        apt: name=docker-ce state=present

      - name: "add docker group"
        group: name=docker state=present

      - name: "add ubuntu to docker group"
        user: name=ubuntu groups=docker append=yes

      - name: "Restart Docker service"
        service: name=docker state=started

      - name: "get docker info"
        shell: docker info
        register: docker_info
        changed_when: False

  - name: Init Swarm Master
    hosts: masters
    gather_facts: False
    remote_user: root
    tasks:
      - name: Swarm Init
        command: docker swarm init --advertise-addr {{ hostvars['manager']['ansible_host'] }}:2377

      - name: Get Worker Token
        command: docker swarm join-token worker -q
        register: worker_token

      - name: Show Worker Token
        debug: var=worker_token.stdout

      - name: Master Token
        command: docker swarm join-token manager -q
        register: master_token

      - name: Show Master Token
        debug: var=master_token.stdout

  - name: Join Swarm Cluster
    hosts: workers
    remote_user: root
    gather_facts: False
    vars:
      token: "{{ hostvars[groups['masters'][0]]['worker_token']['stdout'] }}"
      master: "{{ hostvars['manager']['ansible_host'] }}"
    tasks:
      - name: Join Swarm Cluster as a Worker
        command: docker swarm join --token {{ token }} {{ master }}:2377
        register: worker

      - name: Show Results
        debug: var=worker.stdout

      - name: Show Errors
        debug: var=worker.stderr

Get Metadata:

$ ansible -m debug -a 'var=hostvars' -i hosts masters

Deploy Nodes on Scaleway:

$ scw --region=par1 create --commercial-type=START1-XS --ip-address=51.15.255.190 --ipv6=false --name="swarm-manager-1" --tmp-ssh-key=false  bc75c00b
$ scw --region=par1 create --commercial-type=START1-XS --ip-address=dynamic --ipv6=false --name="swarm-worker-2" --tmp-ssh-key=false  bc75c00b
$ scw --region=par1 create --commercial-type=START1-XS --ip-address=dynamic --ipv6=false --name="swarm-worker-1" --tmp-ssh-key=false  bc75c00b
$ scw start swarm-manager-1
$ scw start swarm-worker-1
$ scw start swarm-worker-2

Deploy Swarm with Ansible:

$ ansible-playbook -i hosts swarm_cluster.yml

Resources: