Managing Clusters¶
Clusters are first-class citizens in Senlin service design. A cluster is defined as a collection of homogeneous objects. The “homogeneous” here means that the objects managed (aka. Nodes) have to be instantiated from the same “profile type”.
List Clusters¶
To examine the list of receivers:
def list_cluster(conn):
print("List clusters:")
for cluster in conn.clustering.clusters():
print(cluster.to_dict())
for cluster in conn.clustering.clusters(sort='name:asc'):
print(cluster.to_dict())
When listing clusters, you can specify the sorting option using the sort
parameter and you can do pagination using the limit
and marker
parameters.
Full example: manage cluster
Create Cluster¶
When creating a cluster, you will provide a dictionary with keys and values according to the cluster type referenced.
def create_cluster(conn):
print("Create cluster:")
spec = {
"name": CLUSTER_NAME,
"profile_id": PROFILE_ID,
"min_size": 0,
"max_size": -1,
"desired_capacity": 1,
}
cluster = conn.clustering.create_cluster(**spec)
print(cluster.to_dict())
Optionally, you can specify a metadata
keyword argument that contains some
key-value pairs to be associated with the cluster.
Full example: manage cluster
Get Cluster¶
To get a cluster based on its name or ID:
def get_cluster(conn):
print("Get cluster:")
cluster = conn.clustering.get_cluster(CLUSTER_ID)
print(cluster.to_dict())
Full example: manage cluster
Find Cluster¶
To find a cluster based on its name or ID:
def find_cluster(conn):
print("Find cluster:")
cluster = conn.clustering.find_cluster(CLUSTER_ID)
print(cluster.to_dict())
Full example: manage cluster
Update Cluster¶
After a cluster is created, most of its properties are immutable. Still, you
can update a cluster’s name
and/or params
.
def update_cluster(conn):
print("Update cluster:")
spec = {
"name": "Test_Cluster001",
"profile_id": "c0e3a680-e270-4eb8-9361-e5c9503fba0a",
"profile_only": True,
}
cluster = conn.clustering.update_cluster(CLUSTER_ID, **spec)
print(cluster.to_dict())
Full example: manage cluster
Delete Cluster¶
A cluster can be deleted after creation, When there are nodes in the cluster, the Senlin engine will launch a process to delete all nodes from the cluster and destroy them before deleting the cluster object itself.
def delete_cluster(conn):
print("Delete cluster:")
conn.clustering.delete_cluster(CLUSTER_ID)
print("Cluster deleted.")
# cluster support force delete
conn.clustering.delete_cluster(CLUSTER_ID, False, True)
print("Cluster deleted")
Add Nodes to Cluster¶
Add some existing nodes into the specified cluster.
def add_nodes_to_cluster(conn):
print("Add nodes to cluster:")
node_ids = [NODE_ID]
res = conn.clustering.add_nodes_to_cluster(CLUSTER_ID, node_ids)
print(res)
Remove Nodes from Cluster¶
Remove nodes from specified cluster.
def remove_nodes_from_cluster(conn):
print("Remove nodes from a cluster:")
node_ids = [NODE_ID]
res = conn.clustering.remove_nodes_from_cluster(CLUSTER_ID, node_ids)
print(res)
Replace Nodes in Cluster¶
Replace some existing nodes in the specified cluster.
def replace_nodes_in_cluster(conn):
print("Replace the nodes in a cluster with specified nodes:")
old_node = NODE_ID
new_node = "cd803d4a-015d-4223-b15f-db29bad3146c"
spec = {old_node: new_node}
res = conn.clustering.replace_nodes_in_cluster(CLUSTER_ID, **spec)
print(res)
Cluster Scale Out¶
Inflate the size of a cluster.
def scale_out_cluster(conn):
print("Inflate the size of a cluster:")
res = conn.clustering.scale_out_cluster(CLUSTER_ID, 1)
print(res)
Cluster Scale In¶
Shrink the size of a cluster.
def scale_out_cluster(conn):
print("Inflate the size of a cluster:")
res = conn.clustering.scale_out_cluster(CLUSTER_ID, 1)
print(res)
Cluster Resize¶
Resize of cluster.
def resize_cluster(conn):
print("Resize of cluster:")
spec = {
'min_size': 1,
'max_size': 6,
'adjustment_type': 'EXACT_CAPACITY',
'number': 2,
}
res = conn.clustering.resize_cluster(CLUSTER_ID, **spec)
print(res)
Attach Policy to Cluster¶
Once a policy is attached (bound) to a cluster, it will be enforced when related actions are performed on that cluster, unless the policy is (temporarily) disabled on the cluster
def attach_policy_to_cluster(conn):
print("Attach policy to a cluster:")
spec = {'enabled': True}
res = conn.clustering.attach_policy_to_cluster(
CLUSTER_ID, POLICY_ID, **spec
)
print(res)
Detach Policy from Cluster¶
Once a policy is attached to a cluster, it can be detached from the cluster at user’s request.
def detach_policy_from_cluster(conn):
print("Detach a policy from a cluster:")
res = conn.clustering.detach_policy_from_cluster(CLUSTER_ID, POLICY_ID)
print(res)
Cluster Check¶
Check cluster health status, Cluster members can be check.
def check_cluster(conn):
print("Check cluster:")
res = conn.clustering.check_cluster(CLUSTER_ID)
print(res)
Cluster Recover¶
To restore a specified cluster, members in the cluster will be checked.
def recover_cluster(conn):
print("Recover cluster:")
spec = {'check': True}
res = conn.clustering.recover_cluster(CLUSTER_ID, **spec)
print(res)