Saturday, November 5, 2016

mod_cluster as a Service Discovery with Wildfly and Docker

In this blog post I'll show how to deploy microservices which uses mod_cluster instead Service Discovery. First let's create Dockerfile with mod_cluster as load balancer:
FROM fedora
RUN dnf -y update
RUN dnf -y install httpd
RUN dnf -y install mod_cluster
RUN dnf -y install mod_ssl
RUN dnf clean all

RUN sed -i "s|LoadModule proxy_balancer_module|#LoadModule proxy_balancer_module|" /etc/httpd/conf.modules.d/00-proxy.conf

ADD mod_cluster.conf /etc/httpd/conf.d/mod_cluster.conf
RUN mkdir /etc/httpd/conf/certs
RUN openssl req \
    -new \
    -newkey rsa:4096 \
    -days 365 \
    -nodes \
    -x509 \
    -subj "/C=PL/ST=Slaskie/L=Katowice/O=aszywala/OU=aszywala/CN=Andrzej Szywala" \
    -keyout www.aszywala.key \
    -out www.aszywala.crt
EXPOSE 80
EXPOSE 443
CMD ["/sbin/httpd", "-DFOREGROUND"]

Next I'll create mod_cluster.conf
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule cluster_slotmem_module modules/mod_cluster_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule advertise_module modules/mod_advertise.so

<IfModule manager_module>

  Maxhost 100

  <VirtualHost *:80>
    <Directory />
        Require all granted
    </Directory>
    <Location /mod_cluster_manager>
        SetHandler mod_cluster-manager
        Require all granted
    </Location>
    <Location /server-status>
        SetHandler server-status
    </Location>

    KeepAliveTimeout 60
    ManagerBalancerName mycluster
    ServerAdvertise On
    EnableMCPMReceive On
  </VirtualHost>
</IfModule>

Build Dockerfile
docker build -t lb .
Create docker network
docker network create mynet
Run container with load balancer
docker run --name lb -it --rm --net mynet -p 80:80 -p 443:443 lb
You can open web browser and check if mod_cluster works http://localhost/mod_cluster_manager
Create Dockerfile with micro service application.
FROM jboss/wildfly
MAINTAINER Andrzej Szywala 
ADD customization $JBOSS_HOME
RUN /opt/jboss/wildfly/bin/jboss-cli.sh --file=/opt/jboss/wildfly/wildfly_conf.txt
# FIX for Error: WFLYCTL0056: Could not rename /opt/jboss/wildfly/standalone/configuration/standalone_xml_history/current...
RUN rm -rf /opt/jboss/wildfly/standalone/configuration/standalone_xml_history/current/*
ADD hellojee.war $JBOSS_HOME/standalone/deployments/hellojee.war
wildfly_conf.txt file configures mod_cluster and ajp
embed-server --server-config=standalone.xml
batch
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=lb:add(host="lb",port="80")
/extension=org.jboss.as.modcluster:add
/subsystem=undertow/server=default-server/ajp-listener=ajp:add(socket-binding=ajp, scheme="http")
/:composite(steps=[{"operation" => "add", "address" => [ ("subsystem" => "modcluster") ]},{ "operation" => "add", "address" => [ ("subsystem" => "modcluster"), ("mod-cluster-config" => "configuration") ], "connector" => "ajp", "balancer" => "hellojee", "advertise" => "false", "proxies" => ["lb"] }])
# Execute the batch
run-batch
reload
stop-embedded-server
quit
Build Dockerfile
docker build -t hellojee_cluster .
Run container
docker run --name hellojee_cluster -it --rm --net mynet hellojee_cluster
Check if application registers in loadbalancer http://localhost/mod_cluster_manager

Open browser http://localhost/hellojee/resources/hello. You should see response from application
{"database":"H2 - 1.3.173 (2013-07-28)","host":"0461b41cff7f (172.20.0.3)","hello":["Hello","Witaj","Hallo","Hej","Ahoj","Bonjour","Hola"]}
When you stop the container application will be automatically deregistered from mod_cluster. When you kill the container mod_cluster will notice that and change application status to NOTOK.

Sources for this post can be found on Github https://github.com/andrzejszywala/docker-images/tree/master/hellojee/mod_cluster

No comments:

Post a Comment