概述
在微服系統中,因為服務特別多,如果手動部署以及啟動、重啟等工作會比較繁瑣,這里會涉及到自動化部署,自動化部署就需要用到容器技術,雖然這里基本屬于運維范疇,但是開發者還是需要了解一下,以及簡單操作使用需要會。
現在比較熱門的容器技術當然還是Docker了。關于容器和Docker的名詞解釋請百度/Google/Bing一下,解釋比較詳細,這里不過多描述。
Docker生命周期
Docker安裝
- 安裝環境如下:
[root@localhost etc]# cat redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost etc]# uname -r
3.10.0-957.10.1.el7.x86_64
#節點1
[root@localhost etc]# hostname -I
192.168.0.241
#節點2
[root@localhost ~]# hostname -I
192.168.0.242
- 開始安裝,一次輸入以下指令
> wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
> sed -i 's#download.docker.com#mirrors.ustc.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
> yum install docker-ce -y
#可以安裝前查看倉庫中的docker所有版本,并選擇特定版本安裝,如17.12.0
#選擇版本的時候需要注意不要過高,需匹配containerd.io的版本
#出現containerd.io版本錯誤,可以降低docker版本或者更新containerd.io版本
> yum list docker-ce --showduplicates | sort -r
> yum install docker-ce-17.12.0.ce
顯示下圖,安裝完畢
- 開始配置
#修改配置
> vim /usr/lib/systemd/system/docker.service
#找到ExecStart,開啟遠程API訪問端口:-H 192.168.0.241:2375
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H 192.168.0.241:2375
#修改完,保存并重啟docker服務
> systemctl daemon-reload && systemctl enable docker.service && systemctl restart docker
查看網橋信息
> docker network inspect bridge
默認的為:172.17.0.0/16,也可以修改上述配置文件docker.service中的ExecStart參數,添加自定義ip段,如:--bip 10.0.0.1/16,這里不作修改保持默認。
- 訪問192.168.0.241驗證一下配置是否正確
> docker -H 192.168.0.241 info
- 顯示信息如下:
Docker基礎命令操作
- 查看docker相關信息:docker version
> docker version
顯示如下
-
配置docker中國鏡像加速
有時候默認的鏡像源比較卡會超時,出現類似以下的錯誤
docker: error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/ae/ae2feff98a0cc5095d97c6c283dcd33090770c76d63877caa99aefbbe4343bdd/data?verify=1608604222-lIzhXR%2Fys89hEl3C9yzHrbN6mSQ%3D: net/http: TLS handshake timeout.
可以更新一下源的配置來加速
> vi /etc/docker/daemon.json
#加入以下內容
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
#當然,也可以使用其他源在后邊加逗號添加地址即可
#比如阿里云鏡像https://xxx.mirror.aliyuncs.com,這里的xxx為系統分配前綴 。
修改完記得重載、重啟
systemctl daemon-reload && systemctl restart docker
- 啟動第一個容器
> docker run -d -p 80:80 nginx
# 沒有的話會從源上 拉取一個鏡像
如下圖所示
※這里出現一個錯誤,因為服務器中之前安裝過lnmp,所以啟動docker中的nginx的時候,提示80端口被占用了,這里需要關閉原來的nginx
> lnmp nginx stop
訪問http://192.168.0.241,顯示如下
參數說明如下:
- 搜索倉庫中的鏡像
#如搜索redis 鏡像
> docker search redis
#顯示結果如下:
參數說明
- 獲取鏡像
> docker pull redis
#如果鏡像是第三方鏡像,則需要加具體第三方地址
> docker pull index.tenxcloud.com/tenxcloud/httpd
- 獲取完之后可以查看鏡像
[root@localhost docker]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 6 days ago 133MB
redis latest ef47f3b6dc11 10 days ago 104MB
- 導出鏡像
[root@localhost docker]# docker image save redis > docker-redis.tar.gz
[root@localhost docker]# ls
daemon.json docker-redis.tar.gz key.json
- 刪除鏡像
[root@localhost docker]# docker image rm redis:latest
#查詢鏡像列表
[root@localhost docker]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 6 days ago 133MB
- 導入鏡像
[root@localhost docker]# docker image load -i docker-redis.tar.gz
#查詢鏡像列表
[root@localhost docker]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 6 days ago 133MB
redis latest ef47f3b6dc11 10 days ago 104MB
- 查看鏡像的詳細信息
[root@localhost docker]# docker image inspect redis
容器的日常管理
- 啟動容器
[root@localhost docker]# docker run nginx
- 查看正在運行的容器
[root@localhost docker]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6620631e4975 nginx "/docker-entrypoint.…" 35 minutes ago Up 35 minutes 0.0.0.0:80->80/tcp affectionate_rhodes
- 快速啟動容器
[root@localhost docker]# docker run mysql:latest /usr/bin/sleep 20;
- 啟動redis 并配置端口以及賬號密碼
[root@localhost docker]# docker run -d --name root -p 6379:6379 redis --requirepass "123123"
a528bbba31ab60cabdda0f4398ef4cf4bebb4d6996819c26b5280add744881e1
- 查看你容器詳細信息
[root@localhost docker]# docker container inspect 6620631e4975
#6620631e4975 為redis容器的id,可以在查看正在運行的容器的結果里看到
- 查看你所有容器(包括未運行的)
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76d59fca157f mysql "docker-entrypoint.s…" 4 minutes ago Exited (127) 4 minutes ago agitated_lamport
61c9ecd03a3d mysql:latest "docker-entrypoint.s…" 5 minutes ago Exited (127) 5 minutes ago festive_golick
a528bbba31ab redis "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:6379->6379/tcp root
226447f2093e redis "docker-entrypoint.s…" 15 minutes ago Exited (0) 10 minutes ago musing_wu
604f18efaf3c redis:latest "docker-entrypoint.s…" 15 minutes ago Exited (127) 15 minutes ago priceless_pare
94aac2583416 nginx "/docker-entrypoint.…" 20 minutes ago Exited (0) 19 minutes ago inspiring_mahavira
6620631e4975 nginx "/docker-entrypoint.…" 53 minutes ago Up 53 minutes 0.0.0.0:80->80/tcp affectionate_rhodes
7b9a34037aba nginx "/docker-entrypoint.…" About an hour ago Created elated_lederberg
- 停止容器
[root@localhost docker]# docker stop 7b9a34037aba
#或
[root@localhost docker]# docker container kill 76d59fca157f
- 刪除所有容器
[root@localhost docker]# docker rm -f `docker ps -a -q`
- 啟動時進行端口映射
[root@localhost docker]# docker run -d -p 80:80 nginx:latest
af85d7e1670dbb7e391cd19733c521af41824ba7c70a5f51af9a8f967bb78e06
不同指定映射方法
- 進入容器
> docker run -it nginx:latest
> docker attach 1bf0f43c4d2f
#exec 進入容器方法(推薦使用)
> docker exec -it 1bf0f43c4d2f /bin/bash
Docker 數據卷的管理
- 掛在卷
[root@localhost docker]# docker run -d -p 80:80 -v /data:/usr/share/nginx/html nginx:latest
6a7cea392577e0c14b1eb21117bdc8eb7c2b6cbf921c7f472dc180e823d03431
容器內站點目錄: /usr/share/nginx/html,可以測試一下
#寫入數據到/data/index.html,并訪問
[root@localhost html]# echo "http://www.docker-test.com" > /data/index.html
[root@localhost html]# curl 192.168.0.241
http://www.docker-test.com
- 設置共享卷,使用同一個卷啟動一個新的容器
[root@localhost docker]# docker run -d -p 8080:80 -v /data:/usr/share/nginx/html nginx:latest
8a24e6e2e314ae99ba37cb5a5e29093cf7d721d600662872642abbc0c0b04680
測試一下
[root@localhost html]# curl 192.168.0.241:8080
http://www.docker-test.com
- 查看卷列表
[root@localhost docker]# docker volume ls
DRIVER VOLUME NAME
local 98634ca089006f43966f067067b49f27c94c591cd0de971ba776257cef3492ff
local d18512ab022fabe00b682906efdefc759ae93bf288664279ecfd8e7f12e2d11b
local d116492c60ce15ce9df3f796990e72a302e4b048b91179b7370ebbc17592e653
local e5b5e0e325411d38b5b953819c9a52d725762b8fd07030cc41839c18355d55c3
local e9ce7babf62514608dc98f7656c65bb10d59804beb59ddcf8fdd32a5064abd33
- 創建一個卷
[root@localhost docker]# docker volume create
85cfd88f320a1f83318cdfdd53916ac124e31acba97242536b9406097adf27f9
- 查看卷路徑
[root@localhost docker]# docker volume inspect 85cfd88f320a1f83318cdfdd53916ac124e31acba97242536b9406097adf27f9
- 使用新創建的卷 掛載數據
[root@localhost docker]# docker volume create zxlab
zxlab
[root@localhost docker]# docker volume ls
DRIVER VOLUME NAME
local 85cfd88f320a1f83318cdfdd53916ac124e31acba97242536b9406097adf27f9
local 98634ca089006f43966f067067b49f27c94c591cd0de971ba776257cef3492ff
local d18512ab022fabe00b682906efdefc759ae93bf288664279ecfd8e7f12e2d11b
local d116492c60ce15ce9df3f796990e72a302e4b048b91179b7370ebbc17592e653
local e5b5e0e325411d38b5b953819c9a52d725762b8fd07030cc41839c18355d55c3
local e9ce7babf62514608dc98f7656c65bb10d59804beb59ddcf8fdd32a5064abd33
local zxlab
[root@localhost docker]# docker run -d -p 9000:80 -v zxlab:/usr/share/nginx/html nginx:latest
d7cfc4c6b480e6714ad20a06fe3ff8948e35c253b755752f309e4278d2f38f72
[root@localhost docker]# echo 'test.docker.cc' >/var/lib/docker/volumes/zxlab/_data/index.html
[root@localhost docker]# curl 192.168.0.241:9000
test.docker.cc
- 查看使用的端口
[root@localhost docker]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.0.241:2375 0.0.0.0:* LISTEN 16200/dockerd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3004/sshd
tcp6 0 0 :::9000 :::* LISTEN 19344/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 19085/docker-proxy
tcp6 0 0 :::80 :::* LISTEN 18896/docker-proxy
手動將容器保存為鏡像
官方鏡像列表:
https://hub.docker.com/explore/
- 啟動一個centos6.8的鏡像
> docker pull centos:7.6
#啟動并進入容器
> docker run -it -p 1022:22 centos:7.6 /bin/bash
# 在容器種安裝sshd服務,并修改系統密碼
> yum install openssh-server -y
> echo "root:123456" |chpasswd
> /etc/init.d/sshd start
啟動完成后鏡像ssh連接測試
- 將容器提交為鏡像
# 容器名稱 鏡像名稱
> docker commit brave_mcclintock centos7-ssh
#使用新的鏡像啟動容器
> docker run -d -p 1122:22 centos7-ssh:latest /usr/sbin/sshd -D
#進入容器后,在容器安裝httpd服務
> yum install httpd -y
#編寫啟動腳本腳本 init.sh,內容如下
[root@5b8161fda2a9 /]# cat init.sh
#!/bin/bash
/etc/init.d/httpd start
/usr/sbin/sshd -D
> chmod +x init.sh
#退出容器,重新提交
[root@localhost docker]# docker commit 5b8161fda2a9 centos7-httpd
#啟動容器,并測試
[root@localhost docker]# docker run -d -p 1222:22 -p 80:80 centos6-httpd /init.sh
46fa6a06644e31701dc019fb3a8c3b6ef008d4c2c10d46662a97664f838d8c2c
Dockerfile自動構建docker鏡像
可以參考官方的dockerfile文件:https://github.com/CentOS/CentOS-Dockerfiles
以下為nginx的dockerfile
# "ported" by Adam Miller <maxamillion@fedoraproject.org> from
# https://github.com/fedora-cloud/Fedora-Dockerfiles
#
# Originally written for Fedora-Dockerfiles by
# scollier <scollier@redhat.com>
#
# Enriched by patterns found at https://github.com/openshift/postgresql/blob/master/9.4/Dockerfile.rhel7 by
# Christoph G?rn <goern@redhat.com>
FROM centos:centos7
MAINTAINER The CentOS Project <cloud-ops@centos.org>
# Labels consumed by Red Hat build service
LABEL Component="nginx" \
Name="centos/nginx-180-centos7" \
Version="1.8.0" \
Release="1"
# Labels could be consumed by OpenShift
LABEL io.k8s.description="nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev." \
io.k8s.display-name="nginx 1.8.0" \
io.openshift.expose-services="80:http" \
io.openshift.tags="nginx"
RUN yum -y install --setopt=tsflags=nodocs centos-release-scl-rh && \
yum -y update --setopt=tsflags=nodocs && \
yum -y install --setopt=tsflags=nodocs scl-utils rh-nginx18 && \
yum clean all && \
mkdir -p /usr/share/nginx/html
# Get prefix path and path to scripts rather than hard-code them in scripts
ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/nginx \
ENABLED_COLLECTIONS=rh-nginx18
# When bash is started non-interactively, to run a shell script, for example it
# looks for this variable and source the content of this file. This will enable
# the SCL for all scripts without need to do 'scl enable'.
ENV BASH_ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \
ENV=${CONTAINER_SCRIPTS_PATH}/scl_enable \
PROMPT_COMMAND=". ${CONTAINER_SCRIPTS_PATH}/scl_enable"
ADD root /
# ADD https://git.centos.org/sources/httpd/c7/acf5cccf4afaecf3afeb18c50ae59fd5c6504910 /usr/share/nginx/html/
# RUN sed -i -e 's/Apache/nginx/g' -e '/apache_pb.gif/d' /usr/share/nginx/html/index.html
RUN echo "nginx on CentOS7" > /usr/share/nginx/html/index.html
EXPOSE 80
USER nginx
ENTRYPOINT ["container-entrypoint"]
CMD [ "nginx18" ]
dockerfile主要組成部分:
- 基礎鏡像信息 FROM centos:centos7
- 制作鏡像操作指令RUN yum -y install --setopt=tsflags=nodocs centos-release-scl-rh
- 容器啟動時執行指令 CMD [ "nginx18" ]
dockerfile常用指令:
- FROM : 指定基礎鏡像
- MAINTAINER 告訴別人,誰負責養它?(指定維護者信息,可以沒有)
- RUN : 操作,比如安裝、更新、創建文件夾等,在命令前面加上RUN即可
- ADD : 給它設置權限,比如COPY文件,會自動解壓
- WORKDIR : 設置當前工作目錄
- VOLUME : 設置卷,掛載主機目錄
- EXPOSE : 指定對外的端口
- CMD : 指定容器啟動后的要干的事情
- COPY : 復制文件
- ENV : 環境變量
- ENTRYPOINT : 容器啟動后執行的命令
如何創建一個Dockerfile?
#進入一個創建好的目錄
> cd /opt/base
#編寫Dockerfile
[root@localhost base]# vim Dockerfile
[root@localhost base]# cat Dockerfile
FROM centos:centos7
RUN yum install openssh-server -y
RUN echo "root:123123" |chpasswd
RUN /etc/init.d/sshd start
CMD ["/usr/sbin/sshd","-D"]
#使用編寫好的Dockerfile ,構建docker鏡像, -t 給鏡像打標簽, . 為當前目錄
[root@localhost base]# docker image build -t centos7-ssh .
#完成后查看鏡像列表
[root@localhost base]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> ae20c8b4f2ad 2 minutes ago 300MB
mysql latest a347a5928046 10 hours ago 545MB
nginx latest ae2feff98a0c 6 days ago 133MB
redis latest ef47f3b6dc11 10 days ago 104MB
centos centos7 8652b9f0cb4c 5 weeks ago 204MB
#更多的Dockerfile可以參考官方方法。
總結
好了,以上是Docker入門的一些日常操作,后續還有Docker容器操作的拓展內容。