网易数帆基于 Kubernetes 的 Redis 云原生实践

InfoQ 2021-01-21 10:48:03
网易 redis Kubernetes 实践 原生


{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"随着云原生时代的到来和Kubernetes(简称K8s)的日渐成熟,越来越多的互联网团队开始将Kubernetes作为新的重要基础设施,一些云计算厂商也将其视作云服务及应用交付的新底座。在大家的普遍认知里,Kubernetes是一个容器编排系统,擅长无状态的应用部署管理,在微服务领域起到了重要作用。由于容器对外部基础环境的不感知和状态易失的特性,与有状态应用的管理似乎有天然的矛盾。Operator就是“有状态应用容器化”的一个优雅的解决方案,本文将介绍网易数帆旗下的轻舟中间件基于Operator的Redis容器化实践。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Redis、Kubernetes预备知识"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/b7\/f6\/b707529f8c8c8f101d2a6e98a35c13f6.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Redis是基于Key-Value的缓存数据库,具有高性能、数据结构丰富、原生的高可用和分布式支持等特点,是目前业界使用最广泛的缓存中间件之一。从Redis 3.0版本开始,推出了"},{"type":"text","marks":[{"type":"strong"}],"text":"Redis Cluster"},{"type":"text","text":"这一原生的、集群自治的分布式解决方案,支持"},{"type":"text","marks":[{"type":"strong"}],"text":"在线水平扩缩容"},{"type":"text","text":"和"},{"type":"text","marks":[{"type":"strong"}],"text":"故障自动转移"},{"type":"text","text":",帮助我们突破了单机内存、并发、流量的瓶颈。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kubernetes是Google开源的容器编排管理系统,是Google多年大规模容器管理技术Borg的开源版本,主要功能有:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于容器的应用部署、维护和滚动升级"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡和服务发现"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跨机器和跨地区的集群调度"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自动伸缩"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"无状态服务和有状态服务"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"插件机制保证扩展性"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面简要介绍几种Kubernetes中的几种常用的"},{"type":"text","marks":[{"type":"strong"}],"text":"Kubernetes 核心组件"},{"type":"text","text":"和"},{"type":"text","marks":[{"type":"strong"}],"text":"资源对象"},{"type":"text","text":",便于下文的理解。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先是几个本文中要多次提及的Kubernetes基础组件:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Controller Manager"},{"type":"text","text":":"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kubernetes中的大多资源对象都有对应的"},{"type":"text","marks":[{"type":"strong"}],"text":"控制器"},{"type":"text","text":"(Controller),他们合在一起组成了kube-controller-manager。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在机器人技术和自动化领域,控制回路(Control Loop)是一个非终止回路,用于调节系统状态。生活中最典型的控制回路的例子:空调。比如我们设置了温度,相当于告诉了空调我们的期望状态,房间的实际温度是当前状态,通过对设备的各种控制,空调使温度接近于我们的期望。控制器通过控制回路将Kubernetes内的资源调整为声明式API对象期望的状态。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"ETCD"},{"type":"text","text":":"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是CoreOS基于Raft开发的分布式Key-Value存储,保存了整个Kubernetes集群的状态,可用于服务发现和一致性保证(比如选主)。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Etcd被部署为一个集群,几个节点的通信由Raft算法处理。在生产环境中,集群包含奇数个节点,并且至少需要三个,通常部署在Kubernetes Master上。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"API Server"},{"type":"text","text":":"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提供了资源操作的唯一入口,并提供认证、授权、访问控制、API 注册和发现等机制。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它是其他模块之间的数据交互和通信的枢纽(其他模块通过API Server查询或修改数据,只有API Server才直接操作ETCD)。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Kubelet"},{"type":"text","text":":"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负责维护容器的生命周期,同时也负责 持久卷(CVI)和网络(CNI)的 管理。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每个Node节点上都运行一个Kubelet服务进程,默认监听10250端口,接收并执行Master发来的指令,管理Pod及Pod中的容器。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面介绍一些常用的资源对象:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Pod"},{"type":"text","text":":Pod是一组紧密关联的容器集合,是Kubernetes调度的基本单位。多个容器在一个Pod中共享网络和文件系统,Pod和容器的概念和关系有些类似于主机和进程。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Node"},{"type":"text","text":":Node是Pod真正运行的主机,可以是物理机,也可以是虚拟机,Node上需要运行着container runtime(比如Docker)、kubelet和kube-proxy服务。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"StatefulSet"},{"type":"text","text":":为了解决有状态服务的问题(对应 Deployments 和 ReplicaSets 是为无状 态服务而设计),通过稳定的存储、网络标识、Pod编序等功能来支持有状态应用。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Deployment"},{"type":"text","text":":是一种更高阶资源,用千部署应用程序并以声明的方式升级应用,替代和ReplicationController来更方便的管理应用。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"ConfigMap"},{"type":"text","text":":用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置 文件,在Kubernetes的使用中我们通过ConfigMap实现应用和配置分离。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Service"},{"type":"text","text":":Kubernetes中用于容器的服务发现和负载均衡的资源对象,分为ClusterIP(生成Kubernetes集群内的虚拟IP作为入口)、NodePort(在ClusterIP基础上在每台机器上绑定一个端口暴露,使得能通过机器IP加该端口访问)、LoadBalancer等。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"整体Kubernetes架构如下图所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/77\/e2\/7786145ae8681304e3be7733ff8928e2.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"从传统主机到Kubernetes,从无状态到有状态"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在介绍Kubernetes Operator之前,我们先来分析一下传统的有状态应用部署方式所存在的问题。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以前当开发者想要在物理机或云主机上部署Redis、Kafka等有状态应用,并且对于这些应用的集群有一定程度的运维控制能力时,往往需要编写一套复杂的管理脚本、或者开发一个拥有诸多依赖的Web管控服务。站在普通使用者的角度,他们不得不为此学习与本身业务开发无关的运维知识。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果是以脚本和运维文档的方式沉淀,缺乏标准化的管理会使得运维的学习成本和使用门槛随着使用过程中的修改而急速升高;而使用管控服务,则需要引入更多的底层依赖去满足管控服务与主机侧的交互的虚拟化和管理设施,云计算厂商许多都使用了OpenStack作为基础设施管理平台,严格来说,OpenStack和Kubernetes不属于同一层面的框架,前者更多是属于IaaS层,更多是面向基础设施做虚拟化,Kubernetes则更偏向上层的应用容器化编排管理。在实际使用中,我们认为自行组织OpenStack对于小规模用户私有云的使用需求,有些过于沉重了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"于是我们将目光投向"},{"type":"text","marks":[{"type":"strong"}],"text":"对基础设施关注更少、自动化程度更高"},{"type":"text","text":"的Kubernetes。我们都知道Kubernetes在无状态应用部署管理,尤其是微服务领域,已经大放异彩。例如管理一个无状态的Web服务,我们可以使用Kubernetes的Deployment部署多副本并且进行弹性伸缩和滚动升级,然后使用Sevice进行负载均衡,依靠Kubernetes原生的资源对象基本上可以覆盖无状态服务的整个生命周期管理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然而,对于Redis、Kafka这类“有状态”的应用,Kubernetes似乎并没有准备好接纳它们。首先我们总结一下有状态应用的两个重要的特点:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"对"},{"type":"text","marks":[{"type":"strong"}],"text":"外部存储或网络"},{"type":"text","text":"有依赖,比如Kafka、TiDB每个副本都需要稳定的存储卷。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"实例之间有"},{"type":"text","marks":[{"type":"strong"}],"text":"拓扑关系"},{"type":"text","text":",比如Redis Cluster中存在主从关系和数据路由分布。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这就给Kubernetes带来了挑战,如果仍使用Deployment和ReplicaSet这些对象,Pod在故障时,对于Redis Cluster无法只是简单地重启Pod就能恢复到健康的集群状态,同理Kafka的Pod也会“忘记”自己所使用的存储卷。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kubernetes推出了StatefulSet这一资源对象旨在解决有状态应用的管理问题,允许在StatefulSet中配置持久卷(PersistentVolume,简称PV)、稳定的网络标识,对其内部的Pod进行编号排序等。但是对于Redis Cluster这种拥有自治能力的集群,StatefulSet也显得不够灵活而且会与其自治能力有冲突。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Redis也想要声明式管理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kubernetes的自动化哲学有两个核心概念:"},{"type":"text","marks":[{"type":"strong"}],"text":"声明式API和控制器(controller)模式"},{"type":"text","text":"。比如我们声明一个三副本的Deployment提交给API Server,Kubernetes中负责Deployment的controller就会监视(Watch)它的变化,发现该Deployment的Pod数为零,对其进行调谐(Reconcile),创建三个Pod,使它达到我们所声明的状态。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这引发了我们的思考:是否可以将Redis视作像Deployment一样的资源对象进行声明式的管理,同样拥有一个controller对它进行调谐呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这是一个合理且强烈的需求,即资源对象和controller都是由我们自定义,由我们自行编写资源的生命周期和拓扑关系的管理。于是Operator应运而生,可以将其简单的理解为:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Operator = CRD(Custom Resources Definition)+ Custom Controller\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如今Operator在社区中已经非常火热,但我们在最初设计做调研时,发现社区的Redis Operator实现上虽简洁,但运维能力和我们作为云计算服务商所强调的风险掌控、兜底能力不足,于是我们NCR(Netease Cloud Redis)团队借鉴社区的经验开发了自己的Redis Operator,下面针对Redis Cluster模式的管理进行解读,总体架构如下图所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/8b\/30\/8b5dea060e10f6ec27a82026b1f72730.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Redis Operator自身采用Deployment进行三副本部署,通过ETCD选主。Redis Cluster每个分片内的两个Pod上的Redis实例为一主一从,每个分片由一个StatefulSet管理;Pod的调度策略由Kubernetes原生调度器和网易轻舟Kubernetes提供的扩展调度器共同保障,对于StatefulSet、Pod等原生资源对象的管理仍使用原生API。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"开始使用Redis Operator,首先我们需要提交一个属于Redis的资源定义(CRD),定义一个Redis集群所必要的规格描述(Specification),之后用户便可以提交CR(通常是以yaml文件的形式),并在CR的Spec中填写自己需要的规格信息后提交,剩下的工作,无论是"},{"type":"text","marks":[{"type":"strong"}],"text":"创建、弹性扩缩容、故障处理"},{"type":"text","text":",统统交给Redis Operator自动化调谐。下面是一个典型的CR实例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"apiVersion: ncr.netease.com\/v1alpha1\nkind: NcrCluster # 资源类型,在CRD中所定义\nmetadata:\n name: cluster-redis # CR名称\n namespace: ns # CR所在的namespace\nspec:\n availableZones: # 可用区列表,支持单、多AZ\n - azName: az1\n configFile: ncr-cluster-configmap # 默认的配置参数模板\n master: 3 # 分片数\n port: 6379 # 端口号\n version: redis:4.0.14 # Redis引擎版本\n resourceReqs: # K8s标准资源规格描述\n requests:\n cpu: 1\n memory: 2Gi\n limits:\n cpu: 2\n memory: 4Gi\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面简要分析一个Redis Cluster的CR提交后的Operator主干工作流程:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"Redis Cluster对应的Informer感知到CR的Add事件。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"对该CR的Spec进行校验,包括是否符合CRD中的校验规则以及Operator内置的校验逻辑。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"校验通过后,开始对该集群进行调谐(Reconcile),感知到CR的Status为空判断为新创建的Redis集群,进入创建流程。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"根据默认Redis配置模板生成引擎配置,生成一个该CR专用的引擎参数ConfigMap和一个调度ConfigMap,我们通过在调度层面的策略针对单、多机房做了高可用保障。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"创建NodePortService用于服务发现访问。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"为Redis Cluster每个分片创建StatefulSet并将CR中的资源规格传入,作为最终Pod的资源规格。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"待所有StatefulSet的Pod启动成功后,在每个Pod上运行Redis Server进程,并进行Redis Cluster的组建流程,如cluster meet和add slots等。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"完成后将CR的状态更新为Online,表明已完成创建,之后根据Infomer的机制,每30s会收到到Update事件,轮询式的进行Reconcile。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Operator,拥有运维知识的专家"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下图为Redis Operator的工作流程图,实际上Operator就是我们对Kubernetes API进行了扩展和自定义,整体的工作流程与原生内置的controller是一致的."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/f9\/57\/f9623fec2630d1bbf383e6fb56f8b757.jpg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那么这种设计理念有什么好处呢?借用Operator Framework官网的一句话:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"The goal of an Operator is to put operational knowledge into software."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即“Operator旨在将领域性的运维知识编写成代码融入其中”。这个理念会影响我们对Redis Operator的设计与开发。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"列举一个场景说明,假如我们有在物理机上手工部署的Redis Cluster集群其中一个Master实例故障,并发生了Failover,此时该实例处于宕机状态,如果是人工恢复我们需要做的工作大致为:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"判断分片是否正常工作,即分片内另一个节点是否已经接替成为Master。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"准备将该实例作为Slave重启,做操作的预检查。比如Master的流量很高时,需要更大的复制缓冲区,需要检查Master的内存空闲情况,或者CPU占用超过一定阈值时不做操作,待流量低峰时进行重启和复制。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"重启实例,加入集群,并且复制指定Master实例,持续检查复制状态直到稳定。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述每一步操作判断都涵盖了我们的Redis运维知识,根绝Operator的理念,我们应该将这些知识编写成代码。通过明确的指标去做判断,比如判断QPS低于5000、CPU使用低于60%,Master内存空闲大于2GB时允许自动重建修复,将故障自动恢复或调谐的能力交给Operator,极大地提高了自动化运维的程度,可以称Redis Operator为运行在Kubernetes上,拥有Redis运维知识和判断能力的“运维专家”。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"优势总结和落地情况"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最后总结一下Redis采用Operator方案做容器化的优势:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"降低运维成本,支持对更大规模Redis集群的管理。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"容器部署相对于传统主机部署在资源方面更加有弹性。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"调度控制实现反亲和更加简单,借助Kubernetes本身的调度器和Taint、Toleration等机制。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于Kubernetes的方案对基础设施耦合低,用户可以根据实际规模选择用物理机或云服务器部署。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前网易轻舟自研的Redis Operator提供的功能有:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"两种模式的Redis:Redis Cluster和Redis主从版(Sentinel管理)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"单、多AZ高可用部署支持"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"集群创建、删除"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"集群在线水平、垂直扩缩容,热配置更新"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"实例重启、重建的运维功能"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Prometheus监控和报警功能"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RDB数据冷备与恢复"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"时至今日,Redis Operator已经在网易云音乐线上环境自2019年底稳定运行至今,网易传媒、网易严选也在逐渐扩大线上的使用规模,得益于自动化运维的高效、资源成本的优化,相信Operator将成为成规模的有状态分布式应用容器化的标准。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://www.infoq.cn/article/XUC4BJijylD9ap86eUZG?utm_source=rss&utm_medium=article

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云