文章目录

  • 使用 Docker 运行服务
  • 部署中的一些问题
  • Why Docker
  • Why Kubernetes
  • Dockerizing the Posts Service
  • Docker 的一些基础命令
  • Dockering Other Services


使用 Docker 运行服务

部署中的一些问题

  • 在之前的项目中,我们部署都是在本地计算机或者服务器上,直接开一个 port 进行的

react项目本地docker部署 react docker_docker

  • 在上线后,可能会遇到某一种服务的 port 请求次数太多 比如 comment 服务,那就再开一个 port 吧!
  • 可能这个 machine 的性能也直接降低了,那就再来第二个 virtual machine 吧!
  • 于是,我们就在 event-bus 中,增加很多接口咯,每多开一个,就手动多加一个接口,可见非常的笨拙
// event-bus/index.js
app.post("/events", (req, res) => {
  const event = req.body;
  events.push(event);

  axios.post("http://localhost:4000/events", event);
  axios.post("http://localhost:4001/events", event);
  axios.post("http://localhost:4002/events", event);
  axios.post("http://localhost:4003/events", event);

  axios.post("http://localhost:4006/events", event);
  axios.post("http://localhost:4007/events", event);

  res.send({ status: "OK" });
});
  • 或者,想优化的话,就固定某时间开几个 port 咯?还是很笨
// event-bus/index.js
app.post('/events', (req, res) => {
  const event = req.body;
  events.push(event);

  axios.post('http://localhost:4000/events', event);
  axios.post('http://localhost:4001/events', event);
  axios.post('http://localhost:4002/events', event);
  axios.post('http://localhost:4003/events', event);

  if(it is not 1 am) {
    axios.post('http://181.143.203.151/events', event);
    axios.post('http://181.143.203.152/events', event);
  }

  res.send({ status: 'OK' });
});

⬆ back to top

Why Docker

现在遇到的问题

  • 运行我们的 app 需要很多环境 和 配置 (npm start)

Docker 就能解决这些问题

  • Containers 容器包含了程序所需的一切 + 如何启动和运行它

    ⬆ back to top

Why Kubernetes

Kubernetes 是一个运行一堆不同容器 container 的工具 我们给它一些配置来描述希望我们的容器如何运行和相互交互

react项目本地docker部署 react docker_docker_02

⬆ back to top

Dockerizing the Posts Service

下面这个表格是 Dockerfile 的结构:

操作指令

配置参数

解释

FROM

node:alpine

指定基础镜像是那种操作系统,比如 Ubuntu,node:alpine 是专为 nodejs 的 alpine 系统

WORKDIR

/app

在 container 中将工作目录设置为“/app”,就是说只关注 /app 下的代码

COPY

package.json ./

只拷贝 package.json 到 container,如果和下面那个拷贝合并,那么每次修改非 package.json 的代码 build 和 run 的时候就会重新拉取 package.json 的依赖,为了性能

RUN

npm install

Install all dependencies

COPY

./ ./

复制所有 非 package.json 的代码

CMD

[“npm”, “start”]

在 container 启动的时候 需要执行的命令,而非 构建镜像的时候要执行的命令

⬆ back to top

Docker 的一些基础命令

Docker Commands

解释

docker build -t heysirius/posts .

根据当前目录下的 dockerfile 构建镜像。将其标记为“heysirius/posts”

docker run [image id or image tag]

根据提供的镜像 id 或标签创建并启动容器

docker run -it [image id or image tag] [cmd]

创建和启动 container,但会覆盖我们在 Dockerfile 里的命令

docker ps

查看所有正在运行的 container 的信息

docker exec -it [container id] [cmd]

在正在运行的 container 中,执行给定的命令,一般是加个 sh 开启 shell,-i 是 input,-t 是能显示出来

docker logs [container id]

container 运行肯定会有 logs 吧

docker build -t heysirius/posts .
docker run heysirius/posts
docker run -it heysirius/posts sh
docker ps
docker exec -it a643fdbf134e sh
docker logs a643fdbf134e

⬆ back to top

Dockering Other Services

cd app/blog/event-bus
docker build -t heysirius/event-bus .
docker run heysirius/event-bus
docker run -it heysirius/event-bus sh
docker ps
docker exec -it a643fdbf134e sh
docker logs a643fdbf134e
  • 至此,我们所有服务都有了一个 image 和 运行过后的 container
    ⬆ back to top