# Delegation Node Deployment Tutorial

In this tutorial, we will guide you through the process of deploying Delegation Node in the Kubernetes environment.&#x20;

## System Requirements

For delegation node deployment, two servers are required: the Node Operations Server to run core program functions, and the Data Server dedicated to user data storage. This architecture optimizes performance and reliability, with the Node Operations Server handling messaging tasks, while the Data Server is reserved for securely storing user information, ensuring both efficiency and data integrity.

### **Node Operations Server:**

* **Hardware:**
  * **CPU:** An 8-core x86-64 CPU (AMD or Intel) CPU is required.
  * **Memory:** At least 16GB of RAM.
  * **Storage:** SSDs are required. The server requires 150GB of free space.
* **Software:**
  * **Operating System:** Ubuntu 22.04 LTS or above.
* **Network:**&#x20;
  * A static public IP address is needed to facilitate incoming connections.
  * Set up a valid domain name and SSL/TLS certificate, available from registrars like GoDaddy. Configuring a domain name for your node enables users to find and connect to it through SendingMe, ZuChat, or other applications supported by SendingNetwork.
  * Allow traffic on port 443.

### **Data Server:**

* **Hardware:**
  * **CPU:** An 8-core x86-64 CPU (AMD or Intel) CPU is required.
  * **Memory:** At least 16GB of RAM.
  * **Storage:** SSDs are required. The server requires at least 500GB of free space.
* **Software:**
  * **Operating System:** Ubuntu 22.04 LTS or above.

***

## Install Prerequisites

### Install Kubernetes on the Node Operation Server

Kubernetes is an open-source container orchestration platform designed for automating the deployment, scaling, and management of containerized applications. Choose from the following installation methods based on your use case:

* **kubeadm**: Recommended for setting up production-grade Kubernetes clusters.
* **K3s**: Lightweight Kubernetes distribution for edge computing and resource-constrained environments.
* **Cloud Provider Kubernetes Services**: Managed Kubernetes services such as Google Kubernetes Engine (GKE), Azure Kubernetes Service (AKS), or Amazon Elastic Kubernetes Service (EKS).

For detailed installation instructions, refer to the [official Kubernetes documentation](https://kubernetes.io/docs/setup/).

On your Node Operation Server, install K3s by executing the following command:

```bash
curl -sfL https://get.k3s.io | sh -
```

After installation, check the status of your node:

```bash
sudo k3s kubectl get node
```

#### Install Dependencies

Before deploying the Delegation Node, ensure that the following services are installed and properly configured using Kubernetes Deployment and Service:

**Download the YAML Reference Files**:

```bash
sudo apt-get install -y wget && wget http://node4.sending.network:8120/delegation_install.tar.gz -P . && sudo tar zxvf delegation_install.tar.gz && cd delegation_install
```

### Deploy Redis

Redis is a high-performance, open-source, in-memory data store often used as a cache, database, and message broker. To deploy Redis on Kubernetes:

1. **Create the Redis ConfigMap**:

   ```bash
   sudo kubectl create -f redis-configmap.yaml
   ```
2. **Deploy Redis**:

   ```bash
   sudo kubectl apply -f redis-deployment.yaml
   ```
3. **Create the Redis Service**:

   <pre class="language-bash"><code class="lang-bash"><strong>sudo kubectl create -f redis-service.yaml
   </strong></code></pre>

This ensures Redis is correctly configured for your application, enabling caching and message brokering with high availability.

### **Deploy NATS**

NATS is a high-performance, open-source messaging system designed for real-time, low-latency communication in distributed systems. It is widely used in microservices architectures for pub/sub messaging, event streaming, and service communication.

To deploy NATS on Kubernetes, execute the following commands:

1. **Deploy NATS:**

   ```bash
   sudo kubectl apply -f nats-deployment.yaml
   ```
2. **Create NATS Service:**

   ```bash
   sudo kubectl create -f nats-service.yaml
   ```

***

## **Install PostgreSQL on Data Server**

PostgreSQL is a robust, open-source relational database management system. To install and configure PostgreSQL on your Data Server, follow the steps below. Or you can download and install the version suitable for your operating system from the [PostgreSQL official website](https://www.postgresql.org/download/).

Install PostgreSQL on Ubuntu:

<pre class="language-bash"><code class="lang-bash"><strong>sudo apt-get update
</strong>sudo apt-get install postgresql postgresql-client
</code></pre>

Login to PostgreSQL:

```bash
sudo -u postgres psql postgres
```

Set the password for the default user name as prompted：

```sql
\password postgres
```

Create database:

```sql
CREATE DATABASE delegationnode;
CREATE DATABASE admin_platform0;
```

To allow external connections, configure PostgreSQL to accept remote connections by modifying the following files:

* **Modify `postgresql.conf`:** \
  Edit the *postgresql.conf* file, typically located in `/etc/postgresql/x/main/` (where `x` is the version number). Add or modify the following line:

  ```
  /listen_addresses = '*'
  ```
* **Modify `pg_hba.conf`:** \
  While the previous configuration enables PostgreSQL to accept connections from the specified address, additional setup is required to define allowed authentication methods at the server level. Edit `pg_hba.conf` to add or modify the following line. You may replace `0.0.0.0/0` with your specific network segment:

  ```
  host  all  all 0.0.0.0/0 md5
  ```

After making the necessary changes, restart the PostgreSQL service to apply the new configurations:

```bash
sudo service postgresql restart
```

This completes the installation and configuration of PostgreSQL, enabling remote access and setting up the required databases for the Delegation Node.

***

## Deploy Delegation Node

Once the above services are properly running, you can proceed to deploy Delegation Node on your Node Operation Server. The specific steps may depend on your Delegation Node configuration and requirements. Here is a basic example configuration.

If you wish to rename the node, simply replace all instances of `dele-delegationnode` with your preferred name.

### Deploy Delegation Cluster Nodes in Kubernetes

To deploy the Delegation Cluster Nodes, use a **StatefulSet** in Kubernetes. Follow the steps below to create the necessary resources:

**Create the ServiceAccount (sa.yaml)** \
First, create the ServiceAccount required for the Delegation Node deployment. Run the following command:

```bash
sudo kubectl create -f sa.yaml
```

**Edit the `delegationsts.yaml` Configuration File** \
Modify the `delegationsts.yaml` file to reflect the required configuration:

* **Entropy Configuration**:\
  Replace the placeholder for entropy (`--entropy=xxxxxxxxxxxxxxxxxxxxxx`) with a securely generated 32-character string. Use the following command to generate it:

  ```bash
  head -c 32 /dev/urandom | xxd -ps -c 64
  ```
* **PostgreSQL Connection String**:\
  Update the `-connString` placeholder with your actual PostgreSQL connection details. Replace the `PASSWORD` and ip address with your actual one.

  ```yaml
  --connString=postgresql://postgres:PASSWORD@127.0.0.1/delegationnode?sslmode=disable
  ```
* **Admin Platform Configuration**:\
  Similarly, replace the admin platform connection string:

  ```yaml
  --connString=postgresql://postgres:PASSWORD@127.0.0.1/admin_platform0?sslmode=disable
  ```

  with your admin platform database connection details.\
  Here're some configuration reference:<br>

  | Parameter                                                                            | Description                                             |
  | ------------------------------------------------------------------------------------ | ------------------------------------------------------- |
  | `-port=8008`                                                                         | Specifies the port for the admin platform to listen on. |
  | `-dendritePort=8012`                                                                 | Specifies the port for Dendrite to listen on.           |
  | `-username={{ ADMIN_USERNAME }}`                                                     | The admin platform username.                            |
  | `-password={{ ADMIN_PASSWORD }}`                                                     | The admin platform password.                            |
  | `-connString=postgresql://postgres:123456@127.0.0.1/admin_platform0?sslmode=disable` | The database connection string.                         |
* **Admin Account Credentials**:\
  Replace the placeholders for admin username and password:

  ```yaml
  --username={{ ADMIN_USERNAME }}
  --password={{ ADMIN_PASSWORD }}
  ```

  with the credentials you want to assign to the admin account.<br>

  <table><thead><tr><th width="288">Parameter</th><th>Description</th></tr></thead><tbody><tr><td><code>- -whiteListEnable=false</code></td><td>If set to true, only wallet addresses on the whitelist are permitted to connect to your node.</td></tr><tr><td><code>- -blackListEnable=false</code></td><td>Set to true to block users through the blacklist.</td></tr><tr><td><code>- -developerKeyEnable=false</code></td><td>Set to true to allow users from applications with a whitelisted developer key.</td></tr></tbody></table>

**Apply the configuration** \
Once the `delegationsts.yaml` file has been updated, apply it to your Kubernetes cluster:

```bash
sudo kubectl apply -f delegationsts.yaml
```

### Create the Service

Next, create the necessary Kubernetes service for the Delegation Node by applying the `delegationsvc.yaml` file:

```bash
sudo kubectl create -f delegationsvc.yaml
```

### Verify Deployment

After deploying the Delegation Node and service, you should verify that the deployment was successful:

1. **Check Pod Status**:\
   Ensure that all pods are up and running:

   ```bash
   sudo kubectl get pods
   ```
2. **Check Services**:\
   Verify that the services are running correctly:

   ```bash
   sudo kubectl get services
   ```
3. **Check Logs**:\
   Review the logs of the Delegation Node to confirm it is functioning properly. Replace `dele-delegationnode-0` with the name of your Delegation Node pod:

   ```bash
   sudo kubectl logs dele-delegationnode-0
   ```

By following these steps, you will have successfully deployed the Delegation Node in Kubernetes and verified its status.

### **Install Nginx**

NGINX is a high-performance, open-source web server and reverse proxy server, commonly used for load balancing, HTTP caching, and routing client requests to backend servers.

**Install Nginx on Ubuntu:**

```bash
sudo apt-get install nginx
```

**Configure Nginx**

Move the downloaded `nginx.conf` file to `/etc/nginx/conf.d/`. Ensure that you update the configuration for the SSL certificate and domain name as the actual one.

Replace `<domain_name>` with your domain name.

Use the *CLUSTER-IP* obtained by the `kubectl get svc` command to replace the `<ip_address>`.

```bash
mv nginx.conf /etc/nginx/conf.d/
```

Once you complete the above steps, NGINX will be installed and configured to handle your traffic routing.

***

At this point, you have successfully deployed the Delegation Node along with all required services in the Kubernetes environment. You may proceed to further configure and optimize your cluster to meet production standards.

If you encounter any issues or have further needs, don't hesitate to seek support from the SendingNetwork [community](https://t.me/SendingNetworkOfficial/1).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sending-network.gitbook.io/sending.network/delegation-node/delegation-node-deployment-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
