Python通過kerberos安全認證操作kafka方式
如何通過Kerberos認證.
1.安裝Kerberos客戶端
CentOS:
yum install krb5-workstation
使用which kinit檢視是否安裝成功;
2.拷貝Kerberos配置檔案
conf目錄下krb5.conf和kafka.keytab和jaas.conf拷貝到客戶端機器的etc目錄,同時,krb5.conf中的kdc叢集主機名和IP配置到客戶端機器hosts配置檔案中
3.Kinit客戶端通過kerberos認證
獲取Principal
klist -kt kafka.keytab
4.安裝python-gssapi
pip install gssapi
遇到的問題,如下:
a.在linux中執行wget命令提示 -bash: wget: command not found 解決方法
yum -y install wget
b.報錯:bash: pip: command not found
wget https://bootstrap.pypa.io/get-pip.py python get-pip.py pip -V #檢視pip版本
python -m pip install --upgrade --force pip easy_install -U setuptools pip install --upgrade setuptools
3.pip安裝出現Command “python setup.py egg_info” failed with error code 1 的解決方法
Traceback (most recent call last): File “”,line 1,in File “/tmp/pip-install-6HfDE3/gssapi/setup.py”,line 109,in raise Exception("Could not find main GSSAPI shared library. Please " Exception: Could not find main GSSAPI shared library. Please try setting GSSAPI_MAIN_LIB yourself or setting ENABLE_SUPPORT_DETECTION to ‘false'
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-install-6HfDE3/gssapi/
yum install -y krb5-devel.x86_64
4.關於error: command ‘gcc' failed with exit status 1錯誤的解決方法
yum install gcc python-devel
安裝kafka-python
pip install kafka-python
初始化環境變數
export KAFKA_OPTS="-Djava.security.auth.login.config=/etc/conf/jaas.conf -Djava.security.krb5.conf=/etc/krb5.conf"
Python操作kafka樣例
from kafka import KafkaProducer from kafka.errors import KafkaError import os class Kafka_Producer(): def __init__(self,kafkahost,kafkaport,kafkatopic): self.kafkaHost = kafkahost self.kafkaPort = kafkaport self.kafkatopic = kafkatopic self.producer = KafkaProducer( bootstrap_servers = '{kafka_host}:{kafka_port}'.format(kafka_host=self.kafkaHost,kafka_port=self.kafkaPort),security_protocol="SASL_PLAINTEXT",sasl_mechanism="GSSAPI",sasl_kerberos_service_name="kafka",compression_type='gzip' #壓縮方式 ) def sendFileData(self,params): try: f = open(params,'rb') parmasMessage = f.read(-1).strip() producer = self.producer producer.send(self.kafkatopic,parmasMessage) producer.flush() except KafkaError as e: print (e) def main(): filePath = "/home/public/data/" topic = "demo" producer = Kafka_Producer("xxx.xx.xx.xx","9092",topic) dirList = os.listdir(filePath) for fileName in dirList: producer.sendFileData(filePath+fileName) print('send success!!!') if __name__=='__main__': main()
以上這篇Python通過kerberos安全認證操作kafka方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。