> For the complete documentation index, see [llms.txt](https://mohamed-amins-personal-organizat.gitbook.io/smart-grid-ledger/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mohamed-amins-personal-organizat.gitbook.io/smart-grid-ledger/deployment-guide.md).

# Deployment Guide

Given the scope of the request, let's consolidate the deployment guide for the **SmartGridLedger** project, covering the Kubernetes deployment with the HLF Operator, including configurations for the three Go chaincodes, and also outline deployment methods using Docker and Docker Compose. This comprehensive approach will provide flexibility in managing environments across development and production settings.

### Comprehensive Deployment Guide for SmartGridLedger

#### Prerequisites Installation

* **Kubernetes Cluster**: Access and administrative rights to a Kubernetes cluster.
* **kubectl**: Installed and configured for cluster access.
* **Helm 3**: Installed for deploying the HLF Operator.
* **Docker**: Installed for Docker and Docker Compose-based deployments.
* **Docker Compose**: Installed for orchestrating multi-container Docker environments.
* **Git**: Installed for accessing the chaincode repository.
* **Hyperledger Fabric Binaries**: Downloaded, especially for Docker Compose deployments and manual configurations.

#### Part 1: Kubernetes Deployment with HLF Operator

**Deploying Network Components**

1. **Install HLF Operator**:

   ```bash
   helm repo add hlf-operator https://charts.hyperledger.org
   helm repo update
   helm install hlf-operator hlf-operator/hlf-operator
   ```
2. Prepare the Network Configuration

* **Create Cryptographic Material**: Use the Fabric CA or `cryptogen` tool to generate the required cryptographic material for your organizations, peers, and orderer nodes.
* **Create a Namespace for Your Network**: Kubernetes namespaces help isolate network components.

```bash
kubectl create namespace smartgridledger
```

* **Store Cryptographic Material in Kubernetes Secrets**: Convert the cryptographic material into Kubernetes secrets within your network's namespace.

```bash
kubectl create secret generic -n smartgridledger hlf--peer--crypto --from-file=path/to/crypto
```

3. **Deploy Orderers and Peers**: Define and apply Kubernetes custom resources for each component.

**Chaincode Deployment**

For each of the three Go chaincodes (e.g., `evcharging`, `energystorage`, `powergeneration`), you'll define a Kubernetes custom resource. Here's a template adjusted for Go chaincodes:

```yaml
apiVersion: hlf.kungfusoftware.es/v1alpha1
kind: Chaincode
metadata:
  name: chaincode-name
  namespace: smartgridledger
spec:
  chaincodeName: chaincode-name
  chaincodeVersion: "1.0"
  chaincodeType: "golang"
  gitRepo:
    url: "https://github.com/yourorg/smartgridledger.git"
    directory: "chaincodes/chaincode-directory"
    reference: "main"
  channelName: mychannel
  sequence: 1
  initRequired: false
  policy: "OR('Org1MSP.member')"
  peers:
    - peer0-org1.smartgridledger
```

Replace `chaincode-name` and `chaincode-directory` with actual names and paths for each chaincode.

#### Part 2: Docker and Docker Compose Deployment

**Network Setup**

1. **Network Configuration**: Create a `docker-compose.yaml` file for your network, defining services for CA, orderers, and peer nodes. Include volumes for cryptographic material.
2. **Start the Network**:

   ```bash
   docker-compose -f docker-compose.yaml up -d
   ```

**Chaincode Deployment**

Chaincode deployment in Docker environments involves packaging the chaincode, installing it on peers, and committing it to the channel. This process is more manual compared to the HLF Operator approach.

1. **Package Chaincode**:

   ```bash
   peer lifecycle chaincode package chaincode-name.tar.gz --path ./chaincode-directory --lang golang --label chaincode-name_1.0
   ```
2. **Install Chaincode on Peers**:

   ```bash
   docker exec -it peer0.org1.smartgridledger peer lifecycle chaincode install chaincode-name.tar.gz
   ```
3. **Approve and Commit Chaincode** (HLF v2.x): Follow the lifecycle commands to approve for your org and commit the chaincode to the channel, adjusting commands for your specific network configuration.

#### Monitoring and Management

Regardless of the deployment method, setting up monitoring and logging is crucial for maintaining network health. Use tools like Prometheus, Grafana, and ELK stack for Kubernetes deployments, and built-in Docker logging and monitoring solutions for Docker-based environments.

#### Conclusion

This guide provides a streamlined path to deploying the **SmartGridLedger** blockchain network and its associated chaincodes either via Kubernetes with the HLF Operator or using Docker and Docker Compose. Each deployment strategy has its benefits and fits different stages of development and production workflows. Ensure all configurations, especially those related to chaincodes and network components, are tailored to your project's specific requirements for a successful deployment.
