ero*_*las 6 kubernetes minikube
我有两个部署
部署1
apiVersion: v1
kind: Service
metadata:
name: first-service
spec:
selector:
key: app1
ports:
- port: 81
targetPort: 5050
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: first-deployment
spec:
replicas: 1
selector:
matchLabels:
run: app1
template:
metadata:
labels:
run: app1
spec:
containers:
- name: ocr
image: ocr_app
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5050
Run Code Online (Sandbox Code Playgroud)
部署2
apiVersion: v1
kind: Service
metadata:
name: second-service
spec:
selector:
key: app2
ports:
- port: 82
targetPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: second-deployment
spec:
replicas: 1
selector:
matchLabels:
run: app2
template:
metadata:
labels:
run: app2
spec:
containers:
- name: ner
image: ner_app
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5000
Run Code Online (Sandbox Code Playgroud)
在 minikube 上启用 ingress 后,我应用了 ingess
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress
spec:
rules:
- host: demo.local
http:
paths:
- path: /ocr
backend:
serviceName: first-service
servicePort: 81
- path: /ner
backend:
serviceName: second-service
servicePort: 82
Run Code Online (Sandbox Code Playgroud)
在我的主机文件中我有
192.168.177.71 demo.local
Run Code Online (Sandbox Code Playgroud)
192.168.177.71我当前的 minikube ip 在哪里
然后我运行这个命令
kubectl port-forward nginx-ingress-controller-6fc5bcc8c9-p6mvj 3000:80 --namespace kube-system
Run Code Online (Sandbox Code Playgroud)
在控制台中是输出
Forwarding from 127.0.0.1:3000 -> 80
Forwarding from [::1]:3000 -> 80
Run Code Online (Sandbox Code Playgroud)
但是当我向邮递员发出请求时demo.local:3000/ocr没有任何回应
无法获得任何响应 连接到 demo.local 时出错:3000。
编辑:使用minikube service first-service给出了这个输出
PS D:\docker> minikube service first-service
|-----------|---------------|-------------|--------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|---------------|-------------|--------------|
| default | first-service | | No node port |
|-----------|---------------|-------------|--------------|
* service default/first-service has no node port
Run Code Online (Sandbox Code Playgroud)
@erotavlas,Mafor 提供的答案可以帮助您解决问题,请接受他的答案。
\n\n我正在发布扩展答案,可能对其他人有帮助。
\n\n这个问题的根本原因是selector/labels.
在 中first-service,spec.selector被设置为key: app1,但是在部署中spec.selector.matchLabels被设置为run: app1。
为了正常工作,您需要具有相同的选择器。因此,您需要将 service、spec.selector 更改为run: app1或将部署更改spec.selector.matchLabels为key: app1。second-service和 的情况相同second-deployment。更多详情可在这找到。
我尝试根据官方文档在 Minikube 上使用 Ingress和您的 YAML 在 Minikube 上使用 Ingress。
\n\n另外,要使用Ingress在Minikube,Ingress addon必须启用。
$ minikube addons list | grep ingress\n- ingress: disabled\nRun Code Online (Sandbox Code Playgroud)\n\n如果它被禁用,您必须启用它。
\n\n$ minikube addons enable ingress\n\xe2\x9c\x85 ingress was successfully enabled\nRun Code Online (Sandbox Code Playgroud)\n\ntargetPort:是容器接受流量的端口 / 应用程序在 pod 内运行的端口
\nport:是抽象的Service端口,可以是其他 pod 用于访问服务的任何端口。
OP 使用自己的图像,其中应用程序在端口上运行5050,5000在本示例中,我将在端口上使用 GCP hello world8080。Labels/matchLabels 已更改为在部署和服务中具有相同的值。
第一次服务
\n\napiVersion: v1\nkind: Service\nmetadata:\n name: first-service\nspec:\n selector:\n key: app1\n ports:\n - port: 81\n targetPort: 8080\n\n---\n\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: first-deployment\nspec:\n replicas: 1\n selector:\n matchLabels:\n key: app1\n template:\n metadata:\n labels:\n key: app1\n spec:\n containers:\n - name: hello1\n image: gcr.io/google-samples/hello-app:1.0\n imagePullPolicy: IfNotPresent\n ports:\n - containerPort: 8080\n\nservice/first-service created\ndeployment.apps/first-deployment created\nRun Code Online (Sandbox Code Playgroud)\n\n第二次服务
\n\napiVersion: v1\nkind: Service\nmetadata:\n name: second-service\nspec:\n selector:\n key: app2\n ports:\n - port: 82\n targetPort: 8080\n\n---\n\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: second-deployment\nspec:\n replicas: 1\n selector:\n matchLabels:\n key: app2\n template:\n metadata:\n labels:\n key: app2\n spec:\n containers:\n - name: hello2\n image: gcr.io/google-samples/hello-app:2.0\n imagePullPolicy: IfNotPresent\n ports:\n - containerPort: 8080\n\nservice/second-service created\ndeployment.apps/second-deployment created\nRun Code Online (Sandbox Code Playgroud)\n\n它将服务作为ClusterIP类型打包。如果需要你可以使用NodePort,但不是必需的。
应用入口
\n\n所提供的 Ingress 足以进行测试。
\n\n正如官方文档中提到的中提到的。您应该将 minikube ip 添加到主机文件中。
\n\n\n\n\n注意:如果您在本地运行 Minikube,请使用 minikube ip 获取外部 IP。入口列表中显示的 IP 地址将是内部 IP。
\n
在 Ubuntu 操作系统中是这样的/etc/hosts(需要使用 sudo 进行编辑)。在 Windows 操作系统中,请检查这篇文章
对于我的集群(使用 GCE):
\n\n$ minikube ip\n10.132.15.208\nRun Code Online (Sandbox Code Playgroud)\n\n添加到hosts文件值:
\n\n\n10.132.15.208 演示.本地
\n
以下回复。
\n\n$ curl demo.local/ocr\nHello, world!\nVersion: 1.0.0\nHostname: first-deployment-85b75bf4f9-qlzrp\n$ curl demo.local/ner\nHello, world!\nVersion: 2.0.0\nHostname: second-deployment-5b5bbb7f4-9sbqr\nRun Code Online (Sandbox Code Playgroud)\n\n但是,版本与rewrite Mafor 提供的版本更加通用。
此外,您还可以考虑LoadBalancer在Minikube. \n更多信息可以在Minikube 文档中找到。
首先,你不需要kubectl port-forward。Ingress 暴露在您的 minikube IP、端口 80 上。
其次,您可能需要在入口配置中使用一些重写规则。默认情况下,入口“按原样”转发请求,即demo.local:3000/ocr转发到first-service:81/ocr. 可能这不是您想要的,除非将第一个服务部署到/ocr上下文中。
我想,你需要这样的东西:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: demo.local
http:
paths:
- path: /ocr(/|$)(.*)
backend:
serviceName: first-service
servicePort: 81
- path: /ner(/|$)(.*)
backend:
serviceName: second-service
servicePort: 82
Run Code Online (Sandbox Code Playgroud)
请参阅https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#rewrite
| 归档时间: |
|
| 查看次数: |
13183 次 |
| 最近记录: |