我正在使用 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访问服务?
答案 0 :(得分:4)
您可以使用以下方式在窗格中访问服务。
使用环境变量
https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services
使用内部服务名称, 即> YOURSERVICENAME.NAMESPACE.svc.cluster.local
https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services