使用不同的机器(本地网络)从 Docker 容器外部访问 KSQL Server
Access KSQL Server from outside of a Docker Container using a different machine (local network)
我还为 运行、zookeeper
、kafka
、ksql
和 ksql-cli
创建了以下 Docker 个容器。当我从 Docker 是 运行ning 的同一台机器 运行ning 命令 docker-compose exec ksqldb-cli ksql http://ksqldb-server:8088
时,ksql-cli
可以很好地访问 ksql-server
。
但是,我想在同一台机器之外访问 ksql-server
,但在同一本地网络下使用不同的笔记本电脑。我该怎么做?
这是相关的 docker-compose.yml
文件:
version: '3.8'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
container_name: zookeeper
networks:
- kafka_network
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- 22181:2181
kafka:
image: confluentinc/cp-kafka:latest
container_name: kafka
networks:
- kafka_network
depends_on:
- zookeeper
ports:
- 29092:29092
- 29093:29093
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_LISTENERS: EXTERNAL_SAME_HOST://:29092,EXTERNAL_DIFFERENT_HOST://:29093,INTERNAL://:9092
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:9092,EXTERNAL_SAME_HOST://localhost:29092,EXTERNAL_DIFFERENT_HOST://192.168.178.218:29093
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL_SAME_HOST:PLAINTEXT,EXTERNAL_DIFFERENT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
ksqldb-server:
image: confluentinc/cp-ksqldb-server:latest
container_name: ksqldb-server
hostname: ksqldb-server
networks:
- kafka_network
depends_on:
- kafka
ports:
- "8088:8088"
environment:
KSQL_CONFIG_DIR: "/etc/ksql"
KSQL_BOOTSTRAP_SERVERS: "kafka:9092"
KSQL_HOST_NAME: ksqldb-server
KSQL_LISTENERS: "http://0.0.0.0:8088"
KSQL_CACHE_MAX_BYTES_BUFFERING: 0
KSQL_KSQL_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_REPLICATION_FACTOR: 1
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: 'true'
KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: 'true'
ksqldb-cli:
image: confluentinc/cp-ksqldb-cli:latest
container_name: ksqldb-cli
networks:
- kafka_network
depends_on:
- kafka
- ksqldb-server
entrypoint: /bin/sh
tty: true
networks:
kafka_network:
name: kafka_docker_sse
当我尝试从同一本地网络下的另一台笔记本电脑访问 ksql-server
时,连接 error/connection 被拒绝。我尝试使用 Python ksql-python 包访问 ksqldb-server
。
pip install ksql
from ksql import KSQLAPI
client = KSQLAPI('http://ksql-server:8088')
# OR
# client = KSQLAPI('http://0.0.0.0:8088')
# client = KSQLAPI('http://192.168.178.218:8088')
if __name__ == '__main__':
print(client)
我也试过把ksqldb-server
下面的KSQL_LISTENERS: "http://0.0.0.0:8088"
改成KSQL_LISTENERS: "http://192.168.178.218:8088"
,但是也没用。
任何提示都会很有帮助,因为我最近两天都被困在这里!
您需要保留 KSQL_LISTENERS: "http://0.0.0.0:8088"
。这将绑定容器以接受所有进入端口 8088
的传入流量
然后在Compose中用ports
将宿主机8088的流量转发给容器8088。
因此,对于任何外部客户端,您需要连接到端口 8088 上的主机 LAN/外部 IP。您可能需要明确允许 TCP 流量进入该服务器主机防火墙的该端口。
我还为 运行、zookeeper
、kafka
、ksql
和 ksql-cli
创建了以下 Docker 个容器。当我从 Docker 是 运行ning 的同一台机器 运行ning 命令 docker-compose exec ksqldb-cli ksql http://ksqldb-server:8088
时,ksql-cli
可以很好地访问 ksql-server
。
但是,我想在同一台机器之外访问 ksql-server
,但在同一本地网络下使用不同的笔记本电脑。我该怎么做?
这是相关的 docker-compose.yml
文件:
version: '3.8'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
container_name: zookeeper
networks:
- kafka_network
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- 22181:2181
kafka:
image: confluentinc/cp-kafka:latest
container_name: kafka
networks:
- kafka_network
depends_on:
- zookeeper
ports:
- 29092:29092
- 29093:29093
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_LISTENERS: EXTERNAL_SAME_HOST://:29092,EXTERNAL_DIFFERENT_HOST://:29093,INTERNAL://:9092
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:9092,EXTERNAL_SAME_HOST://localhost:29092,EXTERNAL_DIFFERENT_HOST://192.168.178.218:29093
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL_SAME_HOST:PLAINTEXT,EXTERNAL_DIFFERENT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
ksqldb-server:
image: confluentinc/cp-ksqldb-server:latest
container_name: ksqldb-server
hostname: ksqldb-server
networks:
- kafka_network
depends_on:
- kafka
ports:
- "8088:8088"
environment:
KSQL_CONFIG_DIR: "/etc/ksql"
KSQL_BOOTSTRAP_SERVERS: "kafka:9092"
KSQL_HOST_NAME: ksqldb-server
KSQL_LISTENERS: "http://0.0.0.0:8088"
KSQL_CACHE_MAX_BYTES_BUFFERING: 0
KSQL_KSQL_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_REPLICATION_FACTOR: 1
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: 'true'
KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: 'true'
ksqldb-cli:
image: confluentinc/cp-ksqldb-cli:latest
container_name: ksqldb-cli
networks:
- kafka_network
depends_on:
- kafka
- ksqldb-server
entrypoint: /bin/sh
tty: true
networks:
kafka_network:
name: kafka_docker_sse
当我尝试从同一本地网络下的另一台笔记本电脑访问 ksql-server
时,连接 error/connection 被拒绝。我尝试使用 Python ksql-python 包访问 ksqldb-server
。
pip install ksql
from ksql import KSQLAPI
client = KSQLAPI('http://ksql-server:8088')
# OR
# client = KSQLAPI('http://0.0.0.0:8088')
# client = KSQLAPI('http://192.168.178.218:8088')
if __name__ == '__main__':
print(client)
我也试过把ksqldb-server
下面的KSQL_LISTENERS: "http://0.0.0.0:8088"
改成KSQL_LISTENERS: "http://192.168.178.218:8088"
,但是也没用。
任何提示都会很有帮助,因为我最近两天都被困在这里!
您需要保留 KSQL_LISTENERS: "http://0.0.0.0:8088"
。这将绑定容器以接受所有进入端口 8088
然后在Compose中用ports
将宿主机8088的流量转发给容器8088。
因此,对于任何外部客户端,您需要连接到端口 8088 上的主机 LAN/外部 IP。您可能需要明确允许 TCP 流量进入该服务器主机防火墙的该端口。