搭建双机热备并迁移docker

实验要求:

1)两台keepalined做负载均衡,让在keepalined上面做nginx的反向代理服务。

2)一台NFS服务器提供nginx的页面

3)两台docker上面做nginx,并且页面必须是NFS上面的页面。

4)最后将docker上面的nginx迁移到另一台主机上面。

使用拓扑图:

搭建双机热备并迁移docker

实验环境:

实验环境:

主机名:

主机IP

使用系统:

keepalined1

192.168.2.4

vip地址:192.168.2.88

Linux

keepalined2

192.168.2.5

vip地址:192.168.2.88

Linux

NFS1

192.168.2.6

Linux

docker1

192.168.2.7

基于Linux安装的docker

docker2

192.168.2.8

基于Linux安装的docker

qianyi

192.168.2.9

基于Linux安装的docker

在192.168.2.4上面搭建keepalined和nginx反向代理

下载keepalined服务

[[email protected] ~]# yum -y install keepalived ipvsadm

搭建双机热备并迁移docker

修改主配置文件并添加vip地址

[[email protected] ~]# vim /etc/keepalived/keepalived.conf

 ! Configuration File for keepalived

 

global_defs {

     router_id keep01 //本地名称,一个服务器一个

   }

vrrp_instance VI_1 { //组名称,一个组内相同

    state MASTER     //热备状态

    interface ens33 //承载VIP地址的网卡

    virtual_router_id 51 //虚拟路由器的ID号,每个组保持一致

    priority 100     //优先级

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.2.88 //漂移IP地址(VIP)

    }

}

重启服务

[[email protected] keepalived]# systemctl restart keepalived.service

查看vip的地址

[[email protected] ~]# ip addr show ens33

搭建双机热备并迁移docker

下载nginx软件包

搭建双机热备并迁移docker

解决依赖、解压并安装nginx。

[[email protected] ~]# yum -y install  openssl-devel  pcre-devel zlib-devel gcc gcc*

[[email protected] ~]# mkdir -p /var/tmp/nginx/client

[[email protected] ~]# chmod -R 777 /var/tmp/nginx/

[[email protected] ~]# useradd -M -s /sbin/nologin nginx

[[email protected] ~]# tar zxf ngx_cache_purge-2.3.tar.gz

[[email protected] ~]# unzip nginx-sticky-module.zip

[[email protected] ~]# tar zxf nginx-1.14.2.tar.gz

[[email protected] ~]# cd nginx-1.14.2/

[[email protected] nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre  --add-module=../ngx_cache_purge-2.3 --with-http_flv_module --add-module=../nginx-sticky-module

[[email protected] nginx-1.14.2]# make && make install

[[email protected] nginx-1.14.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

[[email protected] nginx-1.14.2]# nginx

修改nginx配置文件,支持反向代理、

[[email protected] nginx-1.14.2]# cd

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

修改内容如下:

http {

    include       mime.types;

    default_type  application/octet-stream;

 

        upstream backend {

                 server 192.168.2.7 weight=1 max_fails=2 fail_timeout=10s;

                 server 192.168.2.8 weight=1 max_fails=2 fail_timeout=10s;

                 sticky;

         }

location / {

         root   html;

     #       index  index.html index.htm;

                proxy_pass http://backend;

       }

搭建双机热备并迁移docker

再次重启nginx服务,使其nginx反向代理生效

[[email protected] ~]# nginx -t

[[email protected] ~]# nginx -s reload

搭建双机热备并迁移docker

在 192.168.2.5上面搭建从服务,和nginx反向代理。

下载 keepalived

[[email protected] ~]# yum -y install keepalived ipvsadm

搭建双机热备并迁移docker

修改配置文件

[[email protected] ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

     route_id keep02

   }

 

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 10

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.2.88

    }

}

重启服务

[[email protected] ~]# systemctl restart keepalived.service

下载nginx所支持的软件包。

搭建双机热备并迁移docker

解决依赖,并安装nginx

[[email protected]2 ~]# yum -y install  openssl-devel  pcre-devel zlib-devel gcc gcc*

[[email protected]2 ~]# mkdir -p /var/tmp/nginx/client

[[email protected]2 ~]# chmod -R 777 /var/tmp/nginx/

[[email protected]2 ~]# useradd -M -s /sbin/nologin nginx

[[email protected]2 ~]# tar zxf ngx_cache_purge-2.3.tar.gz

[[email protected]2 ~]# unzip nginx-sticky-module.zip

[[email protected]2 ~]# tar zxf nginx-1.14.2.tar.gz

[[email protected]2 ~]# cd nginx-1.14.2/

[[email protected]2 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre  --add-module=../ngx_cache_purge-2.3 --with-http_flv_module --add-module=../nginx-sticky-module

[[email protected]2 nginx-1.14.2]# make && make install

[[email protected]2 nginx-1.14.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

[[email protected]2 nginx-1.14.2]# nginx

修改nginx配置文件,使其支持nginx反向代理、

[[email protected] nginx-1.14.2]# cd

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

修改内容如下:

http {

    include       mime.types;

    default_type  application/octet-stream;

 

        upstream backend {

                 server 192.168.2.7 weight=1 max_fails=2 fail_timeout=10s;

                 server 192.168.2.8 weight=1 max_fails=2 fail_timeout=10s;

                 sticky;

         }

location / {

         root   html;

     #       index  index.html index.htm;

                proxy_pass http://backend;

       }

搭建双机热备并迁移docker

重启nginx服务,使其支持nginx反向代理。

[[email protected] ~]# nginx -t

[[email protected] ~]# nginx -s reload

搭建双机热备并迁移docker

在192.168.2.6上面做NFS服务

下载NFS

[[email protected] ~]# yum -y install nfs-utils rpcbind

搭建双机热备并迁移docker

设置共享服务

[[email protected] ~]# mkdir -p /opt/wwwroot

[[email protected] ~]# echo "this is nginx" > /opt/wwwroot/index.html

[[email protected] ~]# cat /opt/wwwroot/index.html

this is nginx

[[email protected] ~]# vim /etc/exports

/opt/wwwroot 192.168.2.0/24 (rw,sync,no_root_squash)

启动NFS服务并加入开机自启

[[email protected] ~]# systemctl start rpcbind

[[email protected] ~]# systemctl start nfs

[[email protected] ~]# systemctl enable nfs

[[email protected] ~]# systemctl enable rpc-rquotad

[[email protected] ~]# netstat -anpt | grep rpc

搭建双机热备并迁移docker

搭建双机热备并迁移docker

查看提供共享的目录

[[email protected] ~]# showmount -e

搭建双机热备并迁移docker

在192.168.2.7上面挂载NFS和安装nginx

下载NFS的客户端、开启并加入开机自启

[[email protected] ~]# yum -y install nfs-utils rpcbind

[[email protected] ~]# yum -y install nfs-utils

[[email protected] ~]# systemctl enable rpcbind

[[email protected] ~]# systemctl start rpcbind

查看是否能连接到NFS服务器上面

[[email protected] ~]# showmount -e 192.168.2.6

搭建双机热备并迁移docker

创建需要挂载的目录

[[email protected] ~]# mkdir -p /var/www/html

将创建的目录自动挂载到NFS目录。

[[email protected] ~]# vim /etc/fstab

192.168.2.6:/opt/wwwroot /var/www/html/          nfs    defaults,_netdev 0 0

搭建双机热备并迁移docker

启用挂载

[[email protected] ~]# mount -a

[[email protected] ~]# tail -1 /etc/mtab

搭建双机热备并迁移docker

下载nginx镜像

[[email protected] ~]# docker pull nginx

[[email protected] ~]# docker images

搭建双机热备并迁移docker

基于nginx镜像运行一个容器,并挂载到NFS的目录、映射到本地80端口。

[[email protected] ~]# docker run -itd --name nginx -p 80:80 -v /var/www/html/:/usr/share/nginx/html/ nginx

搭建双机热备并迁移docker

在浏览器访问一下nginx的网页查看是否是NFS上面的页面。

http://192.168.2.7/

搭建双机热备并迁移docker

在浏览器上面使用Vip地址是否能访问nginx的页面

http://192.168.2.88/

搭建双机热备并迁移docker

在192.168.2.8上面挂载NFS和安装nginx

下载NFS的客户端、开启并加入开机自启

[[email protected] ~]# yum -y install nfs-utils rpcbind

[[email protected] ~]# yum -y install nfs-utils

[[email protected] ~]# systemctl enable rpcbind

[[email protected] ~]# systemctl start rpcbind

查看是否能连接到NFS服务器上面

[[email protected] ~]# showmount -e 192.168.2.6

搭建双机热备并迁移docker

创建需要挂载的目录

[[email protected] ~]# mkdir -p /var/www/html

将创建的目录自动挂载到NFS目录。

[[email protected] ~]# vim /etc/fstab

192.168.2.6:/opt/wwwroot /var/www/html/       nfs     defaults,_netdev 0 0

启用挂载

[[email protected] ~]# mount -a

[[email protected] ~]# tail -1 /etc/mtab

搭建双机热备并迁移docker

从docker hub上面下载nginx的镜像

[[email protected] ~]# docker pull nginx

[[email protected] ~]# docker images

搭建双机热备并迁移docker

基于nginx镜像运行一个容器,并且挂载到NFS目录、映射到本地80端口

[[email protected] ~]# docker run -itd --name nginx -p 80:80 -v /var/www/html/:/usr/share/nginx/html/ nginx

搭建双机热备并迁移docker

使用浏览器访问查看是否是NFS上面的网页

http://192.168.2.8/

搭建双机热备并迁移docker

使用vip地址查看也能访问呢

http://192.168.2.88/

搭建双机热备并迁移docker

在192.168.2.9上面数据迁移

在192168.2.8上面编写Dockerfile文件

[[email protected] ~]# cd /var/www/html/

[[email protected] html]# vim Dockerfile

内容如下:

FROM busybox:latest          //基于这个镜像构建

ADD html /usr/share/nginx/html //将html拷贝到 /usr/share/nginx/html

VOLUME /usr/share/nginx/html    //容器中数据卷的路径

创建镜像

[[email protected] www]# docker build -t datapacked .

[[email protected] www]# docker images

搭建双机热备并迁移docker

搭建双机热备并迁移docker

创建一个容器当数据卷

[[email protected] www]# docker create --name vc_data01 datapacked

搭建双机热备并迁移docker

基于数据卷运行一个容器,并指定端口

[[email protected] www]# docker run -itd --name my-nginx -p 81:80 --volumes-from vc_data01 nginx

搭建双机热备并迁移docker

访问一下是否是原网页

[[email protected] www]# curl 127.0.0.1:81

搭建双机热备并迁移docker

my-nginx容器制作成镜像

[[email protected] www]# cd

[[email protected] ~]# docker commit my-nginx my-nginx:v1.0

[[email protected] ~]# docker images

搭建双机热备并迁移docker

再将镜像制作成tar包并传给192.168.2.9的迁移主机上。

[[email protected] ~]# docker save > my-nginx.tar my-nginx:v1.0

[[email protected] ~]# ls

[[email protected] ~]# scp my-nginx.tar [email protected]:/root/

搭建双机热备并迁移docker

搭建双机热备并迁移docker

再次将datapacked 这个挂载数据镜像制作成tar 传输给192.168.2.9的迁移主机上、

[[email protected] ~]# docker save > datapacked.tar datapacked

[[email protected] ~]# ls

[[email protected] ~]# scp datapacked.tar [email protected]:/root/

搭建双机热备并迁移docker

搭建双机热备并迁移docker

在迁移主机192.168.2.9上面查看是否将tar包传输过来了

搭建双机热备并迁移docker

将tar包制作成镜像

[[email protected] ~]# docker load < my-nginx.tar

[[email protected] ~]# docker load < datapacked.tar

[[email protected] ~]# docker images

搭建双机热备并迁移docker

创建一个容器当数据库卷

[[email protected] ~]# docker create --name vc_data01 datapacked

搭建双机热备并迁移docker

创建一个容器当数据库卷

[[email protected] ~]# docker create --name vc_data01 datapacked

搭建双机热备并迁移docker

基于数据卷运行一个容器并指定端口

[[email protected] ~]# docker run -itd --name nginx -p 80:80 --volumes-from vc_data01 my-nginx:v1.0

搭建双机热备并迁移docker

在浏览器上面访问网页查看页面是否一致

http://192.168.2.9/

搭建双机热备并迁移docker

访问vip地址呢是否还能正常

http://192.168.2.88/

搭建双机热备并迁移docker

能看到这个呢,一个简单的搭建负载并迁移就完成了。

相关文章: