k8s 中的 ingress 使用细节

  • k8s中的ingress
    • 什么是ingress
    • Ingress 如何使用
    • ingress 使用细节
    • 参考
k8s中的ingress什么是ingressk8s 中使用 Service 为相同业务的 Pod 对象提供一个固定、统一的访问接口及负载均衡的能力,那么这些 Service 如何被外部的应用访问,其中常用的就是借助于 Ingress对象 。
Ingress 是 Kubernetes 中的一个资源对象,用来管理集群外部访问集群内部服务的方式 。
Ingress 对象由 Ingress Controller 和 Ingress 策略设置来共同完成 。
  • Ingress 策略:用来配置不同的转发规则;
  • Ingress Controller :Ingress 对象的域名解析都由 Ingress Controller 来完成,Ingress Controller 就是一个反向代理程序,它负责解析 Ingress 的反向代理规则,如果 Ingress 有增删改的变动,所有的 Ingress Controller 都会及时更新自己相应的转发规则,当 Ingress Controller 收到请求后就会根据这些规则将请求转发到对应的 Service 。

k8s 中的 ingress 使用细节

文章插图
Ingress 如何使用这里来个简单的 demo 来看下 Ingress 如何使用
1、部署ingress-controller
首先来部署下 Ingress Controller 这是使用的是 ingress-nginx
使用的 k8s 版本是 v1.19.9,所以这里选择的 ingress-nginx 是 v1.1.3
里面的镜像是需要FQ的,这里打包了镜像到 docker-hub 安装脚本
$ kubectl apply -f deploy.yaml2、部署应用
cat <<EOF >./go-web.yaml# deploymentapiVersion: apps/v1kind: Deploymentmetadata:creationTimestamp: nulllabels:app: go-webname: go-webnamespace: study-k8sspec:replicas: 5selector:matchLabels:app: go-webstrategy: {}template:metadata:creationTimestamp: nulllabels:app: go-webspec:containers:- image: liz2019/test-docker-go-hubname: go-app-containerresources: {}status: {}---# serviceapiVersion: v1kind: Servicemetadata:name: go-web-svclabels:run: go-web-svcspec:selector:app: go-webports:- protocol: TCPport: 8000targetPort: 8000name: go-web-http---# ingressapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: go-web-ingressannotations:kubernetes.io/ingress.class: nginxspec:rules:- host: www.go-web.comhttp:paths:- path: /indexpathType: Prefixbackend:service:name: go-web-svcport:number: 8000EOF在最下面放了 ingress 的配置,通过 path: /index 将 ingress 请求转发到 go-web-svc 的 service 。
?~ kubectl get ingress -n study-k8sNAMECLASSHOSTSADDRESSPORTSAGEgo-web-ingress<none>www.go-web.com192.168.56.112,192.168.56.1118028m访问
$ curl '192.168.56.111:80/index' \--header 'Host: www.go-web.com'<h1>hello world</h1><div>你好</div>%ingress 使用细节1、一个集群中可以有多个 Ingress Controller,在 Ingress 中可以指定使用哪一个Ingress Controller
2、多个 Ingress 规则可能出现竞争;
3、Ingress 可以为多个命名空间服务;
4、关于如何暴露 ingress 服务,让外面的服务访问到?
1、Ingress Controller 用 Deployment 方式部署,给它添加一个 Service,类型为 LoadBalancer,这样会自动生成一个 IP 地址,通过这个 IP 就能访问到了,并且一般这个 IP 是高可用的(前提是集群支持 LoadBalancer,通常云服务提供商才支持,自建集群一般没有);
2、使用 hostPort;