Use Ansible to achieve one-click deployment of a 3-node Nacos cluster.
Definition of the hosts file
[root@localhost ansible]# cat inventory/hosts
[all:vars]
# ansible_ssh_pass: Host password
# ansible_user: Host account
ansible_become=true
ansible_ssh_pass=123456
ansible_ssh_user=weihu
ansible_become_pass=123456
ansible_become_user=root
ansible_become_method=sudo
[nacos]
192.168.10.150 nacos_db_init=true
192.168.10.151
192.168.10.152
Definition of global variable file
[root@localhost ansible]# cat inventory/all.yml
all:
vars:
mysql_nacos_host: "192.168.10.128"
mysql_nacos_port: "3306"
mysql_nacos_db: "nacos"
mysql_nacos_user: "nacos"
mysql_nacos_passwd: "Rb992334%"
Definition of systemd service configuration file
[root@ansible ansible]# cat roles/nacos/files/nacos
cat: roles/nacos/files/nacos: No such file or directory
[root@ansible ansible]# cat roles/nacos/files/nacos
nacos-server-2.5.0.zip nacos.service
[root@ansible ansible]# cat roles/nacos/files/nacos.service
[Unit]
Description=nacos
After=network.target
[Service]
Environment="JAVA_HOME=/usr/local/jdk1.8.0_202"
Type=forking
ExecStart=/opt/nacos/bin/startup.sh
ExecReload=/opt/nacos/bin/shutdown.sh
ExecStop=/opt/nacos/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Definition of cluster configuration file
[root@localhost ansible]# cat roles/nacos/templates/cluster.conf
# ip:port
{{ play_hosts[0] }}:8848
{{ play_hosts[1] }}:8848
{{ play_hosts[2] }}:8848
Definition of application.properties file
The main task is to add support for MySQL data source configuration, including the URL, username, and password for the MySQL data source.
[root@localhost ansible]# cat roles/nacos/templates/application.properties
......
spring.sql.init.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://{{ mysql_nacos_host }}:{{ mysql_nacos_port }}/{{ mysql_nacos_db }}?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
db.user.0={{ mysql_nacos_user }}
db.password.0={{ mysql_nacos_passwd }}
......
Database environment initialization configuration
Preparation of the MySQL database environment required for Nacos setup, database file initialization
[root@localhost ansible]# cat roles/nacos/tasks/nacos_db_init.yml
---
- block:
- name: Create Nacos database
shell: |
mysql -h{{ mysql_nacos_host }} -p{{ mysql_nacos_port }} -u{{ mysql_user }} -p{{ mysql_passwd }} -e "CREATE DATABASE IF NOT EXISTS {{ mysql_nacos_db }};"
- name: Create Nacos user
shell: |
mysql -h{{ mysql_nacos_host }} -p{{ mysql_nacos_port }} -u{{ mysql_user }} -p{{ mysql_passwd }} -e "CREATE USER '{{ mysql_nacos_user }}'@'%' IDENTIFIED BY '{{ mysql_nacos_passwd }}';"
- name: Grant all privileges on Nacos database to Nacos user
shell: |
mysql -h{{ mysql_nacos_host }} -p{{ mysql_nacos_port }} -u{{ mysql_user }} -p{{ mysql_passwd }} -e "GRANT ALL PRIVILEGES ON {{ mysql_nacos_db }}.* TO '{{ mysql_nacos_user }}'@'%';"
- name: Flush privileges to apply changes
shell: |
mysql -h{{ mysql_nacos_host }} -p{{ mysql_nacos_port }} -u{{ mysql_user }} -p{{ mysql_passwd }} -e "FLUSH PRIVILEGES;"
- name: Copy SQL schema file to target machine
copy:
src: mysql-schema.sql
dest: /tmp/
mode: '0644'
- name: Import Nacos schema SQL file
shell: |
mysql -h{{ mysql_nacos_host }} -p{{ mysql_nacos_port }} -u{{ mysql_user }} -p{{ mysql_passwd }} {{ mysql_nacos_db }} < /tmp/mysql-schema.sql
when: nacos_db_init is defined
Nacos cluster environment deployment installation
[root@localhost ansible]# cat roles/nacos/tasks/install.yml
---
- name: Distribute Nacos installation package to /opt directory
copy:
src: nacos-server-2.5.0.zip
dest: /opt/
- name: Unzip Nacos installation package to /opt directory
unarchive:
src: /opt/nacos-server-2.5.0.zip
dest: /opt
remote_src: yes
- name: Distribute cluster configuration file cluster.conf
template:
src: cluster.conf
dest: /opt/nacos/conf/
mode: 0644
- name: Distribute configuration file application.properties
template:
src: application.properties
dest: /opt/nacos/conf/
mode: 0644
- name: Distribute systemd configuration file
copy:
src: nacos.service
dest: /usr/lib/systemd/system/
mode: 0600
- name: Configure Nacos to start on boot
systemd:
name: nacos
enabled: yes
daemon_reload: yes
- import_tasks: start.yml
Script execution
# Preparation of the MySQL database environment required for Nacos setup
ansible-playbook -i inventory/ -e operation=nacos_db_init nacos.yml
# Deployment of 3-node Nacos cluster environment
ansible-playbook -i inventory/ -e operation=install nacos.yml
Configuration check
# For the Nacos cluster, perform service start/stop and node status checks
ansible-playbook -i inventory/ -e operation=start|stop|status nacos.yml