容器技術 -- 2
接着我上一篇文章,此篇文章是爲女友吐血整理
鏡像管理
commit 打包鏡像php
docker commit 容器id 新鏡像名稱:標籤html
[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
Dockerfile打包鏡像前端
Dockerfile語法node
語法指令 | 語法說明 |
---|---|
FROM | 基礎鏡像 |
RUN | 製做鏡像時執行的命令,能夠有多個 |
ADD | 複製文件到鏡像,自動解壓 |
COPY | 複製文件到鏡像,不解壓 |
EXPOSE | 聲明開放的端口 |
ENV | 設置容器啓動後的環境變量 |
WORKDIR | 定義容器默認工做目錄(等於cd) |
CMD | 容器啓動時執行的命令,僅能夠有一條CMD |
使用Dockerfile建立鏡像nginx
docker build -t 鏡像名稱:標籤 Dockerfile所在目錄web
製做apache鏡像docker
CMD 指令能夠查看 service 文件的啓動命令 ExecStart(/lib/systemd/system/httpd.service)apache
ENV 環境變量查詢服務文件中的環境變量配置文件 EnvironmentFile 指定的文件內容json
[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 .
查看與驗證鏡像vim
[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
製做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 .
製做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 .
發佈容器服務
對外發布服務
給他 node-0001 綁定一個公網IP
docker run -itd -p 宿主機端口:容器端口 鏡像名稱:標籤
# 把 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
驗證方式: 經過瀏覽器訪問便可
容器共享卷
docker run -itd -v 宿主機對象:容器內對象 鏡像名稱:標籤
使用共享捲動態修改容器內配置文件
[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
驗證方式: 經過瀏覽器訪問便可
容器間網絡通訊
實驗架構圖例
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
實驗步驟
[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
docker私有倉庫
docker私有倉庫圖例
graph TB H1(容器服務器<br>node-0001) H2(容器服務器<br>node-0002) I{ {鏡像倉庫}} style I fill:#77ff77 H1 --> I H2 --> I
私有倉庫配置
主機名 | ip地址 | 最低配置 |
---|---|---|
registry | 192.168.1.100 | 1CPU,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":[]}
docker客戶端配置
全部node節點都須要配置,這裏 node-0001,node-0002都要配置
native.cgroupdriver cgroup驅動,docker默認 cgroupfs
registry-mirrors 默認下載倉庫,使用國內源能快一點
insecure-registries 私有倉庫地址(重點)
[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
上傳鏡像
# 上傳 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
驗證測試
curl http://倉庫IP:5000/v2/_catalog
curl http://倉庫IP:5000/v2/鏡像名稱/tags/list
[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 /]#