-
Notifications
You must be signed in to change notification settings - Fork 38
/
componentdefinition.yaml
106 lines (103 loc) · 3.79 KB
/
componentdefinition.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
apiVersion: apps.kubeblocks.io/v1alpha1
kind: ComponentDefinition
metadata:
name: etcd-{{ .Chart.Version }}
labels:
{{- include "etcd.labels" . | nindent 4}}
spec:
serviceKind: etcd
serviceVersion: {{ .Chart.AppVersion }}
runtime:
containers:
- name: etcd
image: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}
imagePullPolicy: IfNotPresent
ports:
- containerPort: 2379
name: client
- containerPort: 2380
name: peer
volumeMounts:
- name: data
mountPath: /var/run/etcd
command:
- /bin/sh
- -c
- |
echo "start etcd..."
PEERS=""
MY_PEER=""
echo ${PEER_ENDPOINT}
if [ -z "${PEER_ENDPOINT}" ]; then
DOMAIN=$KB_NAMESPACE".svc{{ .Values.clusterDomain }}"
SUBDOMAIN=${KB_CLUSTER_COMP_NAME}-headless
replicas=$(eval echo ${KB_POD_LIST} | tr ',' '\n')
for replica in ${replicas}; do
host=${replica}.${SUBDOMAIN}.${DOMAIN}
PEERS="${PEERS}${replica}=http://${host}:2380,"
done
PEERS=${PEERS%,}
MY_PEER=$KB_POD_FQDN{{ .Values.clusterDomain }}
else
my_id=$(eval echo ${KB_POD_NAME} | grep -oE "[0-9]+\$")
endpoints=$(eval echo ${PEER_ENDPOINT} | tr ',' '\n')
for endpoint in ${endpoints}; do
host=$(eval echo ${endpoint} | cut -d ':' -f 1)
ip=$(eval echo ${endpoint} | cut -d ':' -f 2)
host_id=$(eval echo ${host} | grep -oE "[0-9]+\$")
hostname=${KB_CLUSTER_COMP_NAME}-${host_id}
PEERS="${PEERS}${hostname}=http://${ip}:2380,"
if [ "${my_id}" = "${host_id}" ]; then
MY_PEER=${ip}
fi
done
PEERS=${PEERS%,}
fi
echo "peers: ${PEERS}, my-peer: ${MY_PEER}"
exec etcd --name ${HOSTNAME} \
--experimental-initial-corrupt-check=true \
--listen-peer-urls http://0.0.0.0:2380 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://${MY_PEER}:2379 \
--initial-advertise-peer-urls http://${MY_PEER}:2380 \
--initial-cluster ${PEERS} \
--data-dir /var/run/etcd/default.etcd
services:
- name: peer
serviceName: peer
spec:
ports:
- name: peer
port: 2380
targetPort: peer
podService: true
disableAutoProvision: true
vars:
- name: PEER_ENDPOINT
valueFrom:
serviceVarRef:
name: peer
optional: true
host: Required
loadBalancer: Required
updateStrategy: BestEffortParallel
roles:
- name: leader
serviceable: true
writable: true
votable: true
- name: follower
serviceable: true
writable: false
votable: true
lifecycleActions:
roleProbe:
customHandler:
image: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}
exec:
command:
- |
Status=$(etcdctl --endpoints=127.0.0.1:2379 endpoint status -w simple --command-timeout=300ms --dial-timeout=100m) &&
IsLeader=$(echo $Status | awk -F ', ' '{print $5}') &&
IsLearner=$(echo $Status | awk -F ', ' '{print $6}') &&
if [ "true" = "$IsLeader" ]; then echo -n "leader"; elif [ "true" = "$IsLearner" ]; then echo -n "learner"; else echo -n "follower"; fi