ingress nginx 筆記
阿新 • • 發佈:2022-03-11
背景
記錄一些常見用法,類似faq。
前置條件
全域性變數
ingress 配置一些全域性的變數,比如 server-tokens: "false" ,有以下兩種辦法。
- 修改 configmap,遵循 yaml 語法
kubectl -n ingress-nginx edit cm ingress-nginx-controller # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: server-tokens: "false" ...
- 新建一個yaml檔案,apply -f 應用即可
# cat configmap.yaml
apiVersion: v1
data:
server-tokens: "false"
kind: ConfigMap
metadata:
name: nginx-configuration
namespace: ingress-nginx
# kubectl apply -f configmap.yaml
以上兩種辦法設定後,ingress 均會自動過載讓配置生效
區域性變數
當你需要針對某個 location 做一些設定,比如要開啟 ip 白名單限制,就需要用到 ingress 的 annotations 功能了。
現在需要對 ryb 後臺做白名單限制,新建一個 ingress 資源示例。
--- kind: Ingress apiVersion: extensions/v1beta1 metadata: name: ryb-ingress namespace: app labels: app: ryb annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/whitelist-source-range: '192.168.8.0/24' #設定ip spec: rules: - host: fuck.cn http: paths: - path: /ryb backend: serviceName: ryb-svc servicePort: 7302
部署
私有云自建的 k8s 部署 ingress 通常有2種方案,Deployment + LB、Daemonset + HostNetwork + LB 。優缺點如下:
- Deployment + LB:將 Nginx Ingress Controller 以 Deployment 的方式部署,以 nodeport 方式將埠映射出叢集外,通過 slb 將流量轉發至 ingress_nodeport,ingress_nodeport 再通過 iptables 或 ipvs 將請求路由到 service 對應的後端 pod。
- Daemonset + HostNetwork + LB :在方案一中,流量會經過 nodeport,多了一層轉發,增加網路損耗。所以改為用 hostnetwork。
方案2的缺點是,增刪 ingress 節點時需手動操作。有能自動操作的方案,不過只在公有云上實現,在此忽略。
使用 hostnetwork 的方法如下:
template:
spec:
hostNetwork: true
同時將 nodeport 換成 clusterip即可
spec:
type: ClusterIP
效能優化
此處給出騰訊雲、阿里雲的參考文件,本人親測效能會提高,但也有可能影響業務,請自行取捨。
https://partners-intl.aliyun.com/help/zh/doc-detail/202125.htm