无法通过kubernetes中的nodeport访问docker桌面Windows的服务

时间:2019-03-20 13:56:41

标签: kubernetes kubectl

大家好,我正在尝试kubernetes,并且具有Windows的docker桌面随附的版本,我似乎无法访问类型为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给出的尝试也尝试在部署文件中显式提供端口,但仍然无法使其工作

2 个答案:

答案 0 :(得分:1)

  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的内容
$ 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

答案 1 :(得分:0)

您的部署文件没有供服务使用的Pod选择器!

如果要使用特定端口,请在服务部分本身中更新端口信息。否则,k8会分配一些随机端口,您可以通过运行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