各节点均安装zk
准备工作
准备工作每个节点都进行
安装java环境
可以编译或者yum安装,我这边为了省事直接yum了
yum -y install java
下载zk二进制包
也可下载源码包自己编译
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.1/apache-zookeeper-3.8.1-bin.tar.gz --no-check-certificate tar -xf apache-zookeeper-3.8.1-bin.tar.gz -C /usr/local/ mv /usr/local/{apache-zookeeper-3.8.1-bin,zookeeper} cp /usr/local/zookeeper/conf/{zoo_sample.cfg,zoo.cfg} # 复制一份配置文件模板 mkdir /usr/local/zookeeper/{data,logs}
修改配置文件
我这边3节点默认配置文件都保持一致
cat > /usr/local/zookeeper/conf/zoo.cfg <<EOF tickTime=2000 # 服务器之间或者服务器与客户端之间维持心跳时间间隔,tickTime单位是毫秒 initLimit=10 # 集群中follower与leader之间初始连接心跳数为10 * tickTime syncLimit=5 # 集群中follower与leader之间请求和应答之间能容忍最多心跳 5 * tickTime dataDir=../data # 数据存放目录,使用的相对路径 dataLogDir=../logs # 日志存放目录,使用的相对路径 clientPort=2181 # 客户端连接端口 maxClientCnxns=60 # 客户端最大连接数,默认即60 4lw.commands.whitelist=* # 客户端获取zookeeper服务当前状态及信息 server.1=10.10.21.227:2888:3888 server.2=10.10.21.228:2888:3888 server.3=10.10.21.229:2888:3888 # 三个节点配置,格式为 server.服务编号=服务地址、leader和follower通信端口、选举端口 EOF
修改配置
node01
节点标记ID
echo "1" > /usr/local/zookeeper/data/myid # 每个节点不同即可,范围为1-255
启动zk
/usr/local/zookeeper/bin/zkServer.sh start # 如确定配置无误,但是无法启动zk可以尝试cd进bin目录使用相对路径启动
node02
节点标记ID
echo "2" > /usr/local/zookeeper/data/myid
启动zk
/usr/local/zookeeper/bin/zkServer.sh start
node03
节点标记ID
echo "3" > /usr/local/zookeeper/data/myid
启动zk
/usr/local/zookeeper/bin/zkServer.sh start
集群状态检测
建议等几十秒再检查,避免未完全启动
[root@node01 bin]# ./zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower [root@node02 bin]# ./zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: leader [root@node03 bin]# ./zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower
制作zk集群镜像
从上面我们大概知道了构建zk集群需要准备哪些东西了
下面就准备给它容器化
准备构建容器所需文件
mkdir zookeeper/ # 创建目录存放 cp apache-zookeeper-3.8.1-bin.tar.gz zookeeper/ # 拷贝zk二进制文件包 cd zookeeper/
准备配置文件
cat <<EOF > zoo.cfg tickTime={ZOOK_TICK_TIME} initLimit={ZOOK_INIT_LIMIT} syncLimit={ZOOK_SYNC_LIMIT} dataDir={ZOOK_DATA_DIR} dataLogDir={ZOOK_LOG_DIR} clientPort={ZOOK_CLIENT_PORT} maxClientCnxns={ZOOK_MAX_CLIENT_CNXNS} 4lw.commands.whitelist=* EOF # 这里{}定义了很多占位用的数据,后面会用来替换
准备Dockerfile
cat <<EOF > Dockerfile FROM openjdk:8-jre RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone # 调整时区,也可以之间在k8s的yaml文件将hostpath的/etc/localtime 挂载给pod来定义时区 ENV VERSION=3.8.1 ADD ./apache-zookeeper-${VERSION}-bin.tar.gz / # 解压二进制包到 / 下 COPY ./zoo.cfg /apache-zookeeper-${VERSION}-bin/conf RUN mv /apache-zookeeper-${VERSION}-bin /zookeeper COPY ./entrypoint.sh /entrypoint.sh EXPOSE 2181 2888 3888 CMD ["bin/bash", "/entrypoint.sh"] EOF # 这里使用了环境变量来代替版本,方便后续迭代升级
准备entrypoint.sh
cat entrypoint.sh # 定义zk需要的一些环境变量 ZOOK_BIN_DIR=/zookeeper/bin ZOOK_CONF_DIR=/zookeeper/conf/zoo.cfg # ${ZOOK_TICK_TIME:-2000}是一种Shell中的环境变量替换语法,表示如果环境变量ZOOK_TICK_TIME未定义或为空,则使用默认值2000进行替换 # 在使用sed命令替换文本时,可以在需要替换的文本内容中添加环境变量名并设置默认值。语法格式为 ${变量名:-默认值} # 下面这一部分可以在k8s的yaml中使用环境变量传参,如不传就会使用默认的参数 sed -i s#{ZOOK_TICK_TIME}#${ZOOK_TICK_TIME:-2000}#g ${ZOOK_CONF_DIR} sed -i s#{ZOOK_INIT_LIMIT}#${ZOOK_INIT_LIMIT:-10}#g ${ZOOK_CONF_DIR} sed -i s#{ZOOK_SYNC_LIMIT}#${ZOOK_SYNC_LIMIT:-5}#g ${ZOOK_CONF_DIR} sed -i s#{ZOOK_DATA_DIR}#${ZOOK_DATA_DIR:-/data}#g ${ZOOK_CONF_DIR} sed -i s#{ZOOK_LOG_DIR}#${ZOOK_LOG_DIR:-/logs}#g ${ZOOK_CONF_DIR} sed -i s#{ZOOK_CLIENT_PORT}#${ZOOK_CLIENT_PORT:-2181}#g ${ZOOK_CONF_DIR} sed -i s#{ZOOK_MAX_CLIENT_CNXNS}#${ZOOK_MAX_CLIENT_CNXNS:-60}#g ${ZOOK_CONF_DIR} # 通过ENV的方式注入ZK的地址,并循环加入到配置文件中 for server in ${ZOOK_SERVERS:-server.1=localhost:2181:2888:3888} do echo ${server} >> ${ZOOK_CONF_DIR} done # 建立myid文件,通过pod的主机的名称提取,要求采用StatefulSet方式来编排,否则无法提取到匹配的主机名 ZOOK_MYID=$(( $(hostname | sed 's#.*-##g') + 1 )) echo "${ZOOK_MYID:-99}" > ${ZOOK_DATA_DIR:-/data}/myid # 前台运行zookeeper cd ${ZOOK_BIN_DIR} ./zkServer.sh start-foreground
构建镜像
上面三台机器是没安装docker的,我这边把dockerfile需要的文件都拷贝到docker环境进行构建
[root@node01 ~]# scp -r zookeeper/ 10.10.21.223:/opt # 10.10.21.223是我k8s中的node01 docker build -t harbor.org/library/zk:3.8.1 . # 改好tag方便push
上传镜像
上传镜像涉及到harbor,可以参考这篇blog
docker login -u admin -p Harbor12345 harbor.org # 登录harbor docker push harbor.org/library/zk:3.8.1 # 上传至我自己的harbor中
k8s安装zk集群
Zookeeper属于有状态服务;
Zookeeper集群存在角色之分;
Zookeeper集群每个节点都需要存储自己的数据;
Zookeeper集群每个节点都需要有一个唯一的地
创建docker-register的secret
kubectl create secret docker-registry harbor-admin --docker-username=admin --docker-password=Harbor12345
这样可以避免每个节点都去login登录harbor
创建headless
cat <<EOF > zk-headless.yaml apiVersion: v1 kind: Service metadata: name: zk-svc spec: clusterIP: None selector: app: zk ports: - name: client port: 2181 targetPort: 2181 - name: leader-fllow port: 2888 targetPort: 2888 - name: selection port: 3888 targetPort: 3888 EOF
创建sts
其中涉及到nfs的存储类,可以参考这篇blog
vim zk-sts.yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: zk spec: serviceName: zk-svc replicas: 3 selector: matchLabels: app: zk template: metadata: labels: app: zk spec: affinity: podAntiAffinity: # pod亲和性控制pod不分配到相同节点 requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: ["zk"] topologyKey: "kubernetes.io/hostname" imagePullSecrets: - name: harbor-admin # 和docker-register的secret对应 containers: - name: zk image: harbor.org/library/zk:3.8.1 # 上面新打包的镜像 imagePullPolicy: Always env: - name: ZOOK_SERVERS # 传参进去 value: "server.1=zk-0.zk-svc.default.svc.cluster.local:2888:3888 server.2=zk-1.zk-svc.default.svc.cluster.local:2888:3888 server.3=zk-2.zk-svc.default.svc.cluster.local:2888:3888" ports: - name: client containerPort: 2181 - name: leader-fllow containerPort: 2888 - name: selection containerPort: 3888 readinessProbe: # 就绪探针 exec: command: - "/bin/bash" - "-c" - '[[ "$(/zookeeper/bin/zkServer.sh status 2>/dev/null|grep 2181)" ]] || exit 1' initialDelaySeconds: 10 timeoutSeconds: 5 livenessProbe: # 存活探针 exec: command: - "/bin/bash" - "-c" - '[[ "$(/zookeeper/bin/zkServer.sh status 2>/dev/null|grep 2181)" ]] || exit 1' initialDelaySeconds: 10 timeoutSeconds: 5 volumeMounts: - name: data mountPath: /data volumeClaimTemplates: - metadata: name: data spec: accessModes: ["ReadWriteMany"] storageClassName: nfs-storage # 和已有的存储类需要对应 resources: requests: storage: 10Gi
启动集群并检查
kubectl apply -f zk-headless.yaml kubectl apply -f zk-sts.yaml kubectl get pod -owide
正常结果应该是pod都启动了,且不在一个节点上
[root@node1 zookeeper]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE LABELS kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 29h component=apiserver,provider=kubernetes zk-svc ClusterIP None <none> 2181/TCP,2888/TCP,3888/TCP 79m <none> [root@node1 zookeeper]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES zk-0 1/1 Running 0 5m11s 172.16.120.199 10.10.21.223 <none> <none> zk-1 1/1 Running 0 5m 172.16.78.70 10.10.21.225 <none> <none> zk-2 1/1 Running 0 87s 172.16.152.194 10.10.21.224 <none> <none> 复制成功,转载请保留本站链接: www.chenjiao.cloud
然后进集群检查下zk状态
[root@node1 zookeeper]# kubectl exec zk-0 -- /zookeeper/bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower [root@node1 zookeeper]# kubectl exec zk-1 -- /zookeeper/bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: leader [root@node1 zookeeper]# kubectl exec zk-2 -- /zookeeper/bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower
连接zk集群测试
kubectl exec -it zk-2 -- /bin/bash root@zk-2:/# /zookeeper/bin/zkCli.sh -server zk-svc [zk: zk-svc(CONNECTED) 1] create /kubernetes version1.26.5 Created /kubernetes [zk: zk-svc(CONNECTED) 2] get /kubernetes version1.26.5