![](/img/trans.png)
[英]Apache server runs with docker run but kubernetes pod fails with CrashLoopBackOff
[英]Kubernetes - Unable to access apache Pod from proxy
我在Kubernetes POD上有PHP app + Apache。 PHP应用程序在端口9200上访问Elasticsearch API,并在端口5000上向用户显示一个简单的网站。
我使用kubectl proxy --address='0.0.0.0' --port=8001 --accept-hosts='.*'
运行代理。
当我输入"http://localhost:8001/api/v1/namespaces/default/pods/website/proxy/"
,我看到以下错误:
Error: 'dial tcp 172.17.0.6:5000: connect: connection refused'
Trying to reach: 'http://172.17.0.6:5000/'
* 172.17.0.6是POD ip地址。
这是网站POD yaml:
apiVersion: v1
kind: Pod
metadata:
name: website
labels:
app: website
spec:
containers:
- name: website
image: website_k8s:latest
ports:
- containerPort: 5000
- containerPort: 80
这是website_k8s:latest的dockerfile:
FROM php:7.0-apache
RUN echo "ServerName 0.0.0.0" >> /etc/apache2/apache2.conf
COPY ./index.php /var/www/html/
EXPOSE 5000
我也尝试运行服务:
apiVersion: v1
kind: Service
metadata:
name: website-service
spec:
ports:
- port: 5000
targetPort: 80
protocol: TCP
name: serving
- port: 80
targetPort: 9200
protocol: TCP
name: readfromelasticsearch
selector:
app: website
当我输入http://localhost:8001/api/v1/namespaces/default/services/website-service/proxy/
,我看到:
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "no endpoints available for service \"website-service\"",
"reason": "ServiceUnavailable",
"code": 503
}
即使,当我运行$kubectl get endpoints website-service
,我看到:
NAME ENDPOINTS AGE
website-service 172.17.0.6:80,172.17.0.6:9200 59m
我看到了服务的POD端点。
如何通过代理访问我的网站?
问题在于Apache正在监听端口80,而不是5000。
如果您删除containerPort: 5000
则可以通过http://localhost:8001/api/v1/namespaces/default/pods/website/proxy/
访问该网站。
其次,您看到的服务端点在群集内部。 因此,您可以从Pod中的这些IP端点访问服务: kubectl exec website -- curl 172.17.0.6/
。 如果要在外部公开服务,则该服务的类型应为NodePort
或LoadBalancer
。
最后,通过代理查询服务时遇到的问题是,当给端口命名时,必须将其包括在代理调用中: curl localhost:8001/api/v1/namespaces/default/services/website-service:serving/proxy/
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.