k8s部署单节点zookeeper

给zookeeper服务器节点打标签

kubectl get node --show-labels=true
kubectl label nodes sd-redis  zookeeper-server=master

zookeeper服务器节点创建目录

mkdir -p /home/zookeeper/conf /home/zookeeper/data /home/zookeeper/datalog

编辑部署文件zookeeper.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: k8s-zookeeper

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: zookeeper-cs
  name: zookeeper-cs
  namespace: k8s-zookeeper
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      name: web
      nodePort: 30180
    - port: 2181
      targetPort: 2181
      name: client
      nodePort: 30181
  selector:
    app: zookeeper

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: zookeeper-hs
  name: zookeeper-hs
  namespace: k8s-zookeeper
spec:
  type: ClusterIP
  clusterIP: None
  publishNotReadyAddresses: true
  ports:
    - name: tcp-client
      port: 2181
      targetPort: client
  selector:
    app: zookeeper

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: zookeeper
  name: zookeeper
  namespace: k8s-zookeeper
spec:
  serviceName: zookeeper-hs
  replicas: 1
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      containers:
        - name: zookeeper
          image: zookeeper:3.8.0
          imagePullPolicy: IfNotPresent
          ports:
            - name: client
              containerPort: 2181
          volumeMounts:
            - name: zookeeper-config
              mountPath: /conf/
            - name: zookeeper-data
              mountPath: /data/
            - name: zookeeper-datalog
              mountPath: /datalog/
      volumes:
        - name: zookeeper-config
          hostPath:
            path: /home/zookeeper/conf/
        - name: zookeeper-data
          hostPath:
            path: /home/zookeeper/data/
        - name: zookeeper-datalog
          hostPath:
            path: /home/zookeeper/datalog/
      nodeSelector:
        zookeeper-server: master

执行部署

kubectl apply -f zookeeper.yaml

查看部署情况

kubectl get pod -n k8s-zookeeper -o wide
# 查看pod状态
kubectl -n k8s-zookeeper describe pod zookeeper-0
# 查看日志
kubectl -n k8s-zookeeper logs zookeeper-0 --all-containers=true
# 进入容器
kubectl exec -it pod/zookeeper-0 -n k8s-zookeeper -- bash