通过ansible-playbook,部署Docker Swarm集群。
docker安装目录: /var/lib/docker docker命令目录: /usr/bin/docker compose命令目录: /usr/local/bin/docker-compose weavescope: docker可视化管理工具 scope命令目录: /usr/local/bin/scope
role | ip | hostname |
---|---|---|
manager/scope | 192.168.1.51 | manager1 |
manager/scope | 192.168.1.52 | manager2 |
worker/scope | 192.168.1.53 | worker1 |
worker/scope | 192.168.1.54 | worker2 |
worker/scope | 192.168.1.55 | worker3 |
worker/scope | 192.168.1.56 | worker4 |
worker/scope | 192.168.1.57 | worker5 |
# vim /etc/ansible/hosts[manager]192.168.1.51 hostname=manager1[add_manager]192.168.1.52 hostname=manager2[add_worker]192.168.1.53 hostname=worker1 192.168.1.54 hostname=worker2 192.168.1.55 hostname=worker3 192.168.1.56 hostname=worker4 192.168.1.57 hostname=worker5
# mkdir -p swarm/roles/{docker_install,init_install,manager_install,worker_install,scope_install}/{files,handlers,meta,tasks,templates,vars}# cd swarm/
说明:
files:存放需要同步到异地服务器的源码文件及配置文件; handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空; meta:存放说明信息、说明角色依赖等信息,可留空; tasks:Docker Swarm 安装过程中需要进行执行的任务; templates:用于执行 Docker Swarm 安装的模板文件,一般为脚本; vars:本次安装定义的变量
# tree ..├── roles │ ├── docker_install │ │ ├── files │ │ ├── handlers │ │ ├── meta │ │ ├── tasks │ │ │ ├── install.yml │ │ │ ├── main.yml │ │ │ └── prepare.yml │ │ ├── templates │ │ └── vars │ │ └── main.yml │ ├── init_install │ │ ├── files │ │ ├── handlers │ │ ├── meta │ │ ├── tasks │ │ │ ├── install.yml │ │ │ └── main.yml │ │ ├── templates │ │ └── vars │ ├── manager_install │ │ ├── files │ │ ├── handlers │ │ ├── meta │ │ ├── tasks │ │ │ ├── install.yml │ │ │ └── main.yml │ │ ├── templates │ │ └── vars │ ├── scope_install │ │ ├── files │ │ ├── handlers │ │ ├── meta │ │ ├── tasks │ │ │ ├── install.yml │ │ │ └── main.yml │ │ ├── templates │ │ └── vars │ └── worker_install │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ │ ├── install.yml │ │ └── main.yml │ ├── templates │ └── vars └── swarm.yml 36 directories, 13 files
# vim swarm.yml--- - hosts: all remote_user: root gather_facts: True roles: - docker_install - hosts: manager remote_user: root gather_facts: True roles: - init_install - hosts: add_manager remote_user: root gather_facts: True roles: - manager_install - hosts: add_worker remote_user: root gather_facts: True roles: - worker_install - hosts: all remote_user: root gather_facts: True roles: - scope_install
# vim docker.yml#用于批量安装Docker- hosts: all remote_user: root gather_facts: True roles: - docker_install
# vim roles/docker_install/vars/main.yml#定义docker安装中的变量COMPOSE_VER: 1.24.1
# vim roles/docker_install/tasks/prepare.yml
- name: 关闭firewalld service: name=firewalld state=stopped enabled=no - name: 临时关闭 selinux shell: "setenforce 0" failed_when: false- name: 永久关闭 selinux lineinfile: dest: /etc/selinux/config regexp: "^SELINUX=" line: "SELINUX=disabled"- name: 添加EPEL仓库 yum: name=epel-release state=latest- name: 安装常用软件包 yum: name: - vim - lrzsz - net-tools - wget - curl - bash-completion - rsync - gcc - unzip - git state: latest- name: 更新系统 shell: "yum update -y" ignore_errors: yes args: warn: False
# vim roles/docker_install/tasks/install.yml
- name: 创建software目录 file: name=/software state=directory- name: 更改hostname raw: "echo {{ hostname }} > /etc/hostname"- name: 更改生效 shell: "hostname {{ hostname }}"- name: 下载repo文件 shell: "if [ ! -f /etc/yum.repos.d/docker.repo ]; then curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo; fi"- name: 生成缓存 shell: "yum makecache fast" args: warn: False- name: 安装docker-ce yum: name: docker-ce state: present- name: 启动docker并开机启动 service: name: docker state: started enabled: yes - name: 下载docker-compose shell: "if [ ! -f /usr/local/bin/docker-compose ]; then curl -L https://github.com/docker/compose/releases/download/{{ COMPOSE_VER }}/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose; fi" - name: 给予执行权限 file: name=/usr/local/bin/docker-compose mode=0755 - name: 提高docker pull速度 shell: "curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io" args: warn: False- name: 重启docker service: name: docker state: restarted
# vim roles/docker_install/tasks/main.yml#引用prepare、install模块- include: prepare.yml - include: install.yml
# vim init.yml#用于初始化swarm集群- hosts: manager remote_user: root gather_facts: True roles: - init_install
# vim roles/init_install/tasks/install.yml
- name: manager离开集群 shell: "docker swarm leave -f" ignore_errors: yes- name: 初始化swarm集群 shell: "docker swarm init --advertise-addr {{ ansible_ssh_host }}" - name: 获取manager的token shell: "echo `docker swarm join-token manager |grep docker` > /software/manager.sh" - name: 获取worker的token shell: "echo `docker swarm join-token worker |grep docker` > /software/worker.sh"- name: 获取所有ip shell: "echo `ansible all -m setup -a 'filter='ansible_default_ipv4'' |grep 'address' |grep -v 'macaddress' |awk -F '\"' '{print $4}'` > /software/hosts.txt" - name: 拷贝manager.sh shell: "ansible all -m copy -a 'src=/software/manager.sh dest=/software mode=0755'" args: warn: False - name: 拷贝worker.sh shell: "ansible all -m copy -a 'src=/software/worker.sh dest=/software mode=0755'" args: warn: False- name: 拷贝hosts.txt shell: "ansible all -m copy -a 'src=/software/hosts.txt dest=/software'" args: warn: False
# vim roles/init_install/tasks/main.yml#引用install模块- include: install.yml
# vim manager.yml#用于集群增加manager- hosts: add_manager remote_user: root gather_facts: True roles: - manager_install
# vim roles/manager_install/tasks/install.yml
- name: manager离开集群 shell: "docker swarm leave -f" ignore_errors: yes- name: 集群增加manager script: /software/manager.sh
# vim roles/manager_install/tasks/main.yml#引用install模块- include: install.yml
# vim worker.yml#用于集群增加worker- hosts: add_worker remote_user: root gather_facts: True roles: - worker_install
# vim roles/worker_install/tasks/install.yml
- name: worker离开集群 shell: "docker swarm leave -f" ignore_errors: yes - name: 集群增加worker script: /software/worker.sh
# vim roles/worker_install/tasks/main.yml#引用install模块- include: install.yml
# vim scope.yml#用于安装weavescope- hosts: all remote_user: root gather_facts: True roles: - scope_install
# vim roles/scope_install/tasks/install.yml
- name: 下载scope shell: "if [ ! -f /usr/local/bin/scope ]; then curl -L git.io/scope -o /usr/local/bin/scope; fi" - name: 给予执行权限 file: name=/usr/local/bin/scope mode=0755- name: 停止scope容器 shell: "docker stop weavescope && docker rm weavescope" ignore_errors: yes- name: 启动scope容器 shell: "hosts=`cat /software/hosts.txt`; scope launch $hosts" register: scope_url- debug: msg="{{ scope_url.stdout }}"
# vim roles/scope_install/tasks/main.yml#引用install模块- include: install.yml
# ansible-playbook swarm.yml
# docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION o6zkxtp0qmvq6r1dxydmprmi4 * manager1 Ready Active Leader 19.03.4 nv1t4p8axfr1zn8k99tegsdhy manager2 Ready Active Reachable 19.03.4 thpss999qnn3e0gun3pi20oy4 worker1 Ready Active 19.03.4 y26tkhebj6u8b7psjnwghcbex worker2 Ready Active 19.03.4 7ygwplwy06sukkhag3kdu022p worker3 Ready Active 19.03.4 hzyd0fz8gx1pld5agjs9afri2 worker4 Ready Active 19.03.4 j2r873fcjqxtbf6p3xjplybee worker5 Ready Active 19.03.4
打开浏览器,访问任一节点的ip:4040
测试安装没有问题。已存放至个人gitgub:ansible-playbook