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.解析
3.答案
创建service,service-nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: service-nginx
spec:
type: NodePort
selector:
# 先查看nginx pod的标签是否一致
run: pod-nginx
ports:
- port: 80
targetPort: 80
nodePort: 30007
kubectl apply -f service-nginx.yaml
再创建busybox pod,用来查看service以及pod的dns信息
kubectl run busybox --image=busybox --command sleep 1d
执行nslookup命令,先查看pod的dns,通过以下命令获取pod的internal ip
# 查看pod ip
kubectl get po pod-nginx -owide
# 查看pod dns
kubectl exec -ti busybox -- nslookup <pod_ip>
查看service的dns
kubectl exec -ti busybox -- nslookup service-nginx.default