在这个简短的演示中,我们将在同一台计算机(localhost)上设置并运行三个WildFly实例。它们将一起形成一个集群。这是一个相当经典的设置,其中应用服务器需要同步其应用程序会话的内容,以确保在其中一个实例失败时进行故障转移。这种配置保证了如果一个实例在处理请求时失败,另一个实例可以继续工作而不会丢失任何数据。请注意,我们将使用多播来发现集群的成员,并确保集群的形成是完全自动和动态的。
为WildFly安装Ansible及其集合
在使用包管理器的Linux系统上,安装Ansible非常简单:
sudo dnf install ansible-core
有关在其他操作系统上安装的信息,请参考在线文档。请注意,本演示假设您在Linux系统上同时运行Ansible控制器和目标(在我们的示例中是同一台机器)。但是,只要稍作调整,它就可以在任何其他操作系统上运行。
在继续之前,请仔细检查您运行的Ansible版本是否足够新(2.14或更高版本即可,但2.9是最低版本):
ansible [core 2.15.3]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.11/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.11.2 (main, Jun 6 2023, 07:39:01) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] (/usr/bin/python3.11)
jinja version = 3.1.2
libyaml = True
准备Ansible环境的下一步也是最后一步是在控制器(将运行Ansible的机器)上安装WildFly的Ansible集合:
# ansible-galaxy collection install middleware_automation.wildfly
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/middleware_automation-wildfly-1.4.3.tar.gz to /root/.ansible/tmp/ansible-local-355dkk9kf5/tmpc2qtag11/middleware_automation-wildfly-1.4.3-9propr_x
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/ansible-posix-1.5.4.tar.gz to /root/.ansible/tmp/ansible-local-355dkk9kf5/tmpc2qtag11/ansible-posix-1.5.4-pq0cq2mn
Installing 'middleware_automation.wildfly:1.4.3' to '/root/.ansible/collections/ansible_collections/middleware_automation/wildfly'
middleware_automation.wildfly:1.4.3 was installed successfully
Installing 'ansible.posix:1.5.4' to '/root/.ansible/collections/ansible_collections/ansible/posix'
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/middleware_automation-common-1.1.4.tar.gz to /root/.ansible/tmp/ansible-local-355dkk9kf5/tmpc2qtag11/middleware_automation-common-1.1.4-nks7pvy7
ansible.posix:1.5.4 was installed successfully
Installing 'middleware_automation.common:1.1.4' to '/root/.ansible/collections/ansible_collections/middleware_automation/common'
middleware_automation.common:1.1.4 was installed successfully
建立野花丛
为了简单起见,并允许您在单台机器(物理或虚拟)甚至一个容器上重现该演示,我们选择在一个目标上部署我们的三个实例。我们选择了localhost作为目标,这样即使没有远程主机也可以进行演示。
设置WildFly集群基本上有两个步骤:
在目标主机上安装WildFly(这里,就localhost).这意味着从该网站下载存档并在适当的目录中解压缩存档(JBOSS_HOME).这些任务由wildfly_installAnsible集合为WildFly提供的角色。
创建配置文件来运行WildFly的几个实例。因为我们在单个主机上运行多个实例,所以您还需要确保每个实例都有自己的子目录和一组端口,以便实例可以共存和通信。幸运的是,此功能由Ansible集合中的一个角色提供,该角色名为wildfly_systemd.
安装WildFly的可行剧本
这是我们将用来部署集群的行动手册。它的内容相对来说是不言自明的,至少如果您对Ansible语法有所了解的话。
name: "WildFly installation and configuration"
hosts: "{{ hosts_group_name | default('localhost') }}"
become: yes
vars:
wildfly_install_workdir: '/opt/'
wildfly_config_base: standalone-ha.xml
wildfly_version: 30.0.1.Final
wildfly_java_package_name: java-11-openjdk-headless.x86_64
wildfly_home: "/opt/wildfly-{{ wildfly_version }}"
instance_http_ports:
- 8080
- 8180
- 8280
app:
name: 'info-1.2.war'
url: 'https://drive.google.com/uc?export=download&id=13K7RCqccgH4zAU1RfOjYMehNaHB0A3Iq'
collections:
- middleware_automation.wildfly
roles:
- role: wildfly_install
tasks:
- name: "Set up for WildFly instance {{ item }}."
ansible.builtin.include_role:
name: wildfly_systemd
vars:
wildfly_config_base: 'standalone-ha.xml'
wildfly_instance_id: "{{ item }}"
instance_name: "wildfly-{{ wildfly_instance_id }}"
wildfly_config_name: "{{ instance_name }}.xml"
wildfly_basedir_prefix: "/opt/{{ instance_name }}"
service_systemd_env_file: "/etc/wildfly-{{ item }}.conf"
service_systemd_conf_file: "/usr/lib/systemd/system/wildfly-{{ item }}.service"
loop: "{{ range(0,3) | list }}"
- name: "Wait for each instance HTTP ports to become available."
ansible.builtin.wait_for:
port: "{{ item }}"
loop: "{{ instance_http_ports }}"
- name: "Checks that WildFly server is running and accessible."
ansible.builtin.get_url:
url: "http://localhost:{{ port }}/"
dest: "/opt/{{ port }}"
loop: "{{ instance_http_ports }}"
loop_control:
loop_var: port
简而言之,本行动手册首先使用WildFly的Ansible集合通过使用wildfly_install角色。这将下载所有工件,创建所需的系统组和用户,安装依赖项(unzip)等等。在其执行结束时,在目标主机上运行WildFly所需的所有花絮都已安装,但服务器尚未运行。这就是下一步发生的事情。
在行动手册的“任务”部分中,我们将调用该系列提供的另一个角色:wildfly_systemd。该角色将负责将WildFly作为常规系统服务集成到服务管理器中。这里,我们使用一个循环来确保我们创建的不是一个,而是三个不同的服务。每一个都将具有相同的配置(standalone-ha.xml)但运行在不同的端口上,使用不同的目录集来存储数据。
运行剧本!
现在,让我们运行我们的Ansible剧本并观察其输出:
$ ansible-playbook -i inventory playbook.yml
PLAY [WildFly installation and configuration] **********************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Validating arguments against arg spec 'main'] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure prerequirements are fullfilled.] ***
included: /root/.ansible/collections/ansible_collections/middleware_automation/wildfly/roles/wildfly_install/tasks/prereqs.yml for localhost
TASK [middleware_automation.wildfly.wildfly_install : Validate credentials] ****
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Validate existing zipfiles wildfly-30.0.1.Final.zip for offline installs] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Validate patch version for offline installs] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Validate existing additional zipfiles {{ eap_archive_filename }} for offline installs] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Check that required packages list has been provided.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Prepare packages list] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Add JDK package java-11-openjdk-headless.x86_64 to packages list] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Install required packages (5)] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure required local user exists.] ***
included: /root/.ansible/collections/ansible_collections/middleware_automation/wildfly/roles/wildfly_install/tasks/user.yml for localhost
TASK [middleware_automation.wildfly.wildfly_install : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Set wildfly group] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure group wildfly exists.] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure user wildfly exists.] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure workdir /opt/ exists.] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure archive_dir /opt/ exists.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure server is installed] ***
included: /root/.ansible/collections/ansible_collections/middleware_automation/wildfly/roles/wildfly_install/tasks/install.yml for localhost
TASK [middleware_automation.wildfly.wildfly_install : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Check local download archive path] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Set download paths] ******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Check target archive: /opt//wildfly-30.0.1.Final.zip] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Retrieve archive from website: https://github.com/wildfly/wildfly/releases/download] ***
included: /root/.ansible/collections/ansible_collections/middleware_automation/wildfly/roles/wildfly_install/tasks/install/web.yml for localhost
TASK [middleware_automation.wildfly.wildfly_install : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Download zipfile from https://github.com/wildfly/wildfly/releases/download/30.0.1.Final/wildfly-30.0.1.Final.zip into /work/wildfly-30.0.1.Final.zip] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Retrieve archive from RHN] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Install server using RPM] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Check downloaded archive] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Copy archive to target nodes] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Check target archive: /opt//wildfly-30.0.1.Final.zip] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Verify target archive state: /opt//wildfly-30.0.1.Final.zip] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Read target directory information: /opt/wildfly-30.0.1.Final] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Extract files from /opt//wildfly-30.0.1.Final.zip into /opt/.] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Note: decompression was not executed] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Read information on server home directory: /opt/wildfly-30.0.1.Final] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Check state of server home directory: /opt/wildfly-30.0.1.Final] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Set instance name] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Deploy custom configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Deploy configuration] ****
changed: [localhost]
TASK [Apply latest cumulative patch] *******************************************
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure required parameters for elytron adapter are provided.] ***
skipping: [localhost]
TASK [Install elytron adapter] *************************************************
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Install server using Prospero] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Check wildfly install directory state] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Validate conditions] *****
ok: [localhost]
TASK [Ensure firewalld configuration allows server port (if enabled).] *********
skipping: [localhost]
TASK [Set up for WildFly instance {{ item }}.] *********************************
TASK [middleware_automation.wildfly.wildfly_systemd : Validating arguments against arg spec 'main'] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check current EAP patch installed] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments for yaml configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check if YAML configuration extension is supported in WildFly] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check if YAML configuration extension is supported in EAP] ***
skipping: [localhost]
TASK [Ensure required local user and group exists.] ****************************
TASK [middleware_automation.wildfly.wildfly_install : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Set wildfly group] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure group wildfly exists.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure user wildfly exists.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set destination directory for configuration] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance destination directory for configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set base directory for instance] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance name] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance name] *******
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set bind address] ********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Create basedir /opt/wildfly-00 for instance: wildfly-0] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Create deployment directories for instance: wildfly-0] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy custom configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy configuration] ****
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Include YAML configuration extension] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check YAML configuration is disabled] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set systemd envfile destination] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Determine JAVA_HOME for selected JVM] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Determine JAVA_HOME for selected JVM] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set systemd unit file destination] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy service instance configuration: /etc/wildfly-0.conf] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy Systemd configuration for service: /usr/lib/systemd/system/wildfly-0.service] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Perform daemon-reload to ensure the changes are picked up] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Ensure service is started] ***
included: /root/.ansible/collections/ansible_collections/middleware_automation/wildfly/roles/wildfly_systemd/tasks/service.yml for localhost
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance wildfly-0 state to started] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Validating arguments against arg spec 'main'] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check current EAP patch installed] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments for yaml configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check if YAML configuration extension is supported in WildFly] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check if YAML configuration extension is supported in EAP] ***
skipping: [localhost]
TASK [Ensure required local user and group exists.] ****************************
TASK [middleware_automation.wildfly.wildfly_install : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Set wildfly group] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure group wildfly exists.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure user wildfly exists.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set destination directory for configuration] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance destination directory for configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set base directory for instance] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance name] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance name] *******
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set bind address] ********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Create basedir /opt/wildfly-11 for instance: wildfly-1] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Create deployment directories for instance: wildfly-1] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy custom configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy configuration] ****
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Include YAML configuration extension] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check YAML configuration is disabled] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set systemd envfile destination] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Determine JAVA_HOME for selected JVM] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Determine JAVA_HOME for selected JVM] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set systemd unit file destination] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy service instance configuration: /etc/wildfly-1.conf] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy Systemd configuration for service: /usr/lib/systemd/system/wildfly-1.service] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Perform daemon-reload to ensure the changes are picked up] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Ensure service is started] ***
included: /root/.ansible/collections/ansible_collections/middleware_automation/wildfly/roles/wildfly_systemd/tasks/service.yml for localhost
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance wildfly-1 state to started] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Validating arguments against arg spec 'main'] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check current EAP patch installed] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments for yaml configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check if YAML configuration extension is supported in WildFly] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check if YAML configuration extension is supported in EAP] ***
skipping: [localhost]
TASK [Ensure required local user and group exists.] ****************************
TASK [middleware_automation.wildfly.wildfly_install : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Set wildfly group] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure group wildfly exists.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_install : Ensure user wildfly exists.] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set destination directory for configuration] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance destination directory for configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set base directory for instance] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance name] *******
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance name] *******
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set bind address] ********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Create basedir /opt/wildfly-22 for instance: wildfly-2] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Create deployment directories for instance: wildfly-2] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy custom configuration] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy configuration] ****
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Include YAML configuration extension] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Check YAML configuration is disabled] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set systemd envfile destination] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Determine JAVA_HOME for selected JVM] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Determine JAVA_HOME for selected JVM] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set systemd unit file destination] ***
skipping: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy service instance configuration: /etc/wildfly-2.conf] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Deploy Systemd configuration for service: /usr/lib/systemd/system/wildfly-2.service] ***
changed: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Perform daemon-reload to ensure the changes are picked up] ***
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Ensure service is started] ***
included: /root/.ansible/collections/ansible_collections/middleware_automation/wildfly/roles/wildfly_systemd/tasks/service.yml for localhost
TASK [middleware_automation.wildfly.wildfly_systemd : Check arguments] *********
ok: [localhost]
TASK [middleware_automation.wildfly.wildfly_systemd : Set instance wildfly-2 state to started] ***
changed: [localhost]
TASK [Wait for each instance HTTP ports to become available.] ******************
ok: [localhost] => (item=8080)
ok: [localhost] => (item=8180)
ok: [localhost] => (item=8280)
TASK [Checks that WildFly server is running and accessible.] *******************
changed: [localhost] => (item=8080)
changed: [localhost] => (item=8180)
changed: [localhost] => (item=8280)
PLAY RECAP *********************************************************************
localhost : ok=105 changed=26 unreachable=0 failed=0 skipped=46 rescued=0 ignored=0
请注意,行动手册并没有那么长,但它为我们做了很多事情。它执行近100个不同的任务,从自动安装依赖项开始,包括WildFly所需的JVM,以及下载其二进制文件。这wildfly_systemdrole做得更多,毫不费力地设置了三个不同的服务,每个服务都有自己的一组端口和目录布局来存储特定于实例的数据。
更好的是,WildFly安装没有重复。所有的二进制文件都在/opt/wildfly-27.0.1目录中,但是每个实例的所有数据文件都存储在单独的文件夹中。这意味着我们只需要更新一次二进制文件,然后重启实例以部署补丁或升级到WildFly的新版本。
最重要的是,我们将实例配置为使用独立-ha.xml配置作为基线,因此它们已经为集群设置好了。
检查一切是否按预期运行
确认playbook确实安装了WildFly并启动了三个appserver实例的最简单方法是使用systemctl检查关联服务状态的命令:
# systemctl status wildfly-0
● wildfly-0.service - JBoss EAP (standalone mode)
Loaded: loaded (/usr/lib/systemd/system/wildfly-0.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2024-01-18 07:01:44 UTC; 5min ago
Main PID: 884 (standalone.sh)
Tasks: 89 (limit: 1638)
Memory: 456.3M
CGroup: /system.slice/wildfly-0.service
├─ 884 /bin/sh /opt/wildfly-30.0.1.Final/bin/standalone.sh -c wildfly-0.xml -b 0.0.0.0 -bmanagement 127.0.0.1 -Djboss.bind.address.private=127.0.0.1 -Djboss.default.multicast.address=230.0.0.4 -Djboss.server.config.dir=/opt/wildfly-30.0.1.Final/standalone/configuration/ -Djboss.server.base.dir=/opt/wildfly-00 -Djboss.tx.node.id=wildfly-0 -Djboss.socket.binding.port-offset=0 -Djboss.node.name=wildfly-0 -Dwildfly.statistics-enabled=false
└─1044 /etc/alternatives/jre_11/bin/java -D[Standalone] -Djdk.serialFilter=maxbytes=10485760;maxdepth=128;maxarray=100000;maxrefs=300000 -Xmx1024M -Xms512M --add-exports=java.desktop/sun.awt=ALL-UNNAMED --add-exports=java.naming/com.sun.jndi.ldap=ALL-UNNAMED --add-exports=java.naming/com.sun.jndi.url.ldap=ALL-UNNAMED --add-exports=java.naming/com.sun.jndi.url.ldaps=ALL-UNNAMED --add-exports=jdk.naming.dns/com.sun.jndi.dns=ALL-UNNAMED --add-opens=java.base/com.sun.net.ssl.internal.ssl=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.management/javax.management=ALL-UNNAMED --add-opens=java.naming/javax.naming=ALL-UNNAMED -Dorg.jboss.boot.log.file=/opt/wildfly-00/log/server.log -Dlogging.configuration=file:/opt/wildfly-30.0.1.Final/standalone/configuration/logging.properties -jar /opt/wildfly-30.0.1.Final/jboss-modules.jar -mp /opt/wildfly-30.0.1.Final/modules org.jboss.as.standalone -Djboss.home.dir=/opt/wildfly-30.0.1.Final -Djboss.server.base.dir=/opt/wildfly-00 -c wildfly-0.xml -b 0.0.0.0 -bmanagement 127.0.0.1 -Djboss.bind.address.private=127.0.0.1 -Djboss.default.multicast.address=230.0.0.4 -Djboss.server.config.dir=/opt/wildfly-30.0.1.Final/standalone/configuration/ -Djboss.server.base.dir=/opt/wildfly-00 -Djboss.tx.node.id=wildfly-0 -Djboss.socket.binding.port-offset=0 -Djboss.node.name=wildfly-0 -Dwildfly.statistics-enabled=false
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,090 INFO [org.jboss.modcluster] (ServerService Thread Pool -- 84) MODCLUSTER000032: Listening to proxy advertisements on /224.0.1.105:23364
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,148 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTPS listener https listening on [0:0:0:0:0:0:0:0]:8443
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,149 INFO [org.jboss.as.ejb3] (MSC service thread 1-3) WFLYEJB0493: Jakarta Enterprise Beans subsystem suspension complete
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,183 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,246 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-2) WFLYDS0013: Started FileSystemDeploymentService for directory /opt/wildfly-00/deployments
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,285 INFO [org.jboss.ws.common.management] (MSC service thread 1-5) JBWS022052: Starting JBossWS 7.0.0.Final (Apache CXF 4.0.0)
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,383 INFO [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: Resuming server
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,388 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,388 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
Jan 18 07:01:47 7c4a5dd056d1 standalone.sh[1044]: 07:01:47,390 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 30.0.1.Final (WildFly Core 22.0.2.Final) started in 2699ms - Started 311 of 708 services (497 services are lazy, passive or on-demand) - Server configuration file in use: wildfly-0.xml
将应用程序部署到WildFly集群
现在,我们的三朵野花正在盛开,但集群尚未形成。事实上,没有应用程序,集群就没有存在的理由。让我们修改我们的Ansible剧本,为所有实例部署一个简单的应用程序;这将允许我们检查集群是否按预期工作。为此,我们将利用WildFly collection提供的另一个角色:wildfly_utils.
在我们的例子中,我们将使用jboss_cli.yml任务文件,封装JBoss命令行界面(CLI)查询的运行:
…
post_tasks:
- name: "Ensures webapp {{ app.name }} has been retrieved from {{ app.url }}."
ansible.builtin.get_url:
url: "{{ app.url }}"
dest: "{{ wildfly_install_workdir }}/{{ app.name }}"
- name: "Deploy webapp"
ansible.builtin.include_role:
name: wildfly_utils
tasks_from: jboss_cli.yml
vars:
jboss_home: "{{ wildfly_home }}"
query: "'deploy --force {{ wildfly_install_workdir }}/{{ app.name }}'"
jboss_cli_controller_port: "{{ item }}"
loop:
- 9990
- 10090
- 10190
现在,我们将再次执行我们的行动手册,以便在所有实例上部署web应用程序。自动化成功完成后,部署将触发集群的形成。
验证WildFly群集正在运行并且应用程序已部署
您可以通过查看以下三个实例的日志文件来验证集群的形成:
…
2022-12-23 15:02:08,252 INFO [org.infinispan.CLUSTER] (thread-7,ejb,jboss-eap-0) ISPN000094: Received new cluster view for channel ejb: [jboss-eap-0] (3) [jboss-eap-0, jboss-eap-1, jboss-eap-2]
…
使用Ansible集合作为WildFly的安装程序
最后一句话:虽然该集合旨在在行动手册中使用,但您也可以使用提供的行动手册直接安装Wildfly:
$ ansible-playbook -i inventory middleware_automation.wildfly.playbook
结论
开始吧:通过一个简短的行动手册,我们已经完全自动化了WildFly集群的部署!该行动手册现在可以用于一台、两台、三台远程机器,甚至数百台机器!我希望这篇文章能提供丰富的信息,并说服你使用Ansible来建立自己的WildFly服务器!