文章目录
- 计算机系统
- 5G云计算
- 第四章 LINUX Docker LNMP项目搭建
- 一、项目环境
- 1.环境描述
- 2.容器ip地址规划
- 3.任务需求
- 二、部署过程
- 1.部署构建 nginx 镜像
- 2.部署构建 mysql 镜像
- 3.部署构建 php 镜像
- 4.验证测试
计算机系统
5G云计算
第四章 LINUX Docker LNMP项目搭建
一、项目环境
公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工作
1.环境描述
容器宿主机 | 操作系统 | ip地址 | 主要软件 |
---|---|---|---|
Docker | CentOS 7.3 x86_64 | 192.168.58.60 | Docker 19.03 |
2.容器ip地址规划
宿主机 ip地址:192.168.58.60
nginx容器 ip地址:172.18.0.10
mysql容器 ip地址:172.18.0.20
php容器 ip地址:172.18.0.30
3.任务需求
1)使用Docker构建LNMP环境并运行Wordpress网站平台
2)限制Nginx容器最多使用500Mb的内存和1G的Swap
3)限制Mysql容器写 /dev/sda 的速率为 10 MB/s
4)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地
二、部署过程
###前提本地已经安装docker,配置lnmp的文件目录
cd /opt/
mkdir mylnmp
cd /opt/mylnmp/
mkdir nginx mysql php
###部署一个有所有软件依赖包的镜像,以便进行分阶段构建镜像
cd /opt/
vim Dockerfile
FROM centos:7 ###从公有仓库pull一个centos:7的镜像
COPY qinghua-yum.sh /opt/ ###上传修改yum源的脚本
RUN chmod +x /opt/qinghua-yum.sh && /opt/qinghua-yum.sh && \
yum -y install pcre-devel zlib-devel openssl-devel \
gcc gcc-c++ make ncurses ncurses-devel bison cmake gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel
docker build -t centos7:mylnmp
docker images
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mylnmp
docker network ls
1.部署构建 nginx 镜像
###nginx目录下已上传nginx安装包、论坛安装包、nginx配置文件
cd /opt/mylnmp/nginx/
ls
###部署 nginx 的 Dockerfile
cd /opt/mylnmp/nginx/
vim Dockerfile
FROM centos7:mylnmp
ADD nginx-1.24.0.tar.gz /usr/local/
RUN useradd -M -s /sbin/nologin nginx && \
cd /usr/local/nginx-1.24.0 && \
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
FROM centos:7
COPY --from=0 /usr/local/nginx/ /usr/local/nginx/
RUN useradd -M -s /sbin/nologin nginx
EXPOSE 80
#EXPOSE 443
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
docker build -t nginx:lnmp
docker images
docker run -d --name nginx --network=myLnmp --ip=172.18.0.10 -p 80:80 -v /opt/mylnmp/nginx/html/:/usr/local/nginx/html/ -m 500m - -memory-swap=1.5g centos7:myLnmp
docker ps -a
2.部署构建 mysql 镜像
###mysql目录下已上传mysql安装包、mysql配置文件
cd /opt/mylnmp/mysql/
ls
###部署 mysql 的 Dockerfile
cd /opt/mylnmp/mysql/
vim Dockerfile
FROM centos7:mylnmp
ADD mysql-boost-5.7.41.tar.gz /opt/
RUN cd /opt/mysql-5.7.41/ && \
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && \
make -j4 && make install
FROM centos:7
COPY --from=0 /usr/local/mysql/ /usr/local/mysql/
COPY my.cnf /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql && \
chown -R mysql:mysql /usr/local/mysql/ && \
chown mysql:mysql /etc/my.cnf && \
/usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysgl \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
docker build -t mysql:lnmp
docker images
docker run -d --name mysql --privileged --network mylnmp --ip 172.18.0.20 -v /usr/local/mysql --device-write-bps /dev/sda:10mb mysql:lnmp
docker ps -a
docker exec -it 5066206ee1b6 bash
mysqladmin -u root password "123"
mysql -u root -p123
CREATE DATABASE wordpress;
GRANT all ON wordpress.* TO 'admin'@'%' IDENTIFIED BY '123';
flush privileges;
exit
exit
3.部署构建 php 镜像
###mysql目录下已上传php安装包、php配置文件
cd /opt/mylnmp/php/
ls
###部署 php 的 Dockerfile
cd /opt/mylnmp/php/
vim Dockerfile
FROM centos7:mylnmp
ADD php-7.1.10.tar.bz2 /opt
RUN cd /opt/php-7.1.10 && \
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 2 && make install
COPY php.ini /usr/local/php/lib/php.ini
COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf
COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf
FROM centos:7
COPY --from=0 /usr/local/php /usr/local/php
RUN useradd -M -s /sbin/nologin php && yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
docker build -t php:lnmp
docker images
docker run --name php -d -p 9000:9000 --network mylnmp --ip 172.18.0.30 --volumes-from nginx --volumes-from mysql php:lnmp
docker ps -a
docker exec -it 850d14f706b7 bash
ls /usr/local
ls /usr/local/nginx/html
ls /usr/local/mysql
exit
4.验证测试
http://192.168.58.60/index.html
http://192.168.58.60/index.php
http://192.168.58.60/wordpress/index.php