1. 程式人生 > >Kubernetes編排Kafka Docker映象l的方式搭建叢集

Kubernetes編排Kafka Docker映象l的方式搭建叢集

本文基於已經安裝了k8s和docker的情況下進行的

k8s 編排kafk容器可以有statefulset和deployment兩種方式。分別是有狀態和無狀態的,具體區別可以百度一下,這裡只介紹叢集搭建的方法。

需要先準備若干yml配置檔案(檔名自己定義,不固定),上傳到linux機器的任意資料夾裡(比如home下面自己建立一個k8skafka資料夾)

第一種:statefulset叢集編排

1.PV.yml用於建立statefulset所需要的持久卷pod,

apiVersion: v1
kind: PersistentVolume
metadata:
    name: kafkapv1
    annotations:
      volume.beta.kubernetes.io/storage-class: "anything"
    labels: 
      pv: kafkapv
spec:
    capacity:
      storage: 10Gi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
#    storageClassName: ""
    hostPath:
      path: /opt/pv/kafka1

---

apiVersion: v1
kind: PersistentVolume
metadata:
    name: kafkapv2
    annotations:
      volume.beta.kubernetes.io/storage-class: "anything"
    labels:
      pv: kafkapv
spec:
    capacity:
      storage: 10Gi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
#    storageClassName: ""
    hostPath:
      path: /opt/pv/kafka2
---

apiVersion: v1
kind: PersistentVolume
metadata:
    name: kafkapv3
    annotations:
      volume.beta.kubernetes.io/storage-class: "anything"
    labels:
      pv: kafkapv
spec:
    capacity:
      storage: 10Gi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
#   storageClassName: ""
    hostPath:
      path: /opt/pv/kafka3


2.kafka-hs-svc.yml,  kafka host service本地service服務

apiVersion: v1
kind: Service
metadata:
  name: kafka-hs
  namespace: service-software
  labels:
    app: kafka
spec:
  ports:
  - port: 9093
    name: server
  clusterIP: None
  selector:
    app: kafka

3.kafka-statefulset.yml 用於建立kafka statefulset pod,注意修改kafka映象地址,映象版本號,zk地址、埠號,

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: kafka
  namespace: service-software
spec:
  serviceName: kafka-hs
  replicas: 3
  template:
    metadata:
      labels:
        app: kafka
    spec:
      terminationGracePeriodSeconds: 300
      containers:
      - name: k8skafka
        imagePullPolicy: Always
        image: 172.21.1.205:8088/ucenter/wurstmeister_kafka:2.11-2.0.0
        resources:
          requests:
            memory: 500Mi
          #  cpu: 500m
          limits:
            memory: 2G
        ports:
        - containerPort: 9093
          name: server
        command:
        - sh
        - -c
        - "exec kafka-server-start.sh /opt/kafka/config/server.properties --override broker.id=${HOSTNAME##*-} \
          --override listeners=PLAINTEXT://:9093 \
          --override advertised.listeners=PLAINTEXT://${KAFKA_HOST_NAME}:9093
          --override zookeeper.connect=zk-cs: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=false \
          --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=2.0.0-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 : "-Xmx1024M -Xms1024M"
        - name: KAFKA_OPTS
          value: "-Dlogging.level=INFO"
        - name: KAFKA_HOST_NAME
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
        readinessProbe:
          exec:
           command:
            - sh
            - -c
            - "/opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server=localhost:9093"
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
      annotations:
        volume.beta.kubernetes.io/storage-class: "anything"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi
      selector: 
        matchLabels: 
         pv: kafkapv
 

4.kafka-out-svc.yml  kafka提供對外訪問的服務

apiVersion: v1
kind: Service
metadata:
  name: kafka-svc-0
  namespace: service-software
spec:
  type: NodePort
  ports:
  - port: 9093
    targetPort: 9093
    nodePort: 30006
  selector:
    app: kafka-0
---
apiVersion: v1
kind: Service
metadata:
  name: kafka-svc-1
  namespace: service-software
spec:
  type: NodePort
  ports:
  - port: 9093
    targetPort: 9093
    nodePort: 30007
  selector:
    app: kafka-1
---
apiVersion: v1
kind: Service
metadata:
  name: kafka-svc-2
  namespace: service-software
spec:
  type: NodePort
  ports:
  - port: 9093
    targetPort: 9093
    nodePort: 30008
  selector:
    app: kafka-2

5.在yml檔案對應的目錄下,使用k8s命令列建立pv svc pod 

按照上面檔案的順序依次執行kubectl create --validate=false -f xxx.yaml

6.檢視狀態

檢視pv #: kubectl get pv -o wide |grep kaf

檢視Pod   #:  kubectl get pod -o wide |grep kafka

檢視Service #:  kubectl get service -o wide |grep kafka

 

第二種:建立deployment無狀態叢集

yml檔案的區別:pv不是必須的了,因此這裡就不部署pv了,不需要pv.ml,然後svc檔案跟上面都一樣,

然後把kafka-statefulset.yml換成kafka-dep.yml

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: kafka-0
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: kafka-0
    spec:
      containers:
      - name: kafka-0
        image: 172.21.1.205:8088/ucenter/wurstmeister_kafka:2.11-2.0.0
        imagePullPolicy: Always
        ports:
        - containerPort: 9093
        env:    
        - name: KAFKA_LISTENERS
          value: PLAINTEXT://:9093
#        - name: KAFKA_ADVERTISED_LISTENERS
#          value: PLAINTEXT://172.21.1.206:30006
        - name: KAFKA_ADVERTISED_HOST_NAME 
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: KAFKA_ADVERTISED_PORT
          value: "30006"
        - name: KAFKA_ZOOKEEPER_CONNECT
          value: zk-cs:2181
        - name: KAFKA_BROKER_ID
          value: "0"
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: kafka-1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: kafka-1
    spec:
      containers:
      - name: kafka-1
        image: 172.21.1.205:8088/ucenter/wurstmeister_kafka:2.11-2.0.0
        imagePullPolicy: Always
        ports:
        - containerPort: 9093
        env:       
        - name: KAFKA_LISTENERS
          value: PLAINTEXT://:9093
#        - name: KAFKA_ADVERTISED_LISTENERS
#          value: PLAINTEXT://172.21.1.206:30007
        - name: KAFKA_ADVERTISED_HOST_NAME
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: KAFKA_ADVERTISED_PORT
          value: "30007"
        - name: KAFKA_ZOOKEEPER_CONNECT
          value: zk-cs:2181
        - name: KAFKA_BROKER_ID
          value: "1"
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: kafka-2
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: kafka-2
    spec:
      containers:
      - name: kafka-2
        image: 172.21.1.205:8088/ucenter/wurstmeister_kafka:2.11-2.0.0
        imagePullPolicy: Always
        ports:
        - containerPort: 9093
        env:
        - name: KAFKA_LISTENERS
          value: PLAINTEXT://:9093
#        - name: KAFKA_ADVERTISED_LISTENERS
#          value: PLAINTEXT://172.21.1.206:30008
        - name: KAFKA_ADVERTISED_HOST_NAME 
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: KAFKA_ADVERTISED_PORT
          value: "30008"
        - name: KAFKA_ZOOKEEPER_CONNECT
          value: zk-cs:2181
        - name: KAFKA_BROKER_ID
          value: "2"
          
其他步驟與上面的一致