Skip to content

Commit

Permalink
feat: pika clusters support automatic rebalance (OpenAtomFoundation#2121
Browse files Browse the repository at this point in the history
)

* feat: use component post_start_action to auto rebalance pika cluster

* chore: update default count

* fix: use English comment

* update README
  • Loading branch information
machinly authored Nov 27, 2023
1 parent 71c943e commit 3b74bce
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 70 deletions.
28 changes: 8 additions & 20 deletions tools/kubeblocks_helm/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Install pika cluster by kubeblocks
# Install pika cluster by kubeblocks
Fellow the kubeblock docs [Install kbcli](https://kubeblocks.io/docs/preview/user_docs/installation/install-kbcli)

## install kubeblocks
Expand All @@ -9,7 +9,6 @@ Fellow the kubeblock docs [kubeblocks](https://kubeblocks.io/docs/preview/user_d

### install pika CD and pika cluster
First, use helm install pika cluster definition and install pika cluster.

```bash
cd ./tools/kubeblocks-helm/
helm install pika ./pika
Expand All @@ -28,15 +27,6 @@ Then connect codis front end.
```
Open browser and visit `http://localhost:8080`

Then add pika instance to codis
1. Input group id `1` and click `Add Group` button
2. Input the following pika instance url and group id, then click `Add Server` button
- pika-cluster-pika-0.pika-cluster-pika-headless:9221
- pika-cluster-pika-1.pika-cluster-pika-headless:9221

3. Click `Rebalance all slots` button


### connect to pika cluster
```bash
kubectl port-forward svc/pika-cluster-codis-proxy 19000
Expand All @@ -46,15 +36,13 @@ redis-cli -p 19000 info

## Scale pika cluster

Use kbcli horizontal scale pika instance, you will get 2 new pika instances.
### Scale out

1. First, edit pika-cluster/values.yaml to increase replicaCount.
2. Then upgrade the cluster.
```bash
kbcli cluster hscale pika-cluster --replicas 4 --components pika
helm upgrade pika-cluster ./pika-cluster
```

Add new pika instances to codis
1. Input group id `2` and click `Add Group` button
2. Input the following pika instance url and group id, then click `Add Server` button
- pika-cluster-pika-2.pika-cluster-pika-headless:9221
- pika-cluster-pika-3.pika-cluster-pika-headless:9221
3. Click `Rebalance all slots` button

### Scale in
scale in is not supported now.
2 changes: 1 addition & 1 deletion tools/kubeblocks_helm/pika-cluster/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Pika Codis Cluster Helm chart for KubeBlocks.

type: application

version: 0.6.0-alpha.21
version: 0.7.1-beta.1

appVersion: "3.5.2"

Expand Down
2 changes: 1 addition & 1 deletion tools/kubeblocks_helm/pika/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Pika Codis cluster definition Helm chart for Kubernetes

type: application

version: 0.6.0-alpha.21
version: 0.7.1-beta.1

appVersion: "3.5.2"

Expand Down
2 changes: 1 addition & 1 deletion tools/kubeblocks_helm/pika/config/pika-config.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ max-bytes-for-level-multiplier : 10
# slotmigrate is mainly used to migrate slots, usually we will set it to no.
# When you migrate slots, you need to set it to yes, and reload slotskeys before.
# slotmigrate [yes | no]
slotmigrate : no
slotmigrate : yes

# BlockBasedTable block_size, default 4k
# block-size: 4096
Expand Down
34 changes: 32 additions & 2 deletions tools/kubeblocks_helm/pika/script/admin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ wait_master_registered() {
done
}

# confirm group has the max index of all groups
confirm_max_group() {
max_group_id=0
for component in ${KB_CLUSTER_COMPONENT_LIST//,/ }; do
if [[ ${component} =~ pika-group-([0-9]+) ]]; then
group_id=${BASH_REMATCH[1]}
if [ ${group_id} -gt ${max_group_id} ]; then
max_group_id=${group_id}
fi
fi
done
if [ ${GROUP_ID} -ne ${max_group_id} ]; then
echo "Exit: group id ${GROUP_ID} is not max group id ${max_group_id}"
exit 0
fi

}

reload_until_success() {
until $CODIS_ADMIN --reload 1>/dev/null 2>&1; do
echo waiting for reload success
Expand All @@ -65,12 +83,15 @@ remove_server() {
$CODIS_ADMIN --group-del --gid=${GROUP_ID} --addr=${KB_POD_FQDN}:9221
}

reblance() {
rebalance() {
$CODIS_ADMIN --rebalance --confirm
if [ $? != 0 ]; then
echo "Error: rebalance failed"
exit 1
fi
}

set_group_id
set_instance_role
set_codis_dashboard

if [ $# -eq 1 ]; then
Expand All @@ -84,15 +105,24 @@ if [ $# -eq 1 ]; then
exit 0
;;
--register-server)
set_instance_role
wait_dashboard_running
wait_server_running
register_server
exit 0
;;
--remove-server)
set_instance_role
remove_server
exit 0
;;
--rebalance)
wait_dashboard_running
confirm_max_group
wait_master_registered
rebalance
exit 0
;;
*)
echo "Error: invalid option '$1'"
exit 1
Expand Down
7 changes: 5 additions & 2 deletions tools/kubeblocks_helm/pika/templates/clusterdefinition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ spec:
defaultMode: 0555
postStartSpec:
cmdExecutorConfig:
image: {{ .Values.image.pika.registry | default "docker.io" }}/{{ .Values.image.pika.repository }}:{{ .Values.image.pika.tag }}
image: {{ include "codis.image" . }}
command:
- /script/pika-group-post-start.sh
- "/bin/bash"
args:
- "-c"
- "/script/admin.sh --rebalance"
scriptSpecSelectors:
- name: pika-script
podSpec:
Expand Down
66 changes: 33 additions & 33 deletions tools/kubeblocks_helm/pika/templates/clusterversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ metadata:
spec:
clusterDefinitionRef: pika
componentVersions:
- componentDefRef: pika-group
versionsContext:
containers:
- name: pika
image: {{ include "pika.image" . }}
imagePullPolicy: {{ include "pika.imagePullPolicy" . }}
- name: codis-admin
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
- componentDefRef: etcd
versionsContext:
containers:
- name: etcd
image: {{ include "etcd.image" . }}
imagePullPolicy: {{ include "etcd.imagePullPolicy" . }}
- componentDefRef: codis-proxy
versionsContext:
containers:
- name: codis-proxy
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
- componentDefRef: codis-fe
versionsContext:
containers:
- name: codis-fe
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
- componentDefRef: codis-dashboard
versionsContext:
containers:
- name: codis-dashboard
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
- componentDefRef: pika-group
versionsContext:
containers:
- name: pika
image: {{ include "pika.image" . }}
imagePullPolicy: {{ include "pika.imagePullPolicy" . }}
- name: codis-admin
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
- componentDefRef: etcd
versionsContext:
containers:
- name: etcd
image: {{ include "etcd.image" . }}
imagePullPolicy: {{ include "etcd.imagePullPolicy" . }}
- componentDefRef: codis-proxy
versionsContext:
containers:
- name: codis-proxy
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
- componentDefRef: codis-fe
versionsContext:
containers:
- name: codis-fe
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
- componentDefRef: codis-dashboard
versionsContext:
containers:
- name: codis-dashboard
image: {{ include "codis.image" . }}
imagePullPolicy: {{ include "codis.imagePullPolicy" . }}
10 changes: 0 additions & 10 deletions tools/kubeblocks_helm/pika/templates/script.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,3 @@ metadata:
data:
admin.sh: |-
{{- .Files.Get "script/admin.sh" | nindent 4 }}
pika-group-post-start.sh: |-
#! /bin/bash
set -x
echo "add your post start script logic here"
envParams=$(env)
echo "add envs can be used in script:"
echo $envParams
sleep 60

0 comments on commit 3b74bce

Please sign in to comment.