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
浏览器测试,访问成功:
-----------日常记录---------------