标签:mamicode 入口 ons static lis rem lease name 情况下
目录
向 k8s 集群外部暴露服务的方式有三种:
nodePort
LoadBalancer
Ingress
一般在云环境下是 LB + Ingress Ctroller 方式对外提供服务,这样就可以在一个 LB 的情况下根据域名路由到对应后端的 Service,有点类似于 Nginx 反向代理,只不过在 k8s 集群中,这个反向代理是集群外部流量的统一入口。
Ingress 是 k8s 资源对象,用于对外暴露服务,该资源对象定义了不同主机名(域名)及 URL 和对应后端 Service(k8s Service)的绑定,根据不同的路径路由 http 和 https 流量。
而 Ingress Contoller 是一个 pod 服务,封装了一个 web 前端负载均衡器,同时在其基础上实现了动态感知 Ingress 并根据 Ingress 的定义动态生成 前端 web 负载均衡器的配置文件,比如 Nginx Ingress Controller 本质上就是一个 Nginx,只不过它能根据 Ingress 资源的定义动态生成 Nginx 的配置文件,然后动态 Reload。
独立运行的1个或1组pod,拥有7层代理能力或调度能力的应用程序 (DaemonSet)
Ingress Controller 的重大作用是将前端负载均衡器和 Kubernetes 完美地结合了起来,一方面在云、容器平台下方便配置的管理,另一方面实现了集群统一的流量入口,而不是像 nodePort 那样给集群打多个孔。
总的来说要使用 Ingress
得先部署 Ingress Controller 实体(相当于前端 Nginx),
然后再创建 Ingress (相当于 Nginx 配置的 k8s 资源体现),
Ingress Controller 部署好后会动态检测 Ingress 的创建情况生成相应配置。
Ingress Controller 的实现有很多种:
基于 Nginx
基于 HAProxy
基于 Traefik
基于 Envoy
基于 OpenResty 的 Kong Ingress Controller 等
更多 Controller 见:https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/
apiVersion:v1
kind:Service
metadata:
name: myapp-deploy
namspace: default
spec:
selector:
app: myapp
release: canary
ports:
- name: http
targetPort: 80
port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namspace: default
spec:
replicas:3
selector:
matchLables:
app: myapp
release: canary
template:
metadata:
labels:
app: myapp
release: canary
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v2
ports:
- name: https
containerPort: 80
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
nodePort: 30080
- name: https
port: 443
targetPort: 443
protocol: TCP
nodePort: 30443
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-myapp
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: ingress.evescn.com
http:
paths:
- path:
backend:
serviceName: myapp
servicePort: 80
kubernetes.io/ingress.class: "nginx":Nginx Ingress Controller 根据该注解自动发现 Ingress;
host: ingress.evescn.com:对外访问的域名;
serviceName: myapp:对外暴露的 Service 名称;
servicePort: 80:nginx service 监听的端口;
# kubectl describe ingress ingress-myapp
Name: ingress-myapp
Namespace: default
Address: 10.96.73.17
Default backend: default-http-backend:80 (<none>)
Rules:
Host Path Backends
---- ---- --------
ingress.evescn.com
myapp:80 (10.44.0.2:80,10.44.0.3:80,10.44.0.4:80)
Annotations:
kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"nginx"},"name":"ingress-myapp","namespace":"default"},"spec":{"rules":[{"host":"ingress.evescn.com","http":{"paths":[{"backend":{"serviceName":"myapp","servicePort":80},"path":null}]}}]}}
kubernetes.io/ingress.class: nginx
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CREATE 37m nginx-ingress-controller Ingress default/ingress-myapp
Normal UPDATE 36m nginx-ingress-controller Ingress default/ingress-myapp
Normal CREATE 17m nginx-ingress-controller Ingress default/ingress-myapp
Normal UPDATE 17m nginx-ingress-controller Ingress default/ingress-myapp
192.168.0.101 master1 master ingress.evescn.com
# for i in `seq 0 10`; do curl ingress.evescn.com:30080/hostname.html; done
myapp-deploy-67d58676c9-qnrrg
myapp-deploy-67d58676c9-5s7q8
myapp-deploy-67d58676c9-mkcjr
myapp-deploy-67d58676c9-qnrrg
myapp-deploy-67d58676c9-5s7q8
myapp-deploy-67d58676c9-mkcjr
myapp-deploy-67d58676c9-qnrrg
myapp-deploy-67d58676c9-5s7q8
myapp-deploy-67d58676c9-mkcjr
myapp-deploy-67d58676c9-qnrrg
myapp-deploy-67d58676c9-5s7q8
https://qhh.me/2019/08/12/%E4%BD%BF%E7%94%A8-Kubernetes-Ingress-%E5%AF%B9%E5%A4%96%E6%9A%B4%E9%9C%B2%E6%9C%8D%E5%8A%A1/
https://juejin.im/post/5db8da4b6fb9a0204520b310
https://mritd.me/2017/03/04/how-to-use-nginx-ingress/
https://zhangguanzhang.github.io/2018/10/06/IngressController/
k8s ingress及ingress controller
标签:mamicode 入口 ons static lis rem lease name 情况下
原文地址:https://www.cnblogs.com/python-gm/p/12484975.html