模拟题目:

设置配置环境: [candidate@node-1] $ kubectl config use-context k8s

Task

如下创建一个新的nginx Ingress资源: 名称: ping Namespace: ing-internal 使用服务端口 5678在路径 /hello 上公开服务 hello 可以使用以下命令检查服务 hello的可用性,该命令应返回 hello: curl -kL INTERNAL_IP/hello


参考:

概念 --> 服务、负载均衡和联网 --> Ingress https://kubernetes.io/zh-cn/docs/concepts/services-networking/ingress/#the-ingress-resource https://kubernetes.io/zh-cn/docs/concepts/services-networking/ingress/#default-ingress-class


解答:
方法1 (推荐):

考试的时候务必记住切换集群, 注意集群名称 kubectl config use-context k8s

vim ingress.yaml
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  labels:
    app.kubernetes.io/component: controller
  name: nginx-example #考试时没有ingressClassName手动创建一个
  annotations:
    ingressclass.kubernetes.io/is-default-class: "true"
spec:
  controller: k8s.io/ingress-nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ping
  namespace: ing-internal
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx-example
  rules:
  - http:
      paths:
      - path: /hello
        pathType: Prefix
        backend:
          service:
            name: hello
            port:
              number: 5678
kubectl apply -f ingress.yaml

方法2 (暂不推荐,考试时有问题):
kubectl create ingress ping --rule=/hello=hello:5678 --class=nginx --annotation=nginx.ingress.kubernetes.io/rewrite-target=/ -n ing-internal
检查:
kubectl get ingress -n ing-internal
curl ingressIP/hello

file