我是 kubernetes 新手,正在尝试部署一个简单的 hello-world 应用程序。我正在使用 Ubuntu 20.04 并在 VMware 工作站上运行它。我已经安装了 minikube 并尝试部署。但是,pod 已部署,但无法通过浏览器访问服务。
以下是我的
deployment.yaml
文件:
---
kind: Service
apiVersion: v1
metadata:
name: exampleservice
spec:
selector:
name: myapp
ports:
- protocol: "TCP"
# Port accessible inside cluster
port: 8081
# Port to forward to inside the pod
targetPort: 8080
# Port accessible outside cluster
nodePort: 30000
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myappdeployment
spec:
replicas: 5
selector:
matchLabels:
name: myapp
template:
metadata:
labels:
name: myapp
spec:
containers:
- name: myapp
image: pritishkapli/example:v1.0.0
ports:
- containerPort: 8080
resources:
limits:
memory: 512Mi
cpu: "1"
requests:
memory: 256Mi
cpu: "0.2"
以下是kubernetes服务:
pritish@ubuntu:~$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
exampleservice LoadBalancer 10.101.149.155 <pending> 8081:30000/TCP 12m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9h
下面是运行中的 Pod:
pritish@ubuntu:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
myappdeployment-b85b56d64-knhhc 1/1 Running 0 17m
myappdeployment-b85b56d64-m4vbg 1/1 Running 0 17m
myappdeployment-b85b56d64-qss4l 1/1 Running 0 17m
myappdeployment-b85b56d64-r2jq4 1/1 Running 0 17m
myappdeployment-b85b56d64-tflnz 1/1 Running 0 17m
请帮忙!
PS:我已经更新了
deployment.yaml
文件并且它按预期工作。
这不是
Service
类型的 LoadBalancer
的问题,而是 service.spec.selector
值与
deployment.spec.selector.matchLabels
值之间的 不匹配问题。
要修复您的设置,您可以使用
Service
或 Deployment
中的相同值。
请仅选择以下选项之一:
deployment.yaml
变化:apiVersion: apps/v1
kind: Deployment
metadata:
name: myappdeployment
spec:
replicas: 5
selector:
matchLabels:
app: myapp # <-- CHANGES
template:
metadata:
labels:
app: myapp # <-- CHANGES
spec:
containers:
- name: myapp
image: pritishkapli/example:v1.0.0
ports:
- containerPort: 8080
resources:
limits:
memory: 512Mi
cpu: "1"
requests:
memory: 256Mi
cpu: "0.2"
service.yaml
变化:kind: Service
apiVersion: v1
metadata:
name: exampleservice
spec:
selector:
app.kubernetes.io/name: myapp # <-- CHANGES
ports:
- protocol: "TCP"
# Port accessible inside cluster
port: 8081
# Port to forward to inside the pod
targetPort: 8080
# Port accessible outside cluster
nodePort: 30000
type: LoadBalancer
selector
的问题?这取决于使用 Kubernetes 的经验,但检查它的最简单方法是通过以下任一方法(使用旧示例):
kubectl describe service exampleservice
(由于可读性,此输出的部分内容已被编辑)Name: exampleservice
Namespace: default
Labels: <none>
Selector: app=myapp
Type: LoadBalancer
IP: 10.8.10.103
Port: <unset> 8081/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30000/TCP
Endpoints: <none> # <-- LOOK HERE!
kubectl get endpoints exampleservice
NAME ENDPOINTS AGE
exampleservice <none> 2m3s
正如您在上面的输出中看到的,没有
endpoints
可以将流量发送到 (Pods
)。
LoadBalancer
类型的服务 minikube
从
Service
类型Loadbalancer
的角度来说minikube
:
最简单的方法是按照前面所述通过
NodePort
进行连接。由于 minikube
的本地性质,除非您选择解决方法,否则不会分配 External IP
,例如:
minikube tunnel
tunnel - 创建到使用 LoadBalancer 类型部署的服务的路由,并将其 Ingress 设置为其 ClusterIP。有关详细示例,请参阅 https://minikube.sigs.k8s.io/docs/tasks/loadbalancer
旁注!
我还没有亲自测试过它,但您可以尝试尝试:
$ minikube addons enable metallb
为其分配与您的 CIDR
实例相同的 minikube
中的 IP 地址,然后查询 (curl
) 这个新分配的 IP。
更多参考指南:
其他资源:
您无法使用负载均衡类型部署服务,因为您的集群未设置负载均衡器。尝试使用 NodePort 或 clusterIP 类型部署您的服务
我遇到了这个错误,我花了一整天的时间才弄清楚,对我来说,无法从外部访问 Pod 的原因是因为当您运行“kubectl get svc”时,外部 IP 不存在外部 ip : 是从外部访问集群所需的内容。外部 IP 为 none 的原因是因为您的类型是 type: NodePort,请将其更改为 type: LoadBalancer.type Loadbalancer 是将流量从集群外部(互联网)引导到集群中的 pod。我认为 NodePort 位于集群内,因此您的 service.yaml 文件应该如下所示。
---
kind: Service
apiVersion: v1
metadata:
name: exampleservice
spec:
selector:
name: myapp
ports:
- protocol: "TCP"
# Port accessible inside cluster
port: 8081
# Port to forward to inside the pod
targetPort: 8080
# Port accessible outside cluster
nodePort: 30000
type: LoadBalancer