一 工作目录
root@k8s-master-01:/data/k8s/yaml# pwd
/data/k8s/yaml
二 创建zookeeper namespace
2.1 编写zookeeper-ns.yaml
root@k8s-master-01:/data/k8s/yaml# cat namespace/zookeeper-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
name: zookeeper
2.2 创建zookeeper ns
root@k8s-master-01:/data/k8s/yaml# kubectl apply -f namespace/zookeeper-ns.yaml
namespace/zookeeper created
2.3 验证zookeeper ns
root@k8s-master-01:/data/k8s/yaml# kubectl get ns
NAME STATUS AGE
default Active 26d
kube-node-lease Active 26d
kube-public Active 26d
kube-system Active 26d
kubernetes-dashboard Active 25d
zookeeper Active 28s
三 部署zookeeper
3.1 编写zookeeper.yaml
查看代码
root@k8s-master-01:/data/k8s/yaml# cat zookeeper/zookeeper.yaml
apiVersion: v1
kind: Service
metadata:
name: zookeeper1
namespace: zookeeper
spec:
type: NodePort
ports:
- name: client
port: 2181
nodePort: 42181
- name: followers
port: 2888
- name: election
port: 3888
selector:
app: zookeeper
server-id: "1"
---
apiVersion: v1
kind: Service
metadata:
name: zookeeper2
namespace: zookeeper
spec:
type: NodePort
ports:
- name: client
port: 2181
nodePort: 42182
- name: followers
port: 2888
- name: election
port: 3888
selector:
app: zookeeper
server-id: "2"
---
apiVersion: v1
kind: Service
metadata:
name: zookeeper3
namespace: zookeeper
spec:
type: NodePort
ports:
- name: client
port: 2181
nodePort: 42183
- name: followers
port: 2888
- name: election
port: 3888
selector:
app: zookeeper
server-id: "3"
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: zookeeper1
namespace: zookeeper
spec:
replicas: 1
selector:
matchLabels:
app: zookeeper
template:
metadata:
labels:
app: zookeeper
server-id: "1"
spec:
volumes:
- name: data
emptyDir: {}
- name: wal
emptyDir:
medium: Memory
containers:
- name: server
image: 192.168.174.120/baseimages/zookeeker:3.7.0
imagePullPolicy: Always
env:
- name: ZOO_MY_ID
value: "1"
- name: ZOO_SERVERS
value: "zookeeper1,zookeeper2,zookeeper3"
- name: JVMFLAGS
value: "-Xmx512M"
ports:
- containerPort: 2181
- containerPort: 2888
- containerPort: 3888
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: zookeeper2
namespace: zookeeper
spec:
replicas: 1
selector:
matchLabels:
app: zookeeper
template:
metadata:
labels:
app: zookeeper
server-id: "2"
spec:
volumes:
- name: data
emptyDir: {}
- name: wal
emptyDir:
medium: Memory
containers:
- name: server
image: 192.168.174.120/baseimages/zookeeker:3.7.0
imagePullPolicy: Always
env:
- name: ZOO_MY_ID
value: "2"
- name: ZOO_SERVERS
value: "zookeeper1,zookeeper2,zookeeper3"
- name: JVMFLAGS
value: "-Xmx512M"
ports:
- containerPort: 2181
- containerPort: 2888
- containerPort: 3888
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: zookeeper3
namespace: zookeeper
spec:
replicas: 1
selector:
matchLabels:
app: zookeeper
template:
metadata:
labels:
app: zookeeper
server-id: "3"
spec:
volumes:
- name: data
emptyDir: {}
- name: wal
emptyDir:
medium: Memory
containers:
- name: server
image: 192.168.174.120/baseimages/zookeeker:3.7.0
imagePullPolicy: Always
env:
- name: ZOO_MY_ID
value: "3"
- name: ZOO_SERVERS
value: "zookeeper1,zookeeper2,zookeeper3"
- name: JVMFLAGS
value: "-Xmx512M"
ports:
- containerPort: 2181
- containerPort: 2888
- containerPort: 3888
3.2 创建zookeeper pod
root@k8s-master-01:/data/k8s/yaml# kubectl apply -f zookeeper/zookeeper.yaml
service/zookeeper1 created
service/zookeeper2 created
service/zookeeper3 created
deployment.apps/zookeeper1 created
deployment.apps/zookeeper2 created
deployment.apps/zookeeper3 created
3.3 验证zookeeper集群
root@k8s-master-01:/data/k8s/yaml# kubectl get pod -n zookeeper
NAME READY STATUS RESTARTS AGE
zookeeper1-6848b5c969-st2mm 1/1 Running 0 92s
zookeeper2-56879d5d5f-m5g56 1/1 Running 0 92s
zookeeper3-5bfc89596f-n4znb 1/1 Running 0 92s
3.4 验证zookeeper集群配置
root@k8s-master-01:/data/k8s/yaml# kubectl exec -it zookeeper1-6848b5c969-st2mm -n zookeeper -- cat /usr/local/zookeeper/conf/zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/data/zookeeper/data dataLogDir=/data/zookeeper/datalog autopurge.purgeInterval=1 autopurge.snapRetainCount=3 maxClientCnxns=60 standaloneEnabled=true admin.enableServer=true clientPort=2181 quorumListenOnAllIPs=true
server.1=zookeeper1:2888:3888
server.2=zookeeper2:2888:3888
server.3=zookeeper3:2888:3888
3.5 查看zookeeper集群状态
root@k8s-master-01:/data/k8s/yaml# kubectl exec -it zookeeper1-6848b5c969-st2mm -n zookeeper -- /usr/local/zookeeper/bin/zkServer.sh status
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@k8s-master-01:/data/k8s/yaml# kubectl exec -it zookeeper2-56879d5d5f-m5g56 -n zookeeper -- /usr/local/zookeeper/bin/zkServer.sh status
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@k8s-master-01:/data/k8s/yaml# kubectl exec -it zookeeper3-5bfc89596f-n4znb -n zookeeper -- /usr/local/zookeeper/bin/zkServer.sh status
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
四 访问zookeeper集群
4.1 查看zookeeper svc
root@k8s-master-01:/data/k8s/yaml# kubectl get svc -n zookeeper -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
zookeeper1 NodePort 10.100.229.141 <none> 2181:42181/TCP,2888:43932/TCP,3888:49441/TCP 16m app=zookeeper,server-id=1
zookeeper2 NodePort 10.100.38.155 <none> 2181:42182/TCP,2888:49756/TCP,3888:38345/TCP 16m app=zookeeper,server-id=2
zookeeper3 NodePort 10.100.198.155 <none> 2181:42183/TCP,2888:44017/TCP,3888:32672/TCP 16m app=zookeeper,server-id=3
4.2 客户端访问测试
root@zk-01:~# /usr/local/zookeeper/bin/zkCli.sh -server 192.168.174.106:42181 Connecting to 192.168.174.106:42181 2021-12-15 10:33:41,408 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC 2021-12-15 10:33:41,411 [myid:] - INFO [main:Environment@98] - Client environment:host.name=ceph-node01 2021-12-15 10:33:41,411 [myid:] - INFO [main:Environment@98] - Client environment:java.version=17.0.1 2021-12-15 10:33:41,433 [myid:] - INFO [main:Environment@98] - Client environment:java.vendor=Oracle Corporation 2021-12-15 10:33:41,433 [myid:] - INFO [main:Environment@98] - Client environment:java.home=/usr/local/jdk-17.0.1 2021-12-15 10:33:41,434 [myid:] - INFO [main:Environment@98] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.7.0.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.7.0.jar:/usr/local/zookeeper/bin/../lib/snappy-java-1.1.7.7.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.30.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.30.jar:/usr/local/zookeeper/bin/../lib/simpleclient_servlet-0.9.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient_hotspot-0.9.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient_common-0.9.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient-0.9.0.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-resolver-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-handler-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-common-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-codec-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-buffer-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/metrics-core-4.1.12.1.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/jline-2.14.6.jar:/usr/local/zookeeper/bin/../lib/jetty-util-ajax-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.10.5.1.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.10.5.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.10.5.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.4.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.12.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf:.:/usr/local/jdk/lib:/usr/local/jdk/jre/lib:/usr/local/jdk/lib/tools.jar 2021-12-15 10:33:41,434 [myid:] - INFO [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib 2021-12-15 10:33:41,434 [myid:] - INFO [main:Environment@98] - Client environment:java.io.tmpdir=/tmp 2021-12-15 10:33:41,434 [myid:] - INFO [main:Environment@98] - Client environment:java.compiler=<NA> 2021-12-15 10:33:41,434 [myid:] - INFO [main:Environment@98] - Client environment:os.name=Linux 2021-12-15 10:33:41,434 [myid:] - INFO [main:Environment@98] - Client environment:os.arch=amd64 2021-12-15 10:33:41,435 [myid:] - INFO [main:Environment@98] - Client environment:os.version=5.4.0-91-generic 2021-12-15 10:33:41,435 [myid:] - INFO [main:Environment@98] - Client environment:user.name=root 2021-12-15 10:33:41,435 [myid:] - INFO [main:Environment@98] - Client environment:user.home=/root 2021-12-15 10:33:41,435 [myid:] - INFO [main:Environment@98] - Client environment:user.dir=/root 2021-12-15 10:33:41,435 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.free=27MB 2021-12-15 10:33:41,436 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.max=256MB 2021-12-15 10:33:41,436 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.total=34MB 2021-12-15 10:33:41,440 [myid:] - INFO [main:ZooKeeper@637] - Initiating client connection, connectString=192.168.174.106:42181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@6fadae5d 2021-12-15 10:33:41,472 [myid:] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation 2021-12-15 10:33:41,489 [myid:] - INFO [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes 2021-12-15 10:33:41,514 [myid:] - INFO [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false Welcome to ZooKeeper! 2021-12-15 10:33:41,561 [myid:192.168.174.106:42181] - INFO [main-SendThread(192.168.174.106:42181):ClientCnxn$SendThread@1171] - Opening socket connection to server 192.168.174.106/192.168.174.106:42181. 2021-12-15 10:33:41,562 [myid:192.168.174.106:42181] - INFO [main-SendThread(192.168.174.106:42181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2021-12-15 10:33:41,629 [myid:192.168.174.106:42181] - INFO [main-SendThread(192.168.174.106:42181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /192.168.174.103:33662, server: 192.168.174.106/192.168.174.106:42181 2021-12-15 10:33:41,658 [myid:192.168.174.106:42181] - INFO [main-SendThread(192.168.174.106:42181):ClientCnxn$SendThread@1438] - Session establishment complete on server 192.168.174.106/192.168.174.106:42181, session id = 0x10000da2a0e0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 192.168.174.106:42181(CONNECTED) 0]