简介
k8s中有多种ingress controller,在这儿介绍ingress-nginx controller的安装和使用
- ingress-nginx controller
- Apache APISIX Ingress 控制器
- HAProxy Ingress Controller
- Istio Ingress Controller
参考资料
安装环境
系统:mac pro
k8s:kind(kubernetes in docker),kind安装过程见https://kind.sigs.k8s.io/docs/user/quick-start/
安装过程
- 安装ingress-nginx controller
yaml文件地址:https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/cloud/deploy.yaml - 安装web服务,这儿以一个nginx集群为例
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ingress-nginx
name: "nginx"
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
name: "nginx"
spec:
containers:
- image: "nginx"
name: "nginx"
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
namespace: ingress-nginx
name: "nginx-service"
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
protocol: TCP
- 安装ingress,将web service配置、转发规则、域名等信息加载到ingress-nginx controller中的nginx中
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: ingress-nginx
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx # 这一步需要指定ingressClassName,不然信息无法同步到nginx配置中,也会报 ingress does not contain a valid Ingress Class 错误
# defaultBackend:
# service:
# name: nginx-service
# port:
# number: 80
rules:
- http:
paths:
- path: /test
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
- 暴露ingress-nginx controller到本机中,然后通过http://localhost:8080/test 即可访问web服务(这儿是第二步安装的nginx集群)
kubectl port-forward --namespace=ingress-nginx service/ingress-nginx-controller 8080:80
如果报 failed calling webhook validate.nginx.ingress.kubernetes.io 错误,可以修改ingress-nginx controller 部署文件的webhooks failurePolicy 为Ignore
webhooks:
- name: validate.nginx.ingress.kubernetes.io
matchPolicy: Equivalent
rules:
- apiGroups:
- networking.k8s.io
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- ingresses
failurePolicy: Ignore
sideEffects: None
admissionReviewVersions:
- v1
clientConfig:
service:
namespace: ingress-nginx
name: ingress-nginx-controller-admission
path: /networking/v1/ingresses