简体   繁体   English

如何访问 rpi k8s 集群上的服务

[英]how to access service on rpi k8s cluster

I built a k8s cluster with help of this guide: rpi+k8s .我在本指南的帮助下构建了一个 k8s 集群: rpi+k8s I got some basic nginx service up and running and and I can curl from master node to worker node to get the nginx welcome page content using:我启动并运行了一些基本的 nginx 服务,并且我可以使用 curl 从主节点到工作节点获取 nginx 欢迎页面内容:

k exec nginx-XXX-XXX -it -- curl localhost:80

I tried following suggestions in the following SO posts:我在以下 SO 帖子中尝试了以下建议:

link 1 link 2 链接 1 链接 2

However, I still can't access a simple nginx service on the worker node from my local computer (linux).但是,我仍然无法从本地计算机 (linux) 访问工作节点上的简单 nginx 服务。 I used, NODE IP:NODE PORT .我用过NODE IP:NODE PORT I also installed kubefwd and ran, sudo kubefwd svc -n nginx-ns but I don't see the expected output where it would show the port forwards.我还安装了 kubefwd 并运行, sudo kubefwd svc -n nginx-ns但我没有看到预期的 output 会显示端口转发。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

Output: Output:

NAME                TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/nginx-svc   NodePort   10.101.19.230   <none>        80:32749/TCP   168m

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           168m

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-54485b444f   3         3         3       168m

And here is the yaml file:这是 yaml 文件:

kind: Namespace
apiVersion: v1
metadata:
  name: nginx-ns
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: nginx-ns
spec:
  selector:
    matchLabels:
      app: nginx 
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx 
    spec:
      containers:
      - name: nginx 
        image: nginx:1.19-alpine
        ports:
        - name: nginxport
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: nginx-ns
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    name: nginxport
    port: 80
    targetPort: 80
    nodePort: 32749
  type: NodePort
  selector:
    app: backend

You need to update your service nginx-svc where you have used two selector .您需要更新使用了两个selector的服务nginx-svc

remove below part:删除以下部分:

  selector:
    app: backend

Updated service.yaml :更新service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: nginx-ns
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    name: nginxport
    port: 80
    targetPort: 80
    nodePort: 32749
  type: NodePort

Then, Try this one for port-forwarding.然后,试试这个进行端口转发。

kubectl port-forward -n nginx-ns svc/nginx-svc 8080:80

Template is like this:模板是这样的:

kubectl port-forward -n <namespace> svc/<svc_name> <local_port>:<svc_port>

Then try in the browser with 127.0.0.1:8080 or localhost:8080然后在浏览器中尝试使用127.0.0.1:8080localhost:8080

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM