实验环境:
一台centos7.4本地搭建MySQL ip:192.168.130.128 dockerip:172.17.0.1
docker容器nginx2台 第一台ip:172.17.0.2 第二台ip:172.17.0.3
docker容器php1台 PHP容器ip:172.17.0.4
两台Redhat6.5搭建nginx+keepalived
192.168.130.129
192.168.130.130
先在7.4上生成nginx和PHP的镜像
Nginx的镜像:
vim dockerfile
FROM centos
COPY nginx-1.6.0.tar.gz /
RUN yum -y install zlib-devel
RUN yum -y install pcre-devel
RUN yum -y install vim
RUN yum -y install gcc*
RUN yum -y install net-tools
RUN tar zxf nginx-1.6.0.tar.gz
WORKDIR /nginx-1.6.0
RUN useradd -M -s /sbin/nologin nginx
RUN yum -y install make
WORKDIR /nginx-1.6.0
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
RUN yum install -y psmisc
EXPOSE 80
CMD /usr/local/nginx/sbin/nginx && /bin/bash
保存退出
docker build -t nginx . ——生成镜像
PHP镜像:
vim dockerfile
FROM centos
RUN yum -y install net-tools
RUN yum -y install gcc*
RUN yum -y install psmisc
RUN yum install -y elinks
RUN yum install -y make
RUN yum install -y vim
RUN yum -y install gd libxml2-devel libjpeg-devel libpng-devel
WORKDIR /
COPY php-5.3.28.tar.gz /
RUN tar zxf php-5.3.28.tar.gz
WORKDIR php-5.3.28
RUN ./configure --prefix=/usr/local/php --with-gd --with-zlib --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-config-file-path=/usr/local/php --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib
RUN make && make install
RUN cp /php-5.3.28/php.ini-development /usr/local/php/php.ini
RUN ln -s /usr/local/php/bin/* /usr/local/bin/
RUN ln -s /usr/local/php/sbin/* /usr/local/sbin/
CMD ["/bin/bash"]
docker build -t php . ——生成镜像
打开容器
docker run -it nginx
exit
docker ps -a ——查看容器ID
docker start 807b70c94029 ——启动nginx容器
docker exec -it 807b70c94029 /bin/bash ——进入容器的/bin/bash界面
开启第二台nginx容器
docker run -it nginx
exit
docker ps -a ——查看容器ID
docker start 1f929f838441 ——启动nginx容器
docker exec -it 1f929f838441 /bin/bash ——进入容器的/bin/bash界面
开启PHP容器
docker run -it php
exit
docker ps -a ——查看容器ID
docker start cf1f5bd19d89 ——启动nginx容器
docker exec -it cf1f5bd19d89 /bin/bash ——进入容器的/bin/bash界面
让nginx容器与php容器之间联通
进入php容器
docker exec -it cf1f5bd19d89 /bin/bash
Useradd -M -s /sbin/nologin nginx
vim /usr/local/php/php.ini
修改:
default_charset = "utf-8"
short_open_tag = On
vim /usr/local/php/etc/php-fpm.conf
修改:
pid = run/php-fpm.pid
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
user = nginx
group = nginx
listen = 172.17.0.4:9000 ——监听本地ip
接着创建一个测试页面
mkdir /www
vim /www/index.php
<?php
phpinfo( );
?>
chown nginx /www/ -R
配置nginx容器
docker exec -it 807b70c94029 /bin/bash
vim /usr/local/nginx/conf/nginx.conf
修改:
保存退出
killall -s HUP nginx ——重启服务
两台nginx操作一样
验证测试页面
firefox 172.17.0.2
firefox 172.17.0.3
访问到不同ip看到PHP测试页面即为成功
本地搭建nginx进行一个代理
yum install -y zlib-devel pcre-devel
tar zxf nginx-1.6.0.tar.gz
cd nginx-1.6.0/
useradd -M -s /sbin/nologin nginx
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
vim /usr/local/nginx/conf/nginx.conf
http模块下
添加:
upstream web {
server 172.17.0.2:80;
server 172.17.0.3:80;
}
在server模块下添加:
location =/ {
proxy_pass http://web;
}
nginx ——启动服务
在两个nginx容器里监视连接日志
[root@1f929f838441 ~]# tailf /usr/local/nginx/logs/access.log
[root@807b70c94029 nginx]# tailf /usr/local/nginx/logs/access.log
进行访问
elinks 192.168.130.128 ——访问本地ip
elinks 192.168.130.128 ——访问本地ip
进入两台nginx容器里看
第一台
第二台
出现访问记录说明本地nginx代理成功
搭建nginx+keepalived
vim /usr/local/nginx/conf/nginx.conf
tar zxf nginx-1.6.0.tar.gz
cd nginx-1.6.0
useradd -M -s /sbin/nologin nginx
yum install -y pcre-devel zlib-devel
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
vim /usr/local/nginx/conf/nginx.conf
添加:
upstream webn {
server 192.168.130.128:80;
}
location =/ {
proxy_pass http://webn;
}
nginx -t ——检查语法
nginx ——启动服务
firefox 192.168.130.129 ——访问本地ip
能看到测试页面即为代理成功
另一台操作也是一样
最后效果访问自身ip能看到测试页面即为成功
搭建keepalived
192.168.130.129为master
192.168.130.130为backup
Master上配置:
tar zxf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
make && make install
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak ——备份
vim /etc/keepalived/keepalived.conf
/etc/init.d/keepalived start
Backup上配置:
安装步骤都是一样的
/etc/init.d/keepalived start
在master上查看飘逸ip
ip a | grep eth0
接着我们试着访问虚拟ip看能否看到测试页面
关闭master上的keepalived
/etc/init.d/keepalived stop
在backup上查看飘逸ip
ip a | grep eth0
继续访问寻ip
能访问出测试页面说明keepalived成功
在192.168.130.128上搭建mysql
tar zxf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure
gmake && gmake install
useradd -M -s /sbin/nologin mysql
tar zxf mysql-5.5.22.tar.gz -C /usr/src/
cd /usr/src/mysql-5.5.22/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
make && make install
chown -R mysql:mysql /usr/local/mysql
cd /usr/src/mysql-5.5.22/
cp support-files/my-medium.cnf /etc/my.cnf
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
. /etc/profile
cp /usr/src/mysql-5.5.22/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
mysql -u root
grant all on *.* to 'test'@'172.17.%.%' identified by '123.com'; ——授权
flush privileges;
拖拽论坛报
cp /upload/* /www/ -R
chown -R nginx.nginx /www/
chmod -R 777 /www/
现在可以访问进行安装
firefox 192.168.130.200/install
安装完毕即可说明我们PHP与数据库连接成功