> ## Documentation Index
> Fetch the complete documentation index at: https://enterprise-docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Qdrant Cluster Deployment

## Requirements

* Three VMs with Docker installed
* Docker installed
  * [Install Docker](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository)
  * [Manage Docker as a non-root user](https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user)
* Open ports 6333 and 6335 on the VMs
  * 6333: Port for external communication with qdrant nodes.
  * 6335: Port for internal communication between qdrant nodes. Don't expose to public.

## Installation

### `QDRANT__SERVICE__API_KEY` is a required environment variable

You can generate a random 32-byte hex string using the following command:

```bash theme={null}
openssl rand -hex 32
```

Copy and paste the generated string to replace `#REPLACE_ME#` in the `docker-compose.yml` file.

### \[First VM] Create the `docker-compose.yml` file for first VM

* replace commmand with `--uri` option to current VM address

```bash theme={null}
version: "3.8"
services:
  qdrant:
    image: langgenius/qdrant:v1.7.3
    command: "./qdrant --uri 'http://dify-qdrant-node-01:6335'"
    network_mode: "host"
    restart: always
    volumes:
      - /data/qdrant/data/:/qdrant/storage
    environment:
      QDRANT__SERVICE__API_KEY: #REPLACE_ME#
      QDRANT__CLUSTER__ENABLED: 'true'
```

### \[Second VM] Create the `docker-compose.yml` file for second VM

* replace commmand with `--bootstrap` option to first VM address and `--uri` option to current VM address

```bash theme={null}
version: "3.8"
services:
  qdrant:
    image: langgenius/qdrant:v1.7.3
    command: "./qdrant --bootstrap 'http://dify-qdrant-node-01:6335' --uri 'http://dify-qdrant-node-02:6335'"
    network_mode: "host"
    restart: always
    volumes:
      - /data/qdrant/data/:/qdrant/storage
    environment:
      QDRANT__SERVICE__API_KEY: #REPLACE_ME#
      QDRANT__CLUSTER__ENABLED: 'true'
```

### \[Third VM] Create the `docker-compose.yml` file for third VM

* replace commmand with `--bootstrap` option to first VM address and `--uri` option to current VM address

```bash theme={null}
version: "3.8"
services:
  qdrant:
    image: langgenius/qdrant:v1.7.3
    command: "./qdrant --bootstrap 'http://dify-qdrant-node-01:6335' --uri 'http://dify-qdrant-node-03:6335'"
    network_mode: "host"
    restart: always
    volumes:
      - /data/qdrant/data/:/qdrant/storage
    environment:
      QDRANT__SERVICE__API_KEY: #REPLACE_ME#
      QDRANT__CLUSTER__ENABLED: 'true'
```

## Check the cluster status

Login one of VMs and run the following command to check the cluster status.

```bash theme={null}
curl -H 'api-key: <QDRANT__SERVICE__API_KEY>' localhost:6333/cluster | python3 -m json.tool
```

After you run the above command, you should see the following example output:

```json theme={null}
{
    "result": {
        "status": "enabled",
        "peer_id": 2978525476254873,
        "peers": {
            "8497171801402828": {
                "uri": "http://172.206.68.136:6335/"
            },
            "2978525476254873": {
                "uri": "http://172.206.71.251:6335/"
            },
            "530461904384369": {
                "uri": "http://20.85.121.81:6335/"
            }
        },
        "raft_info": {
            "term": 1,
            "commit": 7,
            "pending_operations": 0,
            "leader": 530461904384369,
            "role": "Follower",
            "is_voter": true
        },
        "consensus_thread_status": {
            "consensus_thread_status": "working",
            "last_update": "2024-06-30T15:13:23.508350153Z"
        },
        "message_send_failures": {}
    },
    "status": "ok",
    "time": 8.7e-06
}
```

## Add Load Balancer

* After deploying the qdrant cluster, you can add a load balancer to distribute the requests to the qdrant nodes.
* Load balancer should be configured to distribute the requests to the qdrant nodes on port 6333.
* 6335 only for internal communication, no need to expose it to the public.

## References

* [qdrant distributed deployment](https://qdrant.tech/documentation/guides/distributed_deployment/)
