Slide 1

Slide 1 text

Zookeeper CONFIGURATION PROVISIONING WITH... mariusz gil

Slide 2

Slide 2 text

About me

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Coordination service for distributed applications and systems

Slide 6

Slide 6 text

/app/feat_3 /app/f_2 /app/f_1 /servers /app In-memory file system model /

Slide 7

Slide 7 text

clients zookeeper cluster leader

Slide 8

Slide 8 text

7use cases name service configuration group membership leader election barriers queues locks

Slide 9

Slide 9 text

many projects Apache Accumulo Apache Hadoop MapReduce Apache HBase Apache Hedwig Apache Kafka Apache S4 Apache Solr Apache Storm Mesos Neo4J

Slide 10

Slide 10 text

ZooKeeper usage

Slide 11

Slide 11 text

Installation wget http://ftp.ps.pl/pub/apache/zookeeper/stable/zookeeper-3.4.5.tar.gz touch conf/zoo.cfg vim conf/zoo.cfg # STANDALONE ENVIRONMENT # tickTime=2000 # dataDir=/var/zookeeper # clientPort=2181 # DISTRIBUTED ENVIRONMENT # tickTime=2000 # dataDir=/var/zookeeper # clientPort=2181 # initLimit=5 # syncLimit=2 # server.1=zoo1:2888:3888 # server.2=zoo2:2888:3888 # server.3=zoo3:2888:3888 bin/zkServer.sh start

Slide 12

Slide 12 text

CLI mode bin/zkServer.sh start bin/zkCli.sh 127.0.0.1:2181 [zk: localhost:2181(CONNECTED) 31] help ZooKeeper -server host:port cmd args connect host:port get path [watch] ls path [watch] set path data [version] rmr path delquota [-n|-b] path quit printwatches on|off create [-s] [-e] path data acl stat path [watch] close ls2 path [watch] history listquota path setAcl path acl getAcl path sync path redo cmdno addauth scheme auth delete path [version]

Slide 13

Slide 13 text

CLI mode bin/zkServer.sh start bin/zkCli.sh 127.0.0.1:2181 [zk: localhost:2181(CONNECTED) 8] ls / [zookeeper] ... [zk: localhost:2181(CONNECTED) 17] create /app/features/pymk '' Created /app/features/pymk [zk: localhost:2181(CONNECTED) 20] create /app/features/pymk/enabled 0 Created /app/features/pymk/enabled [zk: localhost:2181(CONNECTED) 24] set /app/features/pymk/enabled 1 [zk: localhost:2181(CONNECTED) 28] get /app/features/pymk/enabled 1 [zk: localhost:2181(CONNECTED) 26] ls /app/features [pymk, login, friends, messages] [zk: localhost:2181(CONNECTED) 29] rmr /app/features [zk: localhost:2181(CONNECTED) 30] ls /app [] bin/zkServer.sh stop

Slide 14

Slide 14 text

PHP + ZooKeeper

Slide 15

Slide 15 text

Edit config.prod.yml file Commit to repo Wait for CI report Build app package Deploy to production

Slide 16

Slide 16 text

ZooKeeper cluster Web workers Tasks workers

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

$ cd $ git clone https://github.com/andreiz/php-zookeeper.git $ cd php-zookeeper $ phpize $ ./configure $ make $ sudo make install Installation of PHP extensions

Slide 19

Slide 19 text

zookeeper->exists($path)) { $this->makePath($path); $this->makeNode($path, $value); } else { $this->zookeeper->set($path, $value); } } /** * Equivalent of "mkdir -p" on ZooKeeper */ public function makePath($path, $value = '') { $parts = explode('/', $path); $parts = array_filter($parts); $subpath = ''; while (count($parts) > 1) { $subpath .= '/' . array_shift($parts); if (!$this->zookeeper->exists($subpath)) { $this->makeNode($subpath, $value); } } } Example 1 Simple configuration client get('/')); var_dump($zk->getChildren('/')); var_dump($zk->set('/test123', 'abc')); var_dump($zk->get('/test123')); var_dump($zk->getChildren('/')); var_dump($zk->set('/foo/001', 'bar1')); var_dump($zk->set('/foo/002', 'bar2')); var_dump($zk->getChildren('/foo')); Tekst

Slide 20

Slide 20 text

get( '/test', array($this, 'watcher' ) ) . "\n"; } } $zoo = new ZookeeperDemo('localhost:2181'); $zoo->get( '/test', array($zoo, 'watcher' ) ); while( true ) { echo '.'; sleep(2); } Example 2 Key change watcher

Slide 21

Slide 21 text

Zookeeper::PERM_ALL, 'scheme' => 'world', 'id' => 'anyone' ) ); private $isLeader = false; private $znode; public function __construct( $host = '', $watcher_cb = null, $recv_timeout = 10000 ) { parent::__construct( $host, $watcher_cb, $recv_timeout ); } public function register() { if( ! $this->exists( self::CONTAINER ) ) { $this->create( self::CONTAINER, null, $this->acl ); } $this->znode = $this->create( self::CONTAINER . '/w-', null, $this->acl, Zookeeper::EPHEMERAL | Zookeeper::SEQUENCE ); $this->znode = str_replace( self::CONTAINER .'/', '', $this->znode ); Example 3 Multiple workers with automatic leader election # console 1 $ php example_worker.php I'm registred as: w-0000000001 Nobody here, I'm the leader Leading # console 2 $ php example_worker.php I'm registred as: w-0000000002 I'm watching w-0000000001 Working # console 3 $ php example_worker.php I'm registred as: w-0000000003 I'm watching w-0000000002 Working

Slide 22

Slide 22 text

DevOp life?

Slide 23

Slide 23 text

OMG! I have to disable feature X for a while and change database schema, but the rest of the app should works... set /app/feature/pymk/enabled 0 ALTER TABLE... set /app/feature/pymk/enabled 1

Slide 24

Slide 24 text

OMG! I have to add more cache instances and distribute traffic across many servers set /servers/cache/c6/host 192... set /servers/cache/c6/port 11211 set /servers/cache/c7/host 192... set /servers/cache/c7/port 11211 ls /servers/cache

Slide 25

Slide 25 text

leader zookeeper cluster service provider 192.168.12.201 client 192.168.12.201

Slide 26

Slide 26 text

leader zookeeper cluster client service provider 192.168.12.201 service provider 192.168.12.123 192.168.12.123

Slide 27

Slide 27 text

Your app should only know... ...where your ZooKeeper is

Slide 28

Slide 28 text

Zookeeper CONFIGURATION PROVISIONING WITH... mariusz gil THANKS