在kubernetes上部署单机zookeeper

avatar 2022年4月11日18:06:56 评论 1,009 次浏览

因为需求在kubernetes上部署zookeeper,但是zookeeper是有状态的服务,所以我在集群中已经部署了ceph,如有兴趣可以参考:https://www.wulaoer.org/?p=2290 下面看一下zookeeper如何部署的,在部署之前需要先把dockerfile镜像制作好,我把zookeeper已经下载好了,放到我的docker目录下,并把zookeeper的配置文件zoo.cgf文件已经修改。

[wolf@wulaoer.org🔥🔥🔥🔥 docker]# cat Dockerfile
FROM wolf27w/centos7-jdk:1.0
WORKDIR /usr/local
#RUN wget https://downloads.apache.org/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
#RUN tar -zxf apache-zookeeper-3.6.3-bin.tar.gz && mv apache-zookeeper-3.6.3-bin zookeeper && rm -rf apache-zookeeper-3.6.3-bin.tar.gz
COPY apache-zookeeper-3.6.3-bin zookeeper
ENV ZOOKEEPER_HOME /usr/local/zookeeper
ENV PATH $PATH:$ZOOKEEPER_HOME/bin
ADD zoo.cfg $ZOOKEEPER_HOME/conf/zoo.cfg
EXPOSE 2181
CMD $ZOOKEEPER_HOME/bin/zkServer.sh start-foreground

[wolf@wulaoer.org🔥🔥🔥🔥 docker]# docker build -t zookeeper:3.6.3 .

如果你的基础镜像已经安装了wget,可以直接替换成自己的基础镜像即可,但是基础镜像必须安装了jdk,我这里已经安装了,为了方便没有基础镜像的就注释掉了,然后下载到本地即可。

制作成镜像,需要传到仓库中,但是建议在上传到仓库之前可以进入容器运行一下,如果没有问题在上传。

[wolf@wulaoer.org🔥🔥🔥🔥 docker]# docker tag zookeeper:3.6.3 wolf27w/zookeeper:3.6.3
[wolf@wulaoer.org🔥🔥🔥🔥 docker]# docker push wolf27w/zookeeper:3.6.3

镜像已经上传完成,下面开始写kubernetes的yaml文件,这里有两个注意事项,第一个是pv和pvc,第二个就是访问方式,我这里使用的是nodeport,如果在一个集群中可以直接请求。

[wolf@wulaoer.org🔥🔥🔥🔥 docker]# cat zookeeper.yaml
apiVersion: v1
kind: Service
metadata:
  name: zookeeper
  namespace: zookeeper-system
  labels:
    app: zookeeper
spec:
  type: NodePort
  ports:
    - port: 2181
  selector:
    app: zookeeper
    tier: zookeeper
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zookeeper-pv-claim
  namespace: zookeeper-system
  labels:
    app: zookeeper
spec:
  storageClassName: rook-cephfs
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: zookeeper
  namespace: zookeeper-system
  labels:
    app: zookeeper
    tier: zookeeper
spec:
  selector:
    matchLabels:
      app: zookeeper
      tier: zookeeper
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: zookeeper
        tier: zookeeper
    spec:
      containers:
        - image: wolf27w/zookeeper:3.6.3
          name: zookeeper
          ports:
            - containerPort: 2181
              name: zookeeper
          volumeMounts:
            - name: zookeeper-persistent-storage
              mountPath: /usr/local/zookeeper/data
      volumes:
        - name: zookeeper-persistent-storage
          persistentVolumeClaim:
            claimName: zookeeper-pv-claim

上面从ceph中创建一个名为zookeeper-pv-claim的pv,并关联到zookeeper的数据目录下,这样就能保证zookeeper的数据能够持续华。

[wolf@wulaoer.org🔥🔥🔥🔥 docker]# kubectl apply -f zookeeper.yaml
service/zookeeper created
persistentvolumeclaim/zookeeper-pv-claim unchanged
[wolf@wulaoer.org🔥🔥🔥🔥 docker]# kubectl get pod -n zookeeper-system
NAME                         READY   STATUS    RESTARTS   AGE
zookeeper-58c6754486-jbflw   1/1     Running   0          35m
[wolf@wulaoer.org🔥🔥🔥🔥 docker]# kubectl get svc -n zookeeper-system
NAME        TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
zookeeper   NodePort   172.16.183.224   <none>        2181:32586/TCP   36m

zookeeper单机集群就创建好了,对外端口也创建出来了,直接利用node的ip加32586端口直接访问即可,zookeeper在kubernetes上单机部署就完成了。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: