平台对比 PAAS

  • Swarm:Docker 官方提供的集群管理工具,主要作用是将多个 Docker 主机抽象成一个整体,并通过一个入口统一管理这些 Docker 主机上的各种资源。

  • Apache Mesos:类似于 YARN 的集群管理器,提供跨分布式应用和框架的资源隔离和共享,可以运行 Hadoop、MPI、Hypertable、Spark 等。

  • Kubernetes 优势

    • 服务发现和负载均衡
    • 存储编排
    • 自动部署和回滚
    • 自动分配 CPU/内存资源 - 弹性伸缩
    • 自我修复(需要时启动新容器)
    • Secret(安全相关信息)和配置管理
    • 大型规模的支持
    • 开源

Pod 组成

  • Nginx 作为反向代理和静态资源服务器,提供高效的 HTTP 服务。
  • Redis 作为内存数据库,提供高速的数据读写能力,常用于缓存和会话管理。
  • php-fpm 作为 PHP 进程管理器,处理 PHP 脚本的执行。
  • Pause 作为 Pod 内部第一个启动的容器,负责初始化网络栈、挂载需要的存储卷和回收僵尸进程。

Pause 特性

  • Pod 内部第一个启动的容器 Pause 容器在 Pod 启动时首先运行,为其他容器提供共享的网络和 IPC 命名空间。
  • 初始化网络栈 Pause 容器负责初始化 Pod 的网络栈,确保所有容器共享相同的网络环境。
  • 挂载需要的存储卷 Pause 容器可以挂载共享存储卷,供其他容器使用。
  • 回收僵尸进程 pause 容器负责回收僵尸进程,确保 Pod 内部的进程管理更加高效。

Nginx、Redis、php-fpm 和 Pause 共享名字空间(Network、PID、IPC)

实验:基于 Docker 模拟 Pod

编写 Nginx 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat <<EOF >> nginx.conf
# 设置错误日志的输出位置为标准错误输出
error_log stderr;
# 事件模块配置
events {
# 每个工作进程的最大连接数
worker_connections 1024;
}
# HTTP模块配置
http {
# 设置访问日志的输出位置为标准输出,并使用combined格式
access_log /dev/stdout combined;
# 服务器模块配置
server {
# 监听80端口,并设置为默认服务器
listen 80 default_server;
# 服务器名称,可以有多个,用空格分隔
server_name example.com www.example.com;
# 根位置的配置
location / {
# 将请求代理到本地的2368端口
proxy_pass http://127.0.0.1:2368;
}
}
}
EOF

创建一台新的虚拟机并使用脚本一键运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
cd /etc/yum.repos.d/
rm -rf *
# 下载阿里云的 CentOS 7 仓库文件
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
systemctl stop firewalld
systemctl disable firewalld
systemctl mask firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
timedatectl set-timezone Asia/Shanghai
# 安装 iptables 服务
yum -y install iptables-services
systemctl start iptables
iptables -F
systemctl enable iptables
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
sudo yum makecache fast
sudo yum -y install docker-ce
# 配置 Docker daemon
cat > /etc/docker/daemon.json <<EOF
{
"default-ipc-mode": "shareable",
"data-root": "/data/docker",
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "100"
},
"insecure-registries": ["harbor.xinxainghf.com"],
"registry-mirrors": ["https://kfp63jaj.mirror.aliyuncs.com"]
}
EOF
mkdir -p /etc/systemd/system/docker.service.d
systemctl daemon-reload
systemctl restart docker
systemctl enable docker

最后使用docke运行三个容器:

1
2
3
docker run --name pause --ipc shareable -d k8s.gcr.io/pause:3.1
docker run --name nginx -v `pwd`/nginx.conf:/etc/nginx/nginx.conf --net=container:pause --ipc=container:pause --pid=container:pause -d nginx
docker run -d --name ghost -e database__client=sqlite3 -e database__connection__filename=content/data/ghost.db -v $(pwd)/ghost-data:/var/lib/ghost/content -p 2368:2368 ghost

使用docker ps 查看如图所示:

成果截图: