我在Kubernetes中部署了一个REST API应用程序YAML,并尝试从另一个命名空间访问该API。但它显示了错误。如何从不同的名称空间访问其余的API。以下是我的部署YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: configuration
labels:
app: configuration
namespace: restapi
spec:
replicas: 1
selector:
matchLabels:
app: configuration
template:
metadata:
labels:
app: configuration
spec:
containers:
- name: configuration
image: global.azurecr.io/config:1
env:
- name: AzureFunctionsJobHost__functions__0
value: configuration
envFrom:
- secretRef:
name: configuration
imagePullSecrets:
- name: pull
来自其他命名空间的API调用的URL为";http://configuration.restapi:80/api/configuration">我尝试在我的url中使用.restapi,但不起作用。我可以在同一个名称空间中调用rest API。
您总是可以做一个简单的测试来检查是否可以从不同的命名空间访问。
假设您没有服务。
然后你可以像下面这样使用ip直接到达吊舱。
kubectl run dnstest --image=busybox:1.28 --restart=Never --rm -ti -- nslookup 10-36-0-2.default.pod
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: 10-36-0-2.default.pod
Address 1: 10.36.0.2
pod "dnstest" deleted
如果你像下面的一样暴露服务
kubectl expose deployment configuration --port 80 -n restapi
那么测试结果如下。
kubectl run dnstest --image=busybox:1.28 --restart=Never --rm -ti -- nslookup configuration.restapi
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: configuration.restapi
Address 1: 10.105.110.174 configuration.restapi.svc.cluster.local
pod "dnstest" deleted
您可以在标准kubernetes环境中使用configuration.restapi或configuration.restapi.svc或configuration.resti.svc.cluster.local。
假设configuration.restapi是您试图访问的服务的名称,并且是您将在该名称空间中使用的名称,那么您将使用"configuration.restapi.othernamespace.svc.cluster.local";。