1. 程式人生 > >玩轉docker-簡單例項

玩轉docker-簡單例項

    主要是想通過一個例項來說明docker的最顯著的特徵:資料隔離,順便帶領大家熟悉一下docker的基本命令用法,揭開其神祕的面紗,讓大家也能領略到,當今網際網路大資料行業,最火的虛擬化技術,也不過如此,哈哈!

建立mysql5.6容器 1master+3個slave

docker run --name mysql-master -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave1 -d -P hub.c.163.com/nce2/mysql:5.6

驗證容器狀態

[root@bogon ~]# docker ps
CONTAINER ID        IMAGE                          COMMAND             CREATED             STATUS              PORTS               NAMES
907bbbf25d25        hub.c.163.com/nce2/mysql:5.6   "/run.sh"           5 minutes ago       Up 5 minutes        3306/tcp            mysql-slave3
a81df6c86808        hub.c.163.com/nce2/mysql:5.6   "/run.sh"           5 minutes ago       Up 5 minutes        3306/tcp            mysql-slave2
375eabd4c598        hub.c.163.com/nce2/mysql:5.6   "/run.sh"           5 minutes ago       Up 5 minutes        3306/tcp            mysql-slave1
1651d1cab219        hub.c.163.com/nce2/mysql:5.6   "/run.sh"           14 minutes ago      Up 14 minutes       3306/tcp            mysql-master

通過主機命令列進入master容器

docker exec -it mysql-master bash
[root@bogon ~]# docker exec -it mysql-master bash
root@1651d1cab219:/#

在master中建立一個數據庫test_docker

root@1651d1cab219:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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 |
| #bak_database      |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.02 sec)

mysql> create database test_docker;
Query OK, 1 row affected (0.06 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #bak_database      |
| mysql              |
| performance_schema |
| test               |
| test_docker        |
+--------------------+
6 rows in set (0.00 sec)

在slave1中建立一個數據庫test_docker

[root@bogon ~]# docker exec -it mysql-slave bash
Error response from daemon: No such container: mysql-slave
[root@bogon ~]# docker exec -it mysql-slave1 bash
root@375eabd4c598:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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 |
| #bak_database      |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

通過以上的資訊說明master與slave是資料隔離的,
所以我們可以通過docker建立N個mysql容器,
然後就能以很小的代價就能學習《高可用MySQL》中的資料模型 再也不用發愁機器不夠用。

後續操作

登入到master容器

[root@bogon ~]# docker exec -it mysql-master bash
root@1651d1cab219:/#

怎麼檢視容器的作業系統環境
一般就是
uname -a
cat /etc/pro
cat /etc/lsb-release

很幸運我們的容器是ubuntu14.04

root@1651d1cab219:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"

但是執行 apt-get install時候卻什麼也裝不了
需要更改

cd /etc/apt/

沒有 vi vim ee 編輯器 只好追加內容到 sources.list

 echo deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse >> sources.list
 echo deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse >> sources.list
 echo deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse >> sources.list
 echo deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse >> sources.list
 echo deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse >> sources.list
 echo deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse >> sources.list
 echo deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse >> sources.list
 echo deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse >> sources.list
 echo deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse >> sources.list
 echo deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse >> sources.list

然後 更新源

apt-get update
apt-get install vim

然後通過vim把sources.list檔案的前兩行刪除掉 再重新update一下。

apt-get update

安裝一個網路工具獲取ip

apt-get install net-tools

獲取到master的ip地址

root@1651d1cab219:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:02
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:15119 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12633 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:34197557 (34.1 MB)  TX bytes:897732 (897.7 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:22 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2212 (2.2 KB)  TX bytes:2212 (2.2 KB)

slave也需要這麼做

還有一種方法
可以建立Dockerfile依賴mysql映象建立一個新的映象。
上訴命令通過RUN來執行建立的新容器會有安裝的軟體。

最後通過slave的docker連線到master的容器mysql伺服器上

master的伺服器mysql賬號root賦值許可權

mysql> grant all privileges on *.* to root@'%' identified by '';
Query OK, 0 rows affected (0.02 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.02 sec)

slave伺服器執行如下命令

[root@bogon ~]# mysql -uroot -p -h 172.17.0.2
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]>

在master上將test_docker資料庫刪除,看slave的終端是否也不顯示已刪除的庫

master操作

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #bak_database      |
| mysql              |
| performance_schema |
| test               |
| test_docker        |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database test_docker;
Query OK, 0 rows affected (0.06 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #bak_database      |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

slave操作

小插曲:

    在初次建立docker mysql容器例項的時候,中間報了一個如下錯誤:

不管如何啟動mysql-master 或者 mysql-slave 例項,總是出現上述異常,後來,問了一下度娘,終於找到了解決方法,原來是因為doker 的 RPM packages需要升級了,   最後打通了此條探索的道路,甚是不容易哈,解決方法,現共享如下:

yum downgrade docker docker-client docker-common

    上述是本節的主要內容,現分享給各位小夥伴們,歡迎各位能提出寶貴的意見或者建議,在成為土豪路上一起成長!