Docker:单机编排工具docker-compose [十二]

一、docker-compose的安装

1、安装

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y python2-pip
pip install -i  https://pypi.tuna.tsinghua.edu.cn/simple docker-compose

2、检查是否安装成功

docker-compose -v

二、docker-compose案例

1、撰写docker-compose.yml文件

[root@luoahong mywordpress]# pwd
/opt/mywordpress
[root@luoahong mywordpress]# ll
total 4
-rw-r--r-- 1 root root 603 Jan 18 10:51 docker-compose.yml
[root@luoahong mywordpress]# cat docker-compose.yml 
version: '3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - web_data:/var/www/html
     ports:
       - "80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:
    web_data:

2、启动容器

[root@luoahong mywordpress]# docker-compose up -d
Starting mywordpress_db_1 ... done
Starting mywordpress_wordpress_1 ... done
[root@luoahong mywordpress]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                   NAMES
7fdb12738746        wordpress:latest    "docker-entrypoint.s…"   5 minutes ago       Up 11 seconds              0.0.0.0:32769->80/tcp   mywordpress_wordpress_1
72e1954332dc        mysql:5.7           "docker-entrypoint.s…"   5 minutes ago       Up 12 seconds              3306/tcp, 33060/tcp     mywordpress_db_1
f82cd0895416        centos:6.9          "/bin/bash"              2 days ago          Exited (255) 2 hours ago   0.0.0.0:80->80/tcp      gifted_swirles

3、容器启动了什么网络

[root@luoahong mywordpress]# docker network ls
NETWORK ID          NAME                  DRIVER              SCOPE
d766f2526542        bridge                bridge              local
30b37ec09ff2        host                  host                local
ad2616372f01        mywordpress_default   bridge              local
03062097926d        none                  null                local

4、容器启动了什么数据卷

[root@luoahong mywordpress]# docker volume ls
DRIVER              VOLUME NAME
local               mywordpress_db_data
local               mywordpress_web_data

5、动态增加容器

[root@luoahong mywordpress]# docker-compose scale wordpress=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting mywordpress_wordpress_1 ... done
Creating mywordpress_wordpress_2 ... done
Creating mywordpress_wordpress_3 ... done
[root@luoahong mywordpress]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                   NAMES
dc0548d44d4f        wordpress:latest    "docker-entrypoint.s…"   10 seconds ago      Up 6 seconds               0.0.0.0:32771->80/tcp   mywordpress_wordpress_3
2ca0fa1916be        wordpress:latest    "docker-entrypoint.s…"   10 seconds ago      Up 6 seconds               0.0.0.0:32770->80/tcp   mywordpress_wordpress_2
7fdb12738746        wordpress:latest    "docker-entrypoint.s…"   6 minutes ago       Up About a minute          0.0.0.0:32769->80/tcp   mywordpress_wordpress_1
72e1954332dc        mysql:5.7           "docker-entrypoint.s…"   6 minutes ago       Up About a minute          3306/tcp, 33060/tcp     mywordpress_db_1
f82cd0895416        centos:6.9          "/bin/bash"              2 days ago          Exited (255) 2 hours ago   0.0.0.0:80->80/tcp      gifted_swirles

6、测试截图

 

 三、docker-compose.yml文件详解

1、全文注解

version: '3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always   #容器重启服务跟着自动重启
     environment:   #容器的环境变量
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
     depends_on:  #依赖db服务
       - db
     image: wordpress:latest
     volumes:      #创建数据圈
       - web_data:/var/www/html    
     ports:
       - "80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:
    web_data:

2、restart: always的作用

[root@oldboy ~]# docker run -d -p 5000:5000 --restart=always  -v /opt/myregistry:/var/lib/registry  registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
[root@luoahong2 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
64842c29c697        registry            "/entrypoint.sh /etc…"   28 minutes ago      Up 28 minutes       0.0.0.0:5000->5000/tcp   heuristic_montalcini
[root@luoahong2 ~]# systemctl restart docker
[root@luoahong2 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
64842c29c697        registry            "/entrypoint.sh /etc…"   29 minutes ago      Up 4 seconds        0.0.0.0:5000->5000/tcp   heuristic_montalcini
[root@luoahong2 ~]# systemctl restart docker
[root@luoahong2 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
64842c29c697        registry            "/entrypoint.sh /etc…"   29 minutes ago      Up 1 second         0.0.0.0:5000->5000/tcp   heuristic_montalcini

容器重启服务跟着自动重启

四、容器负载均衡搭建测试

1、编写测试文件

root@luoahong mywordpress]# docker volume ls
DRIVER              VOLUME NAME
local               mywordpress_db_data
local               mywordpress_web_data
[root@luoahong mywordpress]# cd /var/lib/docker/volumes/mywordpress_web_data/_data/
[root@luoahong _data]# ll
total 196
-rw-r--r--  1 33 33   418 Sep 25  2013 index.php
-rw-r--r--  1 33 33 19935 Jan  7  2018 license.txt
-rw-r--r--  1 33 33  7415 Mar 19  2018 readme.html
-rw-r--r--  1 33 33  6878 Dec 13 09:30 wp-activate.php
drwxr-xr-x  9 33 33  4096 Dec 20 07:23 wp-admin
-rw-r--r--  1 33 33   364 Dec 19  2015 wp-blog-header.php
-rw-r--r--  1 33 33  1889 May  3  2018 wp-comments-post.php
-rw-r--r--  1 33 33  3154 Jan 18 11:21 wp-config.php
-rw-r--r--  1 33 33  2764 Jan 18 11:21 wp-config-sample.php
drwxr-xr-x  4 33 33    52 Dec 20 07:23 wp-content
-rw-r--r--  1 33 33  3669 Aug 20  2017 wp-cron.php
drwxr-xr-x 19 33 33  8192 Dec 20 07:23 wp-includes
-rw-r--r--  1 33 33  2422 Nov 21  2016 wp-links-opml.php
-rw-r--r--  1 33 33  3306 Aug 22  2017 wp-load.php
-rw-r--r--  1 33 33 37296 Dec 13 08:28 wp-login.php
-rw-r--r--  1 33 33  8048 Jan 11  2017 wp-mail.php
-rw-r--r--  1 33 33 17421 Oct 23 15:04 wp-settings.php
-rw-r--r--  1 33 33 30091 Apr 30  2018 wp-signup.php
-rw-r--r--  1 33 33  4620 Oct 24  2017 wp-trackback.php
-rw-r--r--  1 33 33  3065 Sep  1  2016 xmlrpc.php
[root@luoahong _data]# pwd
/var/lib/docker/volumes/mywordpress_web_data/_data
[root@luoahong _data]# 
  oot@luoahong _data]# vim info.php
▽
<?php phpinfo(); ?>

2、部署nginx

[root@luoahong2 ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.CgAfYa: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@luoahong2 ~]# yum install nginx -y

3、配置nginx文件

[root@luoahong2 ~]# cat /etc/nginx/nginx.conf 
user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
upstream wordpress{
    server 192.168.231.136:32769;
    server 192.168.231.136:32770;
    server 192.168.231.136:32771;
}
    server{
	listen		80;
	server_name	localhost;
	location / {
proxy_pass 	http://wordpress;
proxy_set_header	Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	}
   }
}

[root@luoahong2 ~]# systemctl restart nginx.service 
[root@luoahong2 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      18726/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1163/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1163/sshd           
tcp6       0      0 :::5000                 :::*                    LISTEN      18307/docker-proxy  
udp        0      0 0.0.0.0:68              0.0.0.0:*                           15813/dhclient      
udp        0      0 0.0.0.0:38159           0.0.0.0:*                           15813/dhclient      
udp        0      0 127.0.0.1:323           0.0.0.0:*                           615/chronyd         
udp6       0      0 :::26916                :::*                                15813/dhclient      
udp6       0      0 ::1:323                 :::*                                615/chronyd       

4、容器负载均衡测试截图

 

posted @ 2019-01-18 17:08  活的潇洒80  阅读(836)  评论(0编辑  收藏  举报