轻量级容器优化型 Linux 发行版 Photon OS

前段时间捡垃圾东拼西凑搞了台 Homelab 👇 玩玩 👉《垃圾佬的 Homelab 折腾记录》

总体来讲性价比高,Intel i5-6600T 的性能和 i5-6600 持平,比那种 8250U 之类的低压 U 要高很多,跑一堆虚拟机也不成问题,甚至跑个三 master + 两 node 的 kubernetes 集群也是可以 😂。至于功耗,整机待机时不到 30W ,一天不到 1 度电,不用担心电费爆表了;至于价格,一颗 Intel i5-6600T 不到 450 块钱,还不香嘛 🙃。

在 ESXi 运行着一些虚拟机,比如用 WireGuard 打通 VPS 之间的网络,运行在一个 alpine 虚拟机里作为网关机使用;比如使用 Pi-hole® 来构建自己的 DNS 服务器,用来拦截屏蔽域名;比如运行 time-machine 服务用来定时备份 MacBook ;比如 Windows 虚拟机里运行着一些国产毒瘤软件 😡;比如使用 Drone 跑一套轻量级的 CI 流水线系统,总之可玩性非常高哦,只要你又时间瞎折腾,总能找点乐子玩 😂。

Linux Container OS ?

有了一台运行着 ESXi 的 Homelab ,今天就玩一下 Linux container OS ,即 Linux 容器 OS 、容器优化型 OS,这是一类专门针对运行容器定制化开发的 Linux 发行版,裁剪掉一些不必要的软件和内核模块,使系统更加轻量一些。虽然来说民用级的 Intel i5-6600T 性能也不算太差,但和 E3 小王子,E5 老大哥比还是差个十万八千里。因此为了节省一些 CPU 资源,减少虚拟化带来的开销,就选择了容器化运行一些应用,同时再为这些容器找一个轻量级的宿主机 OS ,这就是为什么想要使用 Linux container OS 的原因。

GKE 的 Container-Optimized OS

Kubernetes 的亲爸爸 Google 家的 Google Kubernetes Engine 即 GKE 集群中的每个节点都是使用 Container-Optimized OS 来运行工作负载,不过仅仅是针对 GCE 来进行优化的,可能在 OpenStack 或者 vSphere 上运行不起来,(瞎猜 😂。

Container-Optimized OS 是适用于 Compute Engine 虚拟机的操作系统映像,专为运行 Docker 容器而优化。借助 Container-Optimized OS,您可以快速、高效、安全地在 Google Cloud Platform 上启动 Docker 容器。Container-Optimized OS 由 Google 维护,基于 Chromium OS 开放源代码项目。

特点就是不同于其他的 Linux 发行版,这个是基于 Chromium OS 定制化开发的,对内核版本选用的也比较激进,一般是 4.19.112+ 或者 5.x 版本,这样你就不用再担心像 CentOS 7.x 系列那样各种内核 bug 了。

apiVersion: v1
items:
- apiVersion: v1
  kind: Node
  metadata:
    nodeInfo:
      architecture: amd64
      bootID: 0c517083-aaf6-75fc4b2204ba
      containerRuntimeVersion: docker://19.3.1
      kernelVersion: 4.19.112+
      kubeProxyVersion: v1.16.13-gke.1
      kubeletVersion: v1.16.13-gke.1
      machineID: 33a96ff3203d88c0a542
      operatingSystem: linux
      osImage: Container-Optimized OS from Google
      systemUUID: 33a96ff33d88c0a542

Chromium OS 就是 Google 基于 Linux 内核开发的操作系统,所以 Google 也有这个实力针对自家的 GCP 云平台进行定制化开发个 OS 出来,专门给 kubernetes 集群运行使用,定制化开发的好处就是可以带来更多的新特性,二不受制于上游的 Linux 发行版的限制。比如不久前 Google 宣布将使用 Cilium 作为 GKE 的下一代数据面,New GKE Dataplane V2 increases security and visibility for containers ,而 Cilium 这项技术是依赖于对 eBPF 技术,而 eBPF 又依赖于内核特性的支持。

AWS 的 Bottlerocket OS

这个是 AWS 最近开源专门针对 EC2 进行优化的 Linux Container OS,和 GKE 一样,只是针对于自家的 AWS 公有云,由于是最近刚刚推出的,还没来得及关注,所以就不介绍了。同 GKE 的 Container-Optimized OS 一样,一般公有云定制化开发的 Container OS 仅仅只针对自家的云平台。

Optimized performance through AWS integrations

AWS provided builds of Bottlerocket are optimized to run on Amazon EC2 and include support for the latest Amazon EC2 instance capabilities. They also have built-in integrations with AWS services for container orchestration, registries, and observability.

CoreOS Container Linux

来自 CoreOS 团队的 CoreOS Container Linux ,它应该是最古老的 Linux Container OS ,早在 2013 年 10 月就已经 release 第一个版本,那时候的 docker 还没有在 0.x.x 版本。

v94.0.0

  • Git is now included by default as a number of people use it for shipping around assets, code, etc like a distributed rsync
  • Docker is upgraded to 0.6.3
  • xz is included to support new compression types
  • Custom OEMs can be provided via the cpio on PXE images

它没有像 yum 或 apt 这样的包管理器来安装软件,在 CoreOS 中你不需要安装软件,因为所有的应用程序都要使用 docker 来打包。

  • 最小化的操作系统: 占用内存很少,比典型的服务器版本 Linux 少占 40% 的内存。
  • 易于升级: CoreOS 采用双系统分区(主动分区/被动分区)设计而不是采用传统的通过升级包来升级系统,这使得每次升级更加快速,可靠和易于回滚。这一点有点像 Android 的 A/B 分区?
  • 集成 Docker: CoreOS 默认集成 Docker 并作了很好的支持和优化,省去用户安装,配置,优化 Docker 的时间,极大地方便了用户。
  • 易于集群化: CoreOS 本身提供了大型 Docker 容器集群的整体解决方案,通过内置的 fleet 工具在多台系统中部署容器并进行集群化管理。同时通过提供 Discovery Service,便于动态部署和管理集群,解决方案比较成熟。
  • 自动化的大规模部署: CoreOS 自身提供的解决方案能够自动地大规模批量部署并操作系统,极大地减少用户工作量。
  • 使用 systemd 做为系统服务管理工具,性能比较好,systemd 有现代化的日志功能,同时采用 socket 式与 D-Bus 总线式激活服务.

不过 CoreOS 早在今年四月份就已经 EOF 了,Fedora CoreOS 成为 CoreOS 的继任者:

As we’ve previously announced, Fedora CoreOS is the official successor to CoreOS Container Linux. Fedora CoreOS is a new Fedora Edition built specifically for running containerized workloads securely and at scale. It combines the provisioning tools and automatic update model of Container Linux with the packaging technology, OCI support, and SELinux security of Atomic Host. For more on the Fedora CoreOS philosophy, goals, and design, see the announcement of the preview release and the Fedora CoreOS documentation.

红帽的 RHCOS

来自红帽子家的 Red Hat Enterprise Linux CoreOS (RHCOS),是基于不过这个 OS 仅仅适用于它自家的 OpenShift,而且容器运行时仅支持 CRI-O。

At the moment, CRI-O is only available as a container engine within OpenShift Container Platform clusters.

因为 CoreOS 团队现如今已经被 Red Hat® 收购了,正如在 OpenShift 文档 中提到的: Red Hat Enterprise Linux CoreOS (RHCOS) 代表了下一代单用途容器操作系统技术。RHCOS 由创建了 Red Hat Enterprise Linux Atomic Host 和 CoreOS Container Linux 的同一开发团队打造,它将 Red Hat Enterprise Linux (RHEL) 的质量标准与 Container Linux 的自动化远程升级功能结合在一起。

RancherOS

RancherOS 是 Rancher 团队所维护的开源项目,也是对标 CoreOS 一样,专门用来运行容器,并且可以运行在生产环境(至少官方做了这么样的承诺,咱也没在生产用过,不好说。在 RancherOS 中所有的进程(包括系统所有的服务,比如 udev 和 syslog)都是用 docker 来管理,这一点要比 CoreOS 更加激进一些,而 CoreOS 还是使用传统 Linux 发行版中的 systemd 来管理系统中的服务。RancherOS 通过移除传统 Linux 发行版中不必要的服务和库来最小化系统,使他专注单一的功能,即运行 docker 容器。不过之前体验了一番,占用资源比较多 😂,好像并没有太大的优势(

Everything in RancherOS is a Docker container. 感觉这个要比 CoreOS 更加容器化,甚至使用 docker 取代了 systemd 来管理系统的各种服务。系统启动后运行两个 docker 服务进程,一个是系统 docker ,在此之上在运行系统服务容器,和用户层面的 docker 。不过看一下下面的这张图你就会明白。总的来讲 RancherOS 是使用 docker 来管理整个系统的服务的,包括用户层面的 docker 。

VMware 的 Photon OS

今天的主角,VMware 开源的 Photon OS,这个 OS 你可能没听说过,但 VMware 开源的 Harbor 想必很熟悉,而 Harbor 的基础镜像使用的就是他家的 Photon OS😂。

root [ /harbor ]# cat /etc/os-release
NAME="VMware Photon OS"
VERSION="2.0"
ID=photon
VERSION_ID=2.0
PRETTY_NAME="VMware Photon OS/Linux"
ANSI_COLOR="1;34"
HOME_URL="https://vmware.github.io/photon/"
BUG_REPORT_URL="https://github.com/vmware/photon/issues"

在 Photon OS 的构建脚本 build.py 中可以看到它构建的产物支持 ISO 和 docker 镜像,以及众多公有云的虚拟机格式,两者都是来自相同的源码,根据构建出来的是形式不同,所包含的软件也不同。因为裁剪掉了一些不必要的系统服务, docker 镜像格式的要更精简一些。

targetList = {
        "image":["iso", "ami", "gce", "azure", "rpi3", "ova", "ova_uefi", "all", "src-iso",
                "photon-docker-image", "k8s-docker-images", "all-images", "minimal-iso", "rt-iso"],

        "rpmBuild": ["packages", "packages-minimal", "packages-initrd", "packages-docker",
                "updated-packages", "tool-chain-stage1", "tool-chain-stage2", "check",
                "ostree-repo", "generate-yaml-files", "create-repo", "distributed-build"],

        "buildEnvironment": ["packages-cached", "sources", "sources-cached", "publish-rpms",
                "publish-x-rpms", "publish-rpms-cached", "publish-x-rpms-cached", "photon-stage"]

总的来讲,上面提到的几种容器优化型 OS 中 ,Photon OS 比较开放一些,虽然来说是针对于自家 vSphere 进行优化的,但其他公有云和私有云也是支持,甚至还支持树莓派 🍓!所以个人用户想拿来玩玩,还是不错滴。尤其是针对咱这种玩 ESXi 软路由的垃圾佬,在 Photon OS 上跑一些容器应用,再适合不过了。

对比

Providers Name STAR FORK
Google Container-Optimized OS - -
Amazon Bottlerocket OS 4.8K 185
CoreOS CoreOS Container Linux - -
Red Hat RHCOS - -
Rancher Rancher OS 6k 625
Flatcar Flatcar Linux 117 3
VMware Photon OS 2.1k 591
Providers Name Runtime Support Platform
Google Container-Optimized OS docker GCP
Amazon Bottlerocket OS docker AWS
CoreOS CoreOS Container Linux docker AWS/GCP/OpenStack/VMware
Alibaba Cloud/Azure/DigitalOcean
Red Hat RHCOS CRI-O AWS/vSphere/Bare Metal
Rancher Rancher OS docker AWS/GCP/OpenStack/VMware
Alibaba Cloud/Azure/DigitalOcean
Flatcar Flatcar Linux docker AWS/GCP/OpenStack/VMware
Alibaba Cloud/Azure/DigitalOcean
VMware Photon OS docker AWS/GCE/Azure/OpenStack
vSphere/bare metal/Raspberry Pi 3

目前来讲 Photon OS 作为 Harbor 的基础镜像,在容器方面比较突出一些,但作为容器的宿主机 OS ,还有很长的路要走。由于我是使用的 VMware 家的 ESXi 虚拟化,那么选择他家的 Photon OS 是最好的喽,而且 Photon OS 是针对 ESXi 做过优化的,内核都是针对 ESXi 虚拟化进行了定制化的开发,所以理论上选择 Photon OS 应该是最合适的。

安装

Download Format

Format Description
ISO Image Contains everything needed to install either the minimal or full installation of Photon OS. The bootable ISO has a manual installer or can be used with PXE/kickstart environments for automated installations.
OVA Pre-installed minimal environment, customized for VMware hypervisor environments. These customizations include a highly sanitized and optimized kernel to give improved boot and runtime performance for containers and Linux applications. Since an OVA is a complete virtual machine definition, we’ve made available a Photon OS OVA that has virtual hardware version 11; this will allow for compatibility with several versions of VMware platforms or allow for the latest and greatest virtual hardware enhancements.
Amazon AMI Pre-packaged and tested version of Photon OS made ready to deploy in your Amazon EC2 cloud environment. Previously, we’d published documentation on how to create an Amazon compatible instance, but, now we’ve done the work for you.
Google GCE Image Pre-packaged and tested Google GCE image that is ready to deploy in your Google Compute Engine Environment, with all modifications and package requirements for running Photon OS in GCE.
Azure VHD Pre-packaged and tested Azure HD image that is ready to deploy in your Microsoft Azure Cloud, with all modifications and package requirements for running Photon OS in Azure.
Raspberry Pi3 Image Pre-packaged and tested Raspberry Pi3 Image (Version 3.0 onwards) on ARM64 architecture.

Photon OS 提供了多种安装方式,其中 ISO 是通用性的,就和安装其他 Linux 发行版的过程差不多,OVA 是虚拟机模板,可以导入到 VMware 虚拟化平台上使用,省区安装的步骤,比较方便。OVA 虚拟机模板的版本为 11 ,根据 Virtual machine hardware versions 中的定义,需要 ESXi 6.0 Fusion 7.x Workstation 11.x Player 7.x 版本以上的虚拟化支持。

Virtual Hardware Version Products
18 ESXi 7.0.1
17 ESXi 7.0.0
16 Fusion 11.x Workstation Pro 15.x Workstation Player 15.x
15 VMware Cloud on AWS ESXi 6.7 U2
14 ESXi 6.7 Fusion 10.x Workstation Pro 14.x Workstation Player 14.x
13 ESXi 6.5
12 Fusion 8.x Workstation Pro 12.x Workstation Player 12.x
11 ESXi 6.0 Fusion 7.x Workstation 11.x Player 7.x
10 ESXi 5.5 Fusion 6.x Workstation 10.x Player 6.x

另外还支持 Raspberry Pi 3 ,不过需要拿源码自行编译镜像,然后刷到树莓派上,你吃灰的树莓派又有用途啦 😂。

下载方式 👇

Photon OS 3.0 Revision 2 Update3 Binaries

Aug 14, 2020 An update to 3.0 revision 2 Update3 binaries are now available.

Download Size md5 checksum
Full ISO x86_64 5.2G 2dd9f18c5162a7367f2463f4a9bb4890
Minimal ISO x86_64 299M 0119f0f275f246fd382d419ff41898a4
ISO x86_64 Real-Time flavour 489M d2bfce95b54a29174b5beaef9962dbc1
OVA-hw11 188M ba52abd88c5b22cd4498cf0e88457f28
OVA-hw13_uefi 214M 95614f3b08c1a93306ababcc826572df

在此使用 OVA-hw11 格式的 OVA 虚拟机模板,后面那个带 uefi 的需要设置虚拟机为 EFI 模式启动,比较麻烦。

  • 创建虚拟机的时候使用 OVA 文件导入

  • 默认的用户名为 root ,密码为 changeme,登录之后再输入一遍 changeme,然后修改为新的密码。

系统信息

  • 内核版本为 4.19.132-5.ph3-esx,4.19 是个 LTS 版本的内核,结尾的 esx 则代表着为 ESXi 虚拟化定制的内核。
Linux photon-machine 4.19.132-5.ph3-esx #1-photon SMP Wed Aug 12 21:02:13 UTC 2020 x86_64 GNU/Linux
  • rpm 包数量仅仅为 146 个,一般的 CentOS 发行版自带的 rpm 包数量为 300 个左右。
root@photon-machine [ ~ ]# rpm -qa | wc
    146     146    4307
  • 系统进程信息,还是采用 systemd 来管理进程
root@photon-machine [ ~ ]# systemctl status
● photon-machine
    State: running
     Jobs: 0 queued
   Failed: 0 units
    Since: Tue 2020-09-22 08:07:29 UTC; 10min ago
   CGroup: /
           ├─user.slice
           │   ├─session-c2.scope
           │   │ ├─413 sshd: root@pts/0
           │   │ ├─420 -bash
           │   │ ├─443 systemctl status
           │   │ └─444 systemctl status
           │   └─[email protected]
           │     └─init.scope
           │       ├─393 /lib/systemd/systemd --user
           │       └─394 (sd-pam)
           ├─init.scope
           │ └─1 /lib/systemd/systemd
           └─system.slice
             ├─systemd-networkd.service
             │ └─245 /lib/systemd/systemd-networkd
             ├─systemd-udevd.service
             │ └─124 /lib/systemd/systemd-udevd
             ├─vgauthd.service
             │ └─159 /usr/bin/VGAuthService -s
             ├─systemd-journald.service
             │ └─97 /lib/systemd/systemd-journald
             ├─sshd.service
             │ └─352 /usr/sbin/sshd -D
             ├─vmtoolsd.service
             │ └─161 /usr/bin/vmtoolsd
             ├─systemd-resolved.service
             │ └─247 /lib/systemd/systemd-resolved
             ├─dbus.service
             │ └─157 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
             ├─systemd-timesyncd.service
             │ └─151 /lib/systemd/systemd-timesyncd
             └─systemd-logind.service
               └─158 /lib/systemd/systemd-logind
  • 磁盘根目录只使用了不到 600M
root@photon-machine [ ~ ]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        16G  577M   15G   4% /
  • 内存也只使用了 43Mi,启动 dockerd 之后内存占用 108Mi,换成 containerd 将会小一些。
root@photon-machine [ ~ ]# free -h
              total        used        free      shared  buff/cache   available
Mem:          2.0Gi        43Mi       1.8Gi       0.0Ki       103Mi       1.8Gi
# 启动 dockerd 守护进程之后内存占用 108Mi
root@photon-machine [ ~ ]# free -h
              total        used        free      shared  buff/cache   available
Mem:          2.0Gi       108Mi       1.6Gi       0.0Ki       298Mi       1.8Gi
  • 内核模块的数量也比较少 30 个左右,大部分都是一些网络相关的内核模块。常规的 Linux 发行版的内核模块往往在 60 个以上。
root@photon-machine [ ~ ]# lsmod
Module                  Size  Used by
xt_conntrack           16384  2
ip6table_mangle        16384  0
ip6table_nat           16384  0
nf_nat_ipv6            16384  1 ip6table_nat
iptable_mangle         16384  0
iptable_nat            16384  0
nf_nat_ipv4            16384  1 iptable_nat
nf_nat                 28672  2 nf_nat_ipv6,nf_nat_ipv4
ip6table_filter        16384  1
ip6_tables             24576  3 ip6table_filter,ip6table_nat,ip6table_mangle
iptable_filter         16384  1
xt_LOG                 16384  0
nf_conntrack           90112  4 xt_conntrack,nf_nat,nf_nat_ipv6,nf_nat_ipv4
nf_defrag_ipv6         20480  1 nf_conntrack
nf_defrag_ipv4         16384  1 nf_conntrack
mousedev               20480  0
vfat                   20480  1
fat                    61440  1 vfat
evdev                  20480  1
vmwgfx                253952  1
psmouse                90112  0
drm_kms_helper        106496  1 vmwgfx
ttm                    86016  1 vmwgfx
drm                   323584  4 vmwgfx,drm_kms_helper,ttm
sr_mod                 24576  0
i2c_core               40960  2 drm_kms_helper,drm
cdrom                  49152  1 sr_mod
rdrand_rng             16384  0
rng_core               16384  1 rdrand_rng
ipv6                  368640  16 nf_nat_ipv6,ip6table_mangle
root@photon-machine [ ~ ]# lsmod | wc
     31     109    1273
  • 系统自带的 docker 版本为 19.03.10,存储驱动使用的是 overlay2
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.10
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
 runc version: d736ef14f0288d6993a1845745d6756cfc9ddd5a
 init version: fec3683
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 4.19.132-5.ph3-esx
 Operating System: VMware Photon OS/Linux
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.951GiB
 Name: photon-machine
 ID: HXBT:Z4LZ:4HZM:3YII:U7ZA:RVOH:Z7CL:L4FA:YGA4:Y2V6:DSVR:NWPD
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

系统管理

网络管理

  • 查看网卡状态,可使用自带的 networkctl 命令行工具,像 ifconfig、ip、ss 等命令都已经默认安装。
root@photon-machine [ /mnt/RPMS ]# networkctl status eth02: eth0
       Link File: /usr/lib/systemd/network/99-default.link
    Network File: /etc/systemd/network/99-dhcp-en.network
            Type: ether
           State: routable (configured)
            Path: pci-0000:0b:00.0
          Driver: vmxnet3
          Vendor: VMware
           Model: VMXNET3 Ethernet Controller
      HW Address: 00:0c:29:74:13:16 (VMware, Inc.)
         Address: 192.168.0.235
                  fe80::20c:29ff:fe74:1316
         Gateway: 192.168.0.1 (NETGEAR)
             DNS: 119.29.29.29
                  223.6.6.6
        CLIENTID: ffb6220feb00020000ab113bc2c88225c0d29b
    Connected To: n/a on port 00:e0:4c:68:54:12
                  n/a on port 00:e0:4c:68:54:13
                  n/a on port 00:e0:4c:68:54:15
  • 系统安装时默认使用的 DHCP 获取 IP ,可以使用 systemd 来设置静态 IP
# 首先修改 DHCP 的 systemd 文件,关闭 DHCP
sed -i 's/yes/no/' /etc/systemd/network/99-dhcp-en.network
# 创建静态 IP 的 systemd 文件
cat > /etc/systemd/network/10-static-en.network << "EOF"

[Match]
Name=eth0

[Network]
Address=198.168.0.235/24
Gateway=198.168.0.1
DNS=192.168.0.100
EOF

# 修改以下文件权限为 644 ,不然启动的时候会报错提示权限问题
chmod 644 /etc/systemd/network/10-static-en.network
# 重启一下网络
systemctl restart systemd-networkd
  • 挂载 NFS 文件系统需要安装 nfs-utils
tdnf install nfs-utils -y
mount 192.168.0.100:/nfs /mnt/nfs

包管理

Photon OS 使用的是 yum/tdnf 作为包管理器,使用方法和 RedHat 系的发行版基本相同,repo 主要有以下几个:

ls /etc/yum.repos.d/
lightwave.repo
photon-extras.repo
photon-iso.repo
photon-updates.repo
photon.repo

源码编译

运行容器

Pi-hole

Pi-hole 是一款开源且免费的 DNS 沉洞服务器(DNS sinkhole),能够在不安装任何客户端侧软件的前提下为设备提供网络内容屏蔽服务,非常轻量易用。搭配上家中吃灰已久的树莓派,我们就能够轻松打造属于自己的广告屏蔽助手。
官网的介绍中,Pi-hole 主要具有以下优点:

  • 易于安装和配置(号称 10 分钟安装配置一条龙)。
  • 全平台,广告屏蔽服务可作用于任何设备,包括 PC、手机、平板电脑。
  • 轻量,对硬件要求极低。
  • 功能稳定且强大,能轻松 hold 住百万级别的请求。
  • 提供了美观的 Web 数据监控仪表盘。
  • 开源且免费。
  • 首先需要关闭 systemd 自带的 DNS 服务,不然 Pi-hole 坚挺的 53 端口会被占用
systemctl stop systemd-resolved
systemctl disable systemd-resolved
  • 安装 docker-compose,修改 docker-compose.yaml 文件
version: "3"
# More info at https://github.com/pi-hole/docker-pi-hole/ and https://docs.pi-hole.net/
services:
  pihole:
    container_name: pihole
    image: pihole/pihole:latest
    ports:
      - "53:53/tcp"
      - "53:53/udp"
      - "67:67/udp"
      - "80:80/tcp"
      - "443:443/tcp"
    environment:
      TZ: 'Asia/Shanghai'
      WEBPASSWORD: 'changeme'
    # Volumes store your data between container upgrades
    volumes:
      - './etc-pihole/:/etc/pihole/'
      - './etc-dnsmasq.d/:/etc/dnsmasq.d/'
    # Recommended but not required (DHCP needs NET_ADMIN)
    #   https://github.com/pi-hole/docker-pi-hole#note-on-capabilities
    restart: unless-stopped
  • docker-compose up 走起!
root@PhotonOS [ /opt/docker/pihole ]# docker-compose up
Creating network "pihole_default" with the default driver
Creating pihole ... done
Attaching to pihole
pihole    | [s6-init] making user provided files available at /var/run/s6/etc...exited 0.
pihole    | [s6-init] ensuring user provided files have correct perms...exited 0.
pihole    | [fix-attrs.d] applying ownership & permissions fixes...
pihole    | [fix-attrs.d] 01-resolver-resolv: applying...
pihole    | [fix-attrs.d] 01-resolver-resolv: exited 0.
pihole    | [fix-attrs.d] done.
pihole    | [cont-init.d] executing container initialization scripts...
pihole    | [cont-init.d] 20-start.sh: executing...
pihole    |  ::: Starting docker specific checks & setup for docker pihole/pihole
  [] Update local cache of available packages
pihole    |   [i] Existing PHP installation detected : PHP version 7.0.33-0+deb9u8
pihole    |
pihole    |   [i] Installing configs from /etc/.pihole...
pihole    |   [i] Existing dnsmasq.conf found... it is not a Pi-hole file, leaving alone!
  [] Copying 01-pihole.conf to /etc/dnsmasq.d/01-pihole.conf
pihole    | chown: cannot access '': No such file or directory
pihole    | chmod: cannot access '': No such file or directory
pihole    | chown: cannot access '/etc/pihole/dhcp.leases': No such file or directory
pihole    | ::: Pre existing WEBPASSWORD found
pihole    | Using default DNS servers: 8.8.8.8 & 8.8.4.4
pihole    | DNSMasq binding to default interface: eth0
pihole    | Added ENV to php:
pihole    |                     "PHP_ERROR_LOG" => "/var/log/lighttpd/error.log",
pihole    |                     "ServerIP" => "0.0.0.0",
pihole    |                     "VIRTUAL_HOST" => "0.0.0.0",
pihole    | Using IPv4 and IPv6
pihole    | ::: Preexisting ad list /etc/pihole/adlists.list detected ((exiting setup_blocklists early))
pihole    | https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
pihole    | https://mirror1.malwaredomains.com/files/justdomains
pihole    | ::: Testing pihole-FTL DNS: FTL started!
pihole    | ::: Testing lighttpd config: Syntax OK
pihole    | ::: All config checks passed, cleared for startup ...
pihole    |  ::: Docker start setup complete
pihole    |   [i] Neutrino emissions detected...
  [] Pulling blocklist source list into range
pihole    |
  [] Preparing new gravity database
pihole    |   [i] Target: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
pihole    |   [i] Received 55654 domains
pihole    |   [i] Target: https://mirror1.malwaredomains.com/files/justdomains
  [] Status: No changes detected
pihole    |   [i] Received 26854 domains
  [] Storing downloaded domains in new gravity database
  [] Building tree
  [] Swapping databases
pihole    |   [i] Number of gravity domains: 82508 (82465 unique domains)
pihole    |   [i] Number of exact blacklisted domains: 0
pihole    |   [i] Number of regex blacklist filters: 0
pihole    |   [i] Number of exact whitelisted domains: 0
pihole    |   [i] Number of regex whitelist filters: 0
  [] Cleaning up stray matter
pihole    |
pihole    |   [] DNS service is running
pihole    |   [] Pi-hole blocking is Enabled
pihole    |   Pi-hole version is v5.1.2 (Latest: v5.1.2)
pihole    |   AdminLTE version is v5.1.1 (Latest: v5.1.1)
pihole    |   FTL version is v5.2 (Latest: v5.2)
pihole    | [cont-init.d] 20-start.sh: exited 0.
pihole    | [cont-init.d] done.
pihole    | [services.d] starting services
pihole    | Starting pihole-FTL (no-daemon) as root
pihole    | Starting lighttpd
pihole    | Starting crond
pihole    | [services.d] done.
  • 打开浏览器,输入 http://ip:port/admin 即可进入 Pi-hole 的后台管理页面,在这里可以查看到 DNS 解析的详细记录,关于 Pi-hole 的使用可以参考一些大佬的博客,比如 搭建 Pi-Hole 为网上冲浪保驾护航

如果想要让内网的机器都走 Pi-hole 来进行 DNS ,可以将路由器的 DHCP 的 DNS 的 IP 设置为 Pi-hole 的 IP。

参考