为什么
kubernetes 对外暴露服务得时候,我们要使用Ingress
ingress是什么,ingressController 又是什么
官网地址
Ingress 是对集群中服务的外部访问进行管理的 API 对象,典型的访问方式是 HTTP。Ingress 可以提供负载均衡、SSL 终结和基于名称的虚拟托管。
Ingress 公开了从集群外部到集群内 services 的HTTP和HTTPS路由。 流量路由由 Ingress 资源上定义的规则控制。
必须具有 ingress Controller
才能满足 Ingress 的要求。仅创建 Ingress 资源无效。
ingress Controller有很多种例如: AKS Application Gateway
, Ambassador API Gateway
, AppsCode Inc
, AWS ALB Ingress Controller
, Contour
, Istio
,Nginx
,最常用的实现是nginx
。后面我们使用nginx进行安装
nginx Ingress Controller 会动态感知集群中的Ingress的规则变化,然后读取,动态生成Nginx的配置文件,最后注入到运行nginx的pod的中,然后会自动reload,配置生效。用kubernetes Ingress 是由于它是7层调度,可以直接卸载https会话,代理的后端的pod可以直接使用明文的http协议。而Service NodePort的类型,是4层得调度,做不到这点
github地址介绍里面有个Get started
,我们点击进入了 介绍页面
下载 安装的yaml
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml
添加主机网络模式
修改下载的mandatory.yaml,找到kind: Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ingress-controller
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
# 实例数量
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
template:
metadata:
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
spec:
# wait up to five minutes for the drain of connections
terminationGracePeriodSeconds: 300
serviceAccountName: nginx-ingress-serviceaccount
# 添加hostNetwork: true ,开启主机网络模式,暴露nginx服务端口80
hostNetwork: true
nodeSelector:
kubernetes.io/os: linux
containers:
- name: nginx-ingress-controller
image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
- --publish-service=$(POD_NAMESPACE)/ingress-nginx
- --annotations-prefix=nginx.ingress.kubernetes.io
securityContext:
allowPrivilegeEscalation: true
capabilities:
drop:
- ALL
add:
安装
kubectl apply -f mandatory.yaml
部署Ingress
创建一个资源配置文件 ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
# 指定ingress controller 类型
kubernetes.io/ingress.class: "nginx"
# 指定rules的path 可以使用正则表达式
nginx.ingress.kubernetes.io/use-regex: "true"
# 连接超时时间,默认5s
nginx.ingress.kubernetes.io/proxy-connect-timeout: "600"
# 后端服务器回传数据超时时间,默认60s
nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
# 后端服务器响应超时时间,默认60s
nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
# 客户端上传最大文件的大小,默认20m
nginx.ingress.kubernetes.io/proxy-body-size: "100m"
# url 重写
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
# 路由规则
rules:
# 这里只能使用域名,多个域名配置多个配置
- host: test1.delightful.vip
http:
paths:
- path: /testpath
backend:
# 后台部署的Service Name
serviceName: test
# 后台部署的Service Port
servicePort: 80
# 这里只能使用域名,多个域名配置多个配置
- host: test2.delightful.vip
http:
paths:
- path: /testpath
backend:
# 后台部署的Service Name
serviceName: test
# 后台部署的Service Port
servicePort: 80
查看一下部署情况
kubectl get ingress
也可以使用dashboard 查看