无法通过Kubernetes的NodePort访问Docker桌面窗口的服务



大家好,我正在尝试kubernetes,并且具有docker桌面随附的版本,我似乎无法访问具有type nodePort的服务。以下是相关信息

docker version:
Client: Docker Engine - Community
 Version:           18.09.2
 API version:       1.39
 Built:             Sun Feb 10 04:12:31 2019
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.2
  API version:      1.39 (minimum version 1.12)
  Built:            Sun Feb 10 04:13:06 2019
  OS/Arch:          linux/amd64
  Experimental:     false

kubernetes version:
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:38:32Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:25:46Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

我的部署文件:( deployment.yml)

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
  name: tomcat-app
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: tomcat-app
    spec:
      containers:
        - name: tomcat-app
          image: tomcatapp:v1.0.0
          ports:
            - containerPort: 80

---
kind: Service
apiVersion: v1
metadata:
  name: tomcatappservice
spec:
  type: NodePort
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 80
  selector:
    app: tomcat-app

运行它
kubectl create -f deployment.yml

似乎无法访问Localhost的Tomcat服务器:由Kubernetes给出,也尝试在部署文件中明确给出一个端口,但仍然无法使其工作

  1. 您必须有两个标签nodePort: 30007type: NodePort您的服务yaml应该是这样:
kind: Service
apiVersion: v1
metadata:
  name: tomcatappservice
spec:
  type: NodePort
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 80
      nodePort: 30007
  selector:
    app: tomcat-app
  1. 通过kubectl get services对其进行测试,如果成功,您会看到诸如80:30007/TCP
  2. 之类的东西
$ kubectl get services
NAME           TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
hello          NodePort       10.96.92.53    <none>        80:30007/TCP   3d3h
  1. 访问您的服务。
$ kubectl describe nodes | grep InternalIP -n1
26-Addresses:
27:  InternalIP:  172.17.0.3
28-  Hostname:    kind2-control-plane

$ kubectl get nodes --output json
{
    "apiVersion": "v1",
    "items": [
            ...
            "status": {
                "addresses": [
                    {
                        "address": "172.17.0.3",
                        "type": "InternalIP"
                    },
                    {
                        "address": "kind2-control-plane",
                        "type": "Hostname"
                    }
                ],
    ...
}
$ kubectl get nodes --output jsonpath='{.items[*].status.addresses}'
[map[address:172.17.0.3 type:InternalIP] map[address:kind2-control-plane type:Hostname]]

在上面的示例中,URL为http://127.17.0.3:30007。

您的部署文件没有任何pod的选择器!

如果您想要特定的端口,请在"服务"部分本身中更新端口信息。否则,K8S将分配您通过运行kubectl get svc

获得的一些随机端口

尝试此文件。访问端口30080

的应用程序
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tomcat-app
  name: tomcat-app
spec:
  replicas: 5
  selector:
    matchLabels:
      app: tomcat-app
  template:
    metadata:
      labels:
        app: tomcat-app
    spec:
      containers:
      - image: tomcatapp:v1.0.0
        name: tomcat-app
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: tomcat-app
  name: tomcat-app
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30080
  selector:
    app: tomcat-app
  type: NodePort

相关内容

最新更新