|
1 | | ---- |
2 | | -title: '集群部署说明' |
3 | | -sidebar_position: 6 |
4 | | ---- |
| 1 | +1. |
5 | 2 |
|
| 3 | + ## 集群部署说明 |
6 | 4 |
|
| 5 | + `open-im-server`支持集群部署。以下是**源码集群**部署的步骤: |
7 | 6 |
|
8 | | -## 集群部署说明 |
| 7 | + 场景:两台机器部署`open-im-server`,分别为机器A和机器B,假设两台机器在同一内网中。 |
9 | 8 |
|
10 | | -`open-im-server`支持集群部署。以下是集群部署的步骤: |
| 9 | + 机器A:部署`open-im-server`、`nginx`、`mongo`、`redis`、`etcd`、`kafka`、`minio`、`prometheus`、`grafana`、`aleartmanager`。 |
11 | 10 |
|
12 | | -1. 克隆仓库,源码部署: |
13 | | - ```bash |
14 | | - git clone https://github.com/openimsdk/open-im-server && cd open-im-server |
15 | | - ``` |
| 11 | + 机器B:部署`open-im-server`。 |
16 | 12 |
|
17 | | - docker部署: |
18 | | - ```bash |
19 | | - git clone https://github.com/openimsdk/openim-docker |
20 | | - ``` |
| 13 | + 组件`mongo`、`redis`、`kafka`、`etcd`均支持集群部署,以下场景默认每个组件集群部署3台。 |
21 | 14 |
|
22 | | -2. 修改`kafka`、`minio`、`mongodb`、`etcd`(默认服务发现方式)、`redis`中的地址,将其配置到正确的组件地址中。**保证连接组件的各个端口可访问(关注防火墙规则是否允许端口被访问)。** |
23 | | - 源码部署修改方式:修改`open-im-server/config`目录下,对应组件的地址。 |
24 | | - 对应配置文件字段为: |
| 15 | + 1. 机器A、B:克隆仓库: |
25 | 16 |
|
26 | | - - `kafka`: `kafka.yml:address` |
27 | | - - `minio`: `minio.yml`,`internalAddress`配置内部服务访问地址,`externalAddress`配置外部访问`minio`的地址。 |
28 | | - - `mongo`:`mongodb.yml:address` |
29 | | - - `etcd`: `discovery.yml:etcd.address` |
30 | | - - `redis`: `redis.yml:address` |
| 17 | + ```bash |
| 18 | + git clone https://github.com/openimsdk/open-im-server && cd open-im-server |
| 19 | + ``` |
31 | 20 |
|
32 | | -3. 修改`kafka`广播的端口。如果使用`open-im-server`中的`docker-compose.yml`部署,修改`service.kafka.environment.KAFKA_CFG_ADVERTISED_LISTENERS`中的`EXTERNAL`为访问`kafka`组件的地址。其他部署方式请自行修改。 |
33 | | - 例如:`KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,EXTERNAL://192.168.2.36:19094`。 |
| 21 | + 2. 机器A:修改`kafka`、`minio`、`mongodb`、`etcd`(默认服务发现方式)、`redis`中的地址,将其配置到正确的组件地址中。**保证连接组件的各个端口可访问(关注防火墙规则是否允许端口被访问)。** |
| 22 | + 修改`open-im-server/config`目录下,对应组件的地址。 |
| 23 | + 对应配置文件字段为: |
34 | 24 |
|
35 | | -4. 如果使用内网部署集群,可将需要暴露端口的组件中的`autoSetPorts`配置设为`true`(默认即为`true`),可实现自动配置端口。 |
36 | | - 如果集群机器**不在内网中**,需要将`autoSetPorts`设置为`false`,并修改各个`rpc`组件的`registerIP`为设置部署`etcd`的服务器可访问的`ip`地址,**并保证各个端口可被访问**。如需启用`prometheus`,还需要保证各个组件的`prometheus.port`端口可被访问。 |
37 | | - 拥有`autoSetPorts`配置的组件如下: |
| 25 | + - `kafka`: `kafka.yml:address`,内容为`[ kafkaAddr1, kafkaAddr2, kafkaAddr3 ]` |
| 26 | + - `minio`: `minio.yml`,`internalAddress`配置内部服务访问地址,`externalAddress`配置外部访问`minio`的地址。 |
| 27 | + - `mongo`:`mongodb.yml:address`,内容为`[ mongoAddr1, mongoAddr2, mongoAddr3 ]` |
| 28 | + - `etcd`: `discovery.yml:etcd.address`,内容为`[ etcdAddr1, etcdAddr2, etcdAddr3 ]` |
| 29 | + - `redis`: `redis.yml:address`,内容为`[ redis1, redis2, redis3 ]`,并将`redis.yml`中的`clusterMode`设置为`true`(单机部署不需要)。 |
38 | 30 |
|
39 | | - - `openim-api.yml:prometheus.autoSetPorts` |
40 | | - - `openim-msggateway.yml:rpc.autoSetPorts` |
41 | | - - `openim-msgtransfer.yml:prometheus.autoSetPorts` |
42 | | - - `openim-push.yml:rpc.autoSetPorts` |
43 | | - - `openim-rpc-auth.yml:rpc.autoSetPorts` |
44 | | - - `openim-rpc-conversation.yml:rpc.autoSetPorts` |
45 | | - - `openim-rpc-friend.yml:rpc.autoSetPorts` |
46 | | - - `openim-rpc-group.yml:rpc.autoSetPorts` |
47 | | - - `openim-rpc-msg.yml:rpc.autoSetPorts` |
48 | | - - `openim-rpc-third.yml:rpc.autoSetPorts` |
49 | | - - `openim-rpc-user.yml:rpc.autoSetPorts` |
50 | | - |
51 | | -  |
52 | | - |
53 | | - 非内网部署时,如需启用`prometheus`,部署`promtheus`的机器需要修改`prometheus.yml`,将其中的所有`http_sd_configs`配置项及其子配置项去掉,加上`static_configs`配置项,并将其中的`targets`改为对应的服务的端口。 |
54 | | - 例如:`openimserver-openim-api`表示`api`组件的`prometheus`数据抓取,则其`target`中的端口地址应和`openim-api.yml`中的`prometheus.ports`一致。 |
55 | | - |
56 | | -  |
57 | | - |
58 | | -5. 在`open-im-server/start-config.yml`中根据需求修改各个组件数量。 |
59 | | - |
60 | | -6. 如果通过集群模式部署`redis`,则需将`redis.yml`中的`clusterMode`设置为`true`。 |
61 | | - |
62 | | -7. 如果部署了多个`api`或者`WebSocket`监听端口,推荐使用`nginx`方便快捷地实现负载均衡,参考配置如下: |
63 | | - ```yaml |
64 | | - # nginx.conf |
65 | | - |
66 | | - events { |
67 | | - worker_connections 1024; |
68 | | - } |
| 31 | + 3. 在`open-im-server/start-config.yml`中根据需求修改各个组件数量。 |
69 | 32 |
|
70 | | - http { |
71 | | - upstream websocket_cluster { |
72 | | - server 192.168.2.10:10101; # WebSocket address |
73 | | - server 192.168.2.11:10001; |
74 | | - } |
| 33 | + 4. 机器A部署`nginx`,参考配置如下: |
75 | 34 |
|
76 | | - upstream api_cluster { |
77 | | - server 192.168.2.10:10102; # api address |
78 | | - server 192.168.2.11:10002; |
79 | | - } |
| 35 | + >🚀 **提示**: 确保替换成您的实际域名、SSL 证书路径和 SSL 密钥。 |
80 | 36 | |
81 | | - # WebSocket |
82 | | - server { |
83 | | - listen 10001; |
| 37 | + ```yaml |
| 38 | + events { |
| 39 | + worker_connections 1024; |
| 40 | + } |
| 41 | + |
| 42 | + http { |
| 43 | + #open-im-server chat Corresponding deployment address and port |
| 44 | + upstream msg_gateway{ |
| 45 | + #IM Message server address Multiple can be specified according to the deployment |
| 46 | + server 127.0.0.1:10001; |
| 47 | + server 192.168.2.36:10001; |
| 48 | + } |
| 49 | + upstream im_api{ |
| 50 | + #IM Group user api server address Multiple can be specified according to the deployment |
| 51 | + server 127.0.0.1:10002; |
| 52 | + server 192.168.2.36:10001; |
| 53 | + } |
| 54 | + upstream minio_s3_2{ |
| 55 | + #Minio address can be assigned to multiple modules dependingon deployment |
| 56 | + server 127.0.0.1:10005; |
| 57 | + } |
| 58 | + server { |
| 59 | + listen 443; #Listening on port 443 |
| 60 | + server_name web.xx.xx; #Your domain name |
| 61 | + ssl on; |
| 62 | + #Path of pem file for ssl certificate |
| 63 | + ssl_certificate /usr/local/nginx/conf/ssh/web.xx.xx_bundle.pem; |
| 64 | + #Key file path of ssl certificate |
| 65 | + ssl_certificate_key /usr/local/nginx/conf/ssh/web.xx.xx.key; |
| 66 | + |
| 67 | + gzip on; |
| 68 | + gzip_min_length 1k; |
| 69 | + gzip_buffers 4 16k; |
| 70 | + gzip_comp_level 2; |
| 71 | + gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/wasm; |
| 72 | + gzip_vary off; |
| 73 | + gzip_disable "MSIE [1-6]\."; |
| 74 | + |
| 75 | + location ^~/api/{ |
| 76 | + proxy_http_version 1.1; |
| 77 | + proxy_set_header Upgrade $http_upgrade; |
| 78 | + proxy_set_header Connection "Upgrade"; |
| 79 | + proxy_set_header X-real-ip $remote_addr; |
| 80 | + proxy_set_header X-Forwarded-For $remote_addr; |
| 81 | + proxy_set_header X-Request-Api $scheme://$host/api; |
| 82 | + proxy_pass http://im_api/; |
| 83 | + } |
| 84 | + |
| 85 | + location /msg_gateway{ |
| 86 | + proxy_http_version 1.1; |
| 87 | + proxy_set_header Upgrade $http_upgrade; |
| 88 | + proxy_set_header Connection "Upgrade"; |
| 89 | + proxy_set_header X-real-ip $remote_addr; |
| 90 | + proxy_set_header X-Forwarded-For $remote_addr; |
| 91 | + proxy_pass http://msg_gateway/; |
| 92 | + } |
| 93 | + |
| 94 | + location ^~/im-minio-api/ { |
| 95 | + proxy_set_header Host $http_host; |
| 96 | + proxy_set_header X-Real-IP $remote_addr; |
| 97 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 98 | + proxy_set_header X-Forwarded-Proto $scheme; |
| 99 | + proxy_connect_timeout 300; |
| 100 | + |
| 101 | + proxy_http_version 1.1; |
| 102 | + proxy_set_header Connection ""; |
| 103 | + chunked_transfer_encoding off; |
| 104 | + proxy_pass http://minio_s3_2/; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + ``` |
84 | 109 |
|
85 | | - server_name _; |
| 110 | + 5. `mage`编译,`mage start`启动服务。 |
86 | 111 |
|
87 | | - location / { |
88 | | - proxy_pass http://websocket_cluster; |
89 | | - proxy_http_version 1.1; |
90 | | - proxy_set_header Upgrade $http_upgrade; |
91 | | - proxy_set_header Connection 'upgrade'; |
92 | | - proxy_set_header Host $host; |
93 | | - proxy_cache_bypass $http_upgrade; |
94 | | - } |
95 | | - } |
| 112 | + ## **常见问题/注意事项** |
96 | 113 |
|
97 | | - # API |
98 | | - server { |
99 | | - listen 10002; |
| 114 | + 1. 部署`kafka`时,需要修改`kafka`广播的端口。如果使用`open-im-server`中的`docker-compose.yml`部署,修改`service.kafka.environment.KAFKA_CFG_ADVERTISED_LISTENERS`中的`EXTERNAL`为访问`kafka`组件的地址。其他部署方式请自行修改。 |
| 115 | + 例如:`KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,EXTERNAL://192.168.2.36:19094`。 |
| 116 | + 2. 多台机器部署需要保证时钟一致,服务才可正常运行。例如`token`的签发允许各个机器的时钟误差在`5s`以内。 |
| 117 | + 3. 组件端口无法访问:通过回环地址检测组件启动是否正常,若回环地址可访问,则检查是否被防火墙规则过滤。 |
| 118 | + 4. 如果集群机器**不在内网中**,需要将`autoSetPorts`设置为`false`,并修改各个`rpc`组件的`registerIP`为设置部署`etcd`的服务器可访问的`ip`地址,**并保证各个端口可被访问**。如需启用`prometheus`,还需要保证各个组件的`prometheus.port`端口可被访问。 |
| 119 | + 拥有`autoSetPorts`配置的组件如下: |
| 120 | + |
| 121 | + - `openim-api.yml:prometheus.autoSetPorts` |
| 122 | + - `openim-msggateway.yml:rpc.autoSetPorts` |
| 123 | + - `openim-msgtransfer.yml:prometheus.autoSetPorts` |
| 124 | + - `openim-push.yml:rpc.autoSetPorts` |
| 125 | + - `openim-rpc-auth.yml:rpc.autoSetPorts` |
| 126 | + - `openim-rpc-conversation.yml:rpc.autoSetPorts` |
| 127 | + - `openim-rpc-friend.yml:rpc.autoSetPorts` |
| 128 | + - `openim-rpc-group.yml:rpc.autoSetPorts` |
| 129 | + - `openim-rpc-msg.yml:rpc.autoSetPorts` |
| 130 | + - `openim-rpc-third.yml:rpc.autoSetPorts` |
| 131 | + - `openim-rpc-user.yml:rpc.autoSetPorts` |
100 | 132 |
|
101 | | - server_name _; |
| 133 | +  |
102 | 134 |
|
103 | | - location / { |
104 | | - proxy_pass http://api_cluster; |
105 | | - proxy_set_header Host $host; |
106 | | - proxy_set_header X-Real-IP $remote_addr; |
107 | | - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
108 | | - proxy_set_header X-Forwarded-Proto $scheme; |
109 | | - } |
110 | | - } |
111 | | - } |
| 135 | + 此外,机器A还需要修改`prometheus.yml`,将其中的所有`http_sd_configs`配置项及其子配置项去掉,加上`static_configs`配置项,并将其中的`targets`改为对应的服务的端口。 |
| 136 | + 例如:`openimserver-openim-api`表示`api`组件的`prometheus`数据抓取,则其`target`中的端口地址应和`openim-api.yml`中的`prometheus.ports`一致。 |
112 | 137 |
|
113 | | - ``` |
114 | | - |
115 | | - 如果决定使用`docker`启动`nginx`,可参考以下命令(linux系统): |
116 | | - |
117 | | - ```shell |
118 | | - docker run --name nginx -p 10001:10001 -p 10002:10002 -v $(pwd)/nginx-config:/etc/nginx/conf.d -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf -d nginx |
119 | | - ``` |
120 | | - |
121 | | - 如使用不同系统修改挂载的目录即可。 |
122 | | - |
123 | | -8. 使用`docker compose up -d`启动组件。如果采用源码部署,使用`mage`编译,`mage start`启动服务。 |
124 | | - |
125 | | -## **常见问题/注意事项** |
126 | | - |
127 | | -1. 多台机器部署需要保证时钟一致,服务才可正常运行。例如`token`的签发允许各个机器的时钟误差在`5s`以内。 |
128 | | -2. 组件端口无法访问:通过回环地址检测组件启动是否正常,若回环地址可访问,则检查是否被防火墙规则过滤。 |
| 138 | +  |
0 commit comments