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"
          
其他步骤与上面的一致

你可能感兴趣的:(kafka,docker)