ShardingSphere Operator 实战指南
ShardingSphere on Cloud 子项目
Apache ShardingSphere on Cloud 是 Apache ShardingSphere 面向云上解决方案的子项目,覆盖了在 AWS、GCP、阿里云、华为云等云环境下面向虚机的自动化部署脚本,如 CloudFormation Stack 模板、Terraform 一键部署脚本等,以及在 Kubernetes 环境下的 Helm Charts、Operator、自动水平扩缩容等工具,未来会逐步覆盖云环境下的 ShardingSphere 高可用、可观测性、安全合规等方面的各类实践内容。
🔍 如果您想快速了解,验证或者使用 ShardingSphere Proxy 的功能特性,又没有 Kubernetes 环境的时候,您可以用 AWS CloudFormation 或者是 Terraform 来按需部署,详细操作说明可见:如何使用 Terraform 在 AWS 上创建 ShardingSphere Proxy 高可用集群?
🔍 如果您想在 kubernetes 环境中进行部署,您可以体验我们提供的 Operator 功能,或者不使用 Operator 而是借助 Helm Charts 直接安装 ShardingSphere Proxy。
更多详情请参考官方文档 :
官方文档 🔗 https://shardingsphere.apache.org/oncloud/ 为什么需要 ShardingSphere Operator
Kubernetes 提供了一种实现“平台之上的平台”的方式,即 Operator 模式。Operator 利用自定义的 CustomResourceDefinition,借助 Kubernetes 暴露的调谐(Reconcile)模型框架,方便开发者为指定应用快速实现一个声明式的自定义运维工具。
比如 Prometheus-Operator,Nats-Operator 等。Apache ShardingSphere 作为透明化的数据库增强引擎,理论上支持任何使用 MySQL、PostgreSQL、openGauss 协议的客户端进行接入,ShardingSphere Proxy 对异构语言、高可用场景更加友好。借助 Operator 可以让 ShardingSphere Proxy 在 kubernetes 上的部署和运维变得更加简单和高效。

ShardingSphere Operator 使用示例
在使用 ShardingSphere 之前,您需要准备一个或多个兼容的数据库作为存储节点。详细操作手册可以参考 Apache ShardingSphere on Cloud 子项目的官方文档,或者从 Apache ShardingSphere 官网入口进入:
🔗:https://shardingsphere.apache.org/oncloud/current/en/operation-guide/operator/ 
安装 ShardingSphere Operator
安装所需配置内容和配置文件目录为:
🔗 https://github.com/apache/shardingsphere-on-cloud/tree/main/charts/apache-shardingsphere-operator-charts为了方便 Operator 的安装,社区在源码安装外还提供了在线安装的方式。两种方式如下:
在线安装
 kubectl create ns shardingsphere-operator
 helm repo add shardingsphere https://apache.github.io/shardingsphere-on-cloud
 helm repo update
 helm install demo-release shardingsphere/apache-shardingsphere-operator-charts -n shardingsphere-operator操作结果示例:

注:源码安装请参考官方文档
此时通过 kubectl get crd 可以查看到已经向 kubernetes 集群中注入了 operator 将要操作的 crd:

以及查看部署好的 ShardingSphere-Operator:

部署 ShardingSphere-Proxy 集群
🔗 https://github.com/apache/shardingsphere-on-cloud/tree/main/charts/apache-shardingsphere-operator-charts#parameters 参阅如上链接,即可获取所有可被配置的参数选项。
提示:在运行 ShardingSphere-Proxy 前需要提供一个可以访问的 ZooKeeper 集群。想要运行一套 ShardingSphere-Proxy 需要编写两个 CustomResourceDefinition 文件:
- shardingsphereproxy.yaml
 - shardingsphereproxyserverconfigs.yaml
 
示例如下:
# shardingsphereproxy.yaml
apiVersion: shardingsphere.apache.org/v1alpha1
kind: ShardingSphereProxy
metadata:
  labels:
    app: shardingsphere-proxy
  name: shardingsphere-proxy
  namespace: shardingsphere-demo
spec:
  mySQLDriver:
    version: 5.1.47
  port: 3307
  proxyConfigName: shardingsphere-proxy-configuration
  replicas: 3
  serviceType:
    type: ClusterIP
  version: 5.3.0
---
# shardingsphereproxyserverconfigs.yaml
apiVersion: shardingsphere.apache.org/v1alpha1
kind: ShardingSphereProxyServerConfig
metadata:
  labels:
    app: shardingsphere-proxy
  name: shardingsphere-proxy-configuration
  namespace: shardingsphere-demo
spec:
  authority:
    privilege:
      type: ALL_PERMITTED
    users:
    - password: root
      user: root@%
  mode:
    repository:
      props:
        maxRetries: 3
        namespace: governance_ds
        operationTimeoutMilliseconds: 5000
        retryIntervalMilliseconds: 500
        server-lists: shardingsphere-proxy-zookeeper.shardingsphere:2181 # 此处为示例,根据实际部署进行修改
        timeToLiveSeconds: 600
      type: ZooKeeper
    type: Cluster
  props:
    proxy-frontend-database-protocol-type: MySQL操作示例:



然后可以检查 shardingsphere-demo 命名空间下的 Pod 运行状态:

也可以发现 operator 创建的相关资源:

配置 ShardingSphere-Proxy 实现数据加密
ShardingSphere 支持 DistSQL 帮助 DBA 快速上手存储节点注册、规则配置等操作,详情可查看:
🔗 https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/distsql/随着企业的数字化转型不断深化,越来越多的用户数据流动在各类业务之间。为保护用户数据,各个国家和地区陆续推出了对于数据安全相关的法规和框架,比如欧盟的 GDPR、我国的数据安全法等。Apache ShardingSphere 在数据安全方面可以提供数据加解密等多项能力,以下是实现数据加密的示例:
- 注册存储节点
 

- 创建加密规则
 

- 创建逻辑表
 

- 查看物理表属性
 

- 逻辑表插入和查询
 

- 数据源物理表查询
 

更多特性参考:
🔗 https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/distsql/usage/encrypt-rule/https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/distsql/usage/encrypt-rule/文章转载自公众号: ShardingSphere官微




















