数据库
首页 > 数据库> > ansible 部署mongodb集群(三台主机)

ansible 部署mongodb集群(三台主机)

作者:互联网

ansible 部署mongodb集群

下载地址链接:https://pan.baidu.com/s/1yx7uk-7Jyhk6WGadPFVS_A
提取码:t3l2

一 目录结构

二 运行方式

ansible-playbook -i hosts site.yml

三 运行顺序以及结构分析

[root@mongodb1 mongodb]# cat site.yml

---
# This Playbook would deploy the whole mongodb cluster with replication and sharding.
# 首先运行common模块
- hosts: all
  roles:
  - role: common
#运行mongod角色
- hosts: mongo_servers
  roles:
  - role: mongod

- hosts: mongoc_servers
  roles:
  - role: mongoc

- hosts: mongos_servers
  roles:
  - role: mongos

# run shading test
- include: shard_test.yml

common角色详解

[root@mongodb1 roles]# tree  ./common/
./common/
├── files
│   ├── Centos-ali.repo
│   ├── epel.repo.j2
│   ├── mongodb-org-3.2.repo
│   ├── pip.conf
│   └── RPM-GPG-KEY-EPEL-6
├── handlers
│   └── main.yml
├── tasks
│   └── main.yml
└── templates
    ├── hosts.j2
    └── iptables.j2

查看common的主文件

[root@mongodb1 roles]# cat ./common/tasks/main.yml 
---
# This Playbook runs all the common plays in the deployment

#- name: Create the repository for ali mirror for China area
#  copy: src=Centos-ali.repo dest=/etc/yum.repos.d/CentOS-Base.repo
#
#- name: Refresh repository cache
#  shell: yum clean all && yum makecache

#- name: Install the epel-release package
#  yum: name=epel-release state=present
#配置yum源
- name: Create the repository for mongodb
  copy: src=mongodb-org-3.2.repo dest=/etc/yum.repos.d/mongodb-org-3.2.repo
# 安装mongodb 里面的变量在 group_vars 里面定义
- name: Install the mongodb package
  yum: name={{ item }} state=present
  with_items:
#   - python-pip
   - policycoreutils-python
   - mongodb-org-mongos-{{mongodb_version}}
   - mongodb-org-shell-{{mongodb_version}}
   - mongodb-org-server-{{mongodb_version}}

#- name: Speed pip by tsinghua mirror
#  copy: src=pip.conf dest=/etc/pip.conf

#- name: Install the latest pymongo package
#  pip: name={{item}} state=latest
#  with_items:
#   - pip

# selinux 配置
- name: Enable port On SELinux
  seport:
    ports: "2700-2703,{{mongos_port}},{{mongoc_port}}"
    proto: tcp
    setype: mongod_port_t
    state: present
  when: ansible_selinux is defined and ansible_selinux != False and ansible_selinux.status == 'enabled'
    #创建mongodb运行用户
- name: Create the mongod user
  user: name=mongod comment="MongoD"
#创建目录 /data
- name: Create the data directory for the namenode metadata
  file: path={{ mongodb_datadir_prefix }} owner=mongod group=mongod state=directory setype=mongod_var_lib_t recurse=true
# 创建日志目录
- name: create log directory for mongodb
  file: path=/var/log/mongo state=directory owner=mongod group=mongod setype=mongod_log_t recurse=true
#创建pid目录
- name: create run directory for mongodb
  file: path=/var/run/mongo state=directory owner=mongod group=mongod setype=mongod_var_run_t seuser=system_u recurse=true

mongod 服务部分

[root@mongodb1 roles]# tree mongod/
mongod/
├── files
│   └── secret
├── tasks
│   └── main.yml
└── templates
    ├── mongod.conf.j2
    ├── mongod_init.js.j2
    ├── mongod.service.j2
    └── shard_init.j2

 查看主文件
[root@mongodb1 roles]# cat ./mongod/tasks/main.yml 
---
# This role deploys the mongod processes and sets up the replication set.

#需要注意groups这个内置变量 代表了hosts里面定义的所有组,通过 .组名的方式可以获取到某个组里面所有主机的列表, delegate_to 委派给主机执行
- name: create data directory for mongodb
  file: path={{ mongodb_datadir_prefix }}/mongo-{{ inventory_hostname }} state=directory owner=mongod group=mongod
  delegate_to: '{{ item }}'
  with_items: "{{groups.replication_servers}}"

#创建mongodb 启动文件
- name: Create the mongodb startup file
  template: src=mongod.service.j2 dest=/etc/systemd/system/mongod-{{ inventory_hostname }}.service mode=0644 seuser=system_u
  delegate_to: '{{ item }}'
  with_items: "{{groups.replication_servers}}"

#创建mongodb 配置文件
- name: Create the mongodb configuration file
  template: src=mongod.conf.j2 dest=/etc/mongod-{{ inventory_hostname }}.conf
  delegate_to: '{{ item }}'
  with_items: "{{groups.replication_servers}}"

#创建keyfile 文件
- name: Copy the keyfile for authentication
  copy:
    src: secret
    dest: "{{ mongodb_datadir_prefix }}/secret"
    owner: mongod
    group: mongod
    mode: 0400
    setype: mongod_var_lib_t

#启动服务
- name: Start the mongodb service
  systemd: name=mongod-{{ inventory_hostname }} state=started daemon_reload=yes enabled=yes
  delegate_to: '{{ item }}'
  with_items: "{{groups.replication_servers}}"

# 这里需要注意,mongodb_port 拿到的是一个值 也就是对应主机,后面的变量
- wait_for:
    port: "{{ mongod_port }}"
    delay: 5

- name: Create the file to initialize the mongod replica set
  template: src=mongod_init.js.j2 dest=/tmp/mongod_init.js

- name: Initialize the replication set
  shell: /usr/bin/mongo "localhost:{{ mongod_port }}/admin" /tmp/mongod_init.js
  ignore_errors: yes

mongoc 配置文件 也就是mongoconfig 服务器

[root@mongodb1 mongoc]# cat tasks/main.yml 
---
# This playbook  deploys the mongodb configurationdb  servers

- name: Create data directory for mongoc configuration server
  file: path={{ mongodb_datadir_prefix }}/configdb state=directory owner=mongod group=mongod

- name: Create the mongodb startup file
  template: src=mongoc.service dest=/etc/systemd/system/mongoc.service mode=0644 seuser=system_u

- name: Create the mongodb configuration file
  template: src=mongoc.conf.j2 dest=/etc/mongoc.conf

- name: Copy the keyfile for authentication
  copy:
    src: roles/mongod/files/secret
    dest: "{{ mongodb_datadir_prefix }}/secret"
    owner: mongod
    group: mongod
    mode: 0400
    setype: mongod_var_lib_t

- name: Start the mongodb service
  systemd: name=mongoc state=started daemon_reload=yes enabled=yes

- wait_for:
    port: "{{ mongoc_port }}"
    delay: 3

- name: Create the file to initialize mongo user
  template: src=mongoc_init.js.j2 dest=/tmp/mongoc_init.js
  when: "{{inventory_hostname == groups['mongoc_servers'][0]}}"

- name: Create admin User
  shell: /usr/bin/mongo "localhost:{{ mongoc_port }}/admin" /tmp/mongoc_init.js
  when: "{{inventory_hostname == groups['mongoc_servers'][0]}}"
  ignore_errors: yes

#- name: add the admin user
#  mongodb_user:
#    login_database: admin
#    login_port: "{{ mongoc_port }}"
#    database: admin
#    name: admin
#    password: "{{ mongo_admin_pass }}"
#    state: present
#  ignore_errors: yes
[root@mongodb1 roles]# cat ./mongos/tasks/main.yml 
---
#This Playbook configures the mongos service of mongodb

- name: Create the mongos startup file
  template: src=mongos.service.j2 dest=/etc/systemd/system/mongos.service mode=0655 seuser=system_u

- name: Create the mongos configuration file
  template: src=mongos.conf.j2 dest=/etc/mongos.conf

- name: Copy the keyfile for authentication
  copy: src=roles/mongod/files/secret dest={{ mongodb_datadir_prefix }}/secret owner=mongod group=mongod mode=0400

- name: Start the mongodb service
  systemd: name=mongos state=started daemon_reload=yes enabled=yes

- wait_for:
    port: "{{ mongos_port }}"
    delay: 3

- name: Copy the file enable  sharding
  template: src=mongos_init.js.j2 dest=/tmp/mongos_init.js
  when: "{{inventory_hostname == groups['mongos_servers'][0]}}"

- name: Create sharding
  shell: /usr/bin/mongo "localhost:{{ mongos_port }}/admin" /tmp/mongos_init.js
  when: "{{inventory_hostname == groups['mongos_servers'][0]}}"

标签:mongod,name,mongodb,ansible,mongos,三台,port,mongoc
来源: https://www.cnblogs.com/ZFBG/p/13518138.html