为什么我的K8S服务只能访问三个副本中的一个?

为什么我的K8S服务只能访问三个副本中的一个?

K8S 新手 我在三台 Raspberry Pi 4 电脑(blackpi、bluepi、redpi)上安装了 microk8s。我在本地注册表中有一个小型 hello world REST 应用程序作为 docker 镜像。我已将其部署为具有 3 个副本的服务。但是,这些副本中只有一个正在使用,其他两个无法正确访问。

这是我的配置文件:

joerg@blackpi:~/build$ cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deployment
  labels:
    app.kubernetes.io/name: hellow
spec:
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: hellow
  template:
    metadata:
      labels:
        app.kubernetes.io/name: hellow
    spec:
      containers:
        - name: hellow
          image: blackpi:5000/hellow:registry
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080
joerg@blackpi:~/build$ cat service.yaml
apiVersion: v1
kind: Service
metadata:
  name: hello-service
  labels:
    app.kubernetes.io/name: hellow
spec:
  selector:
    app.kubernetes.io/name: hellow
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
joerg@blackpi:~/build$ cat ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-ingress
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-service
            port:
              number: 8080

所有三个文件均已应用:

joerg@blackpi:~/build$ microk8s kubectl apply -f deployment.yaml
deployment.apps/hello-deployment created
joerg@blackpi:~/build$ microk8s kubectl apply -f service.yaml
service/hello-service created
joerg@blackpi:~/build$ microk8s kubectl apply -f ingress.yaml
ingress.networking.k8s.io/hello-ingress created

这是kubectl get说:

joerg@blackpi:~/build$ microk8s kubectl get nodes,pods,services,ingresses,endpoints,deployments,replicasets -o wide
NAME           STATUS   ROLES    AGE   VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
node/redpi     Ready    <none>   31d   v1.27.6   192.168.178.110   <none>        Ubuntu 22.04.3 LTS   5.15.0-1041-raspi   containerd://1.6.15
node/blackpi   Ready    <none>   31d   v1.27.6   192.168.178.109   <none>        Ubuntu 22.04.3 LTS   5.15.0-1040-raspi   containerd://1.6.15
node/bluepi    Ready    <none>   31d   v1.27.6   192.168.178.111   <none>        Ubuntu 22.04.3 LTS   5.15.0-1041-raspi   containerd://1.6.15

NAME                                    READY   STATUS    RESTARTS   AGE   IP             NODE      NOMINATED NODE   READINESS GATES
pod/hello-deployment-687697cc54-cb9lh   1/1     Running   0          28m   10.1.224.110   bluepi    <none>           <none>
pod/hello-deployment-687697cc54-lms78   1/1     Running   0          28m   10.1.175.187   redpi     <none>           <none>
pod/hello-deployment-687697cc54-sz85h   1/1     Running   0          28m   10.1.239.148   blackpi   <none>           <none>

NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE   SELECTOR
service/kubernetes      ClusterIP   10.152.183.1     <none>        443/TCP    31d   <none>
service/hello-service   ClusterIP   10.152.183.125   <none>        8080/TCP   28m   app.kubernetes.io/name=hellow

NAME                                      CLASS   HOSTS   ADDRESS     PORTS   AGE
ingress.networking.k8s.io/hello-ingress   nginx   *       127.0.0.1   80      28m

NAME                      ENDPOINTS                                               AGE
endpoints/kubernetes      192.168.178.109:16443                                   31d
endpoints/hello-service   10.1.175.187:8080,10.1.224.110:8080,10.1.239.148:8080   28m

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                         SELECTOR
deployment.apps/hello-deployment   3/3     3            3           28m   hellow       blackpi:5000/hellow:registry   app.kubernetes.io/name=hellow

NAME                                          DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                         SELECTOR
replicaset.apps/hello-deployment-687697cc54   3         3         3       28m   hellow       blackpi:5000/hellow:registry   app.kubernetes.io/name=hellow,pod-template-hash=687697cc54

我现在尝试使用该服务:

joerg@blackpi:~/build$ time wget localhost:80/greeting -O - -o /dev/null
{"id":18,"content":"Hello, World!"}
real    0m10.034s
user    0m0.002s
sys     0m0.015s

我重复上述操作,始终看到 10 秒的实时消耗。只使用了一个副本(blackpi 上的副本)。我的配置哪里出了错?

如果我直接从副本使用该服务,我可以看到它按预期工作。示例blackpi

joerg@blackpi:~/build$ time wget 10.1.239.148:8080/greeting -O - -o /dev/null
{"id":19,"content":"Hello, World!"}
real    0m0.026s
user    0m0.000s
sys     0m0.013s

另外两个节点也是一样:我可以正常访问另外两个 pod。

相关内容