Container technology-2
image management
commit imagen empaquetada
Docker commit container id nuevo nombre de imagen: etiqueta
[root@node-0001 ~]# docker run -it centos:latest
[root@02fd1719c038 ~]# rm -f /etc/yum.repos.d/*.repo
[root@02fd1719c038 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-7.repo
[root@02fd1719c038 ~]# yum install -y net-tools vim-enhanced tree bash-completion iproute psmisc && yum clean all
[root@02fd1719c038 ~]# exit
[root@node-0001 ~]# docker commit 02fd1719c038 myos:latest
Imagen empaquetada de Dockerfile
Sintaxis de Dockerfile
Instrucciones de sintaxis | Descripción gramatical |
---|---|
DESDE | Espejo base |
CORRER | Puede haber varios comandos para ejecutar al hacer un espejo |
AGREGAR | Copie archivos al espejo, descomprímalos automáticamente |
COPIAR | Copie el archivo al espejo sin descomprimir |
EXBOSAR | Declarar puertos abiertos |
ENV | Establecer las variables de entorno después de que se inicie el contenedor |
WORKDIR | Defina el directorio de trabajo predeterminado del contenedor (igual a cd) |
CMD | El comando ejecutado cuando se inicia el contenedor solo puede tener una CMD |
Usa Dockerfile para crear un espejo
docker build -t image name: el directorio donde se encuentra la etiqueta Dockerfile
Hacer espejo apache
El comando CMD puede ver el comando de inicio ExecStart del archivo de servicio (/lib/systemd/system/httpd.service)
La variable de entorno ENV consulta el contenido del archivo especificado por el archivo de configuración de la variable de entorno EnvironmentFile en el archivo de servicio
[root@node-0001 ~]# mkdir web; cd web
[root@node-0001 web]# vim Dockerfile
FROM myos:latest
RUN yum install -y httpd php
ENV LANG=C
ADD webhome.tar.gz /var/www/html/
WORKDIR /var/www/html/
EXPOSE 80
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
# 拷贝 webhome.tar.gz 到当前目录中
[root@node-0001 web]# docker build -t myos:httpd .;
Ver y verificar imágenes
[root@node-0001 web]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myos httpd db15034569da 12 seconds ago 412MB
myos latest 867409e412c8 2 hours ago 281MB
[root@node-0001 web]# docker rm -f $(docker ps -aq)
[root@node-0001 web]# docker run -itd myos:httpd
[root@node-0001 web]# curl http://172.17.0.2/info.php
<pre>
Array
(
[REMOTE_ADDR] => 172.17.0.1
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: 6c9e124bee1a
1229
Hacer espejo php-fpm
[root@node-0001 ~]# yum install -y php-fpm
[root@node-0001 ~]# mkdir php; cd php
[root@node-0001 php]# cp /etc/php-fpm.d/www.conf ./
12: listen = 0.0.0.0:9000
24: ;listen.allowed_clients = 127.0.0.1
[root@node-0001 php]# vim Dockerfile
FROM myos:latest
RUN yum install -y php-fpm
COPY www.conf /etc/php-fpm.d/www.conf
EXPOSE 9000
WORKDIR /usr/local/nginx/html
COPY info.php info.php
CMD ["/usr/sbin/php-fpm", "--nodaemonize"]
[root@node-0001 php]# docker build -t myos:php-fpm .
Hacer espejo nginx
[root@node-0001 ~]# yum install -y gcc make pcre-devel openssl-devel
[root@node-0001 ~]# useradd nginx
[root@node-0001 ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@node-0001 ~]# cd nginx-1.12.2
[root@node-0001 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@node-0001 nginx-1.12.2]# make && make install
[root@node-0001 nginx-1.12.2]# cd /usr/local/
[root@node-0001 local]# tar czf nginx.tar.gz nginx
[root@node-0001 local]# mkdir /root/nginx ;cd /root/nginx
[root@node-0001 nginx]# cp /usr/local/nginx.tar.gz ./
[root@node-0001 nginx]# vim Dockerfile
FROM myos:latest
RUN yum install -y pcre openssl && useradd nginx
ADD nginx.tar.gz /usr/local/
EXPOSE 80
WORKDIR /usr/local/nginx/html
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@node-0001 nginx]# docker build -t myos:nginx .
Publicar servicio de contenedor
Servicio de publicación externa
Vincula su nodo-0001 con una IP pública
docker run -itd -p puerto de host: puerto del contenedor nombre de la imagen: etiqueta
# 把 node-0001 变成 apache 服务
[root@node-0001 ~]# docker run -itd -p 80:80 myos:httpd
# 把 node-0001 变成 nginx 服务,首先必须停止 apache
[root@node-0001 ~]# docker stop $(docker ps -q)
[root@node-0001 ~]# docker run -itd -p 80:80 myos:nginx
Método de autenticación: solo visite a través del navegador
Volumen compartido del contenedor
docker run -itd -v objeto host: objeto en el contenedor nombre de la imagen: etiqueta
Utilice el volumen compartido para modificar dinámicamente el archivo de configuración en el contenedor
[root@node-0001 ~]# docker run -itd --name myphp myos:php-fpm
[root@node-0001 ~]# docker inspect myphp
[root@node-0001 ~]# mkdir /var/webconf
[root@node-0001 ~]# cp /usr/local/nginx/conf/nginx.conf /var/webconf/
[root@node-0001 ~]# vim /var/webconf/nginx.conf
... ...
fastcgi_pass 172.17.0.xx:9000;
... ...
# 映射配置文件,并启动容器
[root@localhost ~]# docker run -itd -p 80:80 \
-v /var/webconf/nginx.conf:/usr/local/nginx/conf/nginx.conf myos:nginx
Método de autenticación: solo visite a través del navegador
Comunicación de red entre contenedores
Leyenda de la arquitectura experimental
graph LR
subgraph node-0001
style node-0001 color:#00ff00,fill:#7777ff
subgraph 容器1
style 容器1 color:#00ff00,fill:#88aaff
APP1(Nginx)
NET1{
{共享网络}}
end
subgraph 容器2
style 容器2 color:#00ff00,fill:#88aaff
APP2(PHP)
end
APP1 --> NET1
APP2 --> NET1
L((共享存储卷))
APP1 -.-> L
APP2 -.-> L
end
U((用户)) --> APP1
Pasos experimentales
[root@node-0001 ~]# mkdir -p /var/{webroot,webconf}
[root@node-0001 ~]# cd kubernetes/docker-images
[root@node-0001 ~]# cp info.php info.html /var/webroot/
[root@node-0001 ~]# cp /usr/local/nginx/conf/nginx.conf /var/webconf/
[root@node-0001 ~]# vim /var/webconf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# 启动前端 nginx 服务,并映射共享目录和配置文件
[root@node-0001 ~]# docker run -itd --name nginx -p 80:80 \
-v /var/webconf/nginx.conf:/usr/local/nginx/conf/nginx.conf \
-v /var/webroot:/usr/local/nginx/html myos:nginx
# 启动后端 php 服务,并映射共享目录
[root@node-0001 ~]# docker run -itd --network=container:nginx \
-v /var/webroot:/usr/local/nginx/html myos:php-fpm
# 验证服务
[root@node-0001 ~]# curl http://node-0001/info.html
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World</font>
</marquee>
</html>
[root@node-0001 ~]# curl http://node-0001/info.php
<pre>
Array
(
[REMOTE_ADDR] => 172.17.0.1
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: f705f89b45f9
1229
almacén privado de docker
Leyenda del almacén privado de Docker
graph TB
H1(容器服务器<br>node-0001)
H2(容器服务器<br>node-0002)
I{
{镜像仓库}}
style I fill:#77ff77
H1 --> I
H2 --> I
Configuración de almacén privado
Nombre de la CPU | dirección IP | Configuración mínima |
---|---|---|
registro | 192.168.1.100 | 1CPU, memoria 1G |
[root@registry ~]# yum install -y docker-distribution
[root@registry ~]# systemctl enable --now docker-distribution
[root@registry ~]# curl http://192.168.1.100:5000/v2/_catalog
{"repositories":[]}
Configuración del cliente Docker
Todos los nodos deben configurarse, aquí debe configurarse el nodo-0001, el nodo-0002
native.cgroupdriver controlador cgroup, Docker por defecto es cgroupfs
Los espejos de registro descargan el almacén de forma predeterminada, puede ser más rápido utilizar fuentes nacionales.
Dirección de depósito privado de registros inseguros (énfasis)
[root@node-0001 ~]# vim /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://hub-mirror.c.163.com"],
"insecure-registries":["192.168.1.100:5000", "registry:5000"]
}
[root@node-0001 ~]# docker rm -f $(docker ps -aq)
[root@node-0001 ~]# systemctl restart docker
Cargar imagen
# 上传 myos:latest, myos:httpd, myos:nginx, myos:php-fpm
[root@node-0001 ~]# docker tag myos:latest 192.168.1.100:5000/myos:latest
[root@node-0001 ~]# docker push 192.168.1.100:5000/myos:latest
Prueba de verificación
curl http: // IP del almacén: 5000 / v2 / _catalog
curl http: // IP del almacén: 5000 / v2 / nombre del espejo / etiquetas / lista
[root@node-0002 ~]# curl http://192.168.1.100:5000/v2/_catalog
{"repositories":["myos"]}
[root@node-0002 ~]# curl http://192.168.1.100:5000/v2/myos/tags/list
{"name":"myos","tags":["latest"]}
# 使用远程镜像启动容器
[root@node-0002 ~]# docker run -it 192.168.1.100:5000/myos:latest
Unable to find image '192.168.1.100:5000/myos:latest' locally
latest: Pulling from myos
7dc0dca2b151: Pull complete
95c297b4d705: Pull complete
Digest: sha256:d61ffc053895e2dc16f63b8a2988dfe5f34207b48b1e74d397bb3267650ba4ce
Status: Downloaded newer image for 192.168.1.100:5000/myos:latest
[root@674ebe359e44 /]#