kubernetes版本:1.17.3(快速安裝:https://blog.csdn.net/w50feng/article/details/106470888)
ingress定義
Ingress 公開了從集羣外部到集羣內 services 的HTTP和HTTPS路由。 流量路由由 Ingress 資源上定義的規則控制。具體詳解見官方文檔:https://v1-17.docs.kubernetes.io/zh/docs/concepts/services-networking/ingress/
keepalived介紹:
Keepalived提供了用於負載平衡和高可用性的框架。負載平衡框架依賴於廣爲人知的Linux虛擬服務器(IPVS)內核模塊,該模塊提供第4層負載平衡。Keepalived實施一組運行狀況檢查器,以根據其運行狀況動態,自適應地維護和管理負載平衡的服務器池。虛擬冗餘路由協議(VRRP)實現了高可用性。VRRP是路由器故障轉移的基礎磚。此外,keepalived還實現了一組VRRP有限狀態機的掛鉤,從而提供了低級和高速協議交互。每個Keepalived框架可以獨立使用,也可以一起使用以提供彈性基礎架構。附官方文檔:https://www.keepalived.org/doc/index.html
ingress部署:
給需要安裝ingress的node打標籤,例爲node01和node02
kubectl label nodes node01 ingress=true
kubectl label nodes node02 ingress=true
查詢label:kubectl get node --show-labels
ingress的yaml文件下載:https://github.com/kubernetes/ingress-nginx/blob/nginx-0.30.0/deploy/static/mandatory.yaml
修改文件:
kind: Deployment改成kind: DaemonSet;
註釋#spec.replicas: 1;
spec.spec內增加配置:
hostNetwork: true #使用hostNetwork:true配置網絡,pod中運行的應用程序可以直接看到宿主主機的網絡接口,宿主主機所在的局域網上所有網絡接口都可以訪問到該應用程序
dnsPolicy: ClusterFirstWithHostNet #該設置是使POD使用的k8s的dns,如果不加上dnsPolicy: ClusterFirstWithHostNet ,pod默認使用所在宿主主機使用的DNS,這樣也會導致容器內不能通過service name 訪問k8s集羣中其他POD
nodeSelector:
ingress: 'true' #node標籤
運行文件:kubectl apply -f mandatory1.yaml
查看pod啓動狀態:kubectl get pod -n ingress-nginx
、
dashboard查看部署node爲node01和node02
keepalived安裝:
node01和node02使用yum安裝:yum install keepalived -y
修改配置文件,node01爲MASTER,node02爲BACKUP
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id k8singress #標識信息,隨便取
}
vrrp_instance VI_1 {
state MASTER #角色,node01爲MASTER,node02爲BACKUP
interface eth0 #vip綁定的網絡端口
virtual_router_id 66 #讓master和backup在同一個虛擬路由裏,id號必須相同
priority 120 #優先級,誰的優先級高誰就是master;node01爲120,node02爲110
advert_int 1 #心跳間隔時間
authentication {
auth_type PASS #認證
auth_pass 1111 #密碼
}
virtual_ipaddress {
192.168.2.11 #虛擬ip
}
}
啓動keepalived:systemctl start keepalived
自啓動:systemctl enable keepalived
查詢VIP:node01上可見VIP,node02沒有
node01:
node02:
驗證VIP飄移,斷開node01,vip飄移到node02,成功驗證
注:可添加腳本監控ingress狀態,後續更新。。。
驗證ingree+vip:
vim nginx.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-test
namespace: ingress-nginx
labels:
name: nginx-test
spec:
replicas: 1
template:
metadata:
labels:
name: nginx-test
spec:
containers:
- name: nginx-test
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-test
namespace: ingress-nginx
labels:
name: nginx-test
spec:
type: ClusterIP
ports:
- port: 80
protocol: TCP
targetPort: 80
name: http
selector:
name: nginx-test
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-test
namespace: ingress-nginx
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: test.nginx.com
http:
paths:
- path:
backend:
serviceName: nginx-test
servicePort: 80
啓動應用:kubectl apply -f nginx.yaml
查看pod狀態
更改客戶端的hosts文件(windows路徑:C:\Windows\System32\drivers\etc):
192.168.2.11 test.nginx.com
瀏覽器測試,訪問成功: