Ingress介紹和部署IngressController
-
Ingress是為了彌補nodeport不足而生的,nodeport存在不足:一個端口只能一個服務使用,端口需要提前規划,只支持4層負載均衡。
-
Ingress 公開了從集群外部到集群內部服務的HTTP和HTTPS路由的規則集合,而具體實現流量路由是由Ingress Controller負責。
Ingress: k8s中一個抽象資源,給管理員提供一個暴漏應用的入口定義方法。
Ingress Controller: 根據Ingress生成具體的路由規則,並對Pod負載均衡器。

ingress Controller工作流程
Ingress Contronler通過於k8s API交互,動態去感知集群中Ingress 規則變化,然后讀取它按照自定義規則,規則就是寫明哪個域名對應哪個service ,生成一段nginx配資后,應用到管理Nginx服務, 然后熱加載生效,從而達到Nginx負載均衡器配置及動態更新問題。
ingress-controller流程: 客戶端->LB(公網)-> Ingress Controller(nginx) -> 分布在各pod節點
nodeport流程:客戶端->LB(公網)->Service(nodeport)--> Ingress Controller(nginx) -> 分布在各pod節點
進入ingress-nginx
[root@k8s-master ~]# kubectl exec -it nginx-ingress-controller-5dc64b58f-7qwxn -n ingress-nginx -- bash
-
部署Ingress Controller
-
我們采用nginx控制器
-
下載YAML
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml -
也可以直接下載ingress-controller.yaml,提取碼:hcgm 來直接創建ingress規則。
-
更改名稱
mv mandatory.yaml ingress-controller.yaml -
修改yaml
1. apps/v1 下的kind為 DaemonSet apiVersion: apps/v1 kind: DaemonSet# 如果通過Deployment部署只會在某個節點上能訪問。考慮到高可用,通過DaemonSet會在每一個節點上部署 metadata: name: nginx-ingress-controller namespace: ingress-nginx labels: ... 2. 新增hostNetwork: true 將ingress-controller暴漏出去提供全局入口 spec: hostNetwork: true # wait up to five minutes for the drain of connections terminationGracePeriodSeconds: 300 serviceAccountName: nginx-ingress-serviceaccount nodeSelector: ... 3.更改鏡像地址 containers: - name: nginx-ingress-controller image: lizhenliang/nginx-ingress-controller:0.30.0 args: - /nginx-ingress-controller ... -
加載配置
kubectl apply -f ingress-controller.yaml -
查看部署是否成功
kubectl get pods -n ingress-nginx -
創建規則為其他項目暴漏
# 編輯規則 vi ingress.yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: xujunkai spec: rules: - host: web.xujunkai.com# 指定域名 http: paths: - path: / pathType: Prefix backend: service: name: web# 指定service 名稱 port: number: 80 # service端口 # 創建規則 kubectl apply -f ingress.yaml # 查看規則 [root@k8s-master ~]# kubectl get ingress NAME CLASS HOSTS ADDRESS PORTS AGE xujunkai <none> web.xujunkai.com 80 7s -
查看ingress-nginx在哪個節點上。
kubectl get pods -n ingress-nginx -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-ingress-controller-5dc64b58f-7qwxn 1/1 Running 0 9m50s 172.16.215.140 k8s-node01 <none> <none> -
可以看到在172.16.215.140 上也就是k8s node01節點上
# 通過查看80 443端口已經有監聽了,它就是ingress-nginx-controller監聽的 [root@k8s-node01 logs]# ss -anpt | grep 80 [root@k8s-node01 logs]# ss -anpt | grep 443 # 而在master節點是沒有監聽的 -
訪問
http://web.xujunkai.com/是可以訪問到的
-
-
ingress配置https
-
cfssl下載
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 chmod -x cfssl* for x in cfssl*; do mv $x ${x%*_linux-amd64}; done mv cfssl* /usr/bin -
配置https步驟:
mkdir ssl cd ssl vi certs.sh # 生成證書 cat > ca-config.json <<EOF { "signing": { "default": { "expiry": "87600h" }, "profiles": { "kubernetes": { "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } } EOF cat > ca-csr.json <<EOF { "CN": "kubernetes", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "Beijing", "ST": "Beijing" } ] } EOF cfssl gencert -initca ca-csr.json | cfssljson -bare ca - #----------------------- cat > web.xujunkai.com-csr.json <<EOF { "CN": "web.xujunkai.com", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "name": [ { "C": "CN", "L": "BeiJing", "ST": "Beijing" } ] } EOF cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes web.xujunkai.com-csr.json | cfssljson -bare web.xujunkai.com -
將證書保存到Secret
kubectl create secret tls web-xujunkai-com --cert=web.xujunkai.com.pem --key=web.xujunkai.com-key.pem -
配置ingress https的yaml
vi ingress-https.yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: xujunkai-https spec: tls: - hosts: - web.xujunkai.com secretName: web-xujunkai-com# 密鑰證書名字 rules: - host: web.xujunkai.com http: paths: - path: / pathType: Prefix backend: service: name: web port: number: 80 -
應用ingress
kubectl apply -f ingress-https.yaml -
查看ingress
[root@k8s-master ~]# kubectl get ingress NAME CLASS HOSTS ADDRESS PORTS AGE xujunkai-https <none> web.xujunkai.com 80, 443 35s -
這樣就可以訪問
https://web.xujunkai.com,需要提前本地配置hosts
-
-
之前部署的一個pod的python服務,通過service創建NodePod。這里通過ingress做負載。
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ingress
spec:
rules:
- host: web.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: websvc# 這里是service名稱
port:
number: 9527# 這里是創建pod容器端口
