<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prince Raj on Medium]]></title>
        <description><![CDATA[Stories by Prince Raj on Medium]]></description>
        <link>https://medium.com/@raaz.crzy?source=rss-5ebe349b0658------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SRRu_IKJ3DffhB4o-0CuPA.jpeg</url>
            <title>Stories by Prince Raj on Medium</title>
            <link>https://medium.com/@raaz.crzy?source=rss-5ebe349b0658------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 21:29:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raaz.crzy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Scaling an Elasticsearch Cluster with Kubernetes]]></title>
            <link>https://medium.com/all-things-data-streams/scaling-an-elasticsearch-cluster-with-kubernetes-120991de1451?source=rss-5ebe349b0658------2</link>
            <guid isPermaLink="false">https://medium.com/p/120991de1451</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Prince Raj]]></dc:creator>
            <pubDate>Mon, 09 Apr 2018 11:31:00 GMT</pubDate>
            <atom:updated>2018-04-09T11:31:00.903Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J3FqKZEQ83dUd6PIKH9TGQ.jpeg" /><figcaption><strong>Image:</strong> Scaling Elasticsearch with K8S</figcaption></figure><p>In this post, we will scale a Kubernetes based deployment of Elasticsearch:<br><strong>1.)</strong> by growing a <strong>3-node</strong> ES cluster to a <strong>5-node</strong> ES cluster i.e. horizontal scaling and,<br><strong>2.)</strong> by changing the underlying instance types of each node, i.e. vertical scaling.</p><p>If you are new to the idea of Kubernetes <em>aka K8S</em> as a deployment tool, I highly recommend checking out my earlier post where I show how to go from zero to a persistent Elasticsearch DB deployment in 10 steps.</p><p><a href="https://medium.appbase.io/deploy-elasticsearch-with-kubernetes-on-aws-in-10-steps-7913b607abda">Deploy Elasticsearch with Kubernetes on AWS in 10 steps</a></p><blockquote>We will be using the above post as a baseline to talk about scaling, I will wait here while you read it ☺</blockquote><p>.</p><p>.</p><p>.</p><p>.</p><h4>Quick Recap</h4><p>Here’s a quick <strong>tl;dr</strong> recap: In the previous post, we start out by creating a 3-node K8S cluster and set up a 3-node ES deployment using the following two commands:</p><pre># I am assuming that you already have a K8S cluster setup. If not, go read this post on setting one up: <a href="https://medium.appbase.io/deploy-elasticsearch-with-kubernetes-on-aws-in-10-steps-7913b607abda">https://medium.appbase.io/deploy-elasticsearch-with-kubernetes-on-aws-in-10-steps-7913b607abda</a></pre><pre># Start with deploying ES via statefulset and persistent volumes<br>kubectl create -f <a href="https://raw.githubusercontent.com/raazcrzy/kubernetes/master/elasticsearch/elasticsearch-statefulset.yaml">https://raw.githubusercontent.com/raazcrzy/kubernetes/master/elasticsearch/elasticsearch-statefulset.yaml</a></pre><pre># Finally, create a loadbalancer service to expose the ES cluster<br>kubectl create -f <a href="https://raw.githubusercontent.com/raazcrzy/kubernetes/master/elasticsearch/elasticsearch-loadbalancer.yaml">https://raw.githubusercontent.com/raazcrzy/kubernetes/master/elasticsearch/elasticsearch-loadbalancer.yaml</a></pre><p>You can check on the status of the deployment with the following command:</p><p>kubectl get all</p><pre>[ec2-user@ip-172-31-35-145 test]$ kubectl get all<br>NAME              DESIRED   CURRENT   AGE<br>statefulsets/es   3         3         2h</pre><pre>NAME      READY     STATUS    RESTARTS   AGE<br>po/es-0   1/1       Running   0          45m<br>po/es-1   1/1       Running   0          45m<br>po/es-2   1/1       Running   0          44m</pre><pre>NAME                TYPE           CLUSTER-IP     EXTERNAL-IP        PORT(S)                         AGE<br>svc/elasticsearch   LoadBalancer   100.67.33.11   ac3e3c16f214e...   9200:31243/TCP,9300:31744/TCP   2d<br>svc/kubernetes      ClusterIP      100.64.0.1     &lt;none&gt;             443/TCP                         2d</pre><p>And finally, you can also check the health of the ES cluster with the following request in a browser:</p><p>http://&lt;elasticsearch-service-external-ip&gt;:9200/_cluster/health?pretty</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOqlI51rhqhg10_1WkyNUQ.jpeg" /><figcaption><strong>Image:</strong> Elasticsearch cluster’s health (look out for <strong>status: green</strong>)</figcaption></figure><h3>Scaling Elasticsearch</h3><p><a href="https://github.com/elastic/elasticsearch">Elasticsearch</a> is a distributed search engine that is designed for high availability. In this post, we will look at two common operations with K8S that you will need to perform in a production deployment.</p><ol><li>Scaling of nodes with almost zero downtime. <a href="https://medium.appbase.io/benchmarking-elasticsearch-1-million-writes-per-sec-bf37e7ca8a4c">Elasticsearch throughput scales horizontally</a>, so this is a very common operation to scale up / down ES throughput based on production requirements.</li><li>Changing the underlying hardware or migrating your ES deployment from one <strong>IaaS</strong> provider to another or a bare-metal system.</li></ol><p>To prevent any data loss while scaling out, I also recommend ensuring the following two things:</p><ol><li>Each Elasticsearch container pod should run on at least and at most one K8S agent node — this is to preserve the HA guarantees of Elasticsearch.</li><li>All Elasticsearch indices should have at least one replica. This ensures that at least one copy of a shard is present in the cluster during rolling upgrades, thus minimizing .</li></ol><p>Finally, if you are doing this on a production cluster, I recommend <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html">taking a snapshot</a> and backing it up externally (like AWS S3) before starting out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*A4rey9fXv2GjhvMV-WiOXw.gif" /><figcaption>Image: Things gone wrong!</figcaption></figure><h3>Spectre Server on Twitter</h3><p>look, instead of being so upset, think about all the data that I *didn&#39;t* lose.</p><h3>Growing a 3-Node ES Cluster to a 5-Node Cluster</h3><p>Before we get started, let’s recap how a K8S cluster works. Before we set out to grow our ES cluster to 5 nodes, we should ensure that our K8S cluster has 5 nodes, otherwise more than one pod will get scheduled on the same node which is not desired if we want high availability.</p><p>Since we started with only 3 nodes for our K8S cluster, we need to first scale the K8S agent nodes to 5 and then scale our ES nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_EXsyoFgfheEY8cPn6DLQw.jpeg" /><figcaption><strong>Image: </strong>Representational diagram of horizontally scaling nodes in K8S cluster</figcaption></figure><p><strong>Kops</strong> has the concept of “<a href="https://github.com/kubernetes/kops/blob/master/docs/instance_groups.md">instance groups</a>”, which are a group of similar machines. To scale the nodes, we need to fetch the instance group name to which our nodes belong and then increase the number of nodes in the group. Lets first get the list of instance groups present in our cluster.</p><p>The following command will list down the instance groups of your cluster.</p><pre>[ec2-user@ip-172-31-35-145 examples]$ kops get ig<br>Using cluster from kubectl context: k8s.appbase</pre><pre>NAME    ROLE MACHINETYPE MIN MAX ZONES<br>masters Master t2.large 1 1 us-east-2c<br>nodes   Node t2.medium 3 3 us-east-2c</pre><p>1. Run the following command to edit instance group configuration for the agent nodes. Replace &lt;nodes&gt; with your instance group name.</p><pre>kops edit ig &lt;nodes&gt;</pre><p>You should see a config file open.</p><pre>apiVersion: kops/v1alpha2<br>kind: InstanceGroup<br>metadata:<br>  creationTimestamp: 2018-02-14T12:26:43Z<br>  labels:<br>    kops.k8s.io/cluster: k8s.appbase<br>  name: nodes<br>spec:<br>  image: kope.io/k8s-1.8-debian-jessie-amd64-hvm-ebs-2018-01-14<br>  machineType: t2.medium<br>  maxSize: 3<br>  minSize: 3<br>  nodeLabels:<br>    kops.k8s.io/instancegroup: nodes<br>  role: Node<br>  subnets:<br>  - us-east-2c</pre><p>In order to scale the nodes we need to edit <strong>minSize</strong> and <strong>maxSize</strong> in the config file of the instance group to which our agent nodes belong. The <strong>maxSize</strong> defines the maximum number of nodes we want up at any given time while <strong>minSize </strong>defines the minimum number of nodes that must be up at any given time.</p><p>2. Here, we will change the maxSize and minSize values to 5.</p><pre>apiVersion: kops/v1alpha2<br>kind: InstanceGroup<br>metadata:<br>  creationTimestamp: 2018-02-14T12:26:43Z<br>  labels:<br>    kops.k8s.io/cluster: k8s.appbase<br>  name: nodes<br>spec:<br>  image: kope.io/k8s-1.8-debian-jessie-amd64-hvm-ebs-2018-01-14<br>  machineType: t2.medium<br>  maxSize: 5<br>  minSize: 5<br>  nodeLabels:<br>    kops.k8s.io/instancegroup: nodes<br>  role: Node<br>  subnets:<br>  - us-east-2c</pre><p>3. Next, we need to update the cluster to apply the configuration change we just made.</p><pre>kops update cluster --yes<br>kops rolling-update cluster</pre><p>It may take few minutes to add the nodes as new VMs will be added to the cluster. You can verify the new node count by getting the nodes of the cluster.</p><p>4. Run: kubectl get nodes</p><pre>NAME                                         STATUS    ROLES     AGE       VERSION<br>ip-172-20-44-33.us-east-2.compute.internal   Ready     master    12h       v1.8.6<br>ip-172-20-52-48.us-east-2.compute.internal   Ready     node      2d        v1.8.6<br>ip-172-20-62-30.us-east-2.compute.internal   Ready     node      2d        v1.8.6<br>ip-172-20-64-53.us-east-2.compute.internal   Ready     node      2d        v1.8.6<br>ip-172-20-67-30.us-east-2.compute.internal   Ready     node      1m        v1.8.6<br>ip-172-20-87-68.us-east-2.compute.internal   Ready     node      1m        v1.8.6</pre><p>As you can see, we now have a K8S cluster of 5 agent nodes.</p><p>Now let’s scale the Elasticsearch nodes from 3 → 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*S_r7-3nXe-FKDTbRVxFrjg.png" /></figure><p>We can scale Elasticsearch horizontally by simply specifying the number of replicas. This method can be used for both — scaling up as well as scaling down.</p><pre>kubectl scale --replicas=5 statefulset es</pre><p>You can adjust the number of replicas to suite your need (make sure you have enough nodes for the replicas).</p><pre>[ec2-user@ip-172-31-35-145 ~]$ kubectl scale --replicas=5 statefulset es<br>statefulset &quot;es&quot; scaled</pre><p>You can verify the number of Elasticsearch pods and the nodes on which they are running by the following command:</p><pre>kubectl get pods -o wide</pre><p>You should get a table of your pods along with the nodes on which they are deployed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2251b5f5efbc1c76e7bb5498093791a9/href">https://medium.com/media/2251b5f5efbc1c76e7bb5498093791a9/href</a></iframe><p>When you hit the URL of Elasticsearch to check the health of your nodes, you should now notice the number of Elasticsearch nodes increase to five.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BA0zYZW7q9FGNh860Znx1g.jpeg" /><figcaption><strong>Image: </strong>Elasticsearch cluster health</figcaption></figure><h3>Vertical scaling of Elasticsearch nodes</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rc1B9pnbkKxxJdSsTugaPg.jpeg" /><figcaption><strong>Image: </strong>Scaling ES cluster vertically</figcaption></figure><p>Scaling Elasticsearch vertically means we are going to let our ES nodes use more/less memory, disk and computing resources. In order to achieve this, we need to vertically scale our K8S node by changing the instance type of the node VM to suit our need and then editing the <strong>JAVA_HEAP_SIZE </strong>and number of<strong> CPU </strong>of the <strong>Elasticsearch</strong> <strong>statefulset</strong> resource that we deployed on K8S. The <strong>statefulset</strong> will update the Elasticsearch to use the updated resource limits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ef42W87NsqecibRQVzIvEg.jpeg" /><figcaption><strong>Image: </strong>Vertical scaling of K8S cluster</figcaption></figure><p>We start by vertically scaling the K8S nodes. The process is similar to horizontally-scaling K8S nodes.</p><ol><li>Get the instance group name of nodes.<br>Run: kops get ig</li><li>Edit the config of instance group for the nodes.<br>Run: kops edit ig &lt;ig name&gt;</li><li>Change the type of machine machineType to whichever machine you want to scale up. Make sure that machine type is available in the AZ you have provisioned the cluster. We have our nodes provisioned with t2.medium VMs. Change it to t2.large.</li><li>Save the file and update the cluster.</li></ol><pre>kops update cluster --yes<br>kops rolling-update cluster</pre><p>Wait for few minutes untill all your ec2 instances are terminated and new machines come up. Then check the validity of cluster.</p><p>5. Run: kops validate cluster</p><p>It should show you the cluster with new machine types:</p><pre>[ec2-user@ip-172-31-35-145 examples]$ kops validate cluster<br>Using cluster from kubectl context: k8s.appbase</pre><pre>Validating cluster k8s.appbase</pre><pre>INSTANCE GROUPS<br>NAME    ROLE   MACHINETYPE MIN MAX SUBNETS<br>masters Master t2.large    1   1   us-east-2c<br>nodes   Node   t2.large    5   5   us-east-2c</pre><pre>NODE STATUS<br>NAME                                       ROLE   READY<br>ip-172-20-44-33.us-east-2.compute.internal master True<br>ip-172-20-52-48.us-east-2.compute.internal node   True<br>ip-172-20-62-30.us-east-2.compute.internal node   True<br>ip-172-20-64-53.us-east-2.compute.internal node   True<br>ip-172-20-67-30.us-east-2.compute.internal node   True<br>ip-172-20-87-68.us-east-2.compute.internal node   True</pre><p>We have just scaled our K8S nodes vertically.</p><p>Next, we can scale our ES nodes vertically. For this, we need to update the Elasticsearch <strong>statefulset</strong>.</p><pre>kubectl edit statefulset es</pre><p>Now edit the value of JAVA_HEAP_SIZE in the list of environment variables from -Xms256m -Xms256m to -Xms1g -Xms1g .</p><p>Elasticsearch settings defaults to use total number of available processors. Since instance type <strong>t2.large</strong> has 2 processors, ES nodes will use all 2 processors.</p><p>After saving the file, the <strong>statefulSet</strong> should upgrade the ES nodes with the updated configurations and our ES cluster will scale up vertically. To verify the vertical scaling, you can check the ES _nodes API endpoint by hitting the Elasticsearch cluster’s URL.</p><pre>http://&lt;external address&gt;:9200/_nodes/os,jvm?pretty</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YVT7qWcUGttaavHG2k9Wbg.jpeg" /><figcaption><strong>Image: </strong>Node 1 OS and JVM details</figcaption></figure><p>Look at the <strong>available_processors </strong>and <strong>allocated_processors </strong>under <strong>os</strong>, it should be 2. And the <strong>heap_init_in_bytes</strong> under <strong>jvm </strong>should be approximately equal to 1 GB.</p><pre>{<br>  &quot;_nodes&quot; : {<br>    &quot;total&quot; : 5,<br>    &quot;successful&quot; : 5,<br>    &quot;failed&quot; : 0<br>  },<br>  &quot;cluster_name&quot; : &quot;myesdb&quot;,<br>  &quot;nodes&quot; : {<br>    &quot;tYujtUCNTuOYlXDDMG4Fbg&quot; : {<br>      &quot;name&quot; : &quot;6fe0014d-561d-4457-ba1a-4d287b812f80&quot;,<br>      &quot;transport_address&quot; : &quot;100.96.2.34:9300&quot;,<br>      &quot;host&quot; : &quot;100.96.2.34&quot;,<br>      &quot;ip&quot; : &quot;100.96.2.34&quot;,<br>      &quot;version&quot; : &quot;5.6.4&quot;,<br>      &quot;build_hash&quot; : &quot;8bbedf5&quot;,<br>      &quot;roles&quot; : [<br>        &quot;master&quot;,<br>        &quot;data&quot;,<br>        &quot;ingest&quot;<br>      ],<br>      &quot;os&quot; : {<br>        &quot;refresh_interval_in_millis&quot; : 1000,<br>        &quot;name&quot; : &quot;Linux&quot;,<br>        &quot;arch&quot; : &quot;amd64&quot;,<br>        &quot;version&quot; : &quot;4.4.111-k8s&quot;,<br>        <strong>&quot;available_processors&quot; : 2,<br>        &quot;allocated_processors&quot; : 2</strong><br>      }<br>    },<br>      &quot;jvm&quot; : {<br>        &quot;pid&quot; : 1,<br>        &quot;version&quot; : &quot;1.8.0_131&quot;,<br>        &quot;vm_name&quot; : &quot;OpenJDK 64-Bit Server VM&quot;,<br>        &quot;vm_version&quot; : &quot;25.131-b11&quot;,<br>        &quot;vm_vendor&quot; : &quot;Oracle Corporation&quot;,<br>        &quot;start_time_in_millis&quot; : 1521222438059,<br>        &quot;mem&quot; : {<br>          <strong>&quot;heap_init_in_bytes&quot; : 1073741824,</strong><br>          &quot;heap_max_in_bytes&quot; : 1056309248,<br>          &quot;non_heap_init_in_bytes&quot; : 2555904,<br>          &quot;non_heap_max_in_bytes&quot; : 0,<br>          &quot;direct_max_in_bytes&quot; : 1056309248<br>        },<br>        &quot;gc_collectors&quot; : [<br>          &quot;ParNew&quot;,<br>          &quot;ConcurrentMarkSweep&quot;<br>        ],<br>        &quot;memory_pools&quot; : [<br>          &quot;Code Cache&quot;,<br>          &quot;Metaspace&quot;,<br>          &quot;Compressed Class Space&quot;,<br>          &quot;Par Eden Space&quot;,<br>          &quot;Par Survivor Space&quot;,<br>          &quot;CMS Old Gen&quot;<br>        ],<br>        &quot;using_compressed_ordinary_object_pointers&quot; : &quot;true&quot;,<br>        &quot;input_arguments&quot; : [<br>          &quot;-XX:+UseConcMarkSweepGC&quot;,<br>          &quot;-XX:CMSInitiatingOccupancyFraction=75&quot;,<br>          &quot;-XX:+UseCMSInitiatingOccupancyOnly&quot;,<br>          &quot;-XX:+DisableExplicitGC&quot;,<br>          &quot;-XX:+AlwaysPreTouch&quot;,<br>          &quot;-Xss1m&quot;,<br>          &quot;-Djava.awt.headless=true&quot;,<br>          &quot;-Dfile.encoding=UTF-8&quot;,<br>          &quot;-Djna.nosys=true&quot;,<br>          &quot;-Djdk.io.permissionsUseCanonicalPath=true&quot;,<br>          &quot;-Dio.netty.noUnsafe=true&quot;,<br>          &quot;-Dio.netty.noKeySetOptimization=true&quot;,<br>          &quot;-Dlog4j.shutdownHookEnabled=false&quot;,<br>          &quot;-Dlog4j2.disable.jmx=true&quot;,<br>          &quot;-Dlog4j.skipJansi=true&quot;,<br>          &quot;-XX:+HeapDumpOnOutOfMemoryError&quot;,<br>          <strong>&quot;-Xms1g&quot;,<br>          &quot;-Xmx1g&quot;,</strong><br>          &quot;-Des.path.home=/elasticsearch&quot;<br>        ]<br>      }<br>    },<br>...</pre><h3>Conclusion</h3><p>We walked through two strategies of scaling an Elasticsearch cluster: 1.) <strong>Horizontal Scaling</strong> and 2.) <strong>Vertical Scaling</strong>. We use Kops’s instance groups functionality to declaratively update the K8S nodes first and then make a similar change in the <strong>statefulset</strong> deployment of Elasticsearch.</p><p>While the blog post has only focused on Kops / AWS, a similar setup is possible in a K8S cluster hosted anywhere.</p><p>If you are still new to idea of deploying a distributed system like Elasticsearch with Kubernetes, I recommend reading on my earlier blog post.</p><p><a href="https://medium.appbase.io/deploy-elasticsearch-with-kubernetes-on-aws-in-10-steps-7913b607abda">Deploy Elasticsearch with Kubernetes on AWS in 10 steps</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F5b98a5%3Fas_embed%3Dtrue&amp;url=https%3A%2F%2Fupscri.be%2F5b98a5%2F&amp;image=https%3A%2F%2Fupscri.be%2Fmedia%2Fform.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/67fe596d14714b558338513a54319439/href">https://medium.com/media/67fe596d14714b558338513a54319439/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=120991de1451" width="1" height="1" alt=""><hr><p><a href="https://medium.com/all-things-data-streams/scaling-an-elasticsearch-cluster-with-kubernetes-120991de1451">Scaling an Elasticsearch Cluster with Kubernetes</a> was originally published in <a href="https://medium.com/all-things-data-streams">All things #search</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy Elasticsearch with Kubernetes on AWS in 10 steps]]></title>
            <link>https://medium.com/all-things-data-streams/deploy-elasticsearch-with-kubernetes-on-aws-in-10-steps-7913b607abda?source=rss-5ebe349b0658------2</link>
            <guid isPermaLink="false">https://medium.com/p/7913b607abda</guid>
            <category><![CDATA[appbase]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Prince Raj]]></dc:creator>
            <pubDate>Mon, 12 Mar 2018 12:41:00 GMT</pubDate>
            <atom:updated>2021-02-26T05:28:45.309Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XLtdwWFTR05VBlDVhjNeZA.jpeg" /></figure><p><a href="https://kubernetes.io/">Kubernetes </a>aka k8s is an open-source system for automating deployment, scaling and management of containerized applications. In this tutorial, I will show how to setup a Kubernetes cluster and deploy an Elasticsearch cluster on it in AWS. A similar setup should also work for <a href="https://cloud.google.com/kubernetes-engine/docs/quickstart">GCE</a> and <a href="https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough">Azure</a>.</p><h3><strong>Configuring Kubernetes on AWS</strong></h3><p>Before we get started, you should have administrative access to the following AWS services: <strong>S3, EC2, Route53, IAM, </strong>and<strong> VPC</strong>.</p><p><strong>1.Installation:</strong> I will be showing the installation setup for a Linux based CLI. If you are on a different machine, follow the below tool links to get installation instructions for your OS.</p><p>First, we will get the <a href="https://docs.aws.amazon.com/cli/latest/userguide/installing.html">AWS CLI</a> to access AWS via CLI. If you already have Python and pip in your system, you can run this command:</p><pre><strong>pip install awscli --upgrade --user</strong></pre><p>Next, we will be using a tool called <a href="https://github.com/kubernetes/kops">Kops</a>, a command line tool that offers an opinionated approach for setting up a production grade K8S cluster. <br>We will install the <strong>Kops</strong> binaries directly from github.</p><pre>wget -O kops https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d &#39;&quot;&#39; -f 4)/kops-linux-amd64<br>chmod +x ./kops<br>sudo mv ./kops /usr/local/bin/</pre><p>And finally, we will use <a href="https://kubernetes.io/docs/reference/kubectl/overview/">kubectl</a> — a CLI for managing a K8S cluster (if you have used docker, this is similar to the <strong>docker</strong> CLI). We can install the latest stable release with the following command:</p><pre>wget -O kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl<br>chmod +x ./kubectl<br>sudo mv ./kubectl /usr/local/bin/kubectl</pre><p><strong>Note:</strong> You can also run the Kubernetes cluster and the setup we are going through in this tutorial on your own machine with <a href="https://kubernetes.io/docs/getting-started-guides/minikube/">minikube</a>.</p><p><strong>2. IAM User Creation:</strong> In order to build clusters within AWS, we’ll create a dedicated IAM user for kops. This user requires API credentials in order to use kops. You can create the user and assign credentials using the <a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console">AWS console</a> UI. kops user will require the following IAM permissions.<br>* AmazonEC2FullAccess<br>* AmazonRoute53FullAccess<br>* AmazonS3FullAccess<br>* IAMFullAccess<br>* AmazonVPCFullAccess</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MM1JG5ehym9KutpqKMTQhA.jpeg" /><figcaption>Image: Showing user creation from AWS dashboard</figcaption></figure><p>Alternatively, you can do the same from the CLI as well. Apply the below commands.</p><pre>aws iam create-group --group-name kops<br><br>aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonEC2FullAccess --group-name kops<br>aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonRoute53FullAccess --group-name kops<br>aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess --group-name kops<br>aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/IAMFullAccess --group-name kops<br>aws iam attach-group-policy --policy-arn arn:aws:iam::aws:policy/AmazonVPCFullAccess --group-name kops<br><br>aws iam create-user --user-name kops<br><br>aws iam add-user-to-group --user-name kops --group-name kops<br><br>aws iam create-access-key --user-name kops</pre><p>Note the SecretAccessKey and AccessKeyID of kops.</p><p>Now, let’s configure the AWS CLI to use these credentials using<br>aws configure.</p><p>Confirm that the user you just created is in the list with the following <br>aws iam list-users.</p><p>We will also need to export the AWS credentials as the following environment variables for the kops CLI to be able to use these.</p><pre>export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id)<br>export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key)</pre><blockquote>If you are using Kops 1.6.2 or later, then DNS configuration is optional. Instead, a gossip-based cluster can be easily created. The only requirement to trigger this is to have the cluster name end with .k8s.local.</blockquote><blockquote><strong>Configuring DNS</strong><br>If you have already hosted your domain via AWS and plan to use the domain, you do not need do anything. Alternatively, if you plan to use a sub-domain of your hosted domain, you will have to create a second public hosted zone for this sub-domain. For this tutorial we are going with a private hosted zone. You need to setup a private hosted zone by any name. Use this name for creating Kubernetes clusters. For details on configuring DNS <a href="https://github.com/kubernetes/kops/blob/master/docs/aws.md#configure-dns">check this out</a>.</blockquote><p><strong><em>3.Create an S3 bucket: </em></strong>In order to store the state and representation of our K8S cluster, we need to create a dedicated S3 bucket for use. This bucket will become the source of truth for our cluster configuration.</p><pre>aws s3api create-bucket \<br>    --bucket &lt;your-unique-bucket-name&gt; \<br>    --region us-east-1</pre><p><strong>Note:</strong> If you are provisioning your bucket in a region other us-east-1, in addition to setting the--region switch to your desired region, you will need to add a LocationConstraint to the same region. As an example, the following will be the command for creating the bucket in the us-west-1 region.</p><pre>aws s3api create-bucket \<br>    --bucket &lt;your-unique-bucket-name&gt; \<br>    --region us-west-1 \<br>    --create-bucket-configuration LocationConstraint=us-west-1</pre><p>You might also want to version your S3 bucket with the following command in case you ever need to revert or recover a previous state store.</p><pre>aws s3api put-bucket-versioning \<br>    --bucket &lt;your-unique-bucket-name&gt; \<br>    --versioning-configuration Status=Enabled</pre><p>4. <strong><em>Creating our first Kubernetes cluster:</em></strong> Now<strong><em> </em></strong>we’re ready to start creating our first cluster! Let’s first set up a few environment variables to make this process easier. In case you skipped the DNS configuration (after step 2.), suffix .k8s.local to the value of NAME.</p><pre>export NAME=myfirstcluster.example.com<br>export KOPS_STATE_STORE=s3://your-bucket-name</pre><p>We will also need to note which availability zones are available to us. In this example, we will be deploying our cluster to the <strong>us-east-2</strong> region.</p><pre>aws ec2 describe-availability-zones --region us-east-2</pre><p>Create the cluster with the following command, if you are using a public hosted zone:</p><pre>kops create cluster \<br>    --zones us-east-2c \<br>    --node-count 3 \<br>    ${NAME}</pre><p>If you are using a private hosted zone, run:</p><pre>kops create cluster \<br>    --zones us-east-2c \<br>    --node-count 3 \ <br>    --dns private ${NAME}</pre><p>This command should give you the log of setting up of your K8S cluster. You need to give some time for the cluster to be up as it creates new EC2 machines for the master and minion nodes.</p><pre>[ec2-user@ip-172-31-35-145 test]$ kops create cluster \<br>&gt;     --dns private \<br>&gt;     --zones us-east-2c \ <br>&gt;     --node-count 3 \<br>&gt;     ${NAME} --yes<br>I0306 09:45:29.636834   20628 create_cluster.go:439] Inferred --cloud=aws from zone &quot;us-east-2c&quot;<br>I0306 09:45:29.637030   20628 create_cluster.go:971] Using SSH public key: /home/ec2-user/.ssh/id_rsa.pub<br>I0306 09:45:29.850021   20628 subnets.go:184] Assigned CIDR 172.20.32.0/19 to subnet us-east-2c<br>I0306 09:45:31.118837   20628 dns.go:92] Private DNS: skipping DNS validation<br>I0306 09:45:46.986963   20628 executor.go:91] Tasks: 73 done / 73 total; 0 can run<br>I0306 09:45:46.987101   20628 dns.go:153] Pre-creating DNS records<br>I0306 09:45:47.668392   20628 update_cluster.go:248] Exporting kubecfg for cluster<br>kops has set your kubectl context to k8s.appbase</pre><pre>Cluster is starting.  It should be ready in a few minutes.</pre><p>Voila! Our K8s cluster should be now up and running.</p><p><strong><em>5. Cluster Validation: </em></strong>All instances created by kops will be built within <a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroup.html">ASG (Auto Scaling Groups)</a>. ASG instances are automatically rebuilt and monitored if they suffer a failure.</p><p>If you want to edit any of the cluster’s configurations, you can run the following command:</p><pre>kops edit cluster ${NAME}</pre><p>Every time you make a change in the cluster configuration, you will also need to build the cluster by running the following command:</p><pre>kops update cluster ${NAME} --yes</pre><p>You should see something similar to this.</p><pre>[ec2-user@ip-172-31-35-145 examples]$ kops update cluster --yes<br>Using cluster from kubectl context: k8s.appbase</pre><pre>I0216 05:09:06.074467    2158 dns.go:92] Private DNS: skipping DNS validation<br>I0216 05:09:07.699380    2158 executor.go:91] Tasks: 73 done / 73 total; 0 can run<br>I0216 05:09:07.699486    2158 dns.go:153] Pre-creating DNS records<br>I0216 05:09:07.961703    2158 update_cluster.go:248] Exporting kubecfg for cluster<br>kops has set your kubectl context to k8s.appbase</pre><pre>Cluster changes have been applied to the cloud.</pre><p>Let’s validate our cluster to make sure that everything is working correctly.</p><pre>kops validate cluster</pre><p>You should see that your cluster is up and ready.</p><pre>Using cluster from kubectl context: k8s.appbase</pre><pre>Validating cluster k8s.appbase</pre><pre>INSTANCE GROUPS<br>NAME   ROLE MACHINETYPE MIN MAX SUBNETS<br>master-us-east-2c Master t2.large 1 1 us-east-2c<br>nodes   Node t2.medium 3 3 us-east-2c</pre><pre>NODE STATUS<br>NAME      ROLE READY<br>ip-172-20-44-33.us-east-2.compute.internal master True<br>ip-172-20-52-48.us-east-2.compute.internal node True<br>ip-172-20-62-30.us-east-2.compute.internal node True<br>ip-172-20-64-53.us-east-2.compute.internal node True</pre><pre>Your cluster k8s.appbase is ready</pre><p><strong>Go check out your new k8s!</strong></p><p>A simple Kubernetes API call can be used to check if the API is online and listening. Let&#39;s use kubectl to check the nodes.</p><pre>kubectl get nodes</pre><p>This will give the details of your nodes and their current status.</p><pre>[ec2-user@ip-172-31-35-145 elasticsearch]$ kubectl get nodes<br>NAME                                         STATUS    ROLES     AGE       VERSION<br>ip-172-20-44-33.us-east-2.compute.internal   Ready     master    1m       v1.8.6<br>ip-172-20-52-48.us-east-2.compute.internal   Ready     node      3m       v1.8.6<br>ip-172-20-62-30.us-east-2.compute.internal   Ready     node      2m       v1.8.6<br>ip-172-20-64-53.us-east-2.compute.internal   Ready     node      4m       v1.8.6</pre><p>A Pod in Kubernetes is an abstraction that represents a group of one or more application containers (such as Docker), and some shared resources for those containers. A pod is deployed on a node. As you need to scale out your application, you can add nodes to your K8S deployment.</p><p>To get the available pods, run<em>:<br></em> kubectl get pods</p><p>This command will list the available pods in our cluster.</p><pre>[ec2-user@ip-172-31-35-145 ~]$ kubectl get pods<br>NAME                  READY     STATUS    RESTARTS   AGE<br>es-5967f5d99c-5vcpb   1/1       Running   0          3h<br>es-5967f5d99c-cqk88   1/1       Running   0          3h<br>es-5967f5d99c-lp789   1/1       Running   0          3h</pre><h3><strong><em>Deploying Elasticsearch on our K8S Cluster</em></strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*vnChI-16p43aiZf2AR318w.png" /><figcaption><strong>Image:</strong> A diagrammatic view of the K8S cluster</figcaption></figure><p>If you have never dived into Kubernetes before, I recommend this awesome <a href="https://kubernetes.io/docs/tutorials/kubernetes-basics/cluster-interactive/">interactive K8S Bootcamp</a> with Katacoda that you can try right from your browser.</p><p>So far, in the K8S cluster, we have setup — we have created one master and two agent nodes. The role of the master node is to communicate deployment commands to the apps running within <strong>pods</strong> of the agent nodes.</p><p>App deployments in K8S are declarative and configured via JSON/YAML files. Depending on the kind of app or system you are deploying, you will need to use an appropriate controller for deploying it. Since Elasticsearch is a stateful app that requires persistence, we will use the StatefulSet controller.</p><p><strong><em>6.Deploying via StatefulSet: </em></strong>We are going to use the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/"><strong>StatefulSet</strong></a> controller for our Elasticsearch deployment. A StatefulSet manages Pods that are based on an identical container spec. It manages the deployment and scaling of a set of Pods and provides guarantees about the ordering and uniqueness of these Pods. A <strong>StatefulSet</strong> controller also makes it easy to attach persistent volumes with an application, an essential requirement for Elasticsearch.</p><p>Create a file named es-statefulset.yaml. It will contain the specification of Elasticsearch. Feel free to modify the configuration. For a list of environment variables that can be passed to the Elasticsearch image, we are using <a href="https://github.com/pires/docker-elasticsearch-kubernetes#environment-variables">see here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e76050fc111a9079137c74fb2e017870/href">https://medium.com/media/e76050fc111a9079137c74fb2e017870/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88fa31f9da042e29430eeeb3fb662f84/href">https://medium.com/media/88fa31f9da042e29430eeeb3fb662f84/href</a></iframe><p><strong><em>7.Services: </em></strong>A Kubernetes <strong>service</strong> is an abstraction that defines a logical set of <strong>pods</strong> and a policy to access them. This helps a containerized app to identify another containerized app or its own instance in another pod.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*oMEv4HYD3Oxtzu8Vh_tePg.png" /><figcaption><strong>Image:</strong> A visualization of how a service abstracts pods based on selector labels.</figcaption></figure><p>LoadBalancer is a special type of service that exposes pods to outside network and distributes load on them. We will use it to create an external IP through which anyone can contact our Elasticsearch cluster. We will use this service as a way for the ES nodes to discover each other.</p><p>Create a file named es-svc.yaml. Edit it to specify a load balancer service.</p><pre>apiVersion: v1               #API Version of the resource<br>kind: Service                #Type of resource<br>metadata:                    #Contains metadata of this resource.<br>  name: elasticsearch        #Name of this resource<br>  labels:                    #Additional identifier to put on pods<br>    component: elasticsearch #puts component = elasticsearch<br>spec:                        #Specifications of this resource<br>  type: LoadBalancer         #type of service<br>  selector:                  #will distribute load on pods which <br>    component: elasticsearch #have label `component = elasticsearch`<br>  ports:                     #Port on which LoadBalancer will listen<br>  - name: http               #Name given to port<br>    port: 9200               #Port number<br>    protocol: TCP            #Protocol supported<br>  - name: transport          #Name given to port<br>    port: 9300               #Port number<br>    protocol: TCP            #Protocol supported</pre><p><strong><em>8. App Creation:</em></strong> This is all we need. We will deploy our Elasticsearch cluster on K8S with the following commands.</p><pre>kubectl create -f es-statefulset.yaml <br>kubectl create -f es-svc.yaml</pre><p>The create command is a generic command used for creating any resource within K8S.</p><p>Our 3-node (remember replicas=3? in the StatefulSet config) Elasticsearch cluster should be up and ready in no time.</p><p>We can check on the Elasticsearch pods using this command:<br> kubectl get pods</p><pre>[ec2-user@ip-172-31-35-145 test]$ kubectl get pods,svc,deployment<br>NAME                  READY     STATUS    RESTARTS   AGE<br>es-0                  1/1       Running   0          23m<br>es-1                  1/1       Running   0          17m<br>es-2                  1/1       Running   0          23m</pre><p><strong><em>9.Test our Elasticsearch Cluster: </em></strong>Finally, let’s check if Elasticsearch is set up and working correctly. Get the external IP to connect to Elasticsearch. It will reside in the <strong>LoadBalancer</strong> service that we created. Use the following command to describe the <strong>LoadBalancer</strong>:</p><p>kubectl describe service elasticsearch</p><pre>[ec2-user@ip-172-31-35-145 examples]$ kubectl describe service elasticsearch<br>Name:                     elasticsearch<br>Namespace:                default<br>Labels:                   component=elasticsearch<br>Annotations:              &lt;none&gt;<br>Selector:                 component=elasticsearch<br>Type:                     LoadBalancer<br>IP:                       100.70.114.146<br>LoadBalancer Ingress:     <a href="http://a4d0c157d212811e898430af47d23da1-952261901.us-east-2.elb.amazonaws.com/"><strong>http://a4d0c157d212811e898430af47d23da1-952261901.us-east-2.elb.amazonaws.com</strong></a><br>Port:                     http  9200/TCP<br>TargetPort:               9200/TCP<br>NodePort:                 http  31358/TCP<br>Endpoints:                100.96.4.28:9200<br>Port:                     transport  9300/TCP<br>TargetPort:               9300/TCP<br>NodePort:                 transport  31767/TCP<br>Endpoints:                100.96.4.28:9300<br>Session Affinity:         None<br>External Traffic Policy:  Cluster<br>Events:<br>  Type    Reason                Age   From                Message<br>  ----    ------                ----  ----                -------<br>  Normal  EnsuringLoadBalancer  1m    service-controller  Ensuring load balancer<br>  Normal  EnsuredLoadBalancer   1m    service-controller  Ensured load balancer<br>[ec2-user@ip-172-31-35-145 examples]$</pre><p>Note the LoadBalancer Ingress value. Hit the browser with the URI and suffix Elasticsearch’s external port number to it :9200. You should see a screen similar to this one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*jKYluDkQcqxrVPlDPV6T4A.jpeg" /><figcaption><strong>Image:</strong> Screenshot of Elasticsearch cluster’s GET / request</figcaption></figure><p>You can check the health of your Elasticsearch nodes by suffixing :9200/_cluster/health?pretty to the external IP/address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VuHnWDz5ugv6ukGZruVLIg.jpeg" /><figcaption><strong>Image:</strong> Testing the <strong>/_cluster/health</strong> endpoint for checking cluster health</figcaption></figure><p><strong><em>10.Testing Kubernetes Healing</em></strong></p><p>StatefulSets have the feature of maintaining the number of replicas you specify. So, even if any pod goes down due to any reason the StatefulSet will initialize new pods.</p><p>We will test this by simulating a failure (deleting all pods which run our ES instances) to see if our ES cluster can automatically spawn back up with the data intact.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-viVQMyXX1DWY9szIIYcBg.gif" /></figure><p>Since StatefulSet initializes 1 pod at a time, you may need to wait for some time until all the pods are back up and running.</p><p>As we can see, our indexed record prior to the ES failure scenario can be retrieved once the pods are back up.</p><h3>Recommended Next Steps</h3><p>There are a few more things to keep in mind before you can use a setup like this in production.</p><ol><li>Setting up backups — This helps us recover any data loss. If you can automate this process, all the better.</li><li>Setting up Authorization — We obviously want to secure our Elasticsearch cluster. Setting up a Basic Authentication or Bearer Token-based authorization access would help us ensure security.</li><li>TLS Certificates — We should also set up a LetsEncrypt / other TLS cert provider to map a custom domain for our ES cluster and secure all the requests passing to it.</li></ol><p>While these things go beyond the current scope of the current tutorial, they are possible to achieve with Kubernetes.</p><p>If you are interested in an Elasticsearch offering based on Kubernetes that runs on your own cloud and does all of these, you should <a href="https://appbase.io/pricing#dedicated-card">check out the managed Elasticsearch service</a> from <a href="https://appbase.io/">appbase.io</a>. In addition to the above steps, we also allow the possibility of bundling any other open-source components (Kibana, Logstash, Dejavu, Mirage, ElastAlert).</p><h3>Summary</h3><p>We started out with creating a Kubernetes cluster (which automatically sets up EC2 machines as nodes within the cluster) and then created a StatefulSet controller to deploy the Elasticsearch app with persistent volumes.</p><p>The full YAML file for a complete deployment is available <a href="https://github.com/raazcrzy/kubernetes/tree/master/elasticsearch">here</a> for accessibility.</p><blockquote>Checkout our hosted ElasticSearch Clusters as a service offering — <a href="https://appbase.io/clusters/">https://appbase.io/clusters/</a></blockquote><p>Next up, you should read about scaling our just created Elasticsearch cluster with Kubernetes.</p><p><a href="https://medium.appbase.io/scaling-an-elasticsearch-cluster-with-kubernetes-120991de1451">Scaling an Elasticsearch Cluster with Kubernetes</a></p><p>Happy hacking!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7913b607abda" width="1" height="1" alt=""><hr><p><a href="https://medium.com/all-things-data-streams/deploy-elasticsearch-with-kubernetes-on-aws-in-10-steps-7913b607abda">Deploy Elasticsearch with Kubernetes on AWS in 10 steps</a> was originally published in <a href="https://medium.com/all-things-data-streams">All things #search</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>