You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.7 KiB

  1. ---
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. annotations:
  6. service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
  7. name: counter
  8. labels:
  9. app: counter
  10. spec:
  11. ports:
  12. - port: 26656
  13. name: p2p
  14. - port: 26657
  15. name: rpc
  16. clusterIP: None
  17. selector:
  18. app: tm
  19. ---
  20. apiVersion: v1
  21. kind: ConfigMap
  22. metadata:
  23. name: tm-config
  24. data:
  25. seeds: "tm-0,tm-1,tm-2,tm-3"
  26. validators: "tm-0,tm-1,tm-2,tm-3"
  27. validator.power: "10"
  28. genesis.json: |-
  29. {
  30. "genesis_time": "2016-02-05T23:17:31.164Z",
  31. "chain_id": "chain-B5XXm5",
  32. "validators": [],
  33. "app_hash": ""
  34. }
  35. pub_key_nginx.conf: |-
  36. server {
  37. listen 80 default_server;
  38. listen [::]:80 default_server ipv6only=on;
  39. location /pub_key.json { root /usr/share/nginx/; }
  40. }
  41. ---
  42. apiVersion: policy/v1beta1
  43. kind: PodDisruptionBudget
  44. metadata:
  45. name: tm-budget
  46. spec:
  47. selector:
  48. matchLabels:
  49. app: tm
  50. minAvailable: 2
  51. ---
  52. apiVersion: apps/v1beta1
  53. kind: StatefulSet
  54. metadata:
  55. name: tm
  56. spec:
  57. serviceName: counter
  58. replicas: 4
  59. template:
  60. metadata:
  61. labels:
  62. app: tm
  63. annotations:
  64. pod.beta.kubernetes.io/init-containers: '[{
  65. "name": "tm-gen-validator",
  66. "image": "tendermint/tendermint:0.10.0",
  67. "imagePullPolicy": "IfNotPresent",
  68. "command": ["bash", "-c", "
  69. set -ex\n
  70. if [ ! -f /tendermint/priv_validator.json ]; then\n
  71. tendermint gen_validator > /tendermint/priv_validator.json\n
  72. # pub_key.json will be served by pub-key container\n
  73. cat /tendermint/priv_validator.json | jq \".pub_key\" > /tendermint/pub_key.json\n
  74. fi\n
  75. "],
  76. "volumeMounts": [
  77. {"name": "tmdir", "mountPath": "/tendermint"}
  78. ]
  79. }]'
  80. spec:
  81. containers:
  82. - name: tm
  83. imagePullPolicy: IfNotPresent
  84. image: tendermint/tendermint:0.10.0
  85. ports:
  86. - containerPort: 26656
  87. name: p2p
  88. - containerPort: 26657
  89. name: rpc
  90. env:
  91. - name: SEEDS
  92. valueFrom:
  93. configMapKeyRef:
  94. name: tm-config
  95. key: seeds
  96. - name: VALIDATOR_POWER
  97. valueFrom:
  98. configMapKeyRef:
  99. name: tm-config
  100. key: validator.power
  101. - name: VALIDATORS
  102. valueFrom:
  103. configMapKeyRef:
  104. name: tm-config
  105. key: validators
  106. - name: TMHOME
  107. value: /tendermint
  108. command:
  109. - bash
  110. - "-c"
  111. - |
  112. set -ex
  113. # copy template
  114. cp /etc/tendermint/genesis.json /tendermint/genesis.json
  115. # fill genesis file with validators
  116. IFS=',' read -ra VALS_ARR <<< "$VALIDATORS"
  117. fqdn_suffix=$(hostname -f | sed 's#[^.]*\.\(\)#\1#')
  118. for v in "${VALS_ARR[@]}"; do
  119. # wait until validator generates priv/pub key pair
  120. set +e
  121. curl -s --fail "http://$v.$fqdn_suffix/pub_key.json" > /dev/null
  122. ERR=$?
  123. while [ "$ERR" != 0 ]; do
  124. sleep 5
  125. curl -s --fail "http://$v.$fqdn_suffix/pub_key.json" > /dev/null
  126. ERR=$?
  127. done
  128. set -e
  129. # add validator to genesis file along with its pub_key
  130. curl -s "http://$v.$fqdn_suffix/pub_key.json" | jq ". as \$k | {pub_key: \$k, amount: $VALIDATOR_POWER, name: \"$v\"}" > pub_validator.json
  131. cat /tendermint/genesis.json | jq ".validators |= .+ [$(cat pub_validator.json)]" > tmpgenesis && mv tmpgenesis /tendermint/genesis.json
  132. rm pub_validator.json
  133. done
  134. # construct seeds
  135. IFS=',' read -ra SEEDS_ARR <<< "$SEEDS"
  136. seeds=()
  137. for s in "${SEEDS_ARR[@]}"; do
  138. seeds+=("$s.$fqdn_suffix:26656")
  139. done
  140. seeds=$(IFS=','; echo "${seeds[*]}")
  141. tendermint node --p2p.seeds="$seeds" --moniker="`hostname`" --proxy-app="unix:///socks/app.sock"
  142. volumeMounts:
  143. - name: tmdir
  144. mountPath: /tendermint
  145. - mountPath: /etc/tendermint/genesis.json
  146. name: tmconfigdir
  147. subPath: genesis.json
  148. - name: socksdir
  149. mountPath: /socks
  150. - name: app
  151. imagePullPolicy: IfNotPresent
  152. image: golang:latest
  153. command:
  154. - bash
  155. - "-c"
  156. - |
  157. set -ex
  158. go get github.com/tendermint/tendermint/abci/cmd/abci-cli
  159. rm -f /socks/app.sock # remove old socket
  160. abci-cli counter --serial=true --address="unix:///socks/app.sock"
  161. volumeMounts:
  162. - name: socksdir
  163. mountPath: /socks
  164. - name: pub-key
  165. imagePullPolicy: IfNotPresent
  166. image: nginx:latest
  167. ports:
  168. - containerPort: 80
  169. name: pub-key
  170. command:
  171. - bash
  172. - "-c"
  173. - |
  174. set -ex
  175. # fixes 403 Permission Denied (open() "/tendermint/pub_key.json" failed (13: Permission denied))
  176. # => we cannot serve from /tendermint, so we copy the file
  177. mkdir -p /usr/share/nginx
  178. cp /tendermint/pub_key.json /usr/share/nginx/pub_key.json
  179. nginx -g "daemon off;"
  180. volumeMounts:
  181. - name: tmdir
  182. mountPath: /tendermint
  183. - mountPath: /etc/nginx/conf.d/pub_key.conf
  184. name: tmconfigdir
  185. subPath: pub_key_nginx.conf
  186. volumes:
  187. - name: tmconfigdir
  188. configMap:
  189. name: tm-config
  190. - name: socksdir
  191. emptyDir: {}
  192. volumeClaimTemplates:
  193. - metadata:
  194. name: tmdir
  195. annotations:
  196. volume.alpha.kubernetes.io/storage-class: anything
  197. spec:
  198. accessModes: ["ReadWriteOnce"]
  199. resources:
  200. requests:
  201. storage: 2Gi