k8s(docker-desktop)简单搭建zookeeper三节点集群

  • 前提
    • 已经安装了docker-desktop,并且其中的kubernetes启动运行的。或者安装了k8s环境就行
    • 可以正常下载dockerhub的镜像,这里采用了dockerhub官方制作的镜像
  • 启动集群
    • 执行命令 kubectl apply -f zk1.yaml,执行前先把/data的3个宿主机路径修改一下,把datadir的volumeMounts注释放开。不修改也可以启动集群

zk1.yaml文件如下

apiVersion: v1
kind: Service
metadata:
  name: zk1
  labels:
    zk: zk1
spec:
  selector:
    zk: zk1
  ports:
  - protocol: TCP
    port: 2181
    targetPort: 2181
    name: port-1
  - protocol: TCP
    port: 2888
    targetPort: 2888
    name: port-2
  - protocol: TCP
    port: 3888
    targetPort: 3888
    name: port-3
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  name: zk2
  labels:
    zk: zk2
spec:
  selector:
    zk: zk2
  ports:
  - protocol: TCP
    port: 2181
    targetPort: 2181
    name: port-1
  - protocol: TCP
    port: 2888
    targetPort: 2888
    name: port-2
  - protocol: TCP
    port: 3888
    targetPort: 3888
    name: port-3
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  name: zk3
  labels:
    zk: zk3
spec:
  selector:
    zk: zk3
  ports:
  - protocol: TCP
    port: 2181
    targetPort: 2181
    name: port-1
  - protocol: TCP
    port: 2888
    targetPort: 2888
    name: port-2
  - protocol: TCP
    port: 3888
    targetPort: 3888
    name: port-3
  type: NodePort
---
apiVersion: v1
kind: Pod
metadata:
  name: zk1
  namespace: default
  labels:
    zk: zk1
spec:
  containers:
  - image: zookeeper:3.6.3
    imagePullPolicy: IfNotPresent
    name: zk
    env:
    - name: ZOO_MY_ID
      value: "1"
    # 默认这个值,可以参考容器里面的entry.sh的内容
    # 也可以去dockerhub的链接去查看这个镜像的dockerfile
    # - name: ZOO_DATA_DIR
    #   value: "/data"
    - name: ZOO_INIT_LIMIT
      value: "5000"
    - name: ZOO_SYNC_LIMIT
      value: "2000"
    - name: JVMFLAGS
      value: "-Dzookeeper.electionPortBindRetry=50" # 连接集群重试50次,默认三次
    - name: ZOO_SERVERS
      value: "server.1=0.0.0.0:2888:3888;2181 server.2=zk2.default.svc.cluster.local:2888:3888;2181 server.3=zk3.default.svc.cluster.local:2888:3888;2181"
    ports:
    - containerPort: 2181
      protocol: TCP
      name: client
    - containerPort: 2888
      protocol: TCP
      name: server
    - containerPort: 3888
      protocol: TCP
      name: leader-election
    volumeMounts:
    # 这里用环境变量,zoo.cfg这个文件是不存在
    # - mountPath: /apache-zookeeper-3.6.3-bin/conf
    #  name: conf
    # 为什么一定是data 不是/tmp/zookeeper ZOO_DATA_DIR指定的
    # 不配置数据持久的话,把这个配置注释掉 最好配置,不然pod删了数据就没了
    #- mountPath: /data
    #  name: datadir
  volumes:
  #- name: conf
  #  hostPath:
  #    path: /XXXX/conf1XXXX
  - name: datadir
    hostPath:
    # 第一次启动最好是个空文件夹 配置自己的数据的路径,3个pod三个路径
      path: /XXXX/tmp1/zookeeper
    #  type: Directory
---
apiVersion: v1
kind: Pod
metadata:
  name: zk2
  namespace: default
  labels:
    zk: zk2
spec:
  containers:
  - image: zookeeper:3.6.3
    imagePullPolicy: IfNotPresent
    name: zk
    env:
    - name: ZOO_MY_ID
      value: "2"
    - name: ZOO_INIT_LIMIT
      value: "5000"
    - name: ZOO_SYNC_LIMIT
      value: "2000"
    - name: JVMFLAGS
      value: "-Dzookeeper.electionPortBindRetry=50"
    - name: ZOO_SERVERS
      value: "server.1=zk1.default.svc.cluster.local:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zk3.default.svc.cluster.local:2888:3888;2181"
      # 如果不把本机的ip改成0.0.0.0,会报错"server.1=zk1.default.svc.cluster.local:2888:3888;2181 server.2=zk2.default.svc.cluster.local:2888:3888;2181 server.3=zk3.default.svc.cluster.local:2888:3888;2181"
    ports:
    - containerPort: 2181
      protocol: TCP
      name: client
    - containerPort: 2888
      protocol: TCP
      name: server
    - containerPort: 3888
      protocol: TCP
      name: leader-election
    volumeMounts:
    # - mountPath: /apache-zookeeper-3.6.3-bin/conf
    #  name: conf
    #- mountPath: /data
     # name: datadir
  volumes:
  # - name: conf
  #  hostPath:
  #    path: /XXXX/conf2
  - name: datadir
    hostPath:
      path: /XXXX/tmp2/zookeeper
---
apiVersion: v1
kind: Pod
metadata:
  name: zk3
  namespace: default
  labels:
    zk: zk3
spec:
  containers:
  - image: zookeeper:3.6.3
    imagePullPolicy: IfNotPresent
    name: zk
    env:
    - name: ZOO_MY_ID
      value: "3"
    - name: ZOO_INIT_LIMIT
      value: "5000"
    - name: ZOO_SYNC_LIMIT
      value: "2000"
    - name: JVMFLAGS
      value: "-Dzookeeper.electionPortBindRetry=50"
    - name: ZOO_SERVERS
      value: "server.1=zk1.default.svc.cluster.local:2888:3888;2181 server.2=zk2.default.svc.cluster.local:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181"
    ports:
    - containerPort: 2181
      protocol: TCP
      name: client
    - containerPort: 2888
      protocol: TCP
      name: server
    - containerPort: 3888
      protocol: TCP
      name: leader-election
    volumeMounts:
    # - mountPath: /apache-zookeeper-3.6.3-bin/conf
    #  name: conf
    #- mountPath: /data
    #  name: datadir
  volumes:
  #- name: conf
  #  hostPath:
  #    path: /XXXX/conf3
  - name: datadir
    hostPath:
      path: /XXXX/tmp3/zookeeper
  • 连接pod分别查看状态kubectl exec -it zk1 -- sh kubectl exec -it zk2 -- sh kubectl exec -it zk3 -- sh
# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
上一篇:基于Wireshark HTTPS POST请求报文


下一篇:HttpConnection工具类