etcd

by apecloud

Helm chartv1.2.0-alpha.2App 3.6.12Verified publisher

Etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines.

About this chart

etcd

etcd is a distributed, highly available key-value store designed to securely store data across a cluster of machines. It provides strong consistency guarantees, ensuring that data is reliably replicated and synchronized among all nodes. etcd is commonly used for configuration management, service discovery, and coordinating distributed systems. Its simplicity and robustness make it a critical component in cloud-native environments, particularly within Kubernetes for maintaining cluster state and configuration.

Features In KubeBlocks

Lifecycle Management

Horizontal scalingVertical scalingExpand volumeRestartStop/StartConfigureExposeSwitchover
YesYesYesYesYesYesYesYes

Backup and Restore

FeatureMethodDescription
Full Backupdatafileusing etcdcl snapshot save to create snapshot of the etcd cluster's data

Versions

Major VersionsDescription
3.5.x3.5.6,3.5.15

Prerequisites

  • Kubernetes cluster >= v1.21

  • kubectl installed, refer to K8s Install Tools

  • Helm, refer to Installing Helm

  • KubeBlocks installed and running, refer to Install Kubeblocks

  • ETCD Addon Enabled, refer to Install Addons

  • Create K8s Namespace demo, to keep resources created in this tutorial isolated:

    kubectl create ns demo
    

Examples

Create

Create an etcd cluster with three replicas, one leader and two followers.

# cat examples/etcd/cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: etcd-cluster
  namespace: demo
spec:
  # Specifies the behavior when a Cluster is deleted.
  # Valid options are: [DoNotTerminate, Delete, WipeOut] (`Halt` is deprecated since KB 0.9)
  # - `DoNotTerminate`: Prevents deletion of the Cluster. This policy ensures that all resources remain intact.
  # - `Delete`: Extends the `Halt` policy by also removing PVCs, leading to a thorough cleanup while removing all persistent data.
  # - `WipeOut`: An aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.
  terminationPolicy: Delete
  componentSpecs:
    - name: etcd
      componentDef: etcd
      # ServiceVersion specifies the version of the Service expected to be
      # provisioned by this Component.
      # Valid options are: [3.6.12,3.6.1,3.5.15,3.5.6]
      serviceVersion: 3.6.12
      # Determines whether metrics exporter information is annotated on the
      # Component's headless Service.
      # Valid options are [true, false]
      disableExporter: false
      # Specifies the desired number of replicas in the Component
      replicas: 3
      # Specifies the resources required by the Component.
      resources:
        limits:
          cpu: "0.5"
          memory: "0.5Gi"
        requests:
          cpu: "0.5"
          memory: "0.5Gi"
      # Specifies a list of PersistentVolumeClaim templates that define the storage
      # requirements for the Component.
      volumeClaimTemplates:
        # Refers to the name of a volumeMount defined in
        # `componentDefinition.spec.runtime.containers[*].volumeMounts
        - name: data
          spec:
            # The name of the StorageClass required by the claim.
            # If not specified, the StorageClass annotated with
            # `storageclass.kubernetes.io/is-default-class=true` will be used by default
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                # Set the storage size as needed
                storage: 20Gi

kubectl apply -f examples/etcd/cluster.yaml

Create with TLS Enabled

To create etcd cluster with TLS enabled,

# cat examples/etcd/cluster-with-tls.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: etcd-cluster-tls
  namespace: demo
spec:
  # Specifies the behavior when a Cluster is deleted.
  # Valid options are: [DoNotTerminate, Delete, WipeOut] (`Halt` is deprecated since KB 0.9)
  # - `DoNotTerminate`: Prevents deletion of the Cluster. This policy ensures that all resources remain intact.
  # - `Delete`: Extends the `Halt` policy by also removing PVCs, leading to a thorough cleanup while removing all persistent data.
  # - `WipeOut`: An aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.
  terminationPolicy: Delete
  componentSpecs:
    - name: etcd
      componentDef: etcd
      # A boolean flag that indicates whether the Component should use Transport
      # Layer Security (TLS)
      # for secure communication.
      # Valid options are: [true,false]
      tls: true   # set TLS to true
      issuer:     # if TLS is True, this filed is required.
        name: KubeBlocks  # set Issuer to [KubeBlocks, UserProvided].
      serviceVersion: 3.6.12
      replicas: 3
      resources:
        limits:
          cpu: "0.5"
          memory: "0.5Gi"
        requests:
          cpu: "0.5"
          memory: "0.5Gi"
      volumeClaimTemplates:
        - name: data
          spec:
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 20Gi
kubectl apply -f examples/etcd/cluster-with-tls.yaml

Compared to the default configuration, the only difference here is the tls and issuer fields in the cluster-with-tls.yaml file.

tls: true  # enable tls
issuer:    # set issuer, could be 'KubeBlocks' or 'UserProvided'
  name: KubeBlocks

By default, the issuer is set to KubeBlocks, which means KubeBlocks will generate the certificates for you and store it in a secret, <clusterName>-<componentName>-tls-certs. If you want to use your own certificates, you can set the issuer to UserProvided and provide the certificates in the secretRef field.

Certifications are mounted to path '/etc/pki/tls' by default. To check how secrets will be mounted, you may check the TLS field in ComponentDefinition:

kubectl get cmpd <cmpdName> -oyaml | yq '.spec.tls'
caFile: ca.pem
certFile: cert.pem
keyFile: key.pem
mountPath: /etc/pki/tls
volumeName: tls

Here is a simple test to verify if TLS works.

  • login a read/write ETCD pod (with role=leader)
kubectl get po  -n demo -l kubeblocks.io/role=leader,apps.kubeblocks.io/component-name=etcd
kubectl exec -n demo -it <podName> -- /bin/bash
  • put values
etcdctl \
  --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/pki/tls/ca.pem \
  --cert=/etc/pki/tls/cert.pem \
  --key=/etc/pki/tls/key.pem \
  put foo bar
  • get values
etcdctl \
  --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/pki/tls/ca.pem \
  --cert=/etc/pki/tls/cert.pem \
  --key=/etc/pki/tls/key.pem \
  get foo
foo
bar

Create with LoadBalancer

Create an etcd cluster with LoadBalancer services for enhanced external accessibility and multi-cluster communication.

Ensure your Kubernetes cluster has a LoadBalancer provider configured:

  • Cloud providers: AWS ELB, Azure Load Balancer, GCP Cloud Load Balancing
  • On-premises: MetalLB, HAProxy, NGINX Ingress Controller
  • Other: Any compatible LoadBalancer implementation
  • Peer Service LoadBalancer: Enables etcd members to communicate across different networks or clusters
  • Client Service LoadBalancer: Provides a stable external endpoint for etcd client connections
  • High Availability: External load balancing ensures resilient access to the etcd cluster
# cat examples/etcd/cluster-with-lb.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: etcd-cluster-lb
  namespace: demo
spec:
  # Specifies the behavior when a Cluster is deleted.
  # Valid options are: [DoNotTerminate, Delete, WipeOut] (`Halt` is deprecated since KB 0.9)
  # - `DoNotTerminate`: Prevents deletion of the Cluster. This policy ensures that all resources remain intact.
  # - `Delete`: Extends the `Halt` policy by also removing PVCs, leading to a thorough cleanup while removing all persistent data.
  # - `WipeOut`: An aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.
  terminationPolicy: Delete
  componentSpecs:
    - name: etcd
      componentDef: etcd
      # ServiceVersion specifies the version of the Service expected to be
      # provisioned by this Component.
      # Valid options are: [3.6.12,3.6.1,3.5.15,3.5.6]
      serviceVersion: 3.6.12
      # Determines whether metrics exporter information is annotated on the
      # Component's headless Service.
      # Valid options are [true, false]
      disableExporter: false
      # Specifies the desired number of replicas in the Component
      replicas: 3
      # Peer service configuration for etcd member communication
      services:
        - name: peer
          serviceType: LoadBalancer
          podService: true
      # Specifies the resources required by the Component.
      resources:
        limits:
          cpu: "0.5"
          memory: "0.5Gi"
        requests:
          cpu: "0.5"
          memory: "0.5Gi"
      # Specifies a list of PersistentVolumeClaim templates that define the storage
      # requirements for the Component.
      volumeClaimTemplates:
        # Refers to the name of a volumeMount defined in
        # `componentDefinition.spec.runtime.containers[*].volumeMounts
        - name: data
          spec:
            # The name of the StorageClass required by the claim.
            # If not specified, the StorageClass annotated with
            # `storageclass.kubernetes.io/is-default-class=true` will be used by default
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                # Set the storage size as needed
                storage: 20Gi
  # Client service configuration for external access
  services:
    - name: client
      serviceName: client
      spec:
        type: LoadBalancer
        ports:
          - port: 2379
            targetPort: 2379
      componentSelector: etcd

kubectl apply -f examples/etcd/cluster-with-lb.yaml

Horizontal scaling

Scale-out

Horizontal scaling out ETCD cluster by adding ONE more replica:

# cat examples/etcd/scale-out.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: etcd-scale-out
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: etcd-cluster
  type: HorizontalScaling
  # Lists HorizontalScaling objects, each specifying scaling requirements for a Component, including desired total replica counts, configurations for new instances, modifications for existing instances, and instance downscaling options
  horizontalScaling:
    # Specifies the name of the Component.
  - componentName: etcd
    # Specifies the replica changes for scaling in components
    scaleOut:
      # Specifies the replica changes for the component.
      # add one more replica to current component
      # only support change one replica at a time
      replicaChanges: 1

kubectl apply -f examples/etcd/scale-out.yaml

After applying the operation, you will see a new pod created and the cluster status goes from Updating to Running, and the newly created pod has a new role follower.

And you can check the progress of the scaling operation with following command:

kubectl describe ops -n demo etcd-scale-out

Scale-in

Horizontal scaling in etcd cluster by deleting ONE replica:

# cat examples/etcd/scale-in.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: etcd-scale-in
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: etcd-cluster
  type: HorizontalScaling
  # Lists HorizontalScaling objects, each specifying scaling requirements for a Component, including desired total replica counts, configurations for new instances, modifications for existing instances, and instance downscaling options
  horizontalScaling:
    # Specifies the name of the Component.
  - componentName: etcd
    # Specifies the replica changes for scaling in components
    scaleIn:
      # Specifies the replica changes for the component.
      # add one more replica to current component
      # only support change one replica at a time
      replicaChanges: 1

kubectl apply -f examples/etcd/scale-in.yaml

Scale-in/out using Cluster API

Alternatively, you can update the replicas field in the spec.componentSpecs.replicas section to your desired non-zero number.

# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1

Deploy etcd with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has etcd 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 chartetcd is pinned at v1.2.0-alpha.2 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