How to Scale Hazelcast Cluster With Docker Compose

后端存储 DZone (源链)

Intro

From the perspective of containerization and cloud deployment, Hazelcast IMDG is a perfect candidate.

Since it’s Elastically Scalable, you can just add more servers, and they form the cluster automatically and contribute their memory to the ‘shared memory.’

  • Performs at in-memory transaction speeds.
  • Highly Available can deploy in backup pairs or even WAN replicated .
  • Fault Tolerant no single point of failure.
  • Ready for Cloud .
  • I’ve been working with few clients who have embraced containerization of their IMDG deployments. And in this blog post, I explore how to use Docker Compose to scale Hazelcast IMDG cluster. Additionally, I will use Management Center to monitor IMDG cluster state.

    Getting Docker Images

    In this demonstration, I will be using official Hazelcast IMDG Docker images. My demo application will contain three components:

    • Hazelcast IMDG cluster.
    • Hazelcast Management Center.
    • Client Application.

    I will build a Docker image for my client app that will read and write to the Hazelcast IMDG cluster.

    Official Stuff

    You can find officially supported images in Docker Hub. I will be using:

    Client App

    A client app is a simple hazelcast client:

    public class MyClient {
        public static void main(String[] args) throws InterruptedException {
    
            ClientConfig clientConfig = new XmlClientConfigBuilder().build();   (1)
            final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); (2)
    
            System.out.println(clientConfig.toString());
    
            IQueue queue = client.getQueue("queue");
            queue.put("Hello!");    (3)
            System.out.println("Message sent by Hazelcast Client!");
    
            HazelcastClient.shutdownAll();
        }
    }
    1. Configured with hazelcast-client.xml
    2. Connects to the cluster.
    3. Sends a message to the IQueue.

    Building Client App Docker Image

    In this example, to build a Docker image from ‘fat’ jar, I used Spotify’s docker-maven-plugin .

    Below is my Maven snippet for generating a fat jar and building a Docker image:

        
            (1)
            maven-assembly-plugin
            
                
                    
                        MyClient
                    
                
                
                    jar-with-dependencies
                
            
            
                
                    make-assembly 
                    package 
                    
                        single
                    
                
            
        
        
            (2)
            com.spotify
            docker-maven-plugin
            ${docker-maven-plugin.version}
            
                hazelcast_client_app
                src/main/docker
                
                    
                        /
                        ${project.build.directory}
                        ${project.build.finalName}-jar-with-dependencies.jar
                    
                
            
        
    
    1. Builds a fat jar to be deployed as a Docker image.
    2. Builds a Docker image with the Hazelcast client app and dependencies.

    Enter Docker Compose

    Docker Compose allows you to define a multi-container application with all of its dependencies in a single file, then spin your application and dependencies up in a single command. Navigate to the directory, like so: hazelcast-code-samples/hazelcast-integration/docker-compose/src/main/docker . Then, run the following command to start the cluster, client, and management center containers:

    docker-compose -f hazelcast.yml up -d

    The docker-compose command will pull the images from Docker Hub and then link them together based on the information inside the docker-compose.yml ( hazelcast.yml in our case) file. This will create ports, links between containers, and configure applications as required. After the command completes, we can now view the status of our cluster with the docker-compose ps command.

    Scaling

    The fun part comes with scaling. Let’s scale out our Hazelcast IMDG cluster to 2 nodes.

    Below is the docker-compose command I used for scaling my Hazelcast cluster.

    docker-compose scale hazelcast=5

    With the docker-compose ps command, you can see which containers are running.

    ❯ docker-compose -f src/main/docker/hazelcast.yml ps
               Name                         Command               State            Ports
    ---------------------------------------------------------------------------------------------
    docker_hazelcast-client_1    /bin/sh -c echo "The appli ...   Exit 0
    docker_hazelcast_1           ./server.sh                      Up       5701/tcp
    docker_hazelcast_2           ./server.sh                      Up       5701/tcp
    docker_hazelcast_3           ./server.sh                      Up       5701/tcp
    docker_hazelcast_4           ./server.sh                      Up       5701/tcp
    docker_hazelcast_5           ./server.sh                      Up       5701/tcp
    docker_management-center_1   /bin/sh -c ./start.sh            Up       0.0.0.0:8080->8080/tcp

    If you open the Management Center URL you should see that cluster consists of 5 nodes now.

    Figure 2. Management Center web app displays IMDG cluster of 5 nodes.

    If at this point you get a message from the Management Center to enter the license, you can request a trial key.

    Also, I can use the curl command to get the clusters’ status and the list of members.

    Below is hope I got the query cluster status using Management Center:

    ❯ curl http://localhost:8080/mancenter/rest/clusters/hz-compose/members
    ["172.18.0.3:5701","172.18.0.4:5701","172.18.0.5:5701","172.18.0.6:5701","172.18.0.7:5701"]

    You can shut down those application stacks (IMDG cluster, Management Center, Hazelcast client Java application) with a single command:

    docker-compose -f src/main/docker/hazelcast.yml down

    What’s Next?

    In my future posts, I will explore other tools that allow you to deploy, scale, and orchestrate your Hazelcast IMDG cluster with different tools like Swarm, Kubernetes, and Mesos. If you have questions or suggestions, feel free to comment below.

    Sources:

    https://hazelcast.com/resources/distributed-caching-next-node-js-project/

    https://github.com/hazelcast/hazelcast-code-samples/tree/master/hazelcast-integration/docker-compose

    https://docs.docker.com/compose/gettingstarted/

    https://hazelcast.com/services/consulting/

    https://github.com/spotify/docker-maven-plugin

    https://hazelcast.com/hazelcast-enterprise-download/trial/

    DZone责编内容来自:DZone (源链) | 更多关于

    阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
    本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
    酷辣虫 » 后端存储 » How to Scale Hazelcast Cluster With Docker Compose

    喜欢 (0)or分享给?

    专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

    使用声明 | 英豪名录