k8s zookeeper 问题解决:UnknownHostException: zk-2.zk-hs.default.svc.cluster.local

报错信息:

Cannot open channel to 3 at election address

UnknownHostException: zk-2.zk-hs.default.svc.cluster.local 

 

解决思路:

配置文件$ ZOOKEEPER_HOME / conf / zoo.cfg,每个机器IP设置为“0.0.0.0”,如下所示:

server.1=0.0.0.0:2888:3888
server.2=zk-1.zk-hs.default.svc.cluster.local:2888:3888
server.3=zk-2.zk-hs.default.svc.cluster.local:2888:3888
 

修改方法:

下载镜像guglecontainers/kubernetes-zookeeper:1.0-3.4.10

下载文件:https://github.com/kow3ns/kubernetes-zookeeper/tree/master/docker/scripts/start-zookeeper

修改文件内容:当是本机的时候使用0.0.0.0ip

function print_servers() {
    for (( i=1; i<=$SERVERS; i++ ))
    do

        #echo "server.$i=$NAME-$((i-1)).$DOMAIN:$SERVER_PORT:$ELECTION_PORT"
        if [ $ORD -eq $((i-1)) ]; then
            echo "server.$i=0.0.0.0:$SERVER_PORT:$ELECTION_PORT"
        else
            echo "server.$i=$NAME-$((i-1)).$DOMAIN:$SERVER_PORT:$ELECTION_PORT"
        fi
    done
}

重新生成镜像:

mkdir zookeeper
cd zookeeper

docker pull guglecontainers/kubernetes-zookeeper:1.0-3.4.10

cat <<'END' > Dockerfile
FROM guglecontainers/kubernetes-zookeeper:1.0-3.4.10
MAINTAINER ""
RUN apt-get update
RUN apt-get install -y vim
COPY scripts /opt/zookeeper/bin/
END

docker build -t redblue/kubernetes-zookeeper:1.0-3.4.10 ./

cd ..

 

# zookeeper部署
cat <<'END' > zk-sts.yaml
apiVersion: v1
kind: Service
metadata:
  name: zk-hs
  labels:
    app: zk
spec:
  ports:
  - port: 2888
    name: server
  - port: 3888
    name: leader-election
  clusterIP: None
  selector:
    app: zk
---
apiVersion: v1
kind: Service
metadata:
  name: zk-cs
  labels:
    app: zk
spec:
  type: NodePort
  ports:
  - port: 2181
    targetPort: 2181
    name: client
    nodePort: 32181
  selector:
    app: zk
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  selector:
    matchLabels:
      app: zk
  maxUnavailable: 1
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zk
spec:
  selector:
    matchLabels:
      app: zk
  serviceName: zk-hs
  replicas: 3
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: zk
    spec:
      containers:
      - name: kubernetes-zookeeper
        imagePullPolicy: IfNotPresent
        image: "redblue/kubernetes-zookeeper:1.0-3.4.10"
        resources:
          requests:
            memory: "512M"
            cpu: "0.1"
        ports:
        - containerPort: 2181
          name: client
        - containerPort: 2888
          name: server
        - containerPort: 3888
          name: leader-election
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        command:
        - sh
        - -c
        - "start-zookeeper \
          --servers=3 \
          --data_dir=/var/lib/zookeeper/data \
          --data_log_dir=/var/lib/zookeeper/data/log \
          --conf_dir=/opt/zookeeper/conf \
          --client_port=2181 \
          --election_port=3888 \
          --server_port=2888 \
          --tick_time=2000 \
          --init_limit=10 \
          --sync_limit=5 \
          --heap=256M \
          --max_client_cnxns=60 \
          --snap_retain_count=3 \
          --purge_interval=12 \
          --max_session_timeout=40000 \
          --min_session_timeout=4000 \
          --log_level=INFO"
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - "zookeeper-ready 2181"
          initialDelaySeconds: 10
          timeoutSeconds: 5
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - "zookeeper-ready 2181"
          initialDelaySeconds: 10
          timeoutSeconds: 5
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/zookeeper
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
      annotations:
        volume.beta.kubernetes.io/storage-class: "ceph-rbd"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 3Gi
END
kubectl apply -f zk-sts.yaml

 

有问题联系我的qq:23326199

你可能感兴趣的:(k8s zookeeper 问题解决:UnknownHostException: zk-2.zk-hs.default.svc.cluster.local)