Skip to content

Commit

Permalink
Local Kubernetes setup for BTC Node and Miner (#197)
Browse files Browse the repository at this point in the history
* - Added the script files for k8s deploy
- Added Docker build files for Bitcoin node and miner
- Added service, namespace, and deployment file for bitcoin
- Added wait-for-deployment.sh util script

* added minikube start and stop

* changed scripting files for up.sh and down.sh

* - fixed the utils for port-forwarding
- Added BTC Node k8s secret
- Added better deployment checking logic before port-forwarding

* - Fixed the Dockerfile of bitcoin container to have trailing dashes near COPY
- Split the regtest btc deployment and miner deployment into different entities
  • Loading branch information
SundarGowtham authored May 28, 2024
1 parent 35ceb78 commit 0277a7f
Show file tree
Hide file tree
Showing 23 changed files with 776 additions and 26 deletions.
29 changes: 3 additions & 26 deletions devenv/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,11 @@
cloud

# Local Kubernetes related
k8s
**/k8s/yamls/**
**/k8s/utils/**
**/k8s/custom-k8s-docker-builds/electrs/**
**/k8s/custom-k8s-docker-builds/electrs-blockstream/**
**/k8s/custom-k8s-docker-builds/electrs-romanz/**
**/k8s/custom-k8s-docker-builds/nakamoto-signer/**
**/k8s/custom-k8s-docker-builds/nakamoto-signer-init-container/**
**/k8s/custom-k8s-docker-builds/stacks/**
**/k8s/custom-k8s-docker-builds/stacks-init-container/**
**/k8s/custom-k8s-docker-builds/stacks-api/**
**/k8s/custom-k8s-docker-builds/stacks-explorer/**

**/k8s/custom-k8s-docker-builds/tests/**
**/k8s/custom-k8s-docker-builds/ref.md


# ------------------------------------------------------------

**/k8s/misc/**


**/k8s/build.sh
**/k8s/down.sh
**/k8s/up.sh
**/k8s/local-k8s-deploy.sh



nohup.out
# ------------------------------------------------------------


local-commands.sh
97 changes: 97 additions & 0 deletions devenv/local/k8s/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# LOCAL KUBERNETES INSTRUCTIONS


## [I] INSTALLATION

<details>

### Make sure you have the following installed:

- [1] Install [minikube](https://minikube.sigs.k8s.io/docs/)

https://minikube.sigs.k8s.io/docs/

```
brew install minikube
```

- [2] [Docker Desktop](https://docs.docker.com/desktop/install/mac-install/#system-requirements)

https://docs.docker.com/desktop/install/mac-install/#system-requirements

- [3] Install [`kubectl` & `kubectx`](https://github.com/ahmetb/kubectx) (Kubernetes utils)

https://github.com/ahmetb/kubectx

```
brew install kubectl
brew install kubectx
```

- [4] Set kube-context to point to `minikube`

```
kubectx minikube
```

#### OPTIONAL STEPS:
<details>

- **(OPTIONAL)** [5] Install `k9s` [Kubernetes CLI Viewer](https://k9scli.io/topics/install/)

https://k9scli.io/topics/install/

```
brew install k9s
```

- **(OPTIONAL)** [6] It is highly suggested to also add these aliases to your shell:

```
alias k='kubectl'
alias ka='kubectl apply -f'
alias kg='kubectl get'
alias kp='kubectl port-forward'
alias kd='kubectl delete'
alias kdr='kubectl describe'
alias kdf='kubectl delete -f'
```

</details>

</details>




## [II] DEPLOY

#### [i] Start Minikube:

##### `minikube start`

#### [ii] Build all containers:

##### `sh build.sh`

#### [iii] Deploy all K8s artifacts:

##### `sh up.sh`


#### [iv] Spin down all K8s artifacts:

##### `sh down.sh`


#### **(!! OPTIONAL !!)** [v] Delete Minikube Containers:

> Beware that this will permanently remove containers and it will take some time for the containers to be rebuilt again using `build.sh`
##### `sh remove-minikube-containers.sh`



#### [vi] Stop Minikube:

##### `minikube stop`
13 changes: 13 additions & 0 deletions devenv/local/k8s/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

echo "BUILDING K8S CONTAINERS"

echo "[1] SET KUBERNETES CONTEXT TO `minikube`\n\n"
kubectx minikube

echo "[2] SET THE DOCKER IMAGE REGISTRY TARGET TO MINIKUBE\n\n"
eval $(minikube docker-env)


docker build -t minikube/bitcoin:v1 ./custom-k8s-docker-builds/bitcoin/docker/
docker build -t minikube/bitcoin-miner-sidecar:v1 ./custom-k8s-docker-builds/bitcoin-miner-sidecar/docker/
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM alpine:latest
MAINTAINER Gowtham Sundar <[email protected]>

RUN apk add --no-cache curl jq

ARG BITCOIN_RPC_HOST
ARG BITCOIN_RPC_PORT
ARG MINER_ADDRESS
ARG INIT_BTC_BLOCKS
ARG BTC_BLOCK_GEN_TIME
ARG BTC_RPCPASSWORD
ARG BTC_RPCUSER
ARG BTC_LOG_DEBUG
ARG BTC_REST_ENABLE


ENV BITCOIN_RPC_HOST=$BITCOIN_RPC_HOST
ENV BITCOIN_RPC_PORT=$BITCOIN_RPC_PORT
ENV MINER_ADDRESS=$MINER_ADDRESS
ENV INIT_BTC_BLOCKS=$INIT_BTC_BLOCKS
ENV BTC_BLOCK_GEN_TIME=$BTC_BLOCK_GEN_TIME
ENV BTC_RPCPASSWORD=$BTC_RPCPASSWORD
ENV BTC_RPCUSER=$BTC_RPCUSER
ENV BTC_LOG_DEBUG=$BTC_LOG_DEBUG
ENV BTC_REST_ENABLE=$BTC_REST_ENABLE


COPY entrypoint.sh /bin/entrypoint.sh

RUN chmod a+x /bin/entrypoint.sh

ENTRYPOINT ["/bin/entrypoint.sh"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh
# Wait until bitcoin RPC is ready

RPC_ENDPOINT=http://$BITCOIN_RPC_HOST:$BITCOIN_RPC_PORT/


echo "checking if bitcoin node is online"

until curl -f -s -o /dev/null -u "$BTC_RPCUSER:$BTC_RPCPASSWORD" --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockcount", "params": []}' -H 'content-type: text/plain;' "$RPC_ENDPOINT"
do
echo "bitcoin node is not ready, sleep two seconds"
sleep 2
done
echo "bitcoin node is ready"

echo "--> Create a named 'legacy' wallet named '' -->"
curl -u "$BTC_RPCUSER:$BTC_RPCPASSWORD" --data-binary \
'{ "jsonrpc": "1.0", "id": "curltest", "method": "createwallet", "params": { "wallet_name":"", "descriptors":false }}' \
-H 'content-type: text/plain;' "$RPC_ENDPOINT"


echo "==> main bitcoin wallet created\n\n"


echo "--> Import miner address -->"
curl -u "$BTC_RPCUSER:$BTC_RPCPASSWORD" -d '{"jsonrpc":"1.0","id":"curltext","method":"importaddress","params":["'$MINER_ADDRESS'","",false]}' -H 'content-type:text/plain;' "$RPC_ENDPOINT"
echo "==> Miner address ($MINER_ADDRESS) has been imported\n\n"


echo "--> Mine the first N blocks -->"
curl -u "$BTC_RPCUSER:$BTC_RPCPASSWORD" --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "generatetoaddress", "params": ['$INIT_BTC_BLOCKS', "'$MINER_ADDRESS'"]}' -H 'content-type: text/plain;' "$RPC_ENDPOINT"
echo "=> mined initial blocks \n\n"


# Mine a single block every 10 seconds
while true
do
curl -u "$BTC_RPCUSER:$BTC_RPCPASSWORD" --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "generatetoaddress", "params": [1, "'$MINER_ADDRESS'"]}' -H 'content-type: text/plain;' "$RPC_ENDPOINT"
echo "mined a single block"
sleep ${BTC_BLOCK_GEN_TIME}
done
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker exec -it bitcoin bitcoin-cli -regtest -rpcuser=devnet -rpcpassword=devnet $@
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
FROM debian:stable-slim as builder
MAINTAINER Gowtham Sundar <[email protected]>

ARG VERSION=25.0


ARG BTC_NETWORK
ARG BTC_TXINDEX
ARG BTC_RPCUSER
ARG BTC_RPCPASSWORD
ARG BTC_PRINTTOCONSOLE
ARG BTC_DISABLEWALLET
ARG BTC_RPCBIND
ARG BTC_RPCALLOWIP
ARG BTC_RPC_PORT
ARG BTC_P2P_PORT

# Set ENVs so they persist after image is built
ENV BTC_NETWORK=$BTC_NETWORK
ENV BTC_TXINDEX=$BTC_TXINDEX
ENV BTC_RPCUSER=$BTC_RPCUSER
ENV BTC_RPCPASSWORD=$BTC_RPCPASSWORD
ENV BTC_PRINTTOCONSOLE=$BTC_PRINTTOCONSOLE
ENV BTC_DISABLEWALLET=$BTC_DISABLEWALLET
ENV BTC_RPCBIND=$BTC_RPCBIND
ENV BTC_RPCALLOWIP=$BTC_RPCALLOWIP
ENV BTC_RPC_PORT=$BTC_RPC_PORT
ENV BTC_P2P_PORT=$BTC_P2P_PORT

RUN apt-get update && apt-get install -y \
wget \
file \
&& rm -rf /var/lib/apt/lists/*

RUN file /bin/bash | grep -q x86-64 && echo x86_64-linux-gnu > /tmp/arch || true
RUN file /bin/bash | grep -q aarch64 && echo aarch64-linux-gnu > /tmp/arch || true
RUN file /bin/bash | grep -q EABI5 && echo arm-linux-gnueabihf > /tmp/arch || true

RUN wget https://bitcoincore.org/bin/bitcoin-core-${VERSION}/bitcoin-${VERSION}-$(cat /tmp/arch).tar.gz

RUN wget https://bitcoincore.org/bin/bitcoin-core-${VERSION}/SHA256SUMS

RUN cat SHA256SUMS | grep bitcoin-${VERSION}-$(cat /tmp/arch).tar.gz | sha256sum -c && \
mkdir /bitcoin && \
tar -xzvf bitcoin-${VERSION}-$(cat /tmp/arch).tar.gz -C /bitcoin --strip-components=1

FROM debian:stable-slim as runtime

RUN apt-get update && apt-get install -y \
ca-certificates \
coreutils \
nginx \
sudo \
curl \
jq && rm -rf /var/lib/apt/lists/*

COPY --from=builder /bitcoin/bin/* /usr/local/bin/

COPY default.conf /etc/nginx/conf.d/default.conf/

ADD entrypoint.sh /usr/local/bin
RUN chmod a+x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["entrypoint.sh"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
server {
listen 18433;
server_name bitcoin_cors;

location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, TRACE, PATCH' always;
add_header 'Access-Control-Allow-Headers' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' '0';
return 204;
}

add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, TRACE, PATCH' always;
add_header 'Access-Control-Allow-Headers' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;

proxy_redirect off;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:18443;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -x

nginx


# Make .bitcoin dir if not exists
DOT_BITCOIN_DIR="/root/.bitcoin"
if [ ! -d "$DOT_BITCOIN_DIR" ]; then
mkdir $DOT_BITCOIN_DIR
fi


# COPY the bitcoin.conf from the shared volume (from the init container to the DOT_BITCOIN_DIR)
SHARED_VOL_DIR="/mnt/shared"


mv "$SHARED_VOL_DIR/bitcoin.conf" "$DOT_BITCOIN_DIR"

bitcoind \
-regtest \
-txindex=${BTC_TXINDEX} \
-rpcuser=${BTC_RPCUSER} \
-rpcpassword=${BTC_RPCPASSWORD} \
-printtoconsole=${BTC_PRINTTOCONSOLE} \
-disablewallet=${BTC_DISABLEWALLET} \
-rpcallowip=${BTC_RPCALLOWIP} \
-rpcport=${BTC_RPC_PORT} \
-server=1 \
-conf=${DOT_BITCOIN_DIR}/bitcoin.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
echo "--- SET KUBERENTES CONTEXT TO `minikube` ---\n\n"
kubectx minikube

echo "--- SET THE DOCKER RUNTIME ---\n\n"
eval $(minikube docker-env)


docker build -t minikube/bitcoin:v1 ./bitcoin/docker/
docker build -t minikube/bitcoin-miner-sidecar:v1 ./bitcoin-miner-sidecar/docker/
13 changes: 13 additions & 0 deletions devenv/local/k8s/custom-k8s-docker-builds/ref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
https://stackoverflow.com/questions/54256980/kubernetes-pod-deployment-while-passing-args-to-container

https://stackoverflow.com/questions/48403480/running-a-shell-script-to-initialize-pods-in-kubernetes-initializing-my-db-clus


https://bitcoin.stackexchange.com/questions/116645/mempool-docker-unable-to-connect-to-electrum-server


docker build -t minikube/electrs:v1 ./electrs/docker/
docker build -t minikube/nakamoto-signer:v1 ./nakamoto-signer/docker/
docker build -t minikube/stacks:v1 ./stacks/docker/
docker build -t minikube/stacks-api:v1 ./stacks-api/docker/
docker build -t minikube/stacks-explorer:v1 ./stacks-explorer/docker/
14 changes: 14 additions & 0 deletions devenv/local/k8s/down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

sh ./utils/kill-port-forwards.sh


# [1] Delete the K8s deployments first
kubectl delete -f ./yamls/deployments/bitcoin-deployment.yaml
kubectl delete -f ./yamls/deployments/bitcoin-miner-deployment.yaml

# [2] Delete the K8s services
kubectl delete -f ./yamls/services/services.yaml

# [3] Delete the K8s secrets
kubectl delete -f ./yamls/secrets/secrets.yaml
2 changes: 2 additions & 0 deletions devenv/local/k8s/remove-minikube-containers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minikube image rm minikube/bitcoin:v1
minikube image rm minikube/bitcoin-miner-sidecar:v1
Loading

0 comments on commit 0277a7f

Please sign in to comment.