背景

在上一篇文章中(K8S如何部署ZooKeeper以及如何进行ZooKeeper的平滑替换)我们已经成功部署了ZooKeeper,但是真正切换时,报错如下

Too many connections from /xx.xx.xx.xx - max is 60

问题分析

在相关服务未迁移K8S时,各个服务都是散落在不同的服务器上的。而当迁移到K8S时,为了方便管理,对服务器进行了重新规划,同一个项目组使用相同的服务器资源,当副本数量较多时,从而出现了该问题。

解决方案

确定了问题出现的原因,那么就可以针对性的进行解决,我们可以按照下面的方式来修改上一篇中的部署脚本:

apiVersion: v1
kind: ConfigMap
metadata:
  name: zookeeper-config
  namespace: your-namespace
data:
  zoo.cfg: |
    clientPort=2181
    dataDir=/data
    dataLogDir=/data
    tickTime=2000
    initLimit=10
    syncLimit=5
    maxClientCnxns=6000
---
apiVersion: v1
kind: Service
metadata:
  name: zookeeper
  namespace: your-namespace
spec:
  selector:
    app: zookeeper
  type: NodePort
  ports:
    - name: client
      port: 2181
      targetPort: 2181
      nodePort: 30000
    - name: follower
      port: 2888
      targetPort: 2888
      nodePort: 30001
    - name: leader
      port: 3888
      targetPort: 3888
      nodePort: 30002
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: zookeeper
  namespace: your-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: project.node
                    operator: In
                    values:
                      - your-project-node
      containers:
        - name: zookeeper
          image: zookeeper:3.4.9
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          ports:
            - containerPort: 2181
            - containerPort: 2888
            - containerPort: 3888
          volumeMounts:
            - name: config-zookeeper
              mountPath: /conf/zoo.cfg
              subPath: zoo.cfg
            - name: data
              mountPath: /data
      volumes:
        - name: data
          hostPath:
            path: /var/lib/docker/zookeeper
        - name: config-zookeeper
          configMap:
            name: zookeeper-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai

附录:

ZooKeeper关于maxClientCnxns参数的官方解释:

单个客户端与单台服务器之间的连接数的限制,是ip级别的,默认是60,如果设置为0,那么表明不作任何限制。请注意这个限制的使用范围,仅仅是单台客户端机器与单台ZK服务器之间的连接数限制,不是针对指定客户端IP,也不是ZK集群的连接数限制,也不是单台ZK对所有客户端的连接数限制。

maxClientCnxns

Limits the number of concurrent connections (at the socket level) that a single client, identified by IP address, may make to a single member of the ZooKeeper ensemble. This is used to prevent certain classes of DoS attacks, including file descriptor exhaustion. The default is 60. Setting this to 0 entirely removes the limit on concurrent connections.

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐