我正在尝试使用 k8s 开发一个微服务应用程序,这是我的第一个微服务应用程序方法。我的deployment.yaml配置的目标是创建3个具有相关服务的部署:
这是我的部署.yaml:
apiVersion: v1
kind: Service
metadata:
name: db
spec:
selector:
app: db
ports:
- protocol: TCP
port: 5432
targetPort: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: db
spec:
replicas: 1
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: db
image: myName/db
env:
- name: POSTGRES_PASSWORD
value: root
- name: POSTGRES_USER
value: postgres
ports:
- containerPort: 5432
resources:
requests:
memory: "128Mi"
cpu: "0.5"
limits:
memory: "1024Mi"
cpu: "1"
---
apiVersion: v1
kind: Service
metadata:
name: mainapi
spec:
selector:
app: mainapi
ports:
- protocol: TCP
port: 3000
targetPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mainapi
spec:
replicas: 1
selector:
matchLabels:
app: mainapi
template:
metadata:
labels:
app: mainapi
spec:
containers:
- name: mainapi
image: myName/mainapi
ports:
- containerPort: 3000
resources:
requests:
memory: "128Mi"
cpu: "0.5"
limits:
memory: "1024Mi"
cpu: "2"
---
apiVersion: v1
kind: Service
metadata:
name: fastapi
spec:
selector:
app: fastapi
ports:
- protocol: TCP
port: 8000
targetPort: 8000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: fastapi
spec:
replicas: 2
selector:
matchLabels:
app: fastapi
template:
metadata:
labels:
app: fastapi
spec:
containers:
- name: fastapi
image: myName/fastapi
ports:
- containerPort: 8000
resources:
requests:
memory: "512Mi"
cpu: "0.5"
limits:
memory: "2048Mi"
cpu: "2"
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: progettoviaggio
labels:
name: progettoviaggio
spec:
rules:
- host: progettoviaggio.test
http:
paths:
- path: "/"
pathType: Prefix
backend:
service:
name: mainapi
port:
number: 3000
我的目标是有一个 dns“progettoviaggio.test”来连接前端应用程序。经过一些研究,我发现使用“入口”是可能的,在遵循一些教程后,我设置了入口,并复制了发现的 IP 地址
kubectl get ingress
我已将“progettoviaggio.test”映射到 /etc/hosts
文件中的地址。
然后我检查所有 Pod、服务和入口是否正常工作
kubectl get pods
db-678448cf9c-v46bx 1/1 Running 1 (2d ago) 3d3h
fastapi-d59547d78-bx7zf 1/1 Running 1 (2d ago) 3d3h
fastapi-d59547d78-vr8lh 1/1 Running 1 (2d ago) 3d3h
mainapi-7c75577989-mdzpf 1/1 Running 10 (2d ago) 3d3h
kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.105.116.51 <none> 5432/TCP 3d7h
fastapi ClusterIP 10.110.190.84 <none> 8000/TCP 3d7h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d7h
mainapi ClusterIP 10.101.218.237 <none> 3000/TCP 3d7h
kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
progettoviaggio nginx progettoviaggio.test 192.168.49.2 80 2d1h
毕竟我尝试连接到“progettoviaggio.test”,但响应是:
ERR_CONNECTION_TIMED_OUT
LoadBalancer
和NodePort
,但结果保持不变我的系统配置: MacBookPro 18,1 CPU:M1 Pro 10核 显卡:M1 Pro 16核 内存:32GB 库伯内特斯: 客户端版本:v1.29.1 Kustomize版本:v5.0.4-0.20230601165947-6ce0bf390ce3 服务器版本:v1.26.3 minikube版本:v1.30.1 docker版本:24.0.7 darwin/arm64
** 根据评论的要求进行更新
kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default db ClusterIP 10.105.116.51 <none> 5432/TCP 4d5h
default fastapi ClusterIP 10.110.190.84 <none> 8000/TCP 4d5h
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d5h
default mainapi ClusterIP 10.101.218.237 <none> 3000/TCP 4d5h
ingress-nginx ingress-nginx-controller NodePort 10.96.145.16 <none> 80:30959/TCP,443:31788/TCP 3d
ingress-nginx ingress-nginx-controller-admission ClusterIP 10.104.140.143 <none> 443/TCP 3d
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 4d5h
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.99.56.183 <none> 8000/TCP 4d2h
kubernetes-dashboard kubernetes-dashboard ClusterIP 10.96.209.32 <none> 443/TCP 4d2h
谢谢大家的帮助
我看到你的yaml文件没问题,但是当你在Minikube中运行Kubernetes集群时,这里你需要使用kubectl port-forward或minikubetunnel。