为什么在Kubernetes中找不到服务发现?

时间:2019-08-29 06:32:21

标签: kubernetes coredns

我正在本地环境中使用Kubernetes创建一个简单的grpc示例。

当nodejs使用pythonservice发出请求时,pythonservice会以helloworld进行响应并将其显示在网页上。

但是,可以访问pythonservice的clusterip,但不能访问http://pythoservice:8000

coredns可能有问题,因此我检查了各种内容并删除了kube-system的kube-dns服务。

如果您使用nslookup与pythonservice.default.svc.cluster.local进行检查,则会看到与pythonservice的clusterip不同的地址。 对不起,我英语不好。

这是node.js代码:

var setting = 'test';
var express = require('express');
var app = express();
const port = 80;
var PROTO_PATH = __dirname + '/helloworld.proto';

var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });

// http://pythonservice:8000
// 10.109.228.152:8000
// pythonservice.default.svc.cluster.local:8000
// 218.38.137.28

var hello_proto = 
grpc.loadPackageDefinition(packageDefinition).helloworld;

function main(callback) {
  var client = new hello_proto.Greeter("http://pythonservice:8000", 
    grpc.credentials.createInsecure());

  var user;
  if (process.argv.length >= 3) {
    user = process.argv[2];
  } else {
    user = 'world';
  }
  client.sayHello({name: user}, function(err, response) {
    console.log('Greeting:', response.message);
    setting = response.message;
  });
}

var server = app.listen(port, function () {});

app.get('/', function (req, res) {
    main();    
    res.send(setting);
    //res.send(ip2);
    //main(function(result){
      //  res.send(result);
    //})
});

这是pythonservice的yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: practice-dp2
spec:
  selector:
    matchLabels:
      app: practice-dp2
  replicas: 1 
  template:
    metadata:
      labels:
        app: practice-dp2
    spec:
      hostname: appname
      subdomain: default-subdomain
      containers:
      - name: practice-dp2
        image: taeil777/greeter-server:v1
        ports:
        - containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
  name: pythonservice
spec:
  type: ClusterIP
  selector:
    app: practice-dp2
  ports:
  - port: 8000
    targetPort: 8000

这是kubectl全部获取:

root@pusik-server0:/home/tinyos/Desktop/grpc/node# kubectl get all
NAME                               READY   STATUS    RESTARTS   AGE
pod/practice-dp-55dd4b9d54-v4hhq   1/1     Running   1          68m
pod/practice-dp2-7d4886876-znjtl   1/1     Running   0          18h


NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   
PORT(S)    AGE
service/kubernetes      ClusterIP   10.96.0.1        <none>        
443/TCP    34d
service/nodeservice     ClusterIP   10.100.165.53    <none>        
80/TCP     68m
service/pythonservice   ClusterIP   10.109.228.152   <none>        
8000/TCP   18h


NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/practice-dp    1/1     1            1           68m
deployment.apps/practice-dp2   1/1     1            1           18h

NAME                                     DESIRED   CURRENT   READY   
AGE
replicaset.apps/practice-dp-55dd4b9d54   1         1         1       
68m
replicaset.apps/practice-dp2-7d4886876   1         1         1       
18h
root@pusik-server0:/home/tinyos/Desktop/grpc/python# nslookup 
pythonservice.default.svc.cluster.local
Server:     127.0.1.1
Address:    127.0.1.1#53

Name:   pythonservice.default.svc.cluster.local
Address: 218.38.137.28

1 个答案:

答案 0 :(得分:0)

1 。回答第一个问题:

  • >>> print(df.to_string(float_format=lambda x: '{:.2f}'.format(x))) numbers letters A 0.11 B 0.22 C 0.33

请参阅Connecting Applications with Services

float_format的类型为pythonservice's clusterip is accessible, but not http://pythoservice:8000。如果您有兴趣在群集外公开服务,请使用服务类型service/pythonservice。根据所附的屏幕,可以从群集(ClusterIP服务)内访问您的应用程序。

2 。回答第二个问题:

  • 输出 ClusterIP

意味着您的pod内可能没有NodePort or LoadBalancer之类的工具,因此:请使用已安装的工具在同一命名空间中运行一些pod,然后再次进行验证:

exec failed: container_linux.go:345: starting container process caused "exec: \"nslookup\": executable file not found in $PATH": unknown command terminated with exit code 126

-更新

请验证所有Pod和svc的状态,尤其是在nslookup名称空间中:

kubectl run ubuntu --rm -it --image ubuntu --restart=Never --command -- bash -c 'apt-get update && apt-get -y install dnsutils && bash'

kubectl exec ubuntu2 -- nslookup pythonservice.default.svc.cluster.local

为了开始调试,请获取有关特定问题的更多信息。对于coredns,请使用:

kube-system

请参考:

希望获得帮助。