ingress将来自集群外部的HTTP和HTTPS路由暴露给集群内的服务(k8s Service),流量路由规则由ingress资源来定义。简单的来说,ingress能够将不同域名+ 路径(如 aaa.com/api)映射到集群内的k8s service上。
将下面用一张图来理解ingress的作用;你可以将ingress controller理解为一个nginx反向代理,ingress理解为你为nginx配置的路由规则,而service就是你的upstream server提供的服务
如果这是你需要的,请继续往下看。
1、用户需已经创建好了自己的业务应用,并通过集群服务(k8s service)暴露服务。集群服务可参考文档:点击查看。
如无特殊需求,用户只需要创建一个Ingress Controller类型的应用即可
样例变量替换
Deployment和3个configMap的样例如下,请按以下要求调整Yaml内容
注意:containerPort固定为80 和 443,不要更改。
apiVersion: apps/v1 kind: Deployment metadata: labels: app: ingress-nginx annotations: component.version: '0.22.0' component.revision: '5' spec: selector: matchLabels: app: ingress-nginx template: metadata: labels: app: ingress-nginx annotations: scheduler.alpha.kubernetes.io/critical-pod: '' annotations: prometheus.io/port: "10254" prometheus.io/scrape: "true" spec: #tolerations: # - key: node-role.kubernetes.io/master # effect: NoSchedule affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: app operator: In values: - ingress-nginx topologyKey: "kubernetes.io/hostname" serviceAccountName: nginx-ingress-controller initContainers: - name: init-sysctl image: registry.cn-zhangjiakou.aliyuncs.com/acs/busybox:latest command: - /bin/sh - -c - | sysctl -w net.core.somaxconn=65535 sysctl -w net.ipv4.ip_local_port_range="1024 65535" sysctl -w fs.file-max=1048576 sysctl -w fs.inotify.max_user_instances=16384 sysctl -w fs.inotify.max_user_watches=524288 sysctl -w fs.inotify.max_queued_events=16384 securityContext: privileged: true containers: - name: nginx-ingress-controller image: registry.cn-zhangjiakou.aliyuncs.com/acs/aliyun-ingress-controller:v0.22.0.5-552e0db-aliyun resources: limits: cpu: 1 memory: 2Gi requests: cpu: 1 memory: 2Gi args: - /nginx-ingress-controller - --configmap=$(POD_NAMESPACE)/nginx-configuration-{{变量A}} - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services-{{变量A}} - --udp-services-configmap=$(POD_NAMESPACE)/udp-services-{{变量A}} - --annotations-prefix=nginx.ingress.kubernetes.io - --ingress-class={{IngressClass}} - --v=2 env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace ports: - name: http containerPort: 80 - name: https containerPort: 443 livenessProbe: failureThreshold: 3 httpGet: path: /healthz port: 10254 scheme: HTTP initialDelaySeconds: 10 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 1 readinessProbe: failureThreshold: 3 httpGet: path: /healthz port: 10254 scheme: HTTP periodSeconds: 10 successThreshold: 1 timeoutSeconds: 1 securityContext: capabilities: drop: - ALL add: - NET_BIND_SERVICE runAsUser: 33 volumeMounts: - name: localtime mountPath: /etc/localtime readOnly: true nodeSelector: beta.kubernetes.io/os: linux volumes: - name: localtime hostPath: path: /etc/localtime type: File
configMap_1
apiVersion: v1 data: proxy-body-size: 20m proxy-connect-timeout: '10' max-worker-connections: '65536' enable-underscores-in-headers: 'true' reuse-port: 'true' worker-cpu-affinity: auto server-tokens: 'false' ssl-redirect: 'false' allow-backend-server-header: 'true' ignore-invalid-headers: 'true' generate-request-id: 'true' use-forwarded-headers: 'true' kind: ConfigMap metadata: labels: app: ingress-nginx name: nginx-configuration-{{变量A}}
configMap_2
apiVersion: v1 data: null kind: ConfigMap metadata: labels: null name: tcp-services-{{变量A}}
configMap_3
apiVersion: v1 data: null kind: ConfigMap metadata: labels: null name: udp-services-{{变量A}}
1、nginx-configuration-xxx
聚石塔的Ingress Controller底层实现是nginx,nginx-configuration-xxx是设定nginx参数的地方,支持的参数可参考:
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/
2、tcp-services 和 udp-services功能参见:
https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/
发布成功后,此时Ingress Controller已经成功的部署起来了
为Ingress Controller(本质上是一个nginx)创建SLB接入,接入端口为80、443
监听端口和RS端口都填80;
注意:SLB监听端口可以改,但RS端口固定为80
负载均衡协议要选:TCP
监听端口和RS端口都填443
注意:SLB监听端口可以改,但RS端口固定为443
负载均衡协议要选:TCP
此时,我们已经部署起来了Ingress Conroller,同时为它配置了流量接入,下一步需要创建Ingress资源,指定路由规则
注意:Ingress资源(可以理解为HTTP路由规则)是创建在你的业务应用中的,而不是你刚才创建的Ingress Controller应用中。
路由规则是针对业务应用的,首先找到你需要路由的业务应用。如本例中的"lingfeng_docker_h5",暴露的端口为8080
-----BEGIN RSA PRIVATE KEY----- MII.... -----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE----- MIIF...... -----END CERTIFICATE-----
如果你需要HTTPS安全认证,请一定要在证书过期前更新你的证书配置
默认策略:
请暂时不要配置多个服务,多个服务是为后续灰度发布使用,我们正在加紧支持
到这我们已经创建好了路由规则,可以根据域名+路径路由到我们业务服务了,可以看下效果。
说明:由于本例中的证书是自签名的证书,因此浏览器提示不安全,实际使用中请使用CA签发的合规证书。
如果你需要自定义Cookie的一些策略,比如Cookie的超时时间,保存路径等,则可以通过高级规则来配置。
参数说明:
如果你需要类似下面这样效果的转发,请继续往下看
www.example.com -> /foo service1:4200 /bar service2:8080
实现URI扇出,请按以下步骤操作:
1、路径填写规则
路径是支持正则表达式的,如上面的/foo转发,可以配置为:/foo(/|$)(.*)
同理/bar可配置为/bar(/|$)(.*)
2、配置url重写规则
路径规则需配合url重写才能生效。url重写规则可在高级规则中指定,指定方式如下:
路径:即上面我们说的正则写法
高级规则:
url重写:名称固定为 nginx.ingress.kubernetes.io/rewrite-target ,值 /$2 表示从路径中的"/"隔开的第二段开始作为访问的URI片段。
以本例子举例来看:
www.example.com/foo
相当于访问 你的服务:服务端口/
www.example.com/foo/
相当于访问 你的服务:服务端口/
www.example.com/foo/new
相当于访问 你的服务:服务端口/new
www.example.com/foo/new/v2
相当于访问 你的服务:服务端口/new/v2
更详细的说明请参考nginx ingress controller的说明:
https://kubernetes.github.io/ingress-nginx/examples/rewrite/
其他更多详细参数,如client_max_body_size等,请参考文档:
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/