我正在本地环境中使用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 。回答第一个问题:

  • pythonservice's clusterip is accessible, but not http://pythoservice:8000

  • 请参考Connecting Applications with Services
    service/pythonservice的类型为ClusterIP。如果您有兴趣将服务公开到群集之外,请使用服务类型NodePort or LoadBalancer。根据所附的屏幕,可以从群集(ClusterIP服务)中访问您的应用程序。

    2 。回答第二个问题:
  • 输出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内可能没有nslookup之类的工具,因此:请使用已安装的工具在同一 namespace 中运行一些pod并再次验证:
    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
    

    -更新

    请验证所有Pod和svc的状态,尤其是在kube-system命名空间中:
    kubectl get nodes,pods,svc --all-namespaces -o wide
    

    为了开始调试,请获取有关特定问题的详细信息,例如与coredns一起使用,请使用:
    kubectl describe pod your coredns_pod -n kube-system
    kubectl logs coredns_pod -n kube-system
    

    请参阅:
  • Debug Services
  • Debugging DNS Resolution

  • 希望对您有所帮助。

    关于kubernetes - 为什么在Kubernetes中找不到服务发现?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57704272/

    10-16 16:40