DevOps 网络(高级,面向生产)

发布: (2025年12月30日 GMT+8 06:24)
7 min read
原文: Dev.to

Source: Dev.to

请提供您希望翻译的完整文本内容,我将按照要求保留源链接、格式和技术术语进行简体中文翻译。

核心知识(必须自动掌握)

网络基础(无需思考)

  • IP 地址 – 私有 vs. 公有
  • CIDR/32/24/16
  • 端口 – 公认端口 vs. 瞬时端口
  • TCP 与 UDP
  • DNS 解析流程
  • 路由表
  • 默认网关

Linux 网络命令(必须熟记)

接口与路由

ip a        # show interfaces
ip r        # show routing table

连接与端口

ss -tulnp
netstat -tulnp

可达性

ping 
traceroute 

应用层

curl 
wget 

端口占用

lsof -i :8080

防火墙

ufw status
iptables -L -n

🔥 项目 1 – “服务在运行但不可访问”

(经典面试题)

场景(真实公司案例)

  • 后端服务部署在 Linux 虚拟机上
  • 进程正在运行
  • 端口在本地已打开
  • 外部用户无法访问

目标

系统化地定位故障,而不是盲目猜测。

架构

客户端 → DNS → 网络 → 防火墙 → 操作系统 → 应用程序

步骤实验

步骤 1 – 启动服务

python3 -m http.server 8080

验证它是否在监听:

ss -tulnp | grep 8080

预期: 服务在 0.0.0.0:8080127.0.0.1:8080 上监听。

步骤 2 – 测试本地访问

curl http://localhost:8080
  • ✅ 正常 → 应用健康
  • ❌ 失败 → 应用本身有问题,而非网络

步骤 3 – 检查接口绑定

ip a

关键点: 如果应用绑定到 127.0.0.1,则无法被外部访问。

步骤 4 – 检查路由

ip r

查找以下内容:

  • 默认路由
  • 正确的网关

❌ 没有默认路由 → 流量无法离开虚拟机。

步骤 5 – 防火墙检查(非常常见)

sudo ufw status
# 或
sudo iptables -L -n

修复(UFW 示例):

sudo ufw allow 8080

步骤 6 – 验证端口归属

lsof -i :8080

确认:

  • 正确的进程在监听
  • 没有端口冲突

步骤 7 – 外部测试

在另一台机器上执行:

curl http://<VM_IP>:8080

面试映射(使用的精准表述)

“我会逐层排查:应用程序 → 端口绑定 → 操作系统防火墙 → 路由 → DNS。”

🔥 项目 2 – 生产环境中的 DNS 故障

场景

  • 服务可通过 IP 访问
  • 通过 域名 无法访问

步骤

步骤 1 – 解析 DNS

nslookup myservice.company.com
# or
dig myservice.company.com

步骤 2 – 检查 /etc/resolv.conf

cat /etc/resolv.conf

查找:

  • 有效的 nameserver 条目
  • 企业 DNS 与公共 DNS 的区别

步骤 3 – 对比 IP 与 DNS

curl http://10.0.2.15:8080
curl http://myservice.company.com:8080

面试答案

“如果 IP 正常而 DNS 失效,问题出在 DNS 解析,而不是网络或应用程序本身。”

🔥 PROJECT 3 – Routing Issue (Traffic Goes Nowhere)

场景

  • 服务运行中
  • 防火墙已打开
  • DNS 正常
  • 仍然无法访问

步骤

Step 1 – Check Routes

ip r

常见问题:

  • 缺少默认路由
  • VPN 或云配置错误导致网关不正确

Step 2 – Trace Packet Path

traceroute google.com

如果跟踪提前停止 → 路由问题。

Interview Tip

“Traceroute 告诉我数据包在哪儿死亡。这通常比盲目猜测更快。”

🔥 项目 4 – 端口冲突与幽灵服务

场景

  • 部署成功
  • 应用立即崩溃

步骤

步骤 1 – 检查端口使用情况

ss -tulnp | grep 8080
# or
lsof -i :8080

步骤 2 – 结束进程或重新配置

kill -9 <PID>

面试洞察

“我检查的第一件事就是端口是否已经被占用。”

🔥 项目 5 – 防火墙锁定模拟

(在此添加您自己的步骤 – 例如,启用严格的安全组,验证连通性,然后回滚。)

安全关注

场景

  • 安全团队收紧防火墙
  • 应用崩溃

第 1 步:启用防火墙

sudo ufw enable

第 2 步:阻止所有流量

sudo ufw default deny incoming

第 3 步:仅允许必需端口

sudo ufw allow 22
sudo ufw allow 443

学习成果

  • 最小特权网络
  • 零信任思维

面试回答

“我从不打开宽泛的端口。我只允许应用严格需要的端口。”

Source:

🎯 FINAL INTERVIEW MASTER ANSWER

Question:
“Service is up but not reachable. What do you check?”

Perfect Senior Answer:

“I troubleshoot layer by layer.

  1. Service status – Verify the service is running and listening on the correct interface/port (systemctl status …, ss -ltnp).
  2. Firewall rules – Check ufw/iptables policies to ensure the required ports are allowed.
  3. Routing – Inspect the routing table (ip route) to confirm traffic can reach the host.
  4. DNS resolution – Confirm the hostname resolves to the correct IP (dig, nslookup).
  5. Connectivity tools – Use ping, traceroute, or tcptraceroute to locate where packets are dropped.
  6. Application logs – Look for errors that might indicate binding or permission issues.
    By moving through these layers I can quickly pinpoint whether the blockage is at the host, network, or application level.”

中文翻译:

“我会逐层排查。

  1. 服务状态 – 确认服务已启动并在正确的接口/端口上监听(systemctl status …ss -ltnp)。
  2. 防火墙规则 – 检查 ufw/iptables 策略,确保所需端口已放行。
  3. 路由 – 查看路由表(ip route),确认流量能够到达主机。
  4. DNS 解析 – 确认主机名解析到正确的 IP(dignslookup)。
  5. 连通性工具 – 使用 pingtraceroutetcptraceroute 定位数据包丢失的位置。
  6. 应用日志 – 查找可能指示绑定或权限问题的错误。
    通过逐层检查,我可以快速定位阻塞点是位于主机、网络还是应用层。”
Back to Blog

相关文章

阅读更多 »