【筆記】《CNCF × Alibaba雲原生技術公開課》知識點

一,課時1:第一堂「雲原生」課javascript

 

二,課時2:容器基本概念html

1.已運行 docker run -d -t —name demo ubuntu top 命令, 是否能夠在 demo 這個容器內部中止容器?java

golang

 

2.已運行 docker run -d —name demo busybox:1.25 top 命令,如何使用 docker 命令來獲取容器 demo 的 Init 進程 PID?docker

A. docker inspect demo -f '{{.State.Pid}}'json

 

3.如下哪一個 docker 命令建立出來的容器能夠自動重啓?ubuntu

C. docker run -d --restart always busybox top安全

 

4.已運行 docker run -d -t —name demo ubuntu top 和 docker run --name demo-x --pid container:demo ubuntu ps 命令,是否能夠在 demo-x 容器內部中止容器?app

A. 是curl

 

5.已知容器 Init 進程 PID,在宿主機上經過 kill -9 PID 的方式結束該進程,容器當前的狀態是什麼?

A. Exited

 

6.已運行 docker run -d -t —name demo ubuntu top 和 docker run --name demo-x --pid container:demo ubuntu ps 命令,若是 demo 容器退出了,正在運行的 demo-x 容器是否會退出?

A. 是

 

7.已運行 docker run -d -t —name demo ubuntu top 命令, 在 demo 這個容器內看到 top 命令的 PID 是什麼?

B. 1

 

8.如下哪一個 docker 命令能夠用來建立一個使用宿主機主機名的容器?

A. docker run --uts=host ubuntu hostname

 

9.已運行 docker run -d -t —name demo ubuntu top 命令,如下哪一個 docker 命令建立出的容器能看見 demo 容器進程?

B. docker run --name demo-x --pid container:demo ubuntu ps

 

10.如何快速判斷 docker daemon 是否支持動態接管運行容器?

 

課後實驗

1.1 實驗資源
# lsb_release -a
LSB Version:    core-9.20160110ubuntu0.2-amd64:core-9.20160110ubuntu0.2-noarch:security-9.20160110ubuntu0.2-amd64:security-9.20160110ubuntu0.2-noarch
Distributor ID:    Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:    xenial
# free -m
              total        used        free      shared  buff/cache   available
Mem:           7821         101        6710           0        1008        7437
Swap:             0           0           0
# uname -a
Linux iZbp1ed56c9f3xy81pl2m8Z 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 x86_64 x86_64 x
# cat /proc/cpuinfo 
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 85
model name    : Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
stepping    : 4
省略其餘核信息
# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b1b45

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83875364    41936658+  83  Linux
[root@iZbp1ed56c9f3xy81pl2m8Z ~]# free
              total        used        free      shared  buff/cache   available
Mem:        8008856      104780     6871428         472     1032648     7615876
Swap:             0           0           0

 

 
1.2 登陸阿里雲ECS,安裝docker環境

經過實驗資源中ECS的IP和密碼,登陸遠程ECS機器。

安裝Docker。

sudo yum update -y
sudo yum install docker -y

sudo systemctl start docker

驗證 docker 是否安裝成功並在容器中執行一個測試的鏡像。

sudo docker run hello-world
 

 提示信息

Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete
Digest: sha256:5f179596a7335398b805f036f7e8561b6f0e32cd30a32f5e19d17a3cda6cc33d
Status: Downloaded newer image for docker.io/hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

 

1.3 準備應用代碼和Dockerfile

在ECS上生成一個文件夾‘demo’,將 golang 代碼拷貝到 `demo` 文件夾下的 `main.go`

package main

import (
        "fmt"
        "net/http"
)

func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "Hello! World\n")
        })

        fmt.Println("start to serve...")
        http.ListenAndServe(":80", nil)
}

在當前 `demo` 目錄下編寫 Dockerfile ,以下所示(無需在本地執行操做)

FROM golang:1.12-alpine

# change current working dir
WORKDIR /go/src/app

# copy main.go into /go/src/app
COPY . .

# go build and install the app
RUN go install -v ./...

# run the app by default
CMD ["app"]

 

1.4構建鏡像

一般狀況下,使用如下命令便可構建鏡像

# docker build . -t demo:v1

顯示信息

Sending build context to Docker daemon 3.072 kB
Step 1/5 : FROM golang:1.12-alpine
Trying to pull repository docker.io/library/golang ... 
1.12-alpine: Pulling from docker.io/library/golang
e7c96db7181b: Pull complete 
5297bd381816: Pull complete 
3a664477889c: Pull complete 
bb89ac7faaa9: Pull complete 
8bb43bd9c21d: Pull complete 
Digest: sha256:06ba1dae97f2bf560831497f8d459c68ab75cc67bf6fc95d9bd468ac259c9924
Status: Downloaded newer image for docker.io/golang:1.12-alpine
 ---> c7330979841b
Step 2/5 : WORKDIR /go/src/app
 ---> f0ec88674b57
Removing intermediate container 34bb5acd4139
Step 3/5 : COPY . .
 ---> 84d85454cd6b
Removing intermediate container c7b3608f6593
Step 4/5 : RUN go install -v ./...
 ---> Running in ace6f449ee12

app
 ---> c5c206239f84
Removing intermediate container ace6f449ee12
Step 5/5 : CMD app
 ---> Running in baa4131f9ac8
 ---> 7d4981d0a5eb
Removing intermediate container baa4131f9ac8
Successfully built 7d4981d0a5eb

 

使用docker images查看本機鏡像

# docker images

顯示信息

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
demo                    v1                  7d4981d0a5eb        2 minutes ago       358 MB
docker.io/golang        1.12-alpine         c7330979841b        3 days ago          350 MB
docker.io/hello-world   latest              fce289e99eb9        4 months ago        1.84 kB

注:
在國內訪問 Docker Hub 速度比較慢,能夠在Docker引擎中設置鏡像加速器加速對Docker Hub的訪問。
更新 `/etc/docker/daemon.json`,添加以下參數,並重啓Docker引擎。

{
   "registry-mirrors": ["https://registry.docker-cn.com"]
}

 

構建完畢以後,能夠在本地運行驗證下是否符合預期。若是看到 `Hello! World` 字樣,咱們就能夠進入下一個環節了。

# 映射容器內 80 端到宿主機上的 8000 端口
$ docker run -d -p 8000:80 demo:v1

# curl 一下查看結果
$ curl localhost:8000
Hello! World

 

使用docker ps 查看正在運行的容器

# docker ps

顯示信息

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
d5a2823410aa        demo:v1             "app"               23 seconds ago      Up 22 seconds       0.0.0.0:8000->80/tcp   suspicious_cori

 

1.5 推送鏡像至阿里雲容器鏡像服務

在推送以前,須要註冊阿里雲帳號和開通阿里雲容器鏡像服務。

 

注:

阿里雲註冊連接:https://account.aliyun.com/register/register.htm

阿里雲登陸連接:https://account.aliyun.com/login/login.htm

阿里雲容器鏡像服務頁面:https://cr.console.aliyun.com

容器鏡像服務(Container Registry)提供安全的應用鏡像託管能力,精確的鏡像安全掃描功能,穩定的國內外鏡像構建服務,便捷的鏡像受權功能,方便用戶進行鏡像全生命週期管理。


當咱們擁有阿里雲容器鏡像服務帳號,並在訪問憑證頁面設置固定密碼後,就可使用 docker 客戶端來登陸服務。回到ECS實例,執行如下命令:

$ docker login  -username=**** registry.cn-hangzhou.aliyuncs.com
Password: 
Login Succeeded

注意:此處的密碼是在容器鏡像服務-訪問憑證頁面 設置的固定密碼。

在推送到鏡像以前,咱們在容器鏡像服務的杭州地域,創建一個命名空間爲mydemo、倉庫名爲demo的本地倉庫。以後,咱們在ECS上將鏡像地址修改成對應的鏡像倉庫地址:

# mydemo 能夠替換成本身的命名空間
$ docker tag demo:v1 registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

$ docker push registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

 

1.6 在本地下載demo:v1鏡像

 登出 ECS 實例,在本地(已安裝docker環境) docker pull 來下載鏡像。

# mydemo 請替換成第5小節步驟中指定的命令空間
$ docker pull registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

下載完畢以後,咱們就能夠直接運行該鏡像。

$ docker run -d -p 8000:80 registry.cn-hangzhou.aliyuncs.com/mydemo/demo:v1

並查看本地的 `8000` 端口。

$ curl localhost:8000
相關文章
相關標籤/搜索