From bb31ed2d10f1876a50353abb258c2a5be13e2da2 Mon Sep 17 00:00:00 2001 From: "carey.xu" Date: Thu, 17 Dec 2015 18:16:12 +0800 Subject: deploy mongodb as cluster for HA JIRA: COMPASS-207 Change-Id: I1069bcc2d0dd97dd6a4c7bb61351be993d86b33a Signed-off-by: carey.xu --- .../adapters/ansible/roles/database/tasks/main.yml | 5 ++- .../ansible/roles/database/tasks/mongodb.yml | 39 ---------------------- .../roles/database/tasks/mongodb_config.yml | 35 +++++++++++++++++++ .../roles/database/tasks/mongodb_install.yml | 30 +++++++++++++++++ .../ansible/roles/database/templates/mongodb.conf | 3 +- .../ansible/roles/database/templates/replica.js | 8 +++++ 6 files changed, 79 insertions(+), 41 deletions(-) delete mode 100644 deploy/adapters/ansible/roles/database/tasks/mongodb.yml create mode 100755 deploy/adapters/ansible/roles/database/tasks/mongodb_config.yml create mode 100755 deploy/adapters/ansible/roles/database/tasks/mongodb_install.yml create mode 100644 deploy/adapters/ansible/roles/database/templates/replica.js (limited to 'deploy/adapters/ansible/roles/database') diff --git a/deploy/adapters/ansible/roles/database/tasks/main.yml b/deploy/adapters/ansible/roles/database/tasks/main.yml index 20b6fe4b..bfdcb75b 100644 --- a/deploy/adapters/ansible/roles/database/tasks/main.yml +++ b/deploy/adapters/ansible/roles/database/tasks/main.yml @@ -9,4 +9,7 @@ when: - inventory_hostname == haproxy_hosts.keys()[0] -- include: mongodb.yml +- include: mongodb_install.yml +- include: mongodb_config.yml + when: + - inventory_hostname == haproxy_hosts.keys()[0] diff --git a/deploy/adapters/ansible/roles/database/tasks/mongodb.yml b/deploy/adapters/ansible/roles/database/tasks/mongodb.yml deleted file mode 100644 index ada7f7ef..00000000 --- a/deploy/adapters/ansible/roles/database/tasks/mongodb.yml +++ /dev/null @@ -1,39 +0,0 @@ ---- -- name: install mongodb packages - action: "{{ ansible_pkg_mgr }} name={{ item }} state=present" - with_items: mongodb_packages | union(packages_noarch) - -- name: install pymongod packages - pip: name={{ item }} state=present extra_args='--pre' - with_items: pip_packages - -- name: copy ceilometer configs - template: src=mongodb.conf dest=/opt/os_templates backup=yes - -- name: update mongodb config file - shell: crudini --merge /etc/mongodb.conf < /opt/os_templates/mongodb.conf - -- name: rm prealloc files - file: - dest: "{{ item }}" - state: absent - with_fileglob: - - /var/lib/mongodb/journal/* - -- name: manually restart mongodb server - service: name={{ mongodb_service }} state=restarted enabled=yes - -- name: write mongodb to monitor list - lineinfile: dest=/opt/service create=yes line={{ mongodb_service}} - -- wait_for: port=27017 delay=3 timeout=60 host={{ internal_vip.ip }} - -- name: create mongodb user - run_once: True - mongodb_user: - login_host: "{{ internal_vip.ip }}" - database: ceilometer - name: ceilometer - password: "{{ CEILOMETER_DBPASS }}" - roles: 'readWrite,dbAdmin' - state: present diff --git a/deploy/adapters/ansible/roles/database/tasks/mongodb_config.yml b/deploy/adapters/ansible/roles/database/tasks/mongodb_config.yml new file mode 100755 index 00000000..573b30a9 --- /dev/null +++ b/deploy/adapters/ansible/roles/database/tasks/mongodb_config.yml @@ -0,0 +1,35 @@ +--- +- name: copy replica js + template: + src: replica.js + dest: /opt/replica.js + +- name: init replica servers + shell: mongo compass /opt/replica.js + +- name: wait replica servers are ready + shell: mongo compass --eval 'printjson(rs.status())'|grep -E 'PRIMARY|SECONDARY'|wc -l + register: servers + until: servers.stdout|int == 3 + retries: 20 + delay: 3 + +- debug: msg='{{ servers.stdout |int }}' + +- name: create mongodb user and db + mongodb_user: + login_host: "{{ internal_vip.ip }}" + database: ceilometer + name: ceilometer + password: "{{ CEILOMETER_DBPASS }}" + roles: 'readWrite,dbAdmin' + state: present + +- name: grant user privilege + mongodb_user: + login_host: "{{ internal_vip.ip }}" + database: ceilometer + name: ceilometer + password: "{{ CEILOMETER_DBPASS }}" + roles: 'readWrite,dbAdmin' + state: present diff --git a/deploy/adapters/ansible/roles/database/tasks/mongodb_install.yml b/deploy/adapters/ansible/roles/database/tasks/mongodb_install.yml new file mode 100755 index 00000000..1e7988d2 --- /dev/null +++ b/deploy/adapters/ansible/roles/database/tasks/mongodb_install.yml @@ -0,0 +1,30 @@ +--- +- name: install mongodb packages + action: "{{ ansible_pkg_mgr }} name={{ item }} state=present" + with_items: mongodb_packages | union(packages_noarch) + +- name: install pymongod packages + pip: name={{ item }} state=present extra_args='--pre' + with_items: pip_packages + +- name: copy ceilometer configs + template: src=mongodb.conf dest=/opt/os_templates backup=yes + +- name: update mongodb config file + shell: crudini --merge /etc/mongodb.conf < /opt/os_templates/mongodb.conf + +- name: rm prealloc files + file: + dest: "{{ item }}" + state: absent + with_fileglob: + - /var/lib/mongodb/journal/* + +- name: manually restart mongodb server + service: name={{ mongodb_service }} state=restarted enabled=yes + +- name: write mongodb to monitor list + lineinfile: dest=/opt/service create=yes line={{ mongodb_service}} + +- name: wait for mongod ready + wait_for: host=0.0.0.0 port=27017 delay=10 diff --git a/deploy/adapters/ansible/roles/database/templates/mongodb.conf b/deploy/adapters/ansible/roles/database/templates/mongodb.conf index 68b84d7a..18306154 100644 --- a/deploy/adapters/ansible/roles/database/templates/mongodb.conf +++ b/deploy/adapters/ansible/roles/database/templates/mongodb.conf @@ -1,4 +1,5 @@ # mongodb.conf -bind_ip = {{ internal_vip.ip }} +bind_ip = 0.0.0.0 +replSet = compass port = 27017 smallfiles = true diff --git a/deploy/adapters/ansible/roles/database/templates/replica.js b/deploy/adapters/ansible/roles/database/templates/replica.js new file mode 100644 index 00000000..3282cd6a --- /dev/null +++ b/deploy/adapters/ansible/roles/database/templates/replica.js @@ -0,0 +1,8 @@ +config = { _id:"compass", members:[ +{% for host in haproxy_hosts.values() %} +{% set pair = '%s:27017' % host %} + {_id:{{ loop.index0 }},host:"{{ pair }}"}, +{% endfor %} +] +}; +rs.initiate(config); -- cgit 1.2.3-korg