ETCD is a key-value data store for storing kubernetes state and objects.
ETCDCTL is the tool used to back up with snapshot.
ETCDCTL a command line tool for interacting with the etcd server
Key Features:
- Keep the event history until compaction.
- access to old version of keys.
- user controlled key compaction.
- Support range query.
- pagination support with limit argument.
- support consistency guarantee across multiple queries.
- Replace TTL key with lease.
- Support watch feature.
ETCDTOOL Installation:
- Build binary from source code
- Download tool directly
Build binary from source code:
- Checkout the code repository
- git clone -b v3.4.16 https://github.com/etcd-io/etcd.git
- Change directory to etcd
- Run build script
- The binaries are under the bin directory.
- exportPATH="$PATH:`pwd`/bin"
- Check etcd version
Download ETCD tool directly:
- kubectl exec -it etcd-docker-desktop -n kube-system -- /bin/bash -c 'ETCDCTL_API=3 /usr/local/bin/etcd --version' | head
- export RELEASE='3.4.3'
- wget https://github.com/etcd-io/etcd/releases/download/v{RELEASE}/etcd-v{RELEASE}-linux-amd64.tar.gz
- cd etcd-v{RELEASE}-linux-amd64
- sudo cp etcdctl /usr/local/bin
ETCD pod running on kube-system namespace:
$kubectl get namespace
kube-system Active 11d
$kubectl get pods -n kube-system
etcd-docker-desktop 1/1 Running 8 (15m ago) 11d
Check out etcd configuration information.
$kubectl describe pod etcd-docker-desktop -n kube-system
Configuration for etcd comes from the static pod manifest
$sudo more /etc/kubernetes/manifests/etcd.yaml
--data-dir=/var/lib/etcd # is the path where etcd data is stored inside a pod.
Take ETCD Backup using ETCDCTL tool:
Set which api to user to fetch data from etcd and provide certificates and endpoints so that cluster authenticate etcdctl tool.
$export ETCDCTL_API=3 etcdctl --endpoints=https:/127.0.0.1:2379/ --cacert = /etc/kubernetes/pki/etcd/ca.cert --cert = /etc/kubernetes/pki/etcd/server.crt --key = /etc/kubernetes/pki/etcd/server.key snapshot save /var/lib/data-backup.db
To Validate whether backup is successful or not
export ETCDCTL_API=3 etcdctl --write-out=table snapshot status /var/lib/data-backup.db
Restoring etcd
- Delete corrupted etcd distributed db under /var/lib/etcd
- Stop etcd pod
- Move backup etcd db under /var/lib/etcd
- Kubelet will restart etcd pod.
Restore command
export ETCDCTL_API=3 etcdctl snapshot save <path to backup etcd db>
Practical :
Create secret :
$kubectl create secret generic test-secret --from-literal=username='dayananda' --from-literal=password='password'
Get secrets:
$kubectl get secrets
test-secrets secret stored in etcd datastore.
Define a variable for the endpoint to etcd:
$ENDPOINT="https://127.0.0.1:2379
Verify whether we are connecting to a right cluster using member list:
$sudo ETCDCTL_API=3 etcdctl --endpoints=$ENDPOINT --cacert=/etc/kubernetes/pki/etcd/ca.cert --cert=/etc/kubernetes/pki/etcd/server.crt --key = /etc/kubernetes/pki/etcd/srever.key member list
Backup etcd data:
$sudo ETCDCTL_API=3 etcdctl --endpoints=$ENDPOINT --cacert=/etc/kubernetes/pki/etcd/ca.cert --cert=/etc/kubernetes/pki/etcd/server.crt --key = /etc/kubernetes/pki/etcd/srever.key snapsot save /var/lib/etcd/data-backup.db
Check back up is valid or not:
$sudo ETCDCTL_API=3 etcdctl --write-out=table snapshot status /var/lib/etcd/data-backup.db
Delete the secret which we have created earlier and restore it back using etcdctl tool:
$kubectl delete secret test-secret
Run restore command which will restore test-secret
$sudo ETCDCTL_API=3 etcd snapshot restore /var/lib/etcd/data-backup.db
default.etcd copy will be created with restore copy
stop etcd containier, move deafualt.etcd to /var/lib/etcd/
sudo mv ./default.etcd /var/lib/etcd/
wait for etcd to get restarted.
Other way to restore is :
Restore to a specific directory using --data-dir
$sudo ETCDCTL_API=3 etcdctl snapshot restore /var/lib/data-backup.db --data-dir=/var/lib/etcd-restore
Update the pod manifest to point to /var/lib/etcd-restore
--data-dir=/var/lib/etcd-restore , mountPath under VolumeMounts and path under volumes
Some useful ETCD commands:
Command to set the value of key foo to bar:
$etcdctl put foo bar
OK
Command to set the value of key foo1 to bar1 for 10s.
$etcdctl put foo1 bar1 --lease=1234abcd
Command to get foo values
$etcd get foo
Command to print only key's value
$etcd get foo --print-value-only
Command to get all the keys which are prefixed with foo
$etcdctl get --prefix foo
Command to get keys revision of 4
$etcdctl get --prefix --rev=4
Command to delete key foo
$etcdctl del foo
Command to monitor foo
$etcdctl watch foo
Grant a lease with 60 second TTL
$etcdctl lease grant 60
lease 32695410dcc0ca06 granted with TTL(60s)
Attach key foo to lease 32695410dcc0ca06
$etcdctl put --lease=32695410dcc0ca06 foo bar
OK
Command to revoke the lease which in turn deletes all the associated keys.
$etcdctl lease revoke 32695410dcc0ca06
lease 32695410dcc0ca06 revoked
$ etcdctl get foo
#empty response since foo is deleted due to lease revocation.
Happy Learning😀
Comments
Post a Comment