shardingsphere-proxy

by upstreamyard

Helm chartv0.1.4App 5.5.3Apache-2.0Verified publisher

Apache ShardingSphere-Proxy, the database proxy for sharding, read/write splitting, encryption and masking in front of MySQL and PostgreSQL. Community chart by upstreamyard, not an official Apache ShardingSphere chart.

About this chart

ShardingSphere-Proxy Helm chart

Have any questions, feedback or need help (e.g. setup, hardened images, etc.)? Reach out to [email protected]

Helm chart for Apache ShardingSphere-Proxy, the database proxy that adds sharding, read/write splitting, encryption and masking in front of MySQL and PostgreSQL. Applications connect to it like to a normal MySQL or PostgreSQL server. It uses the image upstreamyard/shardingsphere-proxy, which includes the MySQL JDBC driver.

Community chart by upstreamyard, not an official Apache ShardingSphere chart. Apache ShardingSphere is licensed under Apache-2.0; the image also contains MySQL Connector/J (GPL-2.0 with the Universal FOSS Exception). Apache, Apache ShardingSphere and ShardingSphere are trademarks of the Apache Software Foundation.

Install

Write your users and logical databases into a values file:

# my-values.yaml
users:
  - user: admin@%
    password: change-me
    admin: true            # may run DistSQL, ShardingSphere's admin SQL
  - user: app@%
    password: change-me-too
databases:
  sharding_db: |
    dataSources:
      ds_0:
        url: jdbc:mysql://mysql-0.example:3306/ds_0?sslMode=REQUIRED
        username: shardingsphere
        password: secret
      ds_1:
        url: jdbc:mysql://mysql-1.example:3306/ds_1?sslMode=REQUIRED
        username: shardingsphere
        password: secret
    rules:
      - !SHARDING
        tables:
          t_order:
            actualDataNodes: ds_${0..1}.t_order
            databaseStrategy:
              standard:
                shardingColumn: user_id
                shardingAlgorithmName: by_user_id
        shardingAlgorithms:
          by_user_id:
            type: INLINE
            props:
              algorithm-expression: ds_${user_id % 2}
helm repo add upstreamyard https://upstreamyard.github.io/helm-charts
helm install shardingsphere-proxy upstreamyard/shardingsphere-proxy -f my-values.yaml

Or from the OCI registry:

helm install shardingsphere-proxy oci://ghcr.io/upstreamyard/charts/shardingsphere-proxy -f my-values.yaml

Applications then connect to the Service on port 3307 as user app, database sharding_db, with a MySQL client or driver. With PostgreSQL backends, they use a PostgreSQL client or driver instead.

Verifying the chart

Chart packages are PGP-signed. Import the public key (fingerprint 9B416F989B7E6C65F7F3BFAE7689688B20EC7813) and install with --verify:

curl -s https://upstreamyard.github.io/helm-charts/pgp_keys.asc | gpg --dearmor > upstreamyard.gpg
helm install shardingsphere-proxy upstreamyard/shardingsphere-proxy --verify --keyring upstreamyard.gpg -f my-values.yaml

The OCI copy on GHCR is signed with cosign instead (see the repository README).

Requirements

  • Your backend databases: MySQL, MariaDB or PostgreSQL (tested: MySQL 8.4 and, with the image, MariaDB 11.4 and PostgreSQL 17). This chart does not deploy them.
  • For more than one replica: ZooKeeper (tested: 3.9). This chart does not deploy it.

What the chart does

  • Configuration: the chart writes global.yaml (users, mode, props, globalConfig) and one database-<name>.yaml per entry in databases into a Secret, and mounts it at /opt/shardingsphere-proxy/conf. The entries in databases are text, because ShardingSphere's rules use YAML tags like !SHARDING. ShardingSphere's documentation describes data sources and all rules.
  • Users are required: without them, ShardingSphere accepts root/root as admin from any host, so the chart refuses to install without users (or existingSecret).
  • MySQL and MariaDB URLs need sslMode=REQUIRED (or VERIFY_IDENTITY). ShardingSphere turns TLS off by default, and MySQL 8 then refuses the login with Public Key Retrieval is not allowed. Use jdbc:mysql:// URLs for MariaDB servers too.
  • Probes: TCP on port 3307. The port opens once the configuration is loaded and all backend databases are connected. If a backend is unreachable at startup, the proxy exits and the container restarts until it is reachable.
  • Restarts on change: changing the values restarts the pods (checksum annotation). With existingSecret, restart them yourself (kubectl rollout restart).
  • Shutdown: on SIGTERM the proxy closes its client connections within a second, so the pod first waits preStopSleepSeconds (5) while it's removed from the Service. In our test, a rolling restart of two pods under a constant stream of new connections had no failed connection. Connections that are open when their pod stops are closed; clients and connection pools have to reconnect.
  • XA transactions: set globalConfig.transaction to {defaultType: XA, providerType: Atomikos}. In our test, a transaction across two MySQL databases committed on both and rolled back on both. Atomikos writes its transaction log to an emptyDir at /opt/shardingsphere-proxy/logs. The log survives container restarts but not a replaced pod, so Atomikos can't finish transactions that a deleted pod left in doubt. We haven't tested crash recovery.
  • Memory: the Java heap is 80% of the memory limit (2Gi by default).
  • Security: runs as UID 10001 with a read-only root filesystem (/tmp and logs are emptyDirs), drops all capabilities, and doesn't mount a ServiceAccount token.
  • helm test connects to the Service and, with MySQL backends, checks the proxy's greeting.

Standalone and Cluster mode

mode: Standalone (default)mode: Cluster
Replicas1any number; a PodDisruptionBudget keeps all but one running
NeedsnothingZooKeeper (cluster.zookeeper.serverLists)
Source of configurationthe values, on every startthe values on the first start; after that, ZooKeeper
Changes made with DistSQLlost when the pod restartskept in ZooKeeper and seen by all pods at once
replicaCount: 3
mode: Cluster
cluster:
  zookeeper:
    serverLists: zookeeper-0.zookeeper:2181,zookeeper-1.zookeeper:2181,zookeeper-2.zookeeper:2181

Changing the configuration in Cluster mode: once ZooKeeper holds a configuration (in cluster.namespace, by default the release's full name), ShardingSphere ignores the values completely. That includes users and their passwords, for which DistSQL has no command. You can change the configuration in two ways:

  • Rules and data sources: use DistSQL as an admin user (e.g. CREATE SHARDING TABLE RULE …). The changes apply to all pods at once and survive restarts.
  • Everything else, e.g. users: set a new cluster.namespace with helm upgrade. ShardingSphere then starts again from the values, and DistSQL changes from the old namespace are not carried over. During the rollout, old pods still serve the old configuration until they stop. The old namespace stays in ZooKeeper until you delete it.

Uninstalling and reinstalling a release with the same name reuses the old namespace and its configuration, so set a new cluster.namespace if you want a fresh start.

Your own configuration files

To manage the files yourself, put global.yaml and the database-*.yaml files (optionally logback.xml) into a Secret and set existingSecret. The chart then ignores users, mode, cluster, props, globalConfig and databases, and doesn't check them.

kubectl create secret generic proxy-config --from-file=conf/
helm install shardingsphere-proxy upstreamyard/shardingsphere-proxy --set existingSecret=proxy-config

Extra drivers and plugins

Jar files in /opt/shardingsphere-proxy/ext-lib are on the classpath, e.g. other JDBC drivers or your own sharding algorithms. An init container can download them into an emptyDir:

initContainers:
  - name: ext-lib
    image: busybox:1.37
    command: ["wget", "-q", "-O", "/ext-lib/my-plugin.jar", "https://example.com/my-plugin.jar"]
    volumeMounts:
      - { name: ext-lib, mountPath: /ext-lib }
extraVolumes:
  - name: ext-lib
    emptyDir: {}
extraVolumeMounts:
  - { name: ext-lib, mountPath: /opt/shardingsphere-proxy/ext-lib, readOnly: true }

Production example

# values-prod.yaml
replicaCount: 3
mode: Cluster
cluster:
  zookeeper:
    serverLists: zookeeper-0.zookeeper:2181,zookeeper-1.zookeeper:2181,zookeeper-2.zookeeper:2181
users:
  - user: admin@%
    password: <generated>
    admin: true
  - user: app@%
    password: <generated>
databases:
  sharding_db: |
    dataSources: ...
    rules: ...
service:
  type: LoadBalancer        # only if clients run outside the cluster
resources:
  requests:
    cpu: "2"
    memory: 4Gi
  limits:
    memory: 4Gi
topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: kubernetes.io/hostname
    whenUnsatisfiable: ScheduleAnyway
    labelSelector:
      matchLabels:
        app.kubernetes.io/name: shardingsphere-proxy

Keep the values file out of version control, or use existingSecret: it contains passwords.

Known issues

  • ShardingSphere 5.5.3 ships optional plugins without the libraries they need (Hive, Seata AT transactions, Narayana XA). The image removes them, so these features are not available. XA transactions with Atomikos work (below).
  • jdbc:mariadb:// URLs make ShardingSphere 5.5.3 fail at startup; use jdbc:mysql:// for MariaDB servers.

Values

KeyTypeDefaultDescription
affinityobject{}Affinity.
cluster.namespacestring""ZooKeeper namespace (root node) of this proxy cluster. Defaults to the release's full name. In Cluster mode, ShardingSphere uses these values only while the namespace is empty, i.e. on the first start; set a new namespace to apply changed values (see README).
cluster.propsobject{"maxRetries":3,"operationTimeoutMilliseconds":500,"retryIntervalMilliseconds":500,"timeToLiveSeconds":60}Further ZooKeeper repository properties.
cluster.zookeeper.serverListsstring""ZooKeeper servers, comma-separated, e.g. zookeeper-0.zookeeper:2181,zookeeper-1.zookeeper:2181. Required for mode: Cluster.
databasesobject{}Logical databases: name → content of database-<name>.yaml without databaseName (the chart adds it). Given as text, because ShardingSphere's rules use YAML tags like !SHARDING. Holds passwords, so the chart stores it in a Secret.
existingSecretstring""Name of an existing Secret with the complete configuration: global.yaml and database-*.yaml files (and optionally logback.xml). Replaces users, privilege, mode, cluster, props, globalConfig and databases.
extraEnvlist[]Extra environment variables, e.g. JVM_OPTS.
extraEnvFromlist[]Extra environment variables from Secrets or ConfigMaps.
extraVolumeMountslist[]Extra volume mounts for the proxy container. Jar files in /opt/shardingsphere-proxy/ext-lib are on the classpath.
extraVolumeslist[]Extra volumes for the pods.
fullnameOverridestring""Override the full resource name.
globalConfigobject{}Further top-level sections of global.yaml, e.g. transaction, sqlParser, sqlFederation, logging.
image.pullPolicystring"IfNotPresent"Image pull policy.
image.repositorystring"upstreamyard/shardingsphere-proxy"Image repository. upstreamyard's image includes the MySQL JDBC driver.
image.tagstring""Image tag. Defaults to the chart's appVersion.
imagePullSecretslist[]Secrets for pulling from a private registry.
initContainerslist[]Init containers, e.g. to put extra JDBC drivers or plugins into a volume mounted at /opt/shardingsphere-proxy/ext-lib.
livenessProbeobject{"failureThreshold":3,"periodSeconds":10,"tcpSocket":{"port":"proxy"},"timeoutSeconds":3}Liveness probe.
modestring"Standalone"Standalone (one pod; configuration from these values on every start) or Cluster (several pods sharing their configuration through ZooKeeper).
nameOverridestring""Override the chart name in resource names.
nodeSelectorobject{}Node selector.
podAnnotationsobject{}Extra pod annotations.
podDisruptionBudget.enabledbooltrueCreate a PodDisruptionBudget when replicaCount is greater than 1.
podDisruptionBudget.maxUnavailableint1Pods that may be unavailable during voluntary disruptions.
podLabelsobject{}Extra pod labels.
podSecurityContextobject{"fsGroup":10001,"runAsGroup":10001,"runAsNonRoot":true,"runAsUser":10001,"seccompProfile":{"type":"RuntimeDefault"}}Pod security context. The image runs as UID 10001.
preStopSleepSecondsint5Seconds to wait before the container gets SIGTERM, so the pod is removed from the Service first. The proxy then closes its client connections within a second. 0 disables it.
privilegeobject{"type":"ALL_PERMITTED"}Privilege provider (authority.privilege). ALL_PERMITTED gives every user access to every logical database; DATABASE_PERMITTED limits users through props.user-database-mappings.
propsobject{}Properties (props in global.yaml), e.g. sql-show: true, proxy-frontend-max-connections. The port is set by the chart (3307).
readinessProbeobject`{"failureThreshold":3,"peri

Deploy shardingsphere-proxy with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has shardingsphere-proxy on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartshardingsphere-proxy is pinned at v0.1.4 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply