一、学习使用docker,并完成以下任务
完成这个任务前现在自己机器上创建一个http服务,感受下这个服务的含义。
- 利用docker在单机上创建两个httpd服务,能够提供web服务。分别是8081端口和8082端口。在8081服务的根目录上放置readme.html文件。在8082服务的根目录上放置readme.html文件,这两个html文件要有区别。 里面内容可以随便写(比如8081这个写abc,8082这个写123)。
创建两个html文件
[root@localhost exam2]# echo "abc" >> readme1.html[root@localhost exam2]# echo "123" >> readme2.html
拉取 httpd docker 镜像,并且获取镜像中的httpd的配置
[root@localhost exam2]# docker pull httpd[root@localhost exam2]# docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > my-httpd.conf
修改 my-httpd.conf , 添加内容
Listen 80Listen 81<VirtualHost *:80>DocumentRoot /usr/local/apache2/htdocs/home1ServerName localhost:80</VirtualHost><VirtualHost *:81>DocumentRoot /usr/local/apache2/htdocs/home2ServerName localhost:81</VirtualHost>
创建Dockerfile 内容为
FROM httpd:2.4COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.confCOPY ./readme1.html /usr/local/apache2/htdocs/home1/readme.htmlCOPY ./readme2.html /usr/local/apache2/htdocs/home2/readme.html
编译Dockerfile并运行
[root@localhost exam2]# docker build -t my-apache2 .[root@localhost exam2]# docker run -dit -p 8081:80 -p 8082:81 my-apache2
查看8080端口和8081端口的内容
[root@localhost exam2]# curl localhost:8081/readme.htmlabc[root@localhost exam2]# curl localhost:8082/readme.html123
- iptables封掉8081端口。使得http://127.0.0.1:8081/readme.html 无法访问而http://127.0.0.1:8082/readme.html 可以访问。
添加新的规则,drop所有目的地为8081 端口的请求
[root@localhost exam2]# iptables -t filter -I INPUT -p tcp --dport 8081 -j DROP[root@localhost exam2]# iptables -L DOCKER-USER -n --line-numberChain DOCKER-USER (1 references)num target prot opt source destination1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80812 RETURN all -- 0.0.0.0/0 0.0.0.0/0
验证是否封掉8081端口
[root@localhost ~]# curl http://127.0.0.1:8081/readme.html --connect-timeout 1curl: (28) Connection timed out after 1002 milliseconds[root@localhost ~]# curl http://127.0.0.1:8082/readme.html --connect-timeout 1123
- iptables 做流量均衡(nat),将所有80端口导入的流量50%分配给8081端口,50%的流量分配给8082端口。配置后,刷10次http://127.0.0.1:80/readme.html 次。如果4~5次结果是abc,另外的显示123。说明配置正确。
[root@localhost ~]# iptables -t nat -I PREROUTING 1 -p tcp --dport 80 -j REDIRECT --to-ports 8082 -m statistic --mode random --probability 0.5[root@localhost ~]# iptables -t nat -I PREROUTING 1 -p tcp --dport 80 -j REDIRECT --to-ports 8081 -m statistic --mode random --probability 0.5
二:安装mysql数据库,并完成以下任务
- 安装好后,创建自己的数据库test,然后创建一个表,字段是id(整形),name(255个字符长度的字符串型),ts(时间戳型,默认为当前时间戳),并手动写入三行记录,并删除其中的一条记录,查询是否正确删除,然后利用mysqldump 命令将剩余两条记录的数据库表dump出来,dump的文件叫test.sql, 然后vim test.sql 看看这个文件长什么样子。写出相应的命令。
[root@localhost ~]# mysql -uroot -pmysql> CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8;Query OK, 1 row affected (0.00 sec)mysql> use testDatabase changedmysql> CREATE TABLE IF NOT EXISTS `test` (-> id INT,-> name VARCHAR(255),-> ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP-> );Query OK, 0 rows affected (0.02 sec)mysql> desc test;+-------+--------------+------+-----+-------------------+-------+| Field | Type | Null | Key | Default | Extra |+-------+--------------+------+-----+-------------------+-------+| id | int(11) | YES | | NULL | || name | varchar(255) | YES | | NULL | || ts | timestamp | NO | | CURRENT_TIMESTAMP | |+-------+--------------+------+-----+-------------------+-------+3 rows in set (0.03 sec)mysql> INSERT INTO test (id, name, ts) VALUES (1, 'user1', 20021114), (2, 'user2', 20181011), (3, 'user3', 20200301);Query OK, 3 rows affected (0.00 sec)Records: 3 Duplicates: 0 Warnings: 0mysql> DELETE FROM test where id = 2;Query OK, 1 row affected (0.00 sec)mysql> select * from test;+------+-------+---------------------+| id | name | ts |+------+-------+---------------------+| 1 | user1 | 2002-11-14 00:00:00 || 3 | user3 | 2020-03-01 00:00:00 |+------+-------+---------------------+2 rows in set (0.00 sec)
利用mysqldump 命令将剩余两条记录的数据库表dump出来,-n的目的
[root@localhost ~]# mysqldump -uroot -p --databases test --tables test > test.sql
test.sql 文件内容
[root@localhost ~]# cat test.sql-- MySQL dump 10.13 Distrib 5.7.29, for Linux (x86_64)---- Host: localhost Database: test-- -------------------------------------------------------- Server version 5.7.29/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;/*!40103 SET TIME_ZONE='+00:00' */;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;---- Table structure for table `test`--DROP TABLE IF EXISTS `test`;/*!40101 SET @saved_cs_client = @@character_set_client */;/*!40101 SET character_set_client = utf8 */;CREATE TABLE `test` (`id` int(11) DEFAULT NULL,`name` varchar(255) DEFAULT NULL,`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8;/*!40101 SET character_set_client = @saved_cs_client */;---- Dumping data for table `test`--LOCK TABLES `test` WRITE;/*!40000 ALTER TABLE `test` DISABLE KEYS */;INSERT INTO `test` VALUES (1,'user1','2002-11-14 05:00:00'),(3,'user3','2020-03-01 05:00:00');/*!40000 ALTER TABLE `test` ENABLE KEYS */;UNLOCK TABLES;/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2020-04-10 8:42:13
- 将下面mysqldump出来的数据库表weibo.sql还原到数据库中,http://117.50.100.6:12345/linux/weibo.sql。并写出语句查询这个表的记录数。写出相应的过程。
[root@localhost ~]# mysql -uroot -p test < weibo.sql[root@localhost ~]# mysql -uroot -pmysql> use test;mysql> select count(1) from fresh_data;+----------+| count(1) |+----------+| 500 |+----------+1 row in set (0.00 sec)
- 这个数据库表中有一个字段叫user_type表明微博用户的类型,找到其中是黄V的用户,并用mysqldump语句将记录中的user_type是黄V的记录导出来。(带条件的mysqldump),导出后vim 这个dump文件看看是否正确。
mysql> select distinct user_type from fresh_data;+--------------+| user_type |+--------------+| 普通用户 || 达人 || 黄V || 微博女郎 || 金V || 蓝V |+--------------+6 rows in set (0.01 sec)[root@localhost ~]# mysqldump -uroot -p --databases test --tables fresh_data --where='user_type="黄V"' > test.sql
