> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/devops/kubernetes/ji-chu-zhi-shi/zhong-jian-jian-rong-qi-hua/shi-yong-operator-an-zhuang-redis-ji-qun-outdated.md).

# 使用Operator安装Redis集群（outdated）

本小节演示的Operator是由UCloud开源的一个项目，项目地址为<https://github.com/ucloud/redis-cluster-operator>，打开此链接可以看到详细的操作文档。

## 安装

创建一个具有6个实例的Redis集群。

首先需要**下载该项目至服务器**：

```bash
git clone https://github.com/ucloud/redis-cluster-operator.git
cd redis-cluster-operator
```

之后**创建对应的CRD**，然后就可以通过这些CRD声明一个创建Redis集群的YAML文件：

<pre class="language-properties" data-overflow="wrap"><code class="lang-properties">kubectl create -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml
<strong>kubectl create -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml
</strong></code></pre>

假设需要将Redis集群部署至**redis-cluster**命名空间，需要**先创建redis-cluster命名空间和对应的一些权限，并且将Operator控制器安装至该命名空间**：

```properties
kubectl create ns redis-cluster
kubectl create -f deploy/service_account.yaml -n redis-cluster
kubectl create -f deploy/namespace/role.yaml -n redis-cluster
kubectl create -f deploy/namespace/role_binding.yaml -n redis-cluster
kubectl create -f deploy/namespace/operator.yaml -n redis-cluster
```

查看Redis **Operator**的Pod状态：

```properties
$ kubectl get pod -n redis-cluster
```

接下来通过一个自定义类型的YAML文件一键式启动一个三主三从的节点：

<pre class="language-properties" data-overflow="wrap"><code class="lang-properties"><strong>$ kubectl apply -f deploy\example\redis.kun_v1alpha1_distributedrediscluster_cr.yaml
</strong></code></pre>

创建完成后，可以通过查看自定义**distributedrediscluster**资源的状态来判断Redis集群是否完成了初始化：

```
$ kubectl get distributedrediscluster -n redis-cluster
```

**当状态由Scaling变成了Healthy（此过程可能需要30分钟左右的时间），即表示Redis集群已经创建完成，并完成了槽的配置，此时已经可以正常使用**：

```properties
$ kubectl get distributedrediscluster -n redis-cluster
```

在创建Redis集群后，会创建如下几个Service：

```properties
$ kubectl get svc -n redis-cluster
```

其中example-distributedrediscluster为程序端用来操作Redis集群的Service名称，可以通过redis\://example-distributedrediscluster.redis-cluster:6379链接至该集群。

接下来，可以使用任意具有Redis客户端工具的Pod进行连接测试，比如使用drc-example-distributedrediscluster-0-0这个Pod进行测试，首先登录至该Pod：

```properties
kubectl exec -it drc-example-distributedrediscluster-0-0 -n redis-cluster -- sh
```

```properties
# redis-cli -h example-distributedrediscluster
example-distributedrediscluster:6379> info
```

{% hint style="info" %}
当访问的key不在当前redis节点上时，redis服务器会返回一个Moved响应给到客户端，之后客户端连接Moved响应中的地址对应的redis服务器，在k8s中，Moved响应中返回的地址是Pod的IP。

<mark style="color:orange;">**在Kubernetes集群外部的客户端无法正常使用Kubernetes中的Redis集群，因为在它需要MOVED时，会返回一个Pod的IP，这个IP为Kubernetes内部IP。**</mark>

标准的Kubernetes不会让外部网络和其直接通信，如果需要集群外部连接至Kubernetes内部的Redis集群，可以采用Redis集群的代理工具进行解决
{% endhint %}

## 一键扩容

在自定义资源的配置中，参数**masterSize**决定了该集群一共有多少个Master节点，**clusterReplicas**参数决定了每个Master节点有几个从节点。

**如果想要扩容该集群，只需要更改这两个参数即可**。

比如将集群扩容为具有4个Master节点的集群：

```properties
# 首先更改该文件的masterSize
# 然后执行replace命令
kubectl replace -f deploy\example\redis.kun_v1alpha1_distributedrediscluster_cr.yaml
```

再次查看Pod，会发现创建了两个新的Pod：

```
$ kubectl get pod -n redis-cluster
```

此时集群的状态又会变成**Scaling**状态：

```
$ kubectl get distributedrediscluster 

```

> ## 注意
>
> <mark style="color:orange;">**在扩容和缩容的过程中，Redis集群是不可用状态**</mark>，所以最好在非业务时间段进行扩容。

## 集群清理

如果只是用来测试和学习，在操作完成后可以清理该集群，清理步骤较传统架构简单，只需要将安装小节的create改成delete即可：

<pre><code>kubectl delete -f deploy\example\redis.kun_v1alpha1_distributedrediscluster_cr.yaml
<strong>
</strong><strong>kubectl delete -f deploy/namespace/operator.yaml -n redis-cluster
</strong>kubectl delete -f deploy/namespace/role_binding.yaml -n redis-cluster
kubectl delete -f deploy/namespace/role.yaml -n redis-cluster
kubectl delete -f deploy/service_account.yaml -n redis-cluster
kubectl delete ns redis-cluster
<strong>
</strong>kubectl delete -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml
<strong>kubectl delete -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml
</strong></code></pre>
