apiVersion: v1 kind: Namespace metadata: name: yay --- apiVersion: v1 kind: Pod metadata: name: app-that-uses-etcd namespace: yay spec: initContainers: - name: init-myservice image: busybox:1.28 command: ['sh', '-c', 'until nc -z -w 1 etcd-0.etcd 2379; do echo waiting for etcd-0.etcd; sleep 2; done;'] containers: - name: whatever image: ubuntu command: ["bash", "-c", "sleep 100000"] --- apiVersion: v1 kind: Service metadata: namespace: yay name: "etcd" labels: component: "etcd" annotations: service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" spec: clusterIP: None ports: - port: 2379 name: client - port: 2380 name: peer selector: component: "etcd" --- apiVersion: apps/v1 kind: StatefulSet metadata: namespace: yay name: "etcd" labels: component: "etcd" spec: serviceName: "etcd" selector: matchLabels: component: "etcd" replicas: 1 template: metadata: name: "etcd" labels: component: "etcd" spec: containers: - name: "etcd" image: quay.io/coreos/etcd:v3.3.9 ports: - containerPort: 2379 name: client - containerPort: 2380 name: peer env: - name: CLUSTER_SIZE value: "1" - name: SET_NAME value: "etcd" - name: TXN_OPS value: "512" - name: ETCDCTL_API value: "3" command: - "/bin/sh" - "-ecx" - | IP=$(hostname -i) for i in $(seq 0 $((${CLUSTER_SIZE} - 1))); do while true; do echo "Waiting for ${SET_NAME}-${i}.${SET_NAME} to come up" ping -W 1 -c 1 ${SET_NAME}-${i}.${SET_NAME} > /dev/null && break sleep 1s done done PEERS="" for i in $(seq 0 $((${CLUSTER_SIZE} - 1))); do PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SET_NAME}:2380" done # restore, comment out if not needed # This is the phase where you just start the container in order to copy the file in it {{{ #echo "waiting for restore to happen" #while [ ! -f /snapshot.db ]; do # sleep 10 #done #echo "do the restore!" #etcdctl snapshot restore snapshot.db \ #--name ${HOSTNAME} \ #--initial-cluster ${PEERS} \ #--initial-cluster-token etcd-cluster-1 \ #--initial-advertise-peer-urls http://${HOSTNAME}.${SET_NAME}:2380 \ #--data-dir /var/run/etcd/default.etcd # end of restore routine }}} # start etcd. If cluster is already initialized the --initial-* options will be ignored. exec etcd --name ${HOSTNAME} \ --listen-peer-urls http://${IP}:2380 \ --listen-client-urls http://${IP}:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://${HOSTNAME}.${SET_NAME}:2379 \ --initial-advertise-peer-urls http://${HOSTNAME}.${SET_NAME}:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster ${PEERS} \ --initial-cluster-state new \ --auto-compaction-retention=1 \ --max-txn-ops ${TXN_OPS} \ --data-dir /var/run/etcd/default.etcd