github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/kubeblocks-for-redis/cluster-management/create-and-connect-a-redis-cluster.md (about) 1 --- 2 title: Create and connect to a Redis Cluster 3 description: How to create and connect to a Redis cluster 4 keywords: [redis, create a redis cluster, connect to a redis cluster, cluster, redis sentinel] 5 sidebar_position: 1 6 sidebar_label: Create and connect 7 --- 8 9 import Tabs from '@theme/Tabs'; 10 import TabItem from '@theme/TabItem'; 11 12 # Create and Connect to a Redis cluster 13 14 This tutorial shows how to create and connect to a Redis cluster. 15 16 KuebBlocks for Redis supports Standalone clusters and Replication Cluster. 17 18 But for your better high-availability experience, KubeBlocks creates a Redis Replication Cluster by default. 19 20 ## Create a Redis cluster 21 22 ### Before you start 23 24 * [Install kbcli](./../../installation/install-with-kbcli/install-kbcli.md) if you want to create and connect a cluster by kbcli. 25 * Install KubeBlocks: You can install KubeBlocks by [kbcli](./../../installation/install-with-kbcli/install-kubeblocks-with-kbcli.md) or by [Helm](./../../installation/install-with-helm/install-kubeblocks-with-helm.md). 26 * Make sure the Redis add-on is enabled. 27 28 <Tabs> 29 30 <TabItem value="kbcli" label="kbcli" default> 31 32 ```bash 33 kbcli addon list 34 > 35 NAME TYPE STATUS EXTRAS AUTO-INSTALL INSTALLABLE-SELECTOR 36 ... 37 redis Helm Enabled true 38 ... 39 ``` 40 41 </TabItem> 42 43 <TabItem value="kubectl" label="kubectl"> 44 45 ```bash 46 kubectl get addons.extensions.kubeblocks.io redis 47 > 48 NAME TYPE STATUS AGE 49 redis Helm Enabled 96m 50 ``` 51 52 </TabItem> 53 54 </Tabs> 55 56 * View all the database types and versions available for creating a cluster. 57 58 <Tabs> 59 60 <TabItem value="kbcli" label="kbcli" default> 61 62 ```bash 63 kbcli clusterdefinition list 64 65 kbcli clusterversion list 66 ``` 67 68 </TabItem> 69 70 <TabItem value="kubectl" label="kubectl"> 71 72 Make sure the `redis` cluster definition is installed with `kubectl get clusterdefinitions redis`. 73 74 ```bash 75 kubectl get clusterdefinition Redis 76 > 77 NAME MAIN-COMPONENT-NAME STATUS AGE 78 redis redis Available 96m 79 ``` 80 81 View all available versions for creating a cluster. 82 83 ```bash 84 kubectl get clusterversions -l clusterdefinition.kubeblocks.io/name=redis 85 > 86 NAME CLUSTER-DEFINITION STATUS AGE 87 redis-7.0.6 redis Available 96m 88 ``` 89 90 </TabItem> 91 </Tabs> 92 93 * To keep things isolated, create a separate namespace called `demo` throughout this tutorial. 94 95 ```bash 96 kubectl create namespace demo 97 > 98 namespace/demo created 99 ``` 100 101 ### Create a cluster 102 103 KubeBlocks supports creating two types of Redis clusters: Standalone and Replication Cluster. Standalone only supports one replica and can be used in scenarios with lower requirements for availability. For scenarios with high availability requirements, it is recommended to create a Replication Cluster, which supports automatic failover. And to ensure high availability, Primary and Secondary are distributed on different nodes by default. 104 105 <Tabs> 106 107 <TabItem value="kbcli" label="kbcli" default> 108 109 Create a Standalone. 110 111 ```bash 112 kbcli cluster create redis <clustername> 113 ``` 114 115 Create a Replication Cluster. 116 117 ```bash 118 kbcli cluster create redis --mode replication <clustername> 119 ``` 120 121 If you only have one node for deploying a Replication, set the `availability-policy` as `none` when creating a Replication Cluster. 122 123 ```bash 124 kbcli cluster create redis --mode replication --availability-policy none <clustername> 125 ``` 126 127 :::note 128 129 * In the production environment, it is not recommended to deploy all replicas on one node, which may decrease cluster availability. 130 * Run the command below to view the flags for creating a Redis cluster and the default values. 131 132 ```bash 133 kbcli cluster create redis -h 134 ``` 135 136 ::: 137 138 </TabItem> 139 140 <TabItem value="kubectl" label="kubectl"> 141 142 KubeBlocks implements a `Cluster` CRD to define a cluster. BHere is an example of creating a Standalone. 143 144 ```bash 145 cat <<EOF | kubectl apply -f - 146 apiVersion: apps.kubeblocks.io/v1alpha1 147 kind: Cluster 148 metadata: 149 name: redis 150 namespace: demo 151 labels: 152 helm.sh/chart: redis-cluster-0.6.0-alpha.36 153 app.kubernetes.io/version: "7.0.6" 154 app.kubernetes.io/instance: redis 155 spec: 156 clusterVersionRef: redis-7.0.6 157 terminationPolicy: Delete 158 affinity: 159 podAntiAffinity: Preferred 160 topologyKeys: 161 - kubernetes.io/hostname 162 tenancy: SharedNode 163 clusterDefinitionRef: redis # ref clusterDefinition.name 164 componentSpecs: 165 - name: redis 166 componentDefRef: redis # ref clusterDefinition componentDefs.name 167 monitor: false 168 replicas: 1 169 enabledLogs: 170 - running 171 serviceAccountName: kb-redis 172 switchPolicy: 173 type: Noop 174 resources: 175 limits: 176 cpu: "0.5" 177 memory: "0.5Gi" 178 requests: 179 cpu: "0.5" 180 memory: "0.5Gi" 181 volumeClaimTemplates: 182 - name: data # ref clusterDefinition components.containers.volumeMounts.name 183 spec: 184 accessModes: 185 - ReadWriteOnce 186 resources: 187 requests: 188 storage: 20Gi 189 services: 190 EOF 191 ``` 192 193 * `spec.clusterDefinitionRef` is the name of the cluster definition CRD that defines the cluster components. 194 * `spec.clusterVersionRef` is the name of the cluster version CRD that defines the cluster version. 195 * `spec.componentSpecs` is the list of components that define the cluster components. 196 * `spec.componentSpecs.componentDefRef` is the name of the component definition that is defined in the cluster definition, you can get the component definition names with `kubectl get clusterdefinition redis -o json | jq '.spec.componentDefs[].name'` 197 * `spec.componentSpecs.name` is the name of the component. 198 * `spec.componentSpecs.replicas` is the number of replicas of the component. 199 * `spec.componentSpecs.resources` is the resource requirements of the component. 200 * `spec.componentSpecs.volumeClaimTemplates` is the list of volume claim templates that define the volume claim templates for the component. 201 * `spec.terminationPolicy` is the policy of the cluster termination. The default value is `Delete`. Valid values are `DoNotTerminate`, `Halt`, `Delete`, `WipeOut`. `DoNotTerminate` will block delete operation. `Halt` deletes workload resources such as statefulset and deployment workloads but keep PVCs. `Delete` is based on Halt and deletes PVCs. `WipeOut` is based on Delete and wipe out all volume snapshots and snapshot data from backup storage location. 202 203 KubeBlocks operator watches for the `Cluster` CRD and creates the cluster and all dependent resources. You can get all the resources created by the cluster with `kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=redis -n demo`. 204 205 ```bash 206 kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=redis -n demo 207 ``` 208 209 Run the following command to see the created Redis cluster object: 210 211 ```bash 212 kubectl get cluster redis -n demo -o yaml 213 ``` 214 215 <details> 216 217 <summary>Output</summary> 218 219 ```yaml 220 apiVersion: apps.kubeblocks.io/v1alpha1 221 kind: Cluster 222 metadata: 223 annotations: 224 kubectl.kubernetes.io/last-applied-configuration: | 225 {"apiVersion":"apps.kubeblocks.io/v1alpha1","kind":"Cluster","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"redis","app.kubernetes.io/version":"7.0.6","helm.sh/chart":"redis-cluster-0.6.0-alpha.36"},"name":"redis","namespace":"demo"},"spec":{"affinity":{"podAntiAffinity":"Preferred","tenancy":"SharedNode","topologyKeys":["kubernetes.io/hostname"]},"clusterDefinitionRef":"redis","clusterVersionRef":"redis-7.0.6","componentSpecs":[{"componentDefRef":"redis","enabledLogs":["running"],"monitor":false,"name":"redis","replicas":1,"resources":{"limits":{"cpu":"0.5","memory":"0.5Gi"},"requests":{"cpu":"0.5","memory":"0.5Gi"}},"serviceAccountName":"kb-redis","services":null,"switchPolicy":{"type":"Noop"},"volumeClaimTemplates":[{"name":"data","spec":{"accessModes":["ReadWriteOnce"],"resources":{"requests":{"storage":"20Gi"}}}}]}],"terminationPolicy":"Delete"}} 226 creationTimestamp: "2023-07-19T08:33:48Z" 227 finalizers: 228 - cluster.kubeblocks.io/finalizer 229 generation: 1 230 labels: 231 app.kubernetes.io/instance: redis 232 app.kubernetes.io/version: 7.0.6 233 clusterdefinition.kubeblocks.io/name: redis 234 clusterversion.kubeblocks.io/name: redis-7.0.6 235 helm.sh/chart: redis-cluster-0.6.0-alpha.36 236 name: redis 237 namespace: demo 238 resourceVersion: "12967" 239 uid: 25ae9193-60ae-4521-88eb-70ea4c3d97ef 240 spec: 241 affinity: 242 podAntiAffinity: Preferred 243 tenancy: SharedNode 244 topologyKeys: 245 - kubernetes.io/hostname 246 clusterDefinitionRef: redis 247 clusterVersionRef: redis-7.0.6 248 componentSpecs: 249 - componentDefRef: redis 250 enabledLogs: 251 - running 252 monitor: false 253 name: redis 254 noCreatePDB: false 255 replicas: 1 256 resources: 257 limits: 258 cpu: "0.5" 259 memory: 0.5Gi 260 requests: 261 cpu: "0.5" 262 memory: 0.5Gi 263 serviceAccountName: kb-redis 264 switchPolicy: 265 type: Noop 266 volumeClaimTemplates: 267 - name: data 268 spec: 269 accessModes: 270 - ReadWriteOnce 271 resources: 272 requests: 273 storage: 20Gi 274 terminationPolicy: Delete 275 status: 276 clusterDefGeneration: 2 277 components: 278 redis: 279 phase: Running 280 podsReady: true 281 podsReadyTime: "2023-07-19T08:34:34Z" 282 replicationSetStatus: 283 primary: 284 pod: redis-redis-0 285 conditions: 286 - lastTransitionTime: "2023-07-19T08:33:48Z" 287 message: 'The operator has started the provisioning of Cluster: redis' 288 observedGeneration: 1 289 reason: PreCheckSucceed 290 status: "True" 291 type: ProvisioningStarted 292 - lastTransitionTime: "2023-07-19T08:33:48Z" 293 message: Successfully applied for resources 294 observedGeneration: 1 295 reason: ApplyResourcesSucceed 296 status: "True" 297 type: ApplyResources 298 - lastTransitionTime: "2023-07-19T08:34:34Z" 299 message: all pods of components are ready, waiting for the probe detection successful 300 reason: AllReplicasReady 301 status: "True" 302 type: ReplicasReady 303 - lastTransitionTime: "2023-07-19T08:34:34Z" 304 message: 'Cluster: redis is ready, current phase is Running' 305 reason: ClusterReady 306 status: "True" 307 type: Ready 308 observedGeneration: 1 309 phase: Running 310 ``` 311 312 </details> 313 314 </TabItem> 315 316 </Tabs> 317 318 ## Connect to a Redis Cluster 319 320 <Tabs> 321 322 <TabItem value="kbcli" label="kbcli" default> 323 324 ```bash 325 kbcli cluster connect <clustername> --namespace <name> 326 ``` 327 328 </TabItem> 329 330 <TabItem value="kubectl" label="kubectl"> 331 332 You can use `kubectl exec` to exec into a Pod and connect to a database. 333 334 KubeBlocks operator has created a new Secret called `redis-conn-credential` to store the connection credential of the Redis cluster. This secret contains the following keys: 335 336 * `username`: the root username of the Redis cluster. 337 * `password`: the password of the root user. 338 * `port`: the port of the Redis cluster. 339 * `host`: the host of the Redis cluster. 340 * `endpoint`: the endpoint of the Redis cluster and it is the same as `host:port`. 341 342 1. Get the `username` and `password` for the `kubectl exec` command. 343 344 ```bash 345 kubectl get secrets -n demo redis-conn-credential -o jsonpath='{.data.\username}' | base64 -d 346 > 347 default 348 349 kubectl get secrets -n demo redis-conn-credential -o jsonpath='{.data.\password}' | base64 -d 350 > 351 p7twmbrd 352 ``` 353 354 2. Exec into the pod `redis-redis-0` and connect to the database using username and password. 355 356 ```bash 357 kubectl exec -ti -n demo redis-redis-0 -- bash 358 359 root@redis-redis-0:/# redis-cli -a p7twmbrd --user default 360 ``` 361 362 </TabItem> 363 364 <TabItem value="port-forward" label="port-forward"> 365 366 You can also port forward the service to connect to the database from your local machine. 367 368 1. Run the following command to port forward the service. 369 370 ```bash 371 kubectl port-forward -n demo svc/redis-redis 6379:6379 372 ``` 373 374 2. Open a new terminal and run the following command to connect to the database. 375 376 ```bash 377 root@redis-redis-0:/# redis-cli -a p7twmbrd --user default 378 ``` 379 380 </TabItem> 381 382 </Tabs> 383 384 For the detailed database connection guide, refer to [Connect database](./../../connect_database/overview-of-database-connection.md).