Thursday, September 21, 2017

Mysql container persisting data on Fuse ( Volume Map )

     
                                    Mysql container persisting data on Fuse ( Volume Map )

Docker containers are ephemeral, and so any state stored in them is gone after they are terminated and removed. This blog shows an approach where Docker container running MySQL will Persistence data outside the container to MapR cluster via Fuse .

1) This blog assumes you have a 5.2.x cluster already setup and running .  Also mysql ID is setup with 52 UID and GID

[root@node2rhel73 ~]#maprcli node cldbmaster
cldbmaster                                           
ServerID: 7358880722476044199 HostName: node2rhel73 

[root@node2rhel73 ~]# id mysql
uid=52(mysql) gid=52(mysql) groups=52(mysql)

[root@node2rhel73 ~]# 

2) This blog also assumes you have a client setup with fuse installed on the client . Also mysql ID is setup with 52 UID and GID on client node as well .


[root@noderhel73 ~]# rpm -qa | grep mapr
mapr-client-5.2.1.42646.GA-1.x86_64
mapr-posix-client-basic-5.2.1.42646.GA-1.x86_64
[root@noderhel73 ~]#
[root@noderhel73 ~]# df -hP /mapr
Filesystem          Size  Used Avail Use% Mounted on
posix-client-basic  119G  567M  118G   1% /mapr
[root@noderhel73 ~]# id mysql
uid=52(mysql) gid=52(mysql) groups=52(mysql)
[root@noderhel73 ~]# 

3) Now objective is to launch a docker container with mysql running in it but persist data outside the container via volume Map on Fuse

Below is Docker file with steps self-explanatory .

[root@noderhel73 Centos_MYSQL]# cat Dockerfile 
# Start of CentOS image
FROM centos:7 
# Add mysql user and group first to make sure IDs get assigned consistently
RUN groupadd -r mysql --gid=52 && useradd -r -g mysql --uid=52 mysql
# Install Percona Mysql
RUN rpm --import https://www.percona.com/downloads/RPM-GPG-KEY-percona
RUN yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\
&& yum -y update && yum install -y which nc sysbench perl-Digest-MD5 percona-xtrabackup-24 Percona-Server-{client,server,shared,test}-57 \
# Clean up YUM when done, create mysql dir and update correct ownership/permission.
&& yum clean all && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/log/mysql \
&& chown -R mysql:mysql /var/lib/mysql /var/log/mysql && chmod -R 755 /var/lib/mysql /var/log/mysql

ADD my.cnf /etc/my.cnf
VOLUME ["/var/lib/mysql","/var/lib/mysqld/", "/var/log/mysql"]

# Copy mysql startup script and give permission to entry point

COPY ps-entry.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh

#Copy master_nc file (Script to listen on a slave request) and give execute permission

COPY master_nc /usr/bin/master_nc
RUN chmod 755 /usr/bin/master_nc

EXPOSE 3306
# start Mysql
CMD ["/entrypoint.sh"]
[root@noderhel73 Centos_MYSQL]# 

4) Now building docker image.

[root@noderhel73 Centos_MYSQL]# docker build -t my_mysql .
Sending build context to Docker daemon  18.43kB
Step 1/12 : FROM centos:7
 ---> 196e0ce0c9fb
Step 2/12 : RUN groupadd -r mysql --gid=52 && useradd -r -g mysql --uid=52 mysql
 ---> Using cache
 ---> 5379d2f7e490
Step 3/12 : RUN rpm --import https://www.percona.com/downloads/RPM-GPG-KEY-percona
 ---> Using cache
 ---> 25a29984de29
Step 4/12 : RUN yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm&& yum -y update && yum install -y which nc sysbench perl-Digest-MD5 percona-xtrabackup-24 Percona-Server-{client,server,shared,test}-57 && yum clean all && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/log/mysql && chown -R mysql:mysql /var/lib/mysql /var/log/mysql && chmod -R 755 /var/lib/mysql /var/log/mysql
 ---> Using cache
 ---> 78f18acba3b5
Step 5/12 : ADD my.cnf /etc/my.cnf
 ---> Using cache
 ---> 488d03e80a31
Step 6/12 : VOLUME /var/lib/mysql /var/lib/mysqld/ /var/log/mysql
 ---> Using cache
 ---> 9462325871a8
Step 7/12 : COPY ps-entry.sh /entrypoint.sh
 ---> Using cache
 ---> 59fc6d150f98
Step 8/12 : RUN chmod 755 /entrypoint.sh
 ---> Using cache
 ---> e5f90d39c8b3
Step 9/12 : COPY master_nc /usr/bin/master_nc
 ---> Using cache
 ---> 167c41f6206a
Step 10/12 : RUN chmod 755 /usr/bin/master_nc
 ---> Using cache
 ---> 9270bc111ba3
Step 11/12 : EXPOSE 3306
 ---> Using cache
 ---> 678d9469a90b
Step 12/12 : CMD /entrypoint.sh
 ---> Using cache
 ---> 6da4e1306462
Successfully built 6da4e1306462

5) Verify image is built .

[root@noderhel73 Centos_MYSQL]# docker images
REPOSITORY          TAG                   IMAGE ID            CREATED             SIZE
my_mysql            latest                6da4e1306462        5 minutes ago       948MB

6) Now start the docker container with the image which was just built which in-turn maps the volume and starts the mysql. Also notice i am using option  "--volume" where i am mapping fuse mount /mapr/dsemapr/tmp/mysql on the host to /var/lib/mysql inside the container, so incase container dies data it wrote to /var/lib/mysql volume is still persisted .


[root@noderhel73 Centos_MYSQL]# docker run -e MYSQL_USER=mysql -e MYSQL_ROOT_PASSWORD=Theistareyk -it --volume /mapr/dsemapr/tmp/mysql:/var/lib/mysql my_mysql:latest

Running --initialize-insecure
2017-09-22T05:39:55.246007Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-09-22T05:39:55.887374Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-09-22T05:39:56.004295Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-09-22T05:39:56.098064Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7707139e-9f58-11e7-9dc1-0242ac110004.
2017-09-22T05:39:56.101371Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-09-22T05:39:57.099486Z 0 [Warning] CA certificate ca.pem is self signed.
2017-09-22T05:39:57.512656Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
Finished --initialize-insecure
MySQL init process in progress...
2017-09-22T05:40:02.830342Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-09-22T05:40:02.836639Z 0 [Note] mysqld (mysqld 5.7.19-17-log) starting as process 46 ...
2017-09-22T05:40:02.854261Z 0 [Note] InnoDB: PUNCH HOLE support available
2017-09-22T05:40:02.854306Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-09-22T05:40:02.854319Z 0 [Note] InnoDB: Uses event mutexes
2017-09-22T05:40:02.854326Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-09-22T05:40:02.854334Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.7
2017-09-22T05:40:02.854340Z 0 [Note] InnoDB: Using Linux native AIO
2017-09-22T05:40:02.854698Z 0 [Note] InnoDB: Number of pools: 1
2017-09-22T05:40:02.854981Z 0 [Note] InnoDB: Using CPU crc32 instructions
2017-09-22T05:40:02.861290Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2017-09-22T05:40:02.876750Z 0 [Note] InnoDB: Completed initialization of buffer pool
2017-09-22T05:40:02.879646Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2017-09-22T05:40:02.900435Z 0 [Note] InnoDB: Crash recovery did not find the parallel doublewrite buffer at /var/lib/mysql/xb_doublewrite
2017-09-22T05:40:02.912209Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2017-09-22T05:40:02.959215Z 0 [Note] InnoDB: Created parallel doublewrite buffer at /var/lib/mysql/xb_doublewrite, size 3932160 bytes
2017-09-22T05:40:03.056965Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2017-09-22T05:40:03.059099Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2017-09-22T05:40:03.107810Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2017-09-22T05:40:03.111069Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2017-09-22T05:40:03.111092Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2017-09-22T05:40:03.115666Z 0 [Note] InnoDB: Waiting for purge to start
2017-09-22T05:40:03.167156Z 0 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.7.19-17 started; log sequence number 2545240
2017-09-22T05:40:03.171571Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2017-09-22T05:40:03.172121Z 0 [Note] Plugin 'FEDERATED' is disabled.
2017-09-22T05:40:03.231580Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2017-09-22T05:40:03.231626Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2017-09-22T05:40:03.238222Z 0 [Warning] CA certificate ca.pem is self signed.
2017-09-22T05:40:03.238755Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2017-09-22T05:40:03.255894Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170922  5:40:03
2017-09-22T05:40:03.467463Z 0 [Note] Event Scheduler: Loaded 0 events
2017-09-22T05:40:03.467926Z 0 [Note] mysqld: ready for connections.
Version: '5.7.19-17-log'  socket: '/var/lib/mysql/mysql.sock'  port: 0  Percona Server (GPL), Release 17, Revision e19a6b7b73f
2017-09-22T05:40:03.467949Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. 
2017-09-22T05:40:03.467957Z 0 [Note] Beginning of list of non-natively partitioned tables
2017-09-22T05:40:03.779154Z 0 [Note] End of list of non-natively partitioned tables
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2017-09-22T05:40:09.628621Z 0 [Note] Giving 0 client threads a chance to die gracefully
2017-09-22T05:40:09.628653Z 0 [Note] Shutting down slave threads
2017-09-22T05:40:09.628665Z 0 [Note] Forcefully disconnecting 0 remaining clients
2017-09-22T05:40:09.628673Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2017-09-22T05:40:09.628776Z 0 [Note] Binlog end
2017-09-22T05:40:09.633420Z 0 [Note] Shutting down plugin 'ngram'
2017-09-22T05:40:09.633444Z 0 [Note] Shutting down plugin 'ARCHIVE'
2017-09-22T05:40:09.633451Z 0 [Note] Shutting down plugin 'partition'
2017-09-22T05:40:09.633455Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2017-09-22T05:40:09.633464Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2017-09-22T05:40:09.633495Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2017-09-22T05:40:09.633501Z 0 [Note] Shutting down plugin 'CSV'
2017-09-22T05:40:09.633507Z 0 [Note] Shutting down plugin 'MyISAM'
2017-09-22T05:40:09.633520Z 0 [Note] Shutting down plugin 'MEMORY'
2017-09-22T05:40:09.633533Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2017-09-22T05:40:09.633539Z 0 [Note] Shutting down plugin 'INNODB_CHANGED_PAGES'
2017-09-22T05:40:09.633544Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2017-09-22T05:40:09.633548Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2017-09-22T05:40:09.633552Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2017-09-22T05:40:09.633557Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2017-09-22T05:40:09.633561Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2017-09-22T05:40:09.633565Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2017-09-22T05:40:09.633570Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2017-09-22T05:40:09.633574Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2017-09-22T05:40:09.633578Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2017-09-22T05:40:09.633582Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2017-09-22T05:40:09.633586Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2017-09-22T05:40:09.633590Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2017-09-22T05:40:09.633595Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2017-09-22T05:40:09.633599Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2017-09-22T05:40:09.633603Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2017-09-22T05:40:09.633607Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2017-09-22T05:40:09.633611Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2017-09-22T05:40:09.633615Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2017-09-22T05:40:09.633619Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2017-09-22T05:40:09.633623Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2017-09-22T05:40:09.633627Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2017-09-22T05:40:09.633632Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2017-09-22T05:40:09.633636Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2017-09-22T05:40:09.633640Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2017-09-22T05:40:09.633645Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2017-09-22T05:40:09.633649Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2017-09-22T05:40:09.633653Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2017-09-22T05:40:09.633657Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2017-09-22T05:40:09.633662Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2017-09-22T05:40:09.633666Z 0 [Note] Shutting down plugin 'XTRADB_ZIP_DICT_COLS'
2017-09-22T05:40:09.633671Z 0 [Note] Shutting down plugin 'XTRADB_ZIP_DICT'
2017-09-22T05:40:09.633675Z 0 [Note] Shutting down plugin 'XTRADB_RSEG'
2017-09-22T05:40:09.633679Z 0 [Note] Shutting down plugin 'XTRADB_INTERNAL_HASH_TABLES'
2017-09-22T05:40:09.633683Z 0 [Note] Shutting down plugin 'XTRADB_READ_VIEW'
2017-09-22T05:40:09.633687Z 0 [Note] Shutting down plugin 'InnoDB'
2017-09-22T05:40:09.633765Z 0 [Note] InnoDB: FTS optimize thread exiting.
2017-09-22T05:40:09.634353Z 0 [Note] InnoDB: Starting shutdown...
2017-09-22T05:40:09.736875Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
2017-09-22T05:40:09.741755Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 170922  5:40:09
2017-09-22T05:40:10.335236Z 0 [Note] InnoDB: Waiting for page_cleaner to finish flushing of buffer pool
2017-09-22T05:40:11.547455Z 0 [Note] InnoDB: Shutdown completed; log sequence number 11864206
2017-09-22T05:40:11.548569Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2017-09-22T05:40:11.548596Z 0 [Note] Shutting down plugin 'sha256_password'
2017-09-22T05:40:11.548605Z 0 [Note] Shutting down plugin 'mysql_native_password'
2017-09-22T05:40:11.548797Z 0 [Note] Shutting down plugin 'binlog'
2017-09-22T05:40:11.552774Z 0 [Note] mysqld: Shutdown complete


MySQL init process done. Ready for start up.

Starting listener for slave requests

7) Verify :  Now verify if we can login into the mysql and see all DB .

[root@noderhel73 ~]# docker exec -it hungry_jennings mysql -u root -pTheistareyk
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.19-17-log Percona Server (GPL), Release 17, Revision e19a6b7b73f

Copyright (c) 2009-2017 Percona LLC and/or its affiliates
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.13 sec)

mysql> quit

Also verify "/var/lib/mysql" is mounted and mapped on the host.

[root@noderhel73 ~]# docker exec -it hungry_jennings bash
[root@ca9b7542a703 /]# df -hP | grep -i mysql
posix-client-basic     119G  566M  118G   1% /var/lib/mysql


Bingo we have achieved the objective !!!

No comments:

Post a Comment