Kubernetes kafka系列 | k8s部署kafka+zookeepe集群(可外部通信)

直通车 zookeeper搭建
请参考yaml

kafka.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: kafka-hs
  labels:
    app: kafka
spec:
  ports:
  - port: 9092
    targetPort: 9092
    name: server
  clusterIP: None
  selector:
    app: kafka
--- 
apiVersion: v1
kind: Service
metadata:
  name: kafka-cs
  labels:
    app: kafka
spec:
  selector:
    app: kafka
  type: NodePort
  ports:
  - name: client
    port: 9092
    targetPort: 9092
    nodePort: 30092
  selector:
    statefulset.kubernetes.io/pod-name: kafka-0
---
apiVersion: v1
kind: Service
metadata:
  name: kafka-cs-1
  labels:
    app: kafka
spec:
  ports:
    - port: 9092
      targetPort: 9092
      nodePort: 30093
      name: client
  type: NodePort
  selector:
    statefulset.kubernetes.io/pod-name: kafka-1
---
apiVersion: v1
kind: Service
metadata:
  name: kafka-cs-2
  labels:
    app: kafka
spec:
  ports:
    - port: 9092
      targetPort: 9092
      nodePort: 30094
      name: client
  type: NodePort
  selector:
    statefulset.kubernetes.io/pod-name: kafka-2
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: kafka-pdb
spec:
  selector:
    matchLabels:
      app: kafka
  minAvailable: 2
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kafka
spec:
  serviceName: kafka-hs
  replicas: 3
  selector:
    matchLabels:
      app: kafka
  template:
    metadata:
      labels:
        app: kafka
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                    - kafka
              topologyKey: "kubernetes.io/hostname"
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
             - weight: 1
               podAffinityTerm:
                 labelSelector:
                    matchExpressions:
                      - key: "app"
                        operator: In
                        values:
                        - zk
                 topologyKey: "kubernetes.io/hostname"
      terminationGracePeriodSeconds: 300
      containers:
      - name: kafka
        imagePullPolicy: IfNotPresent
        image: zhaoguanghui6/kubernetes-kafka:v1
        resources:
          requests:
            memory: "1Gi"
            cpu: 1000m
        ports:
        - containerPort: 9092
          name: server
        command:
        - sh
        - -c
        - "exec kafka-server-start.sh /opt/kafka/config/server.properties --override broker.id=${HOSTNAME##*-} 
          --override listeners=PLAINTEXT://:9092 
          --override advertised.listeners=PLAINTEXT://10.10.101.126:$((${HOSTNAME##*-}+30092))
          --override zookeeper.connect=zk-0.zk-hs.default.svc.cluster.local:2181,zk-0.zk-hs.default.svc.cluster.local:2181,zk-0.zk-hs.default.svc.cluster.local:2181 
          --override log.dir=/var/lib/kafka 
          --override auto.create.topics.enable=true 
          --override auto.leader.rebalance.enable=true 
          --override background.threads=10 
          --override compression.type=producer 
          --override delete.topic.enable=true 
          --override leader.imbalance.check.interval.seconds=300 
          --override leader.imbalance.per.broker.percentage=10 
          --override log.flush.interval.messages=9223372036854775807 
          --override log.flush.offset.checkpoint.interval.ms=60000 
          --override log.flush.scheduler.interval.ms=9223372036854775807 
          --override log.retention.bytes=-1 
          --override log.retention.hours=168 
          --override log.roll.hours=168 
          --override log.roll.jitter.hours=0 
          --override log.segment.bytes=1073741824 
          --override log.segment.delete.delay.ms=60000 
          --override message.max.bytes=1000012 
          --override min.insync.replicas=1 
          --override num.io.threads=8 
          --override num.network.threads=3 
          --override num.recovery.threads.per.data.dir=1 
          --override num.replica.fetchers=1 
          --override offset.metadata.max.bytes=4096 
          --override offsets.commit.required.acks=-1 
          --override offsets.commit.timeout.ms=5000 
          --override offsets.load.buffer.size=5242880 
          --override offsets.retention.check.interval.ms=600000 
          --override offsets.retention.minutes=1440 
          --override offsets.topic.compression.codec=0 
          --override offsets.topic.num.partitions=50 
          --override offsets.topic.replication.factor=3 
          --override offsets.topic.segment.bytes=104857600 
          --override queued.max.requests=500 
          --override quota.consumer.default=9223372036854775807 
          --override quota.producer.default=9223372036854775807 
          --override replica.fetch.min.bytes=1 
          --override replica.fetch.wait.max.ms=500 
          --override replica.high.watermark.checkpoint.interval.ms=5000 
          --override replica.lag.time.max.ms=10000 
          --override replica.socket.receive.buffer.bytes=65536 
          --override replica.socket.timeout.ms=30000 
          --override request.timeout.ms=30000 
          --override socket.receive.buffer.bytes=102400 
          --override socket.request.max.bytes=104857600 
          --override socket.send.buffer.bytes=102400 
          --override unclean.leader.election.enable=true 
          --override zookeeper.session.timeout.ms=6000 
          --override zookeeper.set.acl=false 
          --override broker.id.generation.enable=true 
          --override connections.max.idle.ms=600000 
          --override controlled.shutdown.enable=true 
          --override controlled.shutdown.max.retries=3 
          --override controlled.shutdown.retry.backoff.ms=5000 
          --override controller.socket.timeout.ms=30000 
          --override default.replication.factor=1 
          --override fetch.purgatory.purge.interval.requests=1000 
          --override group.max.session.timeout.ms=300000 
          --override group.min.session.timeout.ms=6000 
          --override inter.broker.protocol.version=0.10.2-IV0 
          --override log.cleaner.backoff.ms=15000 
          --override log.cleaner.dedupe.buffer.size=134217728 
          --override log.cleaner.delete.retention.ms=86400000 
          --override log.cleaner.enable=true 
          --override log.cleaner.io.buffer.load.factor=0.9 
          --override log.cleaner.io.buffer.size=524288 
          --override log.cleaner.io.max.bytes.per.second=1.7976931348623157E308 
          --override log.cleaner.min.cleanable.ratio=0.5 
          --override log.cleaner.min.compaction.lag.ms=0 
          --override log.cleaner.threads=1 
          --override log.cleanup.policy=delete 
          --override log.index.interval.bytes=4096 
          --override log.index.size.max.bytes=10485760 
          --override log.message.timestamp.difference.max.ms=9223372036854775807 
          --override log.message.timestamp.type=CreateTime 
          --override log.preallocate=false 
          --override log.retention.check.interval.ms=300000 
          --override max.connections.per.ip=2147483647 
          --override num.partitions=1 
          --override producer.purgatory.purge.interval.requests=1000 
          --override replica.fetch.backoff.ms=1000 
          --override replica.fetch.max.bytes=1048576 
          --override replica.fetch.response.max.bytes=10485760 
          --override reserved.broker.max.id=1000 "
        env:
        - name: KAFKA_HEAP_OPTS
          value : "-Xmx1G -Xms1G"
        - name: KAFKA_OPTS
          value: "-Dlogging.level=INFO"
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
        readinessProbe:
          exec:
           command:
            - sh
            - -c
            - "/opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server=localhost:9092"
          timeoutSeconds: 5
          periodSeconds: 5
          initialDelaySeconds: 70
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
      annotations:
        volume.beta.kubernetes.io/storage-class: "nfs-client"
    spec:
      accessModes: [ "ReadWriteMany" ]
      resources:
        requests:
          storage: 1Gi

最新文章链接,含镜像制作,v3.5.2

好的,以下是一个基本的在k8s部署zookeeper + kafka集群的配置教程: 1. 创建一个namespace 首先,我们需要创建一个namespace,用于部署zookeeper和kafka集群。可以使用以下命令创建一个名为“zookeeper-kafka”的namespace: ``` kubectl create namespace zookeeper-kafka ``` 2. 部署Zookeeper 接下来,我们需要部署Zookeeper。可以使用以下YAML文件创建一个Zookeeper服务: ``` apiVersion: v1 kind: Service metadata: name: zookeeper spec: selector: app: zookeeper ports: - name: client port: 2181 protocol: TCP - name: follower port: 2888 protocol: TCP - name: leader port: 3888 protocol: TCP --- apiVersion: apps/v1 kind: StatefulSet metadata: name: zookeeper spec: serviceName: zookeeper replicas: 3 selector: matchLabels: app: zookeeper template: metadata: labels: app: zookeeper spec: containers: - name: zookeeper image: zookeeper:3.5.8 ports: - name: client containerPort: 2181 - name: follower containerPort: 2888 - name: leader containerPort: 3888 volumeMounts: - name: datadir mountPath: /data env: - name: ZOO_MY_ID valueFrom: fieldRef: fieldPath: metadata.name - name: ZOO_SERVERS value: zookeeper-0.zookeeper:2888:3888,zookeeper-1.zookeeper:2888:3888,zookeeper-2.zookeeper:2888:3888 volumeClaimTemplates: - metadata: name: datadir spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi ``` 这将创建一个3个Pod的Zookeeper StatefulSet,并创建一个名为“zookeeper”的Service,暴露Zookeeper的客户端端口2181,follower端口2888和leader端口3888。 3. 部署Kafka 现在,我们可以部署Kafka。以下是一个Kafka部署的YAML文件示例: ``` apiVersion: v1 kind: Service metadata: name: kafka spec: type: NodePort selector: app: kafka ports: - name: kafka port: 9092 nodePort: 30092 protocol: TCP --- apiVersion: apps/v1 kind: StatefulSet metadata: name: kafka spec: serviceName: kafka replicas: 3 selector: matchLabels: app: kafka template: metadata: labels: app: kafka spec: containers: - name: kafka image: wurstmeister/kafka:2.13-2.7.0 ports: - name: kafka containerPort: 9092 env: - name: KAFKA_BROKER_ID valueFrom: fieldRef: fieldPath: metadata.name - name: KAFKA_ZOOKEEPER_CONNECT value: zookeeper-0.zookeeper:2181,zookeeper-1.zookeeper:2181,zookeeper-2.zookeeper:2181 - name: KAFKA_ADVERTISED_LISTENERS value: PLAINTEXT://$(hostname -f):9092 - name: KAFKA_LISTENERS value: PLAINTEXT://0.0.0.0:9092 - name: KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR value: "3" volumeMounts: - name: datadir mountPath: /data volumeClaimTemplates: - metadata: name: datadir spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi ``` 这将创建一个3个Pod的Kafka StatefulSet和一个名为“kafka”的Service,它将Kafka的9092端口暴露为NodePort 30092。 4. 验证部署 现在,您可以使用以下命令检查Zookeeper和Kafka是否正在运行: ``` kubectl get pods -n zookeeper-kafka ``` 您应该看到3个Zookeeper和3个Kafka Pod处于“Running”状态。 接下来,您可以使用以下命令检查Kafka是否正在监听端口30092(或您自己选择的端口): ``` kubectl get services -n zookeeper-kafka ``` 您应该看到一个名为“kafka”的service,它将Kafka的9092端口暴露为30092端口。可以使用此端口测试Kafka是否正常运行。 至此,您已经成功地在k8s部署zookeeper + kafka集群
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值