簡體   English   中英

如何從本地機器訪問 kubernetes 上的服務器?

[英]How to access a server on kubernetes from your local machine?

我的公司有一個 kubernetes 平台,在該平台上我有一個在命名空間my_namespace上運行的 pod ingress

apiVersion: v1
kind: Pod
metadata:
 name: ingress
 labels:
  app: ingress
spec:
 containers:
  - name: ingress
    image: docker:5000/mma/neurotec-ingress
    imagePullPolicy: Always

kubectl get pods -n my_namespace

NAME            READY   STATUS    RESTARTS   AGE
ingress         1/1     Running   1          11d

Pod 是一個監聽 8080 端口的服務器。

我還定義了一個將 pod 導出到外部的服務:

apiVersion: v1
kind: Service
metadata:
  name: ingress
  labels:
    app: ingress
spec:
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: ingress
  type: LoadBalancer

kubectl describe service -n my_namespace ingress

Name:              ingress
Namespace:         my_namespace
Labels:            app=ingress
Selector:          app=ingress
Type:              LoadBalancer
IP:                10.104.95.96
Port:              <unset>  8080/TCP
TargetPort:        8080/TCP
Endpoints:         10.16.1.232:8080

我現在想從本地計算機向服務器發送消息。 我要做的第一件事是確保它的 IP 地址是可訪問的。 但是,一個簡單的host command會返回錯誤:

host 10.16.1.232
Host 10.16.1.232 not found: 3(NXDOMAIN)

host ingress.my_namespace.nt // .nt is company's prefix
Host ingress.my_namespace not found: 3(NXDOMAIN)

如果我嘗試運行telepresence ,它也會返回一個錯誤:

Looks like there's a bug in our code. Sorry about that!

Traceback (most recent call last):
  File "/usr/bin/telepresence/telepresence/cli.py", line 135, in crash_reporting
    yield
  File "/usr/bin/telepresence/telepresence/main.py", line 65, in main
    remote_info = start_proxy(runner)
  File "/usr/bin/telepresence/telepresence/proxy/__init__.py", line 138, in start_proxy
    run_id=run_id,
  File "/usr/bin/telepresence/telepresence/proxy/remote.py", line 144, in get_remote_info
    runner, deployment_name, deployment_type, run_id=run_id
  File "/usr/bin/telepresence/telepresence/proxy/remote.py", line 91, in get_deployment_json
    return json.loads(output)["items"][0]
IndexError: list index out of range

問題:如何從本地機器訪問 kubernetes 上的服務器

我認為您應該使用服務type: LoadBalancer (不是ClusterIP

來自 kubernetes 文檔

  • ClusterIP:在集群內部的 IP 上公開服務。 選擇此值使服務只能從集群內訪問。 這是默認的服務類型。
  • LoadBalancer:使用雲提供商的負載均衡器向外部公開服務 自動創建外部負載均衡器路由到的 NodePort 和 ClusterIP 服務。

下面是我的示例負載均衡器服務文件。 我將端口 80 暴露給互聯網,將 map 端口 80 暴露給 pod,label run: my-web-portal端口 8000。

apiVersion: v1
kind: Service
metadata:
  name: my-web-portal-svc
  labels:
    run: my-web-portal-svc
spec:
  ports:
  - port: 80
    targetPort: 8000
    protocol: TCP
    name: port-80
  selector:
    run: my-web-portal
  type: LoadBalancer

這是我的部署 yml 文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-web-portal
spec:
  selector:
    matchLabels:
      run: my-web-portal
  replicas: 1

  template:
    metadata:
      labels:
        run: my-web-portal

    spec:
      containers:
      - name: backend
        image: xxxxxx-backend

要獲取端點,我運行命令

kubectl describe service my-web-portal-svc

命令將打印出用於連接到您的 pod 的端點

Type:                     LoadBalancer
IP:                       10.100.108.141
LoadBalancer Ingress:     xxxxxxxxxxxxxx.us-west-2.elb.amazonaws.com

如果您有任何問題,請發表評論。

暫無
暫無

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

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