Skip to content

Instantly share code, notes, and snippets.

@alaypatel07
Last active November 15, 2019 00:46
Show Gist options
  • Save alaypatel07/32c6206da9c1361f811d50c78800c547 to your computer and use it in GitHub Desktop.
Save alaypatel07/32c6206da9c1361f811d50c78800c547 to your computer and use it in GitHub Desktop.
defrag-cluster-etcd.sh
#!/usr/bin/env bash
ETCD_IMAGE=$(oc get configmap -n openshift-machine-config-operator machine-config-operator-images -oyaml | grep etcd | awk '{print $2}')
ETCD_IMAGE=${ETCD_IMAGE::-1}
oc create -f - <<EOF
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: defrag-cluster-etcd
namespace: openshift-etcd
spec:
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
hostNetwork: true
containers:
- command: ["/usr/bin/sh"]
args:
- "-c"
- |
set -euo pipefail;
export ETCDCTL_API=3;
export ETCDCTL_CACERT=/etc/ssl/etcd/ca.crt ETCDCTL_CERT=$(find /etc/ssl/ -name *peer*crt) ETCDCTL_KEY=$(find /etc/ssl/ -name *peer*key)
export ETCDCTL_ENDPOINTS=https://127.0.0.1:2379;
for m in $(etcdctl member list);
do [[ $m =~ "2379" ]] && echo "Defragmenting endpoint $m" && etcdctl --endpoints=$m defrag;
sleep 10;
done
image: "$ETCD_IMAGE"
name: defrag
volumeMounts:
-
mountPath: /etc/ssl/etcd/
name: certs
nodeSelector:
node-role.kubernetes.io/master: ""
restartPolicy: OnFailure
tolerations:
-
effect: NoSchedule
key: node-role.kubernetes.io/master
operator: Exists
-
effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 120
-
effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 120
-
effect: NoSchedule
key: node-role.kubernetes.io/etcd
operator: Exists
volumes:
-
hostPath:
path: /etc/kubernetes/static-pod-resources/etcd-member
name: certs
schedule: "* * */1 * *"
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment