docker基礎部署入門,微服務必備技術,月薪上萬不是夢

為了更美好的明天1發表於2020-11-10

容器技術 -- 2

接著我上一篇文章

映象管理

commit 打包映象

docker commit 容器id 新映象名稱:標籤

[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語法

語法指令語法說明
FROM基礎映象
RUN製作映象時執行的命令,可以有多個
ADD複製檔案到映象,自動解壓
COPY複製檔案到映象,不解壓
EXPOSE宣告開放的埠
ENV設定容器啟動後的環境變數
WORKDIR定義容器預設工作目錄(等於cd)
CMD容器啟動時執行的命令,僅可以有一條CMD

使用Dockerfile建立映象

docker build -t 映象名稱:標籤 Dockerfile所在目錄

製作apache映象

CMD 指令可以檢視 service 檔案的啟動命令 ExecStart(/lib/systemd/system/httpd.service)

ENV 環境變數查詢服務檔案中的環境變數配置檔案 EnvironmentFile 指定的檔案內容

[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 .

檢視與驗證映象

[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地址最低配置
registry192.168.1.1001CPU,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 /]# 

相關文章