欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker-compose配置

程序员文章站 2024-02-25 14:55:51
...

Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速的部署分布式应用。

这是我的docker images(local):
上面的v1就是我封装的httpd

docker-compose配置

先用rhel7镜像封装一个有httpd的服务镜像:

docker-compose配置

Dockerfile文件:
FROM rhel7
MAINTAINER oo
ENV HOSTNAME web1
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dvd.repo
RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
COPY web/index.html /var/www/html/index.html
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
dvd.repo文件:
[dvd]
name=rhel7.3
baseurl=http://172.25.5.250/rhel7.3
gpgcheck=0


[docker]
name=docker
baseurl=http://172.25.5.250/pub/yum
gpgcheck=0
创建images:rhel7:v1
[aaa@qq.com test]# docker build -t rhel7:v1 .
Sending build context to Docker daemon 4.608 kB
Step 1/8 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/8 : MAINTAINER oo
 ---> Using cache
 ---> 45391a65a7b8
Step 3/8 : ENV HOSTNAME web1
 ---> Using cache
 ---> 60580fc7e680
Step 4/8 : EXPOSE 80
 ---> Using cache
 ---> d1277e1311f8
Step 5/8 : COPY dvd.repo /etc/yum.repos.d/dvd.repo
 ---> Using cache
 ---> cbaf2b135bdb
Step 6/8 : RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all

------太长省略

Step 7/8 : COPY web/index.html /var/www/html/index.html
 ---> d18df941f38b
Removing intermediate container 198b410dc345
Step 8/8 : CMD /usr/sbin/httpd -D FOREGROUND
 ---> Running in f1e8ee77bee5
 ---> 91fc7d1a2364
Removing intermediate container f1e8ee77bee5
Successfully built 91fc7d1a2364

生成的镜像:

docker-compose配置

测试httpd:
[root@foundation5 test]# docker run -d --name vm1 rhel7:v1 
b0661d7e92fc495ada22c73e17648c3be33ae80fbba7de2c3882d3c5a1beff61
[root@foundation5 test]# docker inspect vm1

------太长省略

                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"

[aaa@qq.com compose]# curl 172.17.0.2
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
[aaa@qq.com compose]# curl 172.17.0.2
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
[aaa@qq.com compose]# curl 172.17.0.2
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
[aaa@qq.com compose]# 
镜像没问题就可以创建集群了
docker-compose集群配置
创建haproxy集群
[root@foundation5 docker]# mkdir compose/

docker-compose配置

docker-compose.yml文件
apache:
    image: rhel7:v1
    expose:
        - 80
    volumes:
        - ./web:/var/www/html

nginx:
    image: nginx
    expose:
        - 80

haproxy:
    image: haproxy
    volumes:
        - ./haproxy:/usr/local/etc/haproxy
    links:
        - apache
        - nginx
    ports:
        - "8080:80"
    expose:
        - 80
haproxy配置文件
global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice
defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms
    stats uri /status
frontend balancer
    bind 0.0.0.0:80
    default_backend web_backends
backend web_backends
    balance roundrobin
    server web1 apache:80 check
    server web2 nginx:80 check
启动测试:docker-compose up
命令下载:直接再官网下载,做个软连接放在bin下就OK了

docker-compose配置

[aaa@qq.com compose]# docker-compose up
Pulling haproxy (haproxy:)...
latest: Pulling from library/haproxy
be8881be8156: Already exists
6a9c22a0d424: Pull complete
0eaf021ab522: Pull complete
Digest: sha256:dd4f3144e9853042e7434294ce6b06b8366e2071ec235e75291d3b2dc290791b
Status: Downloaded newer image for haproxy:latest
Creating compose_apache_1 ... done
Creating compose_nginx_1  ... done
Creating compose_haproxy_1 ... done
Attaching to compose_apache_1, compose_nginx_1, compose_haproxy_1
apache_1   | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
nginx_1    | 2018/08/22 03:56:39 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.4, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "172.25.5.250:8080"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:39 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:40 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:40 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:41 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:41 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:43 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:44 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:45 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:46 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:56:46 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
^CGracefully stopping... (press Ctrl+C again to force)
Stopping compose_haproxy_1 ... done
Stopping compose_nginx_1   ... done
Stopping compose_apache_1  ... done
[aaa@qq.com compose]# docker-compose up
Starting compose_nginx_1  ... done
Starting compose_apache_1 ... done
Starting compose_haproxy_1 ... done
Attaching to compose_nginx_1, compose_apache_1, compose_haproxy_1
apache_1   | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:58:07 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1    | 172.17.0.4 - - [22/Aug/2018:03:58:07 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"

实现负载均衡:

docker-compose配置
docker-compose配置
docker-compose配置