2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

69
Introduction to Introduction to Percona XtraDB Cluster Percona XtraDB Cluster and HAProxy and HAProxy 2014.04.12 2014.04.12 Bo-Yi Wu Bo-Yi Wu appleboy appleboy

Transcript of 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

Page 1: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

Introduction to Introduction to Percona XtraDB ClusterPercona XtraDB Cluster

and HAProxyand HAProxy2014.04.122014.04.12Bo-Yi WuBo-Yi Wuappleboyappleboy

Page 2: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

22

About meAbout meGithub: @appleboyGithub: @appleboyTwitter: @appleboyTwitter: @appleboyBlog: http://blog.wu-boy.comBlog: http://blog.wu-boy.com

Page 3: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

33

AgendaAgenda About Percona XtraDB ClusterAbout Percona XtraDB Cluster Install the first node of the clusterInstall the first node of the cluster Install subsequent nodes to the clusterInstall subsequent nodes to the cluster Install HAProxy on the application serverInstall HAProxy on the application server Testing with a real-world applicationTesting with a real-world application

Page 4: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

4

Why useWhy usePercona XtraDB Cluster?Percona XtraDB Cluster?

Page 5: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

5

MySQL ReplicationMySQL Replicationvsvs

Percona XtraDB ClusterPercona XtraDB Cluster

Page 6: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

6

Async vs SyncAsync vs Sync

Page 7: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

77

MySQL Replication: MySQL Replication: AsyncAsync

1...10...sec delay

Page 8: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

88

MySQL Replication: MySQL Replication: AsyncAsync

Page 9: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

99

syncsync

Event

Event confirm

Page 10: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

10

Percona XtraDB ClusterPercona XtraDB ClusterFree and Open SourceFree and Open Source

Page 11: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

1111

Percona XtraDB ClusterPercona XtraDB Cluster

Group Communication

Page 12: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

1212

Percona XtraDB Cluster Percona XtraDB Cluster Synchronous replicationSynchronous replication Multi-master replication Multi-master replication Parallel applying on slaves Parallel applying on slaves Data consistencyData consistency Automatic node provisioning Automatic node provisioning

Page 13: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

13

Synchronous Synchronous replicationreplication

Page 14: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

1414

Virtually synchronousVirtually synchronous

Page 15: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

15

Multi-master replication Multi-master replication

Page 16: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

1616

Multi-master: MySQLMulti-master: MySQL

MySQL Replication

Write Fail

Page 17: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

1717

Multi-master: XtraDB ClusterMulti-master: XtraDB Cluster

XtraDB Cluster

WriteWrite

Write

Page 18: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

18

Parallel applying on slaves Parallel applying on slaves

Page 19: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

1919

Parallel apply: MySQLParallel apply: MySQL

Write N threads

Apply 1 thread

Page 20: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

2020

Write N threads

Apply N thread

Parallel apply: XtraDB ClusterParallel apply: XtraDB Cluster

Page 21: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

21

Data consistencyData consistency

Page 22: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

2222

XtraDB Cluster data consistencyXtraDB Cluster data consistency

== ==

Page 23: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

23

Automatic node provisioning Automatic node provisioning

Page 24: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

24

Group Communication

Copy Data

Join Cluster

Page 25: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

25

How many nodes should I have?How many nodes should I have?

Page 26: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

26

3 nodes is the minimal 3 nodes is the minimal recommended configurationrecommended configuration

>=3 nodes for quorum purpose

Page 27: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

2727

Network Failure

Split brain

50% is not a quorum

Page 28: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

28

Network Failure

XtraDB Cluster:Data consistency

Page 29: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

29

garbdgarbdGalera Abitrator DaemonGalera Abitrator Daemon

Page 30: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

30

Percona XtraDB Cluster Percona XtraDB Cluster LimitationsLimitations

Page 31: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

31

Only Support InnoDB TableOnly Support InnoDB TableMyISAM support is limitedMyISAM support is limited

Page 32: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

32

write performance?write performance?limited by weakest nodelimited by weakest node

Page 33: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

33

Joing ProcessJoing Process

Page 34: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

34

Group Communication

Copy Data

Join Cluster

SST1TB take long time

Page 35: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

3535

State TransferState Transfer Full data SSTFull data SST

– New nodeNew node– Node long time disconnectedNode long time disconnected

Incremental ISTIncremental IST– Node disconnected short timeNode disconnected short time

Page 36: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

3636

Snapshot State Transfer Snapshot State Transfer MysqldumpMysqldump

– Small databasesSmall databases RsyncRsync

– Donor disconnected for copy timeDonor disconnected for copy time– fasterfaster

XtraBackupXtraBackup– Donor disconnected for short timeDonor disconnected for short time– slowerslower

Page 37: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

3737

Incremental State TransferIncremental State Transfer Node was in clusterNode was in cluster

– Disconnected for maintenanceDisconnected for maintenance– Node Crashed Node Crashed

Page 38: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

38

Install viaInstall viaPercona's yum repositoryPercona's yum repository

Page 39: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

39

$ yum -y install \$ yum -y install \Percona-XtraDB-Cluster-server \Percona-XtraDB-Cluster-server \Percona-XtraDB-Cluster-client \Percona-XtraDB-Cluster-client \Percona-Server-shared-compat \Percona-Server-shared-compat \percona-xtrabackuppercona-xtrabackup

Page 40: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

40

Configuring the nodesConfiguring the nodes

Page 41: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

41

wsrep_cluster_address=gcomm://wsrep_cluster_address=gcomm://– Initializes a new cluster for first nodeInitializes a new cluster for first node

wsrep_cluster_address=gcomm://<IP addr>, wsrep_cluster_address=gcomm://<IP addr>, <IP addr>, <IP addr><IP addr>, <IP addr>– Default port: 4567Default port: 4567

Page 42: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

42

Don’t use wsrep_urlsDon’t use wsrep_urlswsrep_urls is deprecated since version wsrep_urls is deprecated since version 5.5.285.5.28

Page 43: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

4343

Configuring the first nodeConfiguring the first node [mysqld][mysqld] wsrep_provider=/usr/lib64/libgalera_smm.sowsrep_provider=/usr/lib64/libgalera_smm.so wsrep_cluster_address = "wsrep_cluster_address = "gcomm://gcomm://"" wsrep_sst_auth=username:passwordwsrep_sst_auth=username:password wsrep_provider_options="gcache.size=4G"wsrep_provider_options="gcache.size=4G" wsrep_cluster_name=Perconawsrep_cluster_name=Percona wsrep_sst_method=xtrabackupwsrep_sst_method=xtrabackup wsrep_node_name=db_01wsrep_node_name=db_01 wsrep_slave_threads=4wsrep_slave_threads=4 log_slave_updateslog_slave_updates innodb_locks_unsafe_for_binlog=1innodb_locks_unsafe_for_binlog=1 innodb_autoinc_lock_mode=2innodb_autoinc_lock_mode=2

Page 44: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

4444

Configuring subsequent nodesConfiguring subsequent nodes [mysqld][mysqld] wsrep_provider=/usr/lib64/libgalera_smm.sowsrep_provider=/usr/lib64/libgalera_smm.so wsrep_cluster_address = "wsrep_cluster_address = "gcomm://xxxx,xxxxgcomm://xxxx,xxxx"" wsrep_sst_auth=username:passwordwsrep_sst_auth=username:password wsrep_provider_options="gcache.size=4G"wsrep_provider_options="gcache.size=4G" wsrep_cluster_name=Perconawsrep_cluster_name=Percona wsrep_sst_method=xtrabackupwsrep_sst_method=xtrabackup wsrep_node_name=db_01wsrep_node_name=db_01 wsrep_slave_threads=4wsrep_slave_threads=4 log_slave_updateslog_slave_updates innodb_locks_unsafe_for_binlog=1innodb_locks_unsafe_for_binlog=1 innodb_autoinc_lock_mode=2innodb_autoinc_lock_mode=2

Page 45: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

45

Monitoring MySQL StatusMonitoring MySQL Statusshow global status like 'show global status like 'wsrep%wsrep%'; ';

Page 46: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

4646

Cluster integrityCluster integrity wsrep_cluster_sizewsrep_cluster_size

– Configuration versionConfiguration version wsrep_conf_idwsrep_conf_id

– Number of active nodesNumber of active nodes wsrep_cluster_statuswsrep_cluster_status

– Should be “Primary”Should be “Primary”

Page 47: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

4747

Node StatusNode Status wsrep_readywsrep_ready

– Should be “On”Should be “On” wsrep_local_state_commentwsrep_local_state_comment

– Status messageStatus message wsep_local_send_q_avgwsep_local_send_q_avg

– Possible network bottleneckPossible network bottleneck wsrep_flow_control_pausedwsrep_flow_control_paused

– Replication lagReplication lag

Page 48: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

48

Realtime Wsrep StatusRealtime Wsrep Status

https://github.com/jayjanssen/myq_gadgets

Page 49: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

4949

Realtime Wsrep StatusRealtime Wsrep StatusPercona / db_03 / Galera 2.8(r165)Percona / db_03 / Galera 2.8(r165)Wsrep Cluster Node Queue Ops Bytes Flow Conflct PApply Commit Wsrep Cluster Node Queue Ops Bytes Flow Conflct PApply Commit time P cnf # cmt sta Up Dn Up Dn Up Dn p_ms snt lcf bfa dst oooe oool windtime P cnf # cmt sta Up Dn Up Dn Up Dn p_ms snt lcf bfa dst oooe oool wind11:47:39 P 73 3 Sync T/T 0 0 5 356 30K 149K 0.0 0 0 0 125 0 0 011:47:39 P 73 3 Sync T/T 0 0 5 356 30K 149K 0.0 0 0 0 125 0 0 011:47:40 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:40 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:41 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:41 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:42 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:42 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:43 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:43 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:44 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:44 P 73 3 Sync T/T 0 0 0 0 0 0 0.0 0 0 0 125 0 0 011:47:45 P 73 3 Sync T/T 0 0 0 3 0 1.1K 0.0 0 0 0 126 67 0 111:47:45 P 73 3 Sync T/T 0 0 0 3 0 1.1K 0.0 0 0 0 126 67 0 111:47:46 P 73 3 Sync T/T 0 0 0 2 0 994 0.0 0 0 0 126 0 0 011:47:46 P 73 3 Sync T/T 0 0 0 2 0 994 0.0 0 0 0 126 0 0 0

./myq_status -t 1 -h 127.0.0.1 wsrep

Page 50: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

50

Application / ClusterApplication / Cluster

Page 51: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

51

How Synchronous How Synchronous Writes workWrites work

Page 52: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

52

Source NodeSource Nodepessimistic lockingpessimistic locking

InnoDB transaction locking

Page 53: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

5353

Cluster replicationCluster replication Before source return commitsBefore source return commits

– Certify trx on all other nodesCertify trx on all other nodes Nodes reject on locking conflictsNodes reject on locking conflicts Commit successfully if no conflicts on Commit successfully if no conflicts on

any nodeany node

Page 54: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

54

Node 1Tx Source

Node 2Accepted

Node 3Certify Fails

Client 2

Client 1

Update t set col = '12' where id = '1'

Update t set col = '12' where id = '1'

Update t set col = '12' where id = '1'

Page 55: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

55

Application Care?Application Care?

Page 56: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

56

Write to all nodesWrite to all nodesIncrease of deadlock errorsIncrease of deadlock errors

Page 57: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

57

How to avoid deadlock How to avoid deadlock on all nodes?on all nodes?

Page 58: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

5858

How to avoid deadlockHow to avoid deadlock Writing to only one nodeWriting to only one node

– All pessimistic locking happens on one nodeAll pessimistic locking happens on one node Different nodes can handle writes for Different nodes can handle writes for

different datasetsdifferent datasets– Different database, tables, rows etc.Different database, tables, rows etc.

Page 59: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

59

Application to cluster connectsApplication to cluster connects

Page 60: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

6060

Application to clusterApplication to cluster For writesFor writes

– Best practice: single nodeBest practice: single node For readsFor reads

– All nodes load balancedAll nodes load balanced glbd – Galera Load Balancerglbd – Galera Load Balancer HaproxyHaproxy

Page 61: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

61

192.168.1.100 192.168.1.101 192.168.1.102

HAProxy Load BalancerHAProxy Load Balancer

Read/Write Read Read

Page 62: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

62

HAProxy Load balancingHAProxy Load balancing

Page 63: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

63

Read and Write Read and Write on the same porton the same port

Page 64: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

64

frontend pxc-frontfrontend pxc-front bind *:3307bind *:3307 mode tcpmode tcp default_backend pxc-backdefault_backend pxc-back

backend pxc-backbackend pxc-back mode tcpmode tcp balance leastconnbalance leastconn option httpchkoption httpchk server db1 192.168.1.100:3306 check port 9200 inter server db1 192.168.1.100:3306 check port 9200 inter

12000 rise 3 fall 312000 rise 3 fall 3 server db2 192.168.1.101:3306 check port 9200 inter server db2 192.168.1.101:3306 check port 9200 inter

12000 rise 3 fall 312000 rise 3 fall 3 server db3 192.168.1.102:3306 check port 9200 inter server db3 192.168.1.102:3306 check port 9200 inter

12000 rise 3 fall 312000 rise 3 fall 3

Page 65: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

65

Read and Write Read and Write on different porton different port

Page 66: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

66

frontend pxc-onenode-frontfrontend pxc-onenode-front bind *:3308bind *:3308 mode tcpmode tcp default_backend pxc-onenode-backdefault_backend pxc-onenode-back

backend pxc-onenode-backbackend pxc-onenode-back mode tcpmode tcp balance leastconnbalance leastconn option httpchkoption httpchk server db1 192.168.1.100:3306 check port 9200 inter server db1 192.168.1.100:3306 check port 9200 inter

12000 rise 3 fall 312000 rise 3 fall 3 server db2 192.168.1.101:3306 check port 9200 inter server db2 192.168.1.101:3306 check port 9200 inter

12000 rise 3 fall 3 12000 rise 3 fall 3 backupbackup server db3 192.168.1.102:3306 check port 9200 inter server db3 192.168.1.102:3306 check port 9200 inter

12000 rise 3 fall 3 12000 rise 3 fall 3 backupbackup

Page 67: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

6767

Application serverApplication server CentOS 6 base installationCentOS 6 base installation EPEL repo addedEPEL repo added HaProxy installed from EPEL repoHaProxy installed from EPEL repo Sysbench 0.5 packageSysbench 0.5 package

Page 68: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

68

Live DemoLive Demo

Page 69: 2014 OSDC Talk: Introduction to Percona XtraDB Cluster and HAProxy

69

Thank youThank you