福爸爸 發表於 2020-8-17 15:58:00

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

<h1 id="ansible-部署mongodb集群">ansible 部署mongodb集群</h1>
<p><strong>下载地址链接:https://pan.baidu.com/s/1yx7uk-7Jyhk6WGadPFVS_A<br>
提取码:t3l2</strong><br>
<strong>如果需要安装其他版本的需要修改<br>
1groups_vars 里面的mongodb_version: 4.0.0这里需要注意是 4.0.0不是4.0<br>
2 common/files/mongodb-org-4.0.repo 这个文件名<br>
3 common/files/mongodb-org-4.0.repo里面的baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/</strong><br>
<strong>rpm包安装的方式下载地址<br>
链接:https://pan.baidu.com/s/1nygiYXUwkFPEWwyHBZMlwQ<br>
提取码:l2u5rpm安装比较简单只需要修改主机名 以及对应的common里面的rpm包</strong></p>
<h2 id="一--目录结构">一目录结构</h2>
<p><img src="https://img2020.cnblogs.com/blog/1258067/202008/1258067-20200817155246881-1031669641.png" alt="" loading="lazy"></p>
<p><img src="https://img2020.cnblogs.com/blog/1258067/202008/1258067-20200817155302027-1915134211.png" alt="" loading="lazy"></p>
<h2 id="二-运行方式">二 运行方式</h2>
<p><code>ansible-playbook -i hosts site.yml </code></p>
<h2 id="三-运行顺序以及结构分析">三 运行顺序以及结构分析</h2>
<p># catsite.yml</p>
<pre><code>---
# 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
</code></pre>
<p><strong>common角色详解</strong></p>
<pre><code># 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
</code></pre>
<p><strong>查看common的主文件</strong></p>
<pre><code># 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 &amp;&amp; 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
</code></pre>
<p><strong>mongod 服务部分</strong></p>
<pre><code># tree mongod/
mongod/
├── files
│   └── secret
├── tasks
│   └── main.yml
└── templates
    ├── mongod.conf.j2
    ├── mongod_init.js.j2
    ├── mongod.service.j2
    └── shard_init.j2

查看主文件
# 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
</code></pre>
<p><strong>mongoc 配置文件 也就是mongoconfig 服务器</strong></p>
<pre><code># cat tasks/main.yml
---
# This playbookdeploys the mongodb configurationdbservers

- 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']}}"

- name: Create admin User
shell: /usr/bin/mongo "localhost:{{ mongoc_port }}/admin" /tmp/mongoc_init.js
when: "{{inventory_hostname == groups['mongoc_servers']}}"
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
</code></pre>
<pre><code># 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 enablesharding
template: src=mongos_init.js.j2 dest=/tmp/mongos_init.js
when: "{{inventory_hostname == groups['mongos_servers']}}"

- name: Create sharding
shell: /usr/bin/mongo "localhost:{{ mongos_port }}/admin" /tmp/mongos_init.js
when: "{{inventory_hostname == groups['mongos_servers']}}"
</code></pre><br><br>
来源:https://www.cnblogs.com/ZFBG/p/13518138.html
頁: [1]
查看完整版本: ansible 部署mongodb集群(三台主机)