Kubernetes(k8s)-Ingress案例

作者介绍:简历上没有一个精通的运维工程师。请点击上方的蓝色《运维小路》关注我,下面的思维导图也是预计更新的内容和当前进度(不定时更新)。

我们上一章介绍了Docker基本情况,目前在规模较大的容器集群基本都是Kubernetes,但是Kubernetes涉及的东西和概念确实是太多了,而且随着版本迭代功能在还增加,笔者有些功能也确实没用过,所以只能按照我自己的理解来讲解。

我们上一小节完成了ingress的安装,那么本小节就来来模拟真实的ingress应用。

ini 复制代码
#目前没有任何资源
[root@master01 ]# kubectl get ingress -A
No resources found

1.准备http应用

这里我们准备了一个虚假域名nginxtest.com(通过hosts方式实现),指定到这个域名的根目录访问到后端的my-service服务的80端口。

yaml 复制代码
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: nginxtest.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 80

2.准备https应用

这里由于我没有准备对应的证书,所以这个无法在测试环境演示,但是作为理解它是没问题的。这个htps应用和http的区别就在这里多添加了的https的证书,并且这个证书是通过secret方式注入。我们如果正式环境有正确证书的情况下,只需要通过把证书通过base64转码然后注入到secret即可,然后再这里引用这个证书即可。

yaml 复制代码
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  ingressClassName: nginx # 指定Ingress Class
  rules:
  - host: example.com # 注意这里也需要指定host,与tls中的hosts对应
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80
  tls:
  - hosts:
    - example.com
    secretName: example-tls-secret

3.访问验证

默认的ingess配置的svc,使用的LoadBalancer,但是由于我这个测试环境目前不具备LoadBalancer的能力,所以这里个地方的EXTERNAL-IP还处于pending状态。真正的生产环境这个地方一般都需要解决的。由于我这里只是为了演示,所以我这里就使用ClusterIP进行验证。

3.1集群内访问

这里是做的hosts到ClusterIP,这里只取了访问代码,正常返回成功200代码。

当然我这里是在集群内访问,如果要在集群外必须先把LoadBalancer改成NodePort,并且还需要externalTrafficPolicy改成Cluster(这个信息我们后面可以单独来讲)。

3.2集群外访问

由于没有LB,无法使用80端口。所以这里用的NodeIP(hosts域名)+NodePort访问。

4.ingress内部配置

由于我们采用的这个ingress是nginx的,所以实际上ingress规则在内部就是nginx的一个sverver配置,然后代理到后端的我们配置的服务。

5.Ingress日志

5.1删除ingress日志

php 复制代码
controller.go:188] "Configuration changes detected, backend reload required"
controller.go:205] "Backend successfully reloaded"
event.go:285] Event(v1.ObjectReference{Kind:"Pod", Namespace:"ingress-nginx", Name:"ingress-nginx-controller-858c5d9bd7-b2l5r", UID:"94daf972-e767-4075-aa0f-7afe32a1b7e2", APIVersion:"v1", ResourceVersion:"62711", FieldPath:""}): type: 'Normal' reason: 'RELOAD' NGINX reload triggered due to a change in configuration

5.2增加ingress日志

css 复制代码
controller.go:1163] Service "default/my-service" does not have any active Endpoint.
admission.go:149] processed ingress via admission controller {testedIngressLength:1 testedIngressTime:0.024s renderingIngressLength:1 renderingIngressTime:0s admissionTime:18.1kBs testedConfigurationSize:0.024}
main.go:100] "successfully validated configuration, accepting" ingress="default/nginx-ingress"
store.go:434] "Found valid IngressClass" ingress="default/nginx-ingress" ingressclass="nginx"
controller.go:1163] Service "default/my-service" does not have any active Endpoint.
controller.go:188] "Configuration changes detected, backend reload required"
event.go:285] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"nginx-ingress", UID:"33cc845b-1c3b-4fa5-8128-08a2414f7347", APIVersion:"networking.k8s.io/v1", ResourceVersion:"790353", FieldPath:""}): type: 'Normal' reason: 'Sync' Scheduled for sync
controller.go:205] "Backend successfully reloaded"
event.go:285] Event(v1.ObjectReference{Kind:"Pod", Namespace:"ingress-nginx", Name:"ingress-nginx-controller-858c5d9bd7-b2l5r", UID:"94daf972-e767-4075-aa0f-7afe32a1b7e2", APIVersion:"v1", ResourceVersion:"62711", FieldPath:""}): type: 'Normal' reason: 'RELOAD' NGINX reload triggered due to a change in configuration
status.go:300] "updating Ingress status" namespace="default" ingress="nginx-ingress" currentValue=[] newValue=[{IP:10.100.179.234 Hostname: Ports:[]}]
event.go:285] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"nginx-ingress", UID:"33cc845b-1c3b-4fa5-8128-08a2414f7347", APIVersion:"networking.k8s.io/v1", ResourceVersion:"790360", FieldPath:""}): type: 'Normal' reason: 'Sync' Scheduled for sync
controller.go:1163] Service "default/my-service" does not have any active Endpoint.

从日志上来,无论我们删除还是增加操作,ingress-nginx-controller都能及时感知到,并动态增加,删除或者更改nginx的配置来实现我们的需求。另外如果你的所有流量都通过ingress来实现,那么ingress的压力会很大,需要合理考虑副本数量,以及日志大小的问题。当前版本ingress的日志是通过标准输出来实现的,标准输出的日志依赖Docker自动轮询,如果未配置,则这个日志可能会很大(生产环境见过最大的3T)。下图就是使用了ingress的流量走向。

运维小路

一个不会开发的运维!一个要学开发的运维!一个学不会开发的运维!欢迎大家骚扰的运维!

关注微信公众号《运维小路》获取更多内容。

相关推荐
厦门辰迈智慧科技有限公司1 小时前
城市排水管网流量监测系统解决方案
运维·服务器
我没有开挂1 小时前
旧 docker 版本通过 nvkind 搭建虚拟多节点 gpu 集群的坑
运维·docker·容器
qq_339282231 小时前
centos中libc.so.6No such file的解决方式
linux·运维·centos
leoufung1 小时前
ECPF 简介
linux·网络·kernel
小鸡,啄米2 小时前
centos9安装docker 配置docker代理
运维·docker·容器
水银嘻嘻2 小时前
12 web 自动化之基于关键字+数据驱动-反射自动化框架搭建
运维·前端·自动化
oceanweave2 小时前
【K8S学习之生命周期钩子】详细了解 postStart 和 preStop 生命周期钩子
学习·kubernetes
在肯德基吃麻辣烫2 小时前
Netdata在Ubuntu环境下的安装与配置:构建实时系统监控与性能分析平台
linux·运维·ubuntu
不念霉运3 小时前
Gitee DevOps:中国企业数字化转型的“本土化加速器“
运维·gitee·团队开发·代码规范·devops·代码复审
安迪小宝3 小时前
6 任务路由与负载均衡
运维·python·celery