Bootstrap

拥抱K8S系列-02-服务器部署应用和docker部署应用区别(nginx篇)

废话不多说,我们先上实操,带大家领略一下两种应用部署方式的区别。

先准备两台干净的CentOS系统 (192.168.80.74 和 192.168.80.75),分别在服务器上使用yum和docker方式创建nginx服务

第一步:安装nginx

1、yum安装nginx

192.168.80.74 使用yum方式安装nginx的命令如下

yum -y install nginx-1.16.1
systemctl start nginx
ss -tanlp |grep :80

2、docker安装nginx

192.168.80.75 使用docker方式启动nginx的命令如下:

docker pull nginx:1.16
docker run -d --name=nginx -p 80:80 nginx:1.16
ss -tanlp |grep :80

什么?你系统上的docker还没安装好?请出门左转到 https://xie.infoq.cn/article/346a3848bdb6d2bd8cd7e205e

第二步:访问nginx

这两个nginx都启动完毕了,我们使用浏览器访问看看,两种安装方式安装nginx访问都很正常。

第三步:更改访问的首页

访问首页有什么了不起的,nginx运行肯定要能访问用户自定义页面的,于是接下来我们修改一下访问首页

1、yum方式安装

cd /usr/share/nginx/html/
echo "

WEB PAGE FROM NGINX(YUM)

" > index.html

刷新页面看看效果

2、docker方式安装

# 我们先停止当前运行的nginx容器
docker stop nginx;docker rm nginx;

# 创建一个nginx容器使用的标准化
mkdir -p /data/nginx/
cd /data/nginx/
echo "

WEB PAGE FROM NGINX(DOCKER)

" > index.html docker run -d --name=nginx \ -p 80:80 \ -v /data/nginx/index.html:/usr/share/nginx/html/index.html \ nginx:1.1 说明: -v 参数可以把系统目录下文件或目录映射到docker容器中

刷新页面看看效果

第四步:更改nginx日志配置并查看效果

1、yum方式安装

cd /etc/nginx/
vim nginx.conf   # 在log_format增加三个字段 $status $upstream_status $request_id,其他内容保持不动
    log_format  main  '$status $upstream_status $request_id $remote_addr - $remote_user [$time_local] "$request" '

nginx -t
nginx -s reload

 

我们使用浏览器刷新几次页面然后查看nginx日志前面3列内容

tail /var/log/nginx/access.log
200 - 9638c0892804747e7350ede647f0e29b 192.168.80.1 - - [02/Sep/2020:17:34:32 +0800] "GET / HTTP/1.1" 200 36 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
200 - 42f375ce9ccccde1f71c2cff98b1bfeb 192.168.80.1 - - [02/Sep/2020:17:34:33 +0800] "GET / HTTP/1.1" 200 36 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
200 - f1dd0f63dfe8eacf5146796a250ecb0c 192.168.80.1 - - [02/Sep/2020:17:34:33 +0800] "GET / HTTP/1.1" 200 36 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "

2、docker方式安装

# 我们先停止当前运行的nginx容器
docker stop nginx;docker rm nginx;


# 创建一个nginx容器使用的标准化
mkdir -p /data/nginx/logs
cd /data/nginx/
echo "

WEB PAGE FROM NGINX(DOCKER)

" > index.html # 这份日志格式已经添加了三个字段 tee nginx.conf<<-'EOF' user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$status $upstream_status $request_id $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } EOF docker run -d --name=nginx \ -p 80:80 \ -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \ -v /data/nginx/index.html:/usr/share/nginx/html/index.html \ -v /data/nginx/logs:/var/log/nginx \ nginx:1.16 # 最终目录结构如下 tree /data/nginx/ /data/nginx/ ├── index.html ├── logs │   ├── access.log │   └── error.log └── nginx.conf

 

然后依旧在浏览器上刷新若干次页面,查看nginx日志

tail /data/nginx/logs/access.log | grep ^200
200 - 6f5d41c74846851abb22c558501c757b 192.168.80.1 - - [02/Sep/2020:09:44:05 +0000] "GET / HTTP/1.1" 200 39 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
200 - f4f682eddd8d5aa012aa7550a2b32dd2 192.168.80.1 - - [02/Sep/2020:09:44:06 +0000] "GET / HTTP/1.1" 200 39 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "-"
200 - 25cecd2abc788a918c10be65c4c7f791 192.168.80.1 - - [02/Sep/2020:09:44:06 +0000] "GET / HTTP/1.1" 200 39 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "-

思考和总结

1、经过上面这些简单步骤,请读者回想一下,yum方式安装的nginx影响到到系统上多少个目录或者文件?而如果这台服务器是生产上使用的nginx,操作步骤将会更加复杂,甚至多个运维参与修改,影响到的目录和文件将会更多,最终这台nginx服务器要想进行迁移考虑的问题会很多很多。

 

2、但是docker安装的nginx只影响到的/data/nginx/目录,如果nginx要想迁移,就把/data/nginx/目录打包拷贝到其他服务器上就可以运行起来了,这个就是docker最大的优势--可迁移性。

 

3、yum安装的nginx会在CentOS文件系统上生成大量的文件和目录,散落在各个位置。但是docker安装的nginx就不存在这个问题,所有的文件保存在docker自己管理的数据目录下,用户无需关心,系统还是保持干净的状态。即使是容器依赖的外部文件也可以很方便的进行管理。

 

4、或许你会说,传统方式安装nginx除了使用yum,也可以进行编译安装,编译安装的nginx目录不都统一管理了吗?虽然是统一管理了,但是nginx依旧依赖操作系统的本身环境,nginx要想良好的运行,可能需要对操作系统本身做特定的设定调整。但docker启动的nginx所运行的环境是docker镜像,和服务器本身系统并无关系。

5、操作系统标准化本来就是一个历史难题,同一家公司,在不同时间安装的标准化可能会改变,甚至不同的运维人员也有不同的标准化操作方式,所以大多数的公司会出现不同的服务器使用不同的nginx安装方式,甚至相同的安装方式,但是路径不一致,版本不一致等各种各样的问题。使用容器后,这些问题都不复存在了。容器化是运维标准化的最终产物。