easy ejabberd clustering guide (mnesia, mysql, & postgres)

原创文章,转载请注明: 转载自慢慢的回味

本文链接地址: easy ejabberd clustering guide (mnesia, mysql, & postgres)

Modified with easy-ejabberd-clustering-guide-mnesia-mysql

first things first, let’s configure our master node

(we’re assuming at this point you’ve installed ejabberd from source without a prefix, have it properly configured, and can successfully connect and message using it)


1: shut down your “master node”

ejabberdctl stop

2: edit ejabberdctl.cfg (/etc/ejabberd/ejabberctl.cfg)

2.1: change node name:

ERLANG_NODE=ejabberd@'master.domain.com'

2.2: change ip we listen on: (get this from ifconfig)

note: look closely, that’s a tuple, don’t use .’s use ,’s

INET_DIST_INTERFACE={192,168,10,100}

3: clear your ejabberd mnesia tables after hostname change

note: hostname changes are a pain, it’s easier to just let ejabberd rebuild the mnesia db’s at startup.

rm /var/lib/ejabberd/*

4: add hostname to /etc/hosts using address and hostname from confg

192.168.10.100    master.domain.com
192.168.10.100    xmppDomain

5: edit ejabberd.yml (/etc/ejabberd/ejabberd.yml)

###   ================
###   SERVED HOSTNAMES
##
## hosts: Domains served by ejabberd.
## You can define one or several, for example:
## hosts:
##   - "example.net"
##   - "example.com"
##   - "example.org"
##
hosts:
  - "xmppDomain"

6: start master node, and make sure it’s running

ejabberdctl start
ejabberdctl status

now we’ll need to configure our slaves

(once again we’re assuming the server is properly installed and functional as a stand alone server)


1: if the server is running, stop it.

ejabberdctl stop

2: update ejabberctl.cfg (/etc/ejabberd/ejabberdctl.cfg

2.1: change node name:

ERLANG_NODE=ejabberd@'slave1.domain.com'

2.2: change ip we listen on: (get this from ifconfig)

note: look closely, that’s a tuple, don’t use .’s use ,’s

INET_DIST_INTERFACE={192,168,10,150}

3: clear your ejabberd mnesia tables after hostname change

note: hostname changes are a pain, it’s easier to just let ejabberd rebuild the mnesia db’s at startup.

rm /var/lib/ejabberd/*

4: copy .erlang.cookie from “master node” (/var/lib/ejabberd/.erlang.cookie)

note: this is the unique cookie from ejabberd@master.domain.com, not the one already residing on slave1

5: add hostnames to /etc/hosts using address and hostname from confg

192.168.10.100    master.domain.com
192.168.10.100    xmppDomain
192.168.10.150    slave1.domain.com

6: edit ejabberd.yml (/etc/ejabberd/ejabberd.yml)

###   ================
###   SERVED HOSTNAMES
##
## hosts: Domains served by ejabberd.
## You can define one or several, for example:
## hosts:
##   - "example.net"
##   - "example.com"
##   - "example.org"
##
hosts:
  - "xmppDomain"


7: create and compile easy_cluster module

7.1: go to server binaries folder

cd /lib/ejabberd/ebin/

7.2: create empty module file

touch easy_cluster.erl

7.3: put the following contents in easy_cluster.erl and save the file

https://github.com/chadillac/ejabberd-easy_cluster/blob/master/easy_cluster.erl

-module(easy_cluster).
 
-export([test_node/1,join/1,join_as_master/1,sync_node/1]).
 
test_node(NodeName) ->
        case net_adm:ping(NodeName) of 'pong' ->
                io:format("server is reachable.~n");
        _ ->
                io:format("server could NOT be reached.~n")
        end.
 
join(NodeName) ->
        mnesia:stop(),
        mnesia:delete_schema([node()]),
        mnesia:start(),
        mnesia:change_config(extra_db_nodes, [NodeName]),
        mnesia:change_table_copy_type(schema, node(), disc_copies),
        application:stop(ejabberd),
        application:start(ejabberd).
 
join_as_master(NodeName) ->
        application:stop(ejabberd),
        mnesia:stop(),
        mnesia:delete_schema([node()]),
        mnesia:start(),
        mnesia:change_config(extra_db_nodes, [NodeName]),
        mnesia:change_table_copy_type(schema, node(), disc_copies),
        easy_cluster:sync_node(NodeName),
        application:start(ejabberd).
 
sync_node(NodeName) ->
        [{Tb, mnesia:add_table_copy(Tb, node(), Type)} 
        || {Tb, [{NodeName, Type}]} <- [{T, mnesia:table_info(T, where_to_commit)}
        || T <- mnesia:system_info(tables)]].

7.4: compile easy_cluster.erl

erlc easy_cluster.erl

7.5: confirm compilation succeeded

note: you should see easy_cluster.beam in the directory if compilation was successful

ls | grep easy_cluster.beam

Move easy_cluster.beam to the lib/ejabberd-14.07/ebin/ directory.
8: start the slave node & confirm it’s running

ejabberdctl start
ejabberdctl status

9: drop into a debugging session on the live server

note: you’ll see a warning message, just hit enter to continue

ejabberdctl debug

note: if you successfully drop a debug console, your prompt will resemble this

Erlang (BEAM) emulator version 5.6.5 [source] [64-bit] [smp:4] [async-threads:0] [hipe] [kernel-poll:true]
 
Eshell V5.6.5  (abort with ^G)
(ejabberd@slave1.domain.com)1>

10: test to see if we can connect to the node

note: at the end of each line we put a . this tells erlang to process our command

easy_cluster:test_node('ejabberd@master.domain.com').

if “server is reachable” we can continue, if it’s not, make sure you did ALL of the steps above.

11: join the cluster

easy_cluster:join('ejabberd@master.domain.com').

12: suspend your debug session by hitting Ctrl+c Ctrl+c本作品采用知识共享署名 4.0 国际许可协议进行许可。

发表回复