导语:一直没使用过ingress的ip。即类似nginx里面localhost的功能,甚至一度怀疑ingress只能转发域名不能转发本机ip的请求。记录一下,避免后面忘了。

创建一个ingressClassName
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  labels:
    app.kubernetes.io/component: controller
  name: nginx-example
  namespace: deepwise
  annotations:
    ingressclass.kubernetes.io/is-default-class: "true"
spec:
  controller: k8s.io/ingress-nginx

官方文档解释

创建ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  namespace: deepwise
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx-example
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-headless
            port:
              number: 800

查看svc和ingress

查看ingress对应pod的日志

通过hostport访问时的效果

通过ingress转发的效果

官方文档:

https://kubernetes.io/docs/concepts/services-networking/ingress/#the-ingress-resource

https://kubernetes.io/zh/docs/concepts/services-networking/_print/#pg-199bcc92443dbc9bed44819467d7eb75

安全参考:

https://kubernetes.github.io/ingress-nginx/examples/psp/

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐