首页 > 代码库 > 【分布式协调zookeeper】利用ansible部署篇
【分布式协调zookeeper】利用ansible部署篇
一、环境准备
操作系统:centOS-6.8 2.6.32-642.el6.x86_64【虚拟机环境】 节点主机:10.0.55.3、10.0.55.5、10.0.55.6 目录结构: /opt/ 软件版本: jdk-7u67-linux-x64.rpm zookeeper-3.5.0-alpha.tar.gz
二、安装Java环境
$ rpm -ivh jdk-7u67-linux-x64.rpm $ cat /etc/profile export JAVA_HOME=/usr/java/jdk1.7.0_67 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin 立即生效 $ source /etc/profile
三、下载zookeeper包并创建zookeeper管理数据
$ wget http://archive.apache.org/dist/zookeeper/zookeeper-3.5.0-alpha/zookeeper-3.5.0-alpha.tar.gz $ ln -s /opt/zookeeper-3.5.0-alpha /opt/zookeeper $ mkdir /opt/zookeeper/data $ echo "1" > /opt/zookeeper/data/myid
四、编辑zookeeper/conf/zoo.cfg配置文件,启动时需要
$ cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg --- 查看zoo.cfg内容 --- tickTime=2000 initLimit=10 syncLimit=5 dataDir=/opt/zookeeper/data clientPort=2181 server.1=10.0.55.3:2888:3888 server.2=10.0.55.5:2888:3888 server.3=10.0.55.6:2888:3888
五、启动zookeeper服务并查看
--- 启动服务 --- $ /opt/zookeeper/bin/zkServer.sh start JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED --- 检查端口 --- netstat -lnpt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22678 0.0.0.0:* LISTEN 1074/sshd tcp 0 0 :::2181 :::* LISTEN 121685/java tcp 0 0 :::55369 :::* LISTEN 121685/java tcp 0 0 ::ffff:10.0.55.3:3888 :::* LISTEN 121685/java tcp 0 0 :::8080 :::* LISTEN 121685/java tcp 0 0 :::22678 :::* LISTEN 1074/sshd
使用自动化运维工具 ansible 进行批量分发
这里只介绍如何使用ansible的命令组件模块进行节点部署
为了节省篇幅,这里不介绍ansible的部署安装
1、远程安装zookeeper节点
$ ansible webservers -m copy -a ‘src=http://www.mamicode.com/zookeeper-3.5.0-alpha.tar.gz dest=/opt/ owner=root group=root mode=0644‘"2" >/opt/zookeeper/data/myid‘
2、将配置文件copy到远端节点
$ ansible webservers -m copy -a ‘src=http://www.mamicode.com/opt/zookeeper/conf/zoo.cfg dest=/opt/zookeeper/conf/zoo.cfg owner=root group=root mode=0644‘
3、批量启动zookeeper服务并查看
$ ansible webservers -m shell -a ‘/opt/zookeeper/bin/zkServer.sh stats‘ $ ansible webservers -m shell -a ‘netstat -lnpt‘
4、批量坚持zookeeper状态
$ ansible webservers -m shell -a ‘/opt/zookeeper/bin/zkServer.sh status‘ 10.0.55.5 | SUCCESS | rc=0 >> Client port found: 2181 Mode: leaderJMX enabled by default ## 这里面的mode信息中,该主机被选举为 Leader Using config: /opt/zookeeper/bin/../conf/zoo.cfg 10.0.55.3 | SUCCESS | rc=0 >> Client port found: 2181 Mode: followerJMX enabled by default ## 这里面的mode信息中,该主机被选举为 follower Using config: /opt/zookeeper/bin/../conf/zoo.cfg 10.0.55.6 | SUCCESS | rc=0 >> Client port found: 2181 Mode: followerJMX enabled by default ## 这里面的mode信息中,该主机被选举为 follower Using config: /opt/zookeeper/bin/../conf/zoo.cfg
zookeeper基础操作
启动一个CLI客户端
[root@lbl opt]# ./zookeeper/bin/zkCli.sh
创建、编辑、删除znode
--- 创建一个/workers路径的znode --- [zk: localhost:2181(CONNECTED) 0] create /workers helloworld Created /workers --- 使用get -s命令查看znode数据 --- [zk: localhost:2181(CONNECTED) 1] get -s /workers helloworld cZxid = 0x500000006 ctime = Sat Apr 22 10:33:42 CST 2017 mZxid = 0x500000006 mtime = Sat Apr 22 10:33:42 CST 2017 pZxid = 0x500000006 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 0 --- 使用get -w选项,进行监听/factory的变化 --- [zk: localhost:2181(CONNECTED) 11] get -w /workers strong --- 在 follower节点上启动客户端,并修改/workers --- [root@lbl bin]# ./zkCli.sh [zk: localhost:2181(CONNECTED) 2] set /workers weakness ---查看监听那台主机,返回的通知信息如下: --- [zk: localhost:2181(CONNECTED) 12] WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/workers --- 删除workers的znode --- [zk: localhost:2181(CONNECTED) 7] delete /workers
创建序列znode
[zk: localhost:2181(CONNECTED) 13] create -s /workers/worker- Created /workers/worker-0000000000 [zk: localhost:2181(CONNECTED) 14] create -s /workers/worker- Created /workers/worker-0000000001 [zk: localhost:2181(CONNECTED) 15] create -s /workers/worker- Created /workers/worker-0000000002 [zk: localhost:2181(CONNECTED) 16] create -s /workers/worker- Created /workers/worker-0000000003
通过stat命令,进行查看workers状态
[zk: localhost:2181(CONNECTED) 18] stat /workers cZxid = 0x500000006 ctime = Sat Apr 22 10:33:42 CST 2017 mZxid = 0x500000008 mtime = Sat Apr 22 10:35:25 CST 2017 pZxid = 0x50000000c cversion = 4 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 8 numChildren = 4
通过浏览器查看zookeeper状态
--- 从zookeeper-3.5.0版本以上,可以使用http接口提交4字命令的请求; --- 在zookeeper服务中嵌入了Jetty server作为AdminServer管理服务器,其默认端口为8080; --- 服务访问的URL格式是"/commands/[command name]",例如:访问stats命令 --- 服务器返回的结果是以JSON格式显示 http://10.0.55.3:8080/commands http://10.0.55.3:8080/commands/stats
本文出自 “靠谱儿” 博客,请务必保留此出处http://liubao0312.blog.51cto.com/2213529/1939225
【分布式协调zookeeper】利用ansible部署篇
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。