文章目錄

  • k8s Ingress與安全機制整理
  • 一、Ingress 服務概述
  • 1. 核心作用
  • 二、Ingress 組成
  • 1. Ingress(規則定義)
  • 2. Ingress Controller(轉發器)
  • 三、Ingress 工作原理
  • 四、ingress-nginx-controller 部署
  • 1. 準備清單
  • 2. 修改ClusterRole配置
  • 五、Ingress 暴露服務的三種方式
  • 方式一:Deployment + Service(type=LoadBalancer)
  • 方式二:DaemonSet + HostNetwork + nodeSelector
  • 實施步驟:
  • 方式三:Deployment + Service(type=NodePort)
  • 實施步驟:
  • 六、典型功能示例
  • 1. 虛擬主機代理
  • 2. HTTPS 代理
  • 3. BasicAuth 認證
  • 4. 路徑重寫
  • 七、排錯與檢查
  • 八、總結

k8s Ingress與安全機制整理

一、Ingress 服務概述

1. 核心作用

  • Service的補充:Service解決集羣內部Pod的服務發現與負載均衡,但對外暴露服務需額外方案。
  • 對外暴露方式對比
  • NodePort:暴露節點端口(30000-32767),適合測試,端口管理困難。
  • LoadBalancer:依賴雲廠商,自動創建LB,有額外費用,適合公有云。
  • externalIPs:為Service分配外部IP,需路由到集羣節點。
  • Ingress:七層反向代理,通過少量公網IP/LB暴露多個HTTP/HTTPS服務,基於域名/URL路徑轉發(可理解為“Service的Service”)。

二、Ingress 組成

1. Ingress(規則定義)

  • 以YAML配置的API對象,定義請求轉發規則(外部URL、負載均衡、SSL/TLS、域名代理等)。
  • 功能需依賴Ingress Controller實現。

2. Ingress Controller(轉發器)

  • 解析Ingress規則,實現反向代理與負載均衡,非K8s自帶組件。
  • 常見實現:官方(GCE、ingress-nginx)、第三方。
  • 典型形態:Pod內運行“守護進程(監控集羣生成配置)+ 反向代理(如Nginx)”,例如ingress-nginx動態生成Nginx配置並reload。

三、Ingress 工作原理

  1. Ingress Controller與APIServer交互,動態感知Ingress規則變化。
  2. 按規則生成Nginx配置,寫入ingress-controller Pod的/etc/nginx.conf
  3. 執行reload使配置生效,實現域名/路徑分流與動態更新。

四、ingress-nginx-controller 部署

1. 準備清單

mkdir -p /opt/ingress && cd /opt/ingress
# 官方/國內鏡像獲取部署清單
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.25.0/deploy/static/mandatory.yaml
# 或國內鏡像:wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.25.0/deploy/static/mandatory.yaml

2. 修改ClusterRole配置

mandatory.yaml中為networking.k8s.io添加Ingress資源權限(0.25版本及以上):

apiGroups:
- "extensions"
- "networking.k8s.io"  # 新增,支持networking.k8s.io API組
resources:
- ingresses
verbs:
- get
- list
- watch

五、Ingress 暴露服務的三種方式

方式一:Deployment + Service(type=LoadBalancer)

  • 適用場景:公有云。
  • 原理:通過Deployment部署Controller,創建LoadBalancer類型Service,雲廠商自動生成LB並綁定公網IP,域名解析指向該IP即可暴露服務。

方式二:DaemonSet + HostNetwork + nodeSelector

  • 適用場景:大併發生產環境(性能最優)。
  • 特點:特定節點部署Controller,使用宿主機網絡(直接佔用80/443端口),鏈路最短,一個節點僅運行一個Controller Pod。
實施步驟:
  1. 給節點打標籤(如僅在node02運行):
kubectl label node node02 ingress=true
  1. 修改mandatory.yaml
  • kind: Deployment改為kind: DaemonSet,刪除replicas
  • 啓用宿主機網絡並指定節點:
spec:
hostNetwork: true
nodeSelector:
ingress: "true"
  1. 加載鏡像並啓動,驗證端口(80/443/8181/10254):
docker load -i ingree.contro.tar
kubectl apply -f mandatory.yaml
netstat -lntp | grep nginx  # 檢查監聽端口
  1. 創建Ingress規則(示例):
# networking.k8s.io/v1版本
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-app-ingress
spec:
rules:
- host: www.benet.com
http:
paths:
- path: /
pathType: Prefix  # 前綴匹配(或Exact完全匹配)
backend:
service:
name: nginx-app-svc
port:
number: 80

方式三:Deployment + Service(type=NodePort)

  • 適用場景:無LB環境,需前置負載均衡。
  • 特點:通過NodePort暴露Controller,端口隨機,多一層NAT,大流量可能影響性能。
實施步驟:
  1. 獲取清單並部署:
mkdir -p /opt/ingress-nodeport && cd /opt/ingress-nodeport
# 下載mandatory.yaml和service-nodeport.yaml(NodePort服務配置)
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
docker load -i ingress-controller-0.30.0.tar
kubectl apply -f mandatory.yaml -f service-nodeport.yaml
  1. 測試訪問:通過NodePort端口(如32383)訪問,需配置hosts解析。

六、典型功能示例

1. 虛擬主機代理

通過不同域名轉發到不同Service:

# 示例:www1.benet.com → svc-1;www2.benet.com → svc-2
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress1
spec:
rules:
- host: www1.benet.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: svc-1
port: {number: 80}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress2
spec:
rules:
- host: www2.benet.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: svc-2
port: {number: 80}

2. HTTPS 代理

  • 生成自簽證書並創建secret:
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
kubectl create secret tls tls-secret --key tls.key --cert tls.crt
  • Ingress配置:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-https
spec:
tls:  # 關聯證書secret
- hosts: [www3.benet.com]
secretName: tls-secret
rules:
- host: www3.benet.com
http:
paths:
- path: /
pathType: Prefix
backend:
service: {name: nginx-svc, port: {number: 80}}

3. BasicAuth 認證

  • 生成認證文件並創建secret:
yum install -y httpd
htpasswd -c auth zhangsan  # 生成auth文件
kubectl create secret generic basic-auth --from-file=auth
  • Ingress配置(添加認證註解):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-auth
annotations:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required'
spec:
rules:
- host: auth.benet.com
http:
paths:
- path: /
pathType: Prefix
backend: {service: {name: nginx-svc, port: {number: 80}}}

4. 路徑重寫

通過註解配置重定向目標:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-rewrite
annotations:
nginx.ingress.kubernetes.io/rewrite-target: http://www1.benet.com:32383  # 重定向目標
spec:
rules:
- host: re.benet.com
http:
paths:
- path: /
pathType: Prefix
backend: {service: {name: nginx-svc, port: {number: 80}}}

七、排錯與檢查

  1. 查看Controller狀態
kubectl get pod -n ingress-nginx -o wide
kubectl get cm,daemonset -n ingress-nginx -o wide
  1. 檢查Nginx配置
kubectl exec -it <controller-pod> -n ingress-nginx -- /bin/bash
  more /etc/nginx/nginx.conf  # 查看生成的配置
  1. 調度失敗處理:若因nodeSelector不匹配,可給節點加對應標籤或刪除nodeSelector

八、總結

  • 核心:Ingress = Ingress資源對象(規則) + Ingress Controller(轉發實現)。
  • 選型建議
  • 公有云:優先用Deployment + LoadBalancer
  • 生產環境(大併發):DaemonSet + HostNetwork(高性能)。
  • 無LB環境:Deployment + NodePort(需前置負載均衡)。
  • 生產加固:多節點部署、前置LB/高可用(LVS+keepalived)、監控(Prometheus採集10254端口)、TLS/認證/WAF防護。