簡體   English   中英

mac m1 上 microk8s 上的端口轉發

[英]port forwarding on microk8s on mac m1

我是 microk8s 的新生,我正在嘗試通過部署一個簡單的 apache2 來查看在我的 Mac M1 上運行的東西:

◼ ~ $ microk8s kubectl run apache --image=ubuntu/apache2:2.4-22.04_beta --port=80
pod/apache created
◼ ~ $ microk8s kubectl get pods
NAME                                   READY   STATUS             RESTARTS       AGE
apache                                 1/1     Running            0              5m37s
◼ ~ $ microk8s kubectl port-forward pod/apache 3000:80
Forwarding from 127.0.0.1:3000 -> 80

但:

◼ ~ $ curl http://localhost:3000
curl: (7) Failed to connect to localhost port 3000 after 5 ms: Connection refused

我也嘗試過使用服務:

◼ ~ $ microk8s kubectl expose pod apache --type=NodePort  --port=4000 --target-port=80
service/apache exposed
◼ ~ $ curl http://localhost:4000
curl: (7) Failed to connect to localhost port 4000 after 3 ms: Connection refused

我想我做錯了什么?

Microk8 的行為與 kuberentes 相同。 因此,最好使用NodePort創建服務。 這會暴露您的 apache。

apiVersion: v1
kind: Service
metadata:
  name: my-apache
spec:
  type: NodePort
  selector:
    app: apache
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30004

根據您的要求更改選擇器。 有關創建 NodePort 服務的更多詳細信息,請參閱此官方文檔。您也可以使用ingress 但在您的情況下,僅用於測試,您可以使用NodePort go

我認為您測試它的最簡單方法是添加: externalIPs到您的服務。

kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  externalIPs:
    - 192.168.56.100 #your cluster IP 

編碼愉快!

出於某種原因,我還沒有弄清楚,如果我通過多通道打開 shell 在 VM 內進行端口轉發,它確實有效。 接下來,您只需指向虛擬機的 IP:

在 VM 的 shell 中:

ubuntu@microk8s-vm:~$ sudo microk8s kubectl port-forward service/hellopg 8080:8080 --address="0.0.0.0"
Forwarding from 0.0.0.0:8080 -> 8080
Handling connection for 8080

ubuntu@microk8s-vm:~$ ifconfig enp0s1
enp0s1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.64.2  netmask 255.255.255.0  broadcast 192.168.64.255
        inet6 fde3:1a04:ba31:1209:5054:ff:fea9:9cf4  prefixlen 64  scopeid 0x0<global>

來自主持人:

 curl http://192.168.64.2:8080/hello                                                                                                                                                                                                                   7
{"status": "how you doing?", "env_var":"¡hola mundo!"}

有用。 我想通過microk8s的命令在機器內沒有正確執行? 如果有人可以解釋這個我會更新問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM