Running Bitcoin Core Node On Kubernetes

Why Run a Bitcoin Node?

A Bitcoin node allows you to verify transactions and blocks on the Bitcoin network. Running a full node provides benefits such as improved privacy, security, and support for the decentralized network.

Pros of Running Bitcoin on Kubernetes

  1. Resource Management:
    Kubernetes allows you to allocate CPU, memory, and storage resources to your Bitcoin node. By using requests and limits, you can optimize resource allocation for performance.
  2. Networking and Load Balancing:
    • Kubernetes simplifies network management and enables easy exposure of services. You can use Ingress to expose Bitcoin Core’s RPC services.
    • Kubernetes load balancing ensures that incoming requests are evenly distributed if you’re running multiple nodes.
  3. Persistent Storage:
    Kubernetes supports Persistent Volumes (PVs), which decouple storage from the lifecycle of the containers. This is useful for stateful applications like Bitcoin, where data persistence is critical.

Cons of Running Bitcoin on Kubernetes

  1. Disk I/O Performance:
    Bitcoin Core is disk-intensive, requiring high-performance storage for syncing and validating blockchain data.
  2. Complexity for Stateful Workloads:
    Kubernetes is optimized for stateless, distributed applications. Managing stateful applications like Bitcoin Core on Kubernetes requires careful configuration of storage, networking, and backups.
  3. Resource Overhead:
    Running Bitcoin Core inside containers on Kubernetes introduces some resource overhead compared to running directly on bare-metal hardware, which could reduce overall performance. I didn't run any numbers, but from what's on the internet Kubernetes creates 15% - 30% resource overhead when running on bare metal on Ubuntu. Tradeoff, but saves a lot of time in other areas.
  4. Cost:
    Kubernetes clusters, especially in cloud environments, can incur significant costs, particularly when using high-performance SSD storage. It is not worth while to run it in the cloud financially, but on a single bare metal server with light weight Kubernetes solution like K3s maybe a good option to consider.
  5. Limited Horizontal Scaling:
    Bitcoin Core doesn’t benefit from traditional horizontal scaling. While you can run multiple Bitcoin nodes, each node is independent, and adding more nodes doesn’t increase performance. But it makes it easier to isolate workload from others.

Best Practices for Running Bitcoin on Kubernetes

If you decide to run a Bitcoin node on Kubernetes, here are some guidelines to follow for optimal performance and management:

1. Optimize Storage with SSDs

  • Use SSD-backed persistent storage to handle the heavy disk I/O demands of Bitcoin Core, particularly during initial blockchain synchronization. I don't recommend running on Cloud, as it will be costly. Better get NVMe drive for your computer locally.

2. Allocate Sufficient Resources

  • Bitcoin Core requires some CPU and Memory resources. Set appropriate resource requests and limits in your Kubernetes configuration.
resources:
  requests:
    memory: "4Gi"
    cpu: "2"
  limits:
    memory: "8Gi"
    cpu: "4"

3. Use Persistent Volumes for Blockchain Data

  • Configure persistent volumes to store blockchain data, ensuring that data is not lost if the pod is restarted. HostPath volumes is a good option if you run your own solution like K3s.

4. Optimize Bitcoin Core Configuration

  • Configure Bitcoin Core with -dbcache=1000 (or higher, depending on available RAM) to reduce disk I/O. Using -prune=550 can save disk space if you don’t need full blockchain history.

5. Expose Services Carefully

  • Use Kubernetes Services and Ingress to manage network exposure. Be cautious when exposing RPC ports publicly; ensure that proper authentication (rpcuser/rpcpassword) and firewall rules are in place.

6. Monitor Resource Usage

  • Use Kubernetes monitoring tools (e.g., Prometheus, Grafana) to track CPU, memory, and disk usage. This will help identify bottlenecks and optimize resource allocation.

7. Plan for Reindexing

  • Reindexing can take anywhere from hours to days, depending on your hardware. Allocate sufficient CPU, memory, and SSD storage, and consider using larger dbcache settings during reindexing to speed up the process.

Sample Deployment on Kubernetes

Here’s an example Kubernetes deployment YAML file to run Bitcoin Core on Kubernetes:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bitcoin-core
  labels:
    app: bitcoin-core
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bitcoin-core
  template:
    metadata:
      labels:
        app: bitcoin-core
    spec:
      containers:
      - name: bitcoin-core
        image: ruimarinho/bitcoin-core:latest
        ports:
        - containerPort: 8332  
        - containerPort: 8333
        resources:
          requests:
            memory: "4Gi"
            cpu: "2"
          limits:
            memory: "8Gi"
            cpu: "4"
        args: ["-datadir=/home/bitcoin/.bitcoin", "-dbcache=1000", "-prune=550"]
        volumeMounts:
        - mountPath: /home/bitcoin/.bitcoin
          name: bitcoin-data
      volumes:
      - name: bitcoin-data
        persistentVolumeClaim:
          claimName: bitcoin-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: bitcoin-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Gi
  storageClassName: fast-ssd

Conclusion

For single-node deployments or high-performance needs, traditional setups on bare-metal servers or virtual machines may offer better performance. However, for distributed setups, automated management, and DevOps-friendly environments, Kubernetes can be an effective platform for managing Bitcoin and other cryptocurrency services.