Centos7 Docker 集群实验(实验篇)
2019-08-11 本文已影响0人
remote_pluto
编写Dockerfile
1. 使用PHPStorm Docker插件编写Dockerfile
Docker 服务开启远程连接功能
vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
systemctl daemon-reload
systemctl restart docker
2.nginx php-fpm dockerfile
#Execute: docker run -d --rm --privileged -p 80:80 php72-yaf:v1
FROM centos
LABEL maintainer="jiangfeng.wang@jia1edu.com"
ENV PHP_BIN /opt/remi/php72/root/usr/bin
WORKDIR /workspace/software/
ADD ./yaf-3.0.8.tgz ./
RUN rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm && \
yum install -y nginx && \
yum install -y epel-release && \
yum install -y yum-utils && \
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm && \
yum-config-manager --enable remi-php72 && \
yum -y update && \
yum install -y php72 && \
yum install -y php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache php72-php-pecl-redis5 \
php72-php-bcmath php72-php-devel && \
cd /workspace/software/yaf-3.0.8 && \
$PHP_BIN/phpize && \
./configure --with-php-config=$PHP_BIN/php-config && \
make && \
make install && \
systemctl enable php72-php-fpm && \
systemctl enable nginx
EXPOSE 80
CMD ["/usr/sbin/init"] #因为php-fpm及nginx都是以后台服务运行因此需要执行此指令
ps:期间采用增加 entrypoint.sh的方式 即:
ENTRYPOINT ["./entrypoint.sh"]
entrypoint.sh的内容为:
#!/usr/bin/env bash
set -e #执行中任何语句出错,中止
/usr/sbin/init
exec "$@" #执行后面的指令
运行报错:Couldn't find an alternative telinit implementation to spawn.
需要使用 exec /usr/sbin/init
2.1 ENTRYPOINT CMD使用说明:
No ENTRYPOINT | ENTRYPOINT exec_entry p1_entry | ENTRYPOINT [“exec_entry”, “p1_entry”] | |
---|---|---|---|
No CMD | error, not allowed | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry |
CMD [“exec_cmd”, “p1_cmd”] | exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry exec_cmd p1_cmd |
CMD [“p1_cmd”, “p2_cmd”] | p1_cmd p2_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry p1_cmd p2_cmd |
CMD exec_cmd p1_cmd | /bin/sh -c exec_cmd p1_cmd | /bin/sh -c exec_entry p1_entry | exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd |
3.集群部署
3.1 初始化swarm集群
在192.168.101.252 运行指令:
docker-machine ssh Node1 "docker swarm init --advertise-addr 192.168.101.253"
结果如下:
Swarm initialized: current node (8byxvzemg17dlsq1zt199y2e3) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3c2gclwswbpfynj3jv46beuth4kq95so3ljyodzjqvhim9cqgd-2qhqn9bw51b3sm6wo947o9cut 192.168.101.253:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
执行:
docker-machine ssh Node2 "docker swarm join --token SWMTKN-1-3c2gclwswbpfynj3jv46beuth4kq95so3ljyodzjqvhim9cqgd-2qhqn9bw51b3sm6wo947o9cut 192.168.101.253:2377"
在Manager上运行“docker node ls”来查看节点
[root@192 ~]# docker-machine ssh Node1 "docker node ls"
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
8byxvzemg17dlsq1zt199y2e3 * Node1 Ready Active Leader 19.03.1
66vjclco8d9twjk3w1y0lz82h Node2 Ready Active 19.03.1
3.2 部署nginx,php-fpm到集群上
在192.168.101.252 运行指令:
eval $(docker-machine env Node1)
docker-machine ls
显示如下结果:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
Node1 * generic Running tcp://192.168.101.253:2376 v19.03.1
Node2 - generic Running tcp://192.168.101.254:2376 v19.03.1
部署服务,运行指令:
docker stack deploy -c docker-compose.yml php72yaf
ps:使用的docker-compose.yml文件如下:
version: "3"
services:
web:
image: nginx
deploy:
replicas: 3
resources:
limits:
cpus: "0.2"
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
运行指令:
docker stack ps php72yaf
查看各节点上的容器部署情况
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
34ji2447cjxh php72yaf_web.1 nginx:latest Node2 Running Running 29 minutes ago
77kaoz565uif php72yaf_web.2 nginx:latest Node1 Running Running 29 minutes ago
orxdvbxe9gj1 php72yaf_web.3 nginx:latest Node1 Running Running 29 minutes ago
访问集群:
此时请求 http://192.168.101.253或http://192.168.101.254
都可以访问到nginx服务
ps:本篇在实验过程中想使用第二节中所写的dockerfile来构建自定义镜像,但是因为需要privileged权限,而swarm集群不支持此选项,所以在docker-compose.yml中使用了官方的nginx镜像进行了集群实验。因此本篇的第二、三节内容有点脱节,第二节dockerfile产生的镜像适用于单独运行,第三节仅是集群的一个演示,下一篇会讲php-fpm与nginx进行分拆,重新部署在集群中。