江
江小南
V1
2022/10/23阅读:84主题:默认主题
【CKA、CKS篇】CKA真题解析——创建 Ingress
真题解析
题目:
如下创建一个新的nginx Ingress资源:
名称:ping
Namespace: ing-internal
使用服务端口 5678 在路径 /hello 上公开服务 hello
可以使用以下命令检查服务器 hello 的可用性,该命令返回 hello ;
curl -kL <INTERNAL_IP> /hello
考察点:
Ingress 的创建
解析:
-
根据题意可以得到以下信息:需要新创建的Ingress的名称ping,所属名称空间ing-internal,服务端口5678以及通过路径/hello公开服务hello。
-
给出了检查方法。
帮助文档:
https://kubernetes.io/zh-cn/docs/concepts/services-networking/ingress/

实际操作:
在实际的考试中,可以将官方给的示例拷贝过来,修改相应的参数即可。
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ping
namespace: ing-internal
annotations:
kubernetes.io/ingress.class: "nginx" # 这一行需要自己添加
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
# ingressClassName: nginx-example # 1.24考试这一行注释掉
rules:
- http:
paths:
- path: /hello
pathType: Prefix
backend:
service:
name: hello
port:
number: 5678
candidate@node01:~$ kubectl apply -f ingress.yaml
ingress.networking.k8s.io/ping created
candidate@node01:~$
检查验证:
candidate@node01:~$ kubectl get ingress -n ing-internal
NAME CLASS HOSTS ADDRESS PORTS AGE
ping <none> * 10.109.97.166 80 68s
candidate@node01:~$
candidate@node01:~$ curl 10.109.97.166/hello
Hello World ^_^
candidate@node01:~$
说明:题目的本意是要创建Ingress,说明Service以及Pod都已经创建过了。
candidate@node01:~$ kubectl get svc,pod -n ing-internal
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello ClusterIP 10.96.10.101 <none> 5678/TCP 40d
NAME READY STATUS RESTARTS AGE
pod/hello-85869f9579-fgs9h 1/1 Running 6 (58m ago) 40d
candidate@node01:~$
我们按照要求创建Ingress即可。
可以看到Service本身是ClusterIP的类型,实际考试中如果创建了Ingress发现没有IP。比如这样:
candidate@node01:~$ kubectl get ingress -n ing-internal
NAME CLASS HOSTS ADDRESS PORTS AGE
ping <none> * 80 21s
有可能是Service是NodePort类型,需要修改为ClusterIP类型。
kubectl edit svc hello -n ing-internal
apiVersion: v1
kind: Service
...
selector:
app: hello
sessionAffinity: None
type: NodePort
将NodePort修改为ClusterIP。
至此本道题目完成。
下一篇讲解Ingress扩展知识。
作者介绍
江
江小南
V1