Docker桌面(Mac)-如何使用curl通过pod ip访问pod

时间:2020-04-15 19:03:02

标签: macos kubernetes

我正在Mac上使用docker桌面来学习Kubernetes。我正在创建这样的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-k8s-hands-on
  annotations:
    kubernetes.io/change-cause: "Changing image"
  labels:
    app: backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: nginx
        image: k8s-hands-on
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: backend-k8s-hands-on
  labels:
    app: backend 
spec:
  selector:
    app: backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9090

然后,我创建另一个将作为前端的pod(一个临时的pod),以便访问我使用此命令刚创建的服务:

kubectl run --rm -ti --generator=run-pod/v1 podtest3 --image=nginx:alpine -- sh

所以我在Pod内运行nginx。然后,我用kubectl get pods -o wide

检查广告连播的IP
NAME                                    READY   STATUS    RESTARTS   AGE   IP           NODE             NOMINATED NODE   READINESS GATES
backend-k8s-hands-on-6489c76ff4-4q9th   1/1     Running   0          60m   10.1.0.153   docker-desktop   <none>           <none>
backend-k8s-hands-on-6489c76ff4-6tvns   1/1     Running   0          60m   10.1.0.151   docker-desktop   <none>           <none>
backend-k8s-hands-on-6489c76ff4-jrc7k   1/1     Running   0          60m   10.1.0.152   docker-desktop   <none>           <none>
podtest3                                1/1     Running   0          39m   10.1.0.155   docker-desktop   <none>           <none>

最后,当我在浏览器中输入IP 10.1.0.155时,它告诉我Safari无法找到该页面。

1 个答案:

答案 0 :(得分:3)

您可以先创建一个Service并转发到该服务:

kubectl -n <namespace> port-forward svc/<svc-name> <localhost-port>:<pod-port> 

另一种选择是直接将端口转发到您的Pod:

kubectl -n <namespace> port-forward pod/<podname> <localhost-port>:<pod-port> 

然后:

curl localhost:<localhost-port>

这是更直接的示例:

✗ kubectl run nginx-example --image=nginx --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-example created
✗ kubectl port-forward pod/nginx-example 8080:80
Error from server (NotFound): pods "nginx-example" not found
✗ kubectl get po
NAME                                  READY   STATUS    RESTARTS   AGE
nginx-example-6cdd6bf4c5-km8mh        1/1     Running   0          55s
✗ kubectl port-forward pod/nginx-example-6cdd6bf4c5-km8mh  8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
✗ curl localhost:8080
Handling connection for 8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>