题目主干

Task
如下创建一个新的nginx Ingress资源:
名称: ping
Namespace: ing-internal
使用服务端口 5678在路径 /hello 上公开服务 hello

可以使用以下命令检查服务 hello的可用性,该命令应返回 hello:
curl -kL /hello

参考说明

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

题目解答

创建文件ingress.yaml

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: ping
  5. namespace: ing-internal
  6. annotations:
  7. nginx.ingress.kubernetes.io/rewrite-target: /
  8. spec:
  9. ingressClassName: nginx
  10. rules:
  11. - http:
  12. paths:
  13. - path: /hello
  14. pathType: Prefix
  15. backend:
  16. service:
  17. name: hello
  18. port:
  19. number: 5678

应用生效

student@master01:~$ kubectl -n  ing-internal  get svc 
NAME    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
hello   ClusterIP   10.100.116.157   <none>        5678/TCP   71d
student@master01:~$ 
student@master01:~$ kubectl apply -f ingress.yaml 
ingress.networking.k8s.io/ping created
student@master01:~$ 
student@master01:~$ kubectl -n  ing-internal  get ingress
NAME   CLASS   HOSTS   ADDRESS         PORTS   AGE
ping   nginx   *       10.110.175.39   80      55s
student@master01:~$ 
student@master01:~$ kubectl -n  ing-internal  get ingress
NAME   CLASS   HOSTS   ADDRESS         PORTS   AGE
ping   nginx   *       10.110.175.39   80      55s
student@master01:~$ kubectl describe ingress ping
Error from server (NotFound): ingresses.networking.k8s.io "ping" not found
student@master01:~$ kubectl -n ing-internal  describe ingress ping
Name:             ping
Labels:           <none>
Namespace:        ing-internal
Address:          10.110.175.39
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *           
              /hello   hello:5678 (10.244.2.36:80)
Annotations:  nginx.ingress.kubernetes.io/rewrite-target: /
Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  Sync    13m (x2 over 13m)  nginx-ingress-controller  Scheduled for sync
  Normal  Sync    13m (x2 over 13m)  nginx-ingress-controller  Scheduled for sync
student@master01:~$
student@master01:~$ curl 10.110.175.39/hello
Hello World ^_^
student@master01:~$