1.题目

There is pod name pod-nginx, create a service name service-nginx, use nodePort to expose the pod. Then create a pod use image busybox to nslookup the pod pod-nginx and service service-nginx.

2.解析

本题目考测service,dns。

3.答案

  1. 创建service,service-nginx.yaml

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: service-nginx
    5. spec:
    6. type: NodePort
    7. selector:
    8. # 先查看nginx pod的标签是否一致
    9. run: pod-nginx
    10. ports:
    11. - port: 80
    12. targetPort: 80
    13. nodePort: 30007
    1. kubectl apply -f service-nginx.yaml
  2. 再创建busybox pod,用来查看service以及pod的dns信息

    1. kubectl run busybox --image=busybox --command sleep 1d
  3. 执行nslookup命令,先查看pod的dns,通过以下命令获取pod的internal ip

    1. # 查看pod ip
    2. kubectl get po pod-nginx -owide
    3. # 查看pod dns
    4. kubectl exec -ti busybox -- nslookup <pod_ip>
  4. 查看service的dns

    1. kubectl exec -ti busybox -- nslookup service-nginx.default