【问题标题】:Kubernetes how to access a service from another namespaceKubernetes 如何从另一个命名空间访问服务
【发布时间】:2018-12-11 18:06:28
【问题描述】:

我正在使用 https://github.com/Yolean/kubernetes-kafka 在 minikube 上运行 Kubernetes 我暴露了一个外部端口,并成功使用了集群外部的生产者和消费者

➜  ~ kubectl get svc --namespace kafka
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                     
AGE
bootstrap   ClusterIP   10.108.21.84     <none>        9092/TCP            
1h
broker      ClusterIP   None             <none>        9092/TCP            
1h
outside-0   NodePort    10.99.182.13     <none>        32400:32400/TCP     
1h
outside-1   NodePort    10.108.10.223    <none>        32401:32401/TCP     
1h
outside-2   NodePort    10.101.155.122   <none>        32402:32402/TCP     
1h
pzoo        ClusterIP   None             <none>        2888/TCP,3888/TCP   
1h
zoo         ClusterIP   None             <none>        2888/TCP,3888/TCP   
1h
zookeeper   ClusterIP   10.97.17.36      <none>        2181/TCP            
1h

python 生产者

from kafka import KafkaConsumer, KafkaProducer

KAFKA_TOPIC = 'demo'
KAFKA_BROKERS = '192.168.99.100:32400' # see step 1

producer = KafkaProducer(bootstrap_servers=KAFKA_BROKERS)

messages = [b'hello kafka', b'Falanga', b'3 test messages']


for m in messages:
    print(f"sending: {m}")
    producer.send(KAFKA_TOPIC, m)

producer.flush()

简单的消费者

#!/usr/bin/env python

from kafka import KafkaConsumer

KAFKA_TOPIC = 'demo'
KAFKA_BROKERS = '192.168.99.100:32400' # see step 1

consumer = KafkaConsumer(KAFKA_TOPIC, bootstrap_servers=KAFKA_BROKERS)

for message in consumer:
    print(f"message is of type: {type(message)}")
    print(message)

print('yo')
consumer.subscribe([KAFKA_TOPIC])

如何不使用面向外的 url 按名称从“默认”命名空间中的 pod 访问服务?

【问题讨论】:

    标签: apache-kafka kubernetes


    【解决方案1】:

    您可以通过以下方式访问 pod 中的 Service。

    【讨论】:

    • 谢谢!我刚刚在您回答 bootstrap.kafka.svc.cluster.local:9092 之前找到它,它与第二个答案一致。和自述页面。
    猜你喜欢
    • 1970-01-01
    • 2022-11-23
    • 2021-08-08
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2021-01-21
    • 2020-11-27
    • 1970-01-01
    相关资源
    最近更新 更多