簡體   English   中英

如何在特定節點端口上公開 Kube.netes 服務?

[英]How to expose a Kubernetes service on a specific Nodeport?

我創建了一個具有以下 yaml 定義的 pod。

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: praveensripati/docker-demo:1.2
    ports:
    - containerPort: 3000

現在我公開創建服務的 pod。

kubectl expose pod myapp-pod --type=NodePort

容器上的端口 3000 暴露給節點上的端口 31728。 而且我能夠在端口 31728 上使用 curl 訪問該頁面。

kubectl get service myapp-pod
NAME        TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
myapp-pod   NodePort   10.107.254.254   <none>        3000:31728/TCP   5s

這次我不想公開服務的隨機端口,而是在端口 80 上。因此我使用 --port 將端口號指定為 80。 服務細節有點奇怪。 它說容器上的端口 80 暴露給節點上的端口 31316。 此外,我可以在隨機端口(在本例中為 31316)而不是端口 80 上使用 curl 訪問該頁面。

kubectl expose pod myapp-pod --type=NodePort --target-port=3000 --port=80

kubectl get service myapp-pod
NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
myapp-pod   NodePort   10.105.123.73   <none>        80:31316/TCP   12s

我無法在特定端口而不是隨機端口上公開服務。 我嘗試了幾種組合並閱讀了 k8s 文檔,但沒有成功。

如何在特定端口而不是隨機端口上公開服務?

您的問題是關於在特定端口上公開 NodePort 類型的服務。 為此,您需要在服務定義中的ports下指定nodePort字段。

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: myapp
  ports:
  - protocol: TCP
    port: 3000
    nodePort: 32321
  type: NodePort

請注意,它必須在配置中提供的給定范圍內。 默認為30000-32767 可以使用--service-node-port-range選項在kube-apiserver配置中指定此范圍。

如果您的集群沒有 LoadBalancer Provider,您可以在節點網絡接口的 IP 中指定externalIP

例如:

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:   
  type: ClusterIP
  externalIPs:
    - 125.100.99.101 # Node1-IP
    - 125.100.99.102 # Node2-IP
    - 192.168.55.112 # Node2-IP2
  selector:
    pod: nginx
  ports:
    - name: http
      port: 80      
    - name: https
      port: 443      

這將在指定節點上偵聽80443 ,並轉發到 nginx 服務。

當現有儀表板服務已存在時,將其刪除。

kubectl delete service kubernetes-dashboard -n kube-system

將儀表板部署公開為 NodePort。

kubectl expose deployment kubernetes-dashboard -n kube-system --type=NodePort

以上將分配一個隨機端口>= 30000。因此使用Patch命令將端口分配給一個已知的、未使用的和所需的端口>= 30000。

kubectl patch service kubernetes-dashboard --namespace=kube-system --type='json' --patch='[{"op": "replace", "path": "/spec/ports/0/nodePort", "value":30000}]'

注意: 切勿在未經身份驗證的情況下公開您的儀表板。

我會在這里嘗試回答您的問題。

此外,我可以在隨機端口(在本例中為 31316)而不是端口 80 上使用 curl 訪問該頁面。

-- Because, kubernetes exposed the port 31316 on the host (maps to the service) and hence it can be accessed on host:31316.

-- Service port is visible only within the kubernetes cluster. You can exec into a pod container and do a curl on servicename:service port instead of the NodePort.

請注意術語 - container port:端口容器偵聽。 Service port: kubernetes服務暴露在集群內部ip上並映射到容器端口的端口。 Nodeport:暴露在主機上並映射到kubernetes服務的端口。

我遇到了同樣的問題,我發現不修改文件的唯一方法是:

k expose --type=NodePort deployment nginx --port 80 --name nginx-ep-patch  --overrides '{ "apiVersion": "v1","spec":{"ports": [{"port":80,"protocol":"TCP","targetPort":80,"nodePort":30080}]}}'
service/nginx-ep-patch exposed

這樣我們把配置路徑上網,30080端口就暴露出來了:

$ k describe svc nginx-ep-patch
Name:                     nginx-ep-patch
Namespace:                default
Labels:                   app=nginx
Annotations:              <none>
Selector:                 app=nginx
Type:                     NodePort
IP:                       10.96.51.148
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30080/TCP
Endpoints:                10.244.0.6:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

我們可以在特定節點端口上公開 Kubernetes 服務。

端口值必須在 30000-32767 之間。

我們可以將服務暴露給以下服務類型的特定端口:

  1. 節點端口

  2. 負載均衡器

在下面找到示例 myservice.yaml 文件:

apiVersion: v1
kind: Service
metadata:
  name: app1
spec:
  type: NodePort/LoadBalancer
  ports:
  - name: "80"
    port: 80
    nodePort: 32062
    targetPort: 80
  selector:
    appone: app1
    app: test

注意:在上面的服務 yaml 文件中,我們可以指定服務類型NodePortLoadbalancer

對於需要使用 kubectl 命令的用戶,可以使用create nodeport命令創建一個指定端口的 NodePort 服務:

kubectl create nodeport NAME [--tcp=port:targetPort] [--dry-run=server|client|none]

例如:

kubectl create service nodeport myservice --node-port=31000 --tcp=3000:80

您可以查看 Kubectl 參考以獲取更多信息:

暫無
暫無

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

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