在Mac上的docker上通过单一服务进入

时间:2019-05-09 17:32:13

标签: kubernetes kubernetes-ingress

这里完全是K8的新产品,我正在尝试将Ingress部署/连接到docker for local(适用于Mac的docker)上的示例单一服务,并能够通过它运行该服务

1-示例服务看起来像这样(my_service.js):

const express = require('express')
let app = express()

app.get('/', (req, res) => {
    res.send('Hello service')
})

const PORT = process.env.PORT || 9000
app.listen(
    PORT,
    () => console.log('Listening')
    )

然后我创建映像(带有docker文件):

docker build . -t sample-service-image

2-现在从K8s的角度来看: 这是部署文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-service
  labels:
    app: sample-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sample-service
  template:
    metadata:
      labels:
        app: sample-service
    spec:
      containers:
      - name: sample-service
        image: sample-service-image
        imagePullPolicy: Never
        ports:
        - containerPort: 9000
kubectl apply -f deployment.yaml

并将其公开为服务:

kubectl expose deploy sample-service --type=NodePort

3-现在来自Ingress(ingress-sample.yaml):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-sample
spec:
  backend:
    serviceName: sample-service
    servicePort: 80
kubectl create -f ingress-sample.yaml

[UPDATE]我从here部署了一个入口控制器:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml

这就是我得到的

$kubectl get ingress
NAME             HOSTS   ADDRESS     PORTS   AGE
ingress-sample   *       localhost   80      8m
kubectl get services --all-namespaces | grep nginx
ingress-nginx   ingress-nginx    NodePort    10.106.47.48     <none>        80:31448/TCP,443:31512/TCP   2h

试图在http://localhost:31448/上击中它,但是我从NGINX得到了503 ...所以我想它没有正确连接到sample-service

我应该如何点击Ingress才能调用示例服务?

1 个答案:

答案 0 :(得分:0)

您需要在点击服务时指定 Host 标头。例如

curl -H "HOST: abc" http://localhost:31448/

由于您的入口定义没有指定任何主机配置,您可以传递任何值。

欲了解更多信息 - see