# How to stop/start an elastic cluster in AKS

**URL:** https://discuss.elastic.co/t/how-to-stop-start-an-elastic-cluster-in-aks/371871
**Category:** Elasticsearch
**Created:** [December 11, 2024, 8:19pm UTC](https://discuss.elastic.co/t/how-to-stop-start-an-elastic-cluster-in-aks/371871 "2024-12-11T20:19:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Manuel\_Osorio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manuel_osorio/32/139942_2.png) [@Manuel\_Osorio](https://discuss.elastic.co/u/Manuel_Osorio)
#### Post date: [December 11, 2024, 8:19pm UTC](https://discuss.elastic.co/t/how-to-stop-start-an-elastic-cluster-in-aks/371871/1 "2024-12-11T20:19:28Z")

</div>

Hi everyone,

I need to find a way to stop an elastic cluster for some cost savings, I am looking how to do it and start the cluster only when it's required.

could you please share some guidance for this?

Thanks a lot

---

<div class="post-metadata">

### Author: ![atul\_mohan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atul_mohan/32/137776_2.png) [@atul\_mohan](https://discuss.elastic.co/u/atul_mohan)
#### Post date: [January 1, 2025, 7:30pm UTC](https://discuss.elastic.co/t/how-to-stop-start-an-elastic-cluster-in-aks/371871/2 "2025-01-01T19:30:32Z")

</div>

Stop Elastic Cluster Script

```auto
#!/bin/bash

# Name of your Elasticsearch cluster
CLUSTER_NAME="my-elastic-cluster"
NAMESPACE="default"

# Check if kubectl is available
if ! command -v kubectl &> /dev/null
then
    echo "kubectl could not be found. Please install kubectl and ensure it's in your PATH."
    exit 1
fi

# Function to check if the cluster exists
cluster_exists() {
    kubectl get elasticsearch $CLUSTER_NAME -n $NAMESPACE &> /dev/null
    return $?
}

# Check if the cluster exists
if ! cluster_exists; then
    echo "Elasticsearch cluster $CLUSTER_NAME does not exist in namespace $NAMESPACE."
    exit 1
fi

# Update the cluster spec to scale down to 0 nodes
echo "Scaling down the Elasticsearch cluster $CLUSTER_NAME to 0 nodes..."
kubectl patch elasticsearch/$CLUSTER_NAME \
    -n $NAMESPACE \
    --type='json' \
    -p='[{"op": "replace", "path": "/spec/nodeSets/0/count", "value":0}]'

# Wait for the cluster to scale down
echo "Waiting for the cluster to scale down..."
kubectl wait --for=condition=Ready=False elasticsearch/$CLUSTER_NAME -n $NAMESPACE --timeout=10m

# Confirm the cluster is stopped
echo "Cluster $CLUSTER_NAME should now be stopped. Checking status:"
kubectl get elasticsearch/$CLUSTER_NAME -n $NAMESPACE -o jsonpath='{.status.phase}'

```

Start Elastic Cluster Script

```auto
#!/bin/bash

CLUSTER_NAME="my-elastic-cluster"
NAMESPACE="default"
# Number of nodes to scale to
NODE_COUNT=3

# Check if kubectl is available
if ! command -v kubectl &> /dev/null
then
    echo "kubectl could not be found. Please install kubectl and ensure it's in your PATH."
    exit 1
fi

# Function to check if the cluster exists
cluster_exists() {
    kubectl get elasticsearch $CLUSTER_NAME -n $NAMESPACE &> /dev/null
    return $?
}

# Check if the cluster exists
if ! cluster_exists; then
    echo "Elasticsearch cluster $CLUSTER_NAME does not exist in namespace $NAMESPACE."
    exit 1
fi

# Update the cluster spec to scale up to the desired number of nodes
echo "Scaling up the Elasticsearch cluster $CLUSTER_NAME to $NODE_COUNT nodes..."
kubectl patch elasticsearch/$CLUSTER_NAME \
    -n $NAMESPACE \
    --type='json' \
    -p='[{"op": "replace", "path": "/spec/nodeSets/0/count", "value":'$NODE_COUNT'}]'

# Wait for the cluster to scale up
echo "Waiting for the cluster to scale up..."
kubectl wait --for=condition=Ready elasticsearch/$CLUSTER_NAME -n $NAMESPACE --timeout=10m

# Confirm the cluster is running
echo "Cluster $CLUSTER_NAME should now be running. Checking status:"
kubectl get elasticsearch/$CLUSTER_NAME -n $NAMESPACE -o jsonpath='{.status.phase}'

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 29, 2025, 7:31pm UTC](https://discuss.elastic.co/t/how-to-stop-start-an-elastic-cluster-in-aks/371871/3 "2025-01-29T19:31:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
