[英]Connection error on Kubernetes Docker for Nginx and php-fpm
I have a docker deployment using Kube.netes.我有一个使用 Kube.netes 的 docker 部署。 Out of the blue on a running AWS K8 "pod", I have started getting the following error:在正在运行的 AWS K8“pod”上,突然出现以下错误:
connect() failed (111: Connection refused) while connecting to upstream, client: X.0.XX.XX, server: _, request: "GET /api/endpoint& HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "domain.com"
It seems like it has something to do with the nginx configuration with php-fpm rather than the Dockerfile or the deployment.它似乎与带有 php-fpm 的 nginx 配置有关,而不是与 Dockerfile 或部署有关。 What fixes this error immediately is restarting the Kube.netes pods (whichever pod happens to give this error).立即修复此错误的方法是重新启动 Kube.netes pod(无论哪个 pod 恰好出现此错误)。
I suspect that if php-fpm happens to fail inside a given pod, that's when it stops listening on port 9000?我怀疑如果 php-fpm 碰巧在给定的 pod 内失败,那是它停止侦听端口 9000 的时候吗? I ssh'ed into the pod and did a.netstat to verify this and it shows 9000 being alive.我通过 ssh 进入 pod 并执行 a.netstat 来验证这一点,它显示 9000 还活着。
For reference, here is the Dockerfile:作为参考,这里是 Dockerfile:
FROM trafex/alpine-nginx-php7:1.9.0
USER root
RUN apk add --no-cache file
RUN apk --update add imagemagick
RUN apk --no-cache add php7-redis php7-simplexml php7-iconv php7-imagick
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ARG gh_token
ADD nginx-host.conf /etc/nginx/nginx.conf
RUN rm -Rf /var/www/html/
COPY . /var/www/html/
RUN composer config --global github-oauth.github.com ${gh_token}
RUN cd /var/www/html/ \
&& composer update
RUN composer config --global github-oauth.github.com "none"
USER nobody
And here is the nginx.conf:这是 nginx.conf:
worker_processes auto;
error_log stderr warn;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# Define custom log format to include reponse times
log_format main_timed '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time $pipe $upstream_cache_status';
access_log /dev/stdout main_timed;
error_log /dev/stderr notice;
keepalive_timeout 65;
# Write temporary files to /tmp so they can be created as a non-privileged user
client_body_temp_path /tmp/client_temp;
proxy_temp_path /tmp/proxy_temp_path;
fastcgi_temp_path /tmp/fastcgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
scgi_temp_path /tmp/scgi_temp;
underscores_in_headers on;
map $request_uri $version {
~(?<captured_topdir>^/[a-zA-Z0-9]+[/]) $captured_topdir;
}
# Default server definition
server {
listen [::]:8080 default_server;
listen 8080 default_server;
server_name _;
sendfile off;
client_max_body_size 6M;
root /var/www/html;
index index.php index.html;
# Redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/lib/nginx/html;
}
location / {
rewrite ^/(v[0-9]+|stage|partner)?/(.*)$ /$2 last;
set $new_uri $uri;
try_files $uri $uri/ /index.php?$query_string;
}
# Pass the PHP scripts to PHP-FPM listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param API_VERSION $version;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $new_uri;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 5d;
}
# Deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
# Allow fpm ping and status from localhost
location ~ ^/(fpm-status|fpm-ping)$ {
access_log off;
allow 127.0.0.1;
deny all;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
gzip on;
gzip_proxied any;
gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;
gzip_vary on;
gzip_disable "msie6";
# Include other server configs
include /etc/nginx/conf.d/*.conf;
}
Any idea on the nginx config where this error could be surfacing from?关于 nginx 配置中可能出现此错误的任何想法? It has happened completely out of the blue a few times now.现在已经完全出乎意料地发生了几次。
Looks like Nginx is getting the connection refuse from the stream.看起来 Nginx 从 stream 收到连接拒绝。
i would suggest trying with simple Nginx config, store it inside the Kube.netes configmap
我建议尝试使用简单的 Nginx 配置,将其存储在 Kube.netes configmap
中
apiVersion: v1
kind: ConfigMap
metadata:
name: nginxthroughpass
namespace: development
data:
default.conf: |-
server {
listen 80 default_server;
root /var/www/html;
server_name _;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Injecting the Nginx configmap along with the PHP fpm deployment注入 Nginx configmap 以及 PHP fpm 部署
apiVersion: extensions/v1
kind: Deployment
metadata:
labels:
app: wordpress-site
name: wordpress-site
namespace: development
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: wordpress-site
tier: frontend
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: wordpress-site
tier: frontend
spec:
volumes:
- configMap:
defaultMode: 256
name: nginxthroughpass
optional: false
name: nginxconf
- name: shared-files
emptyDir: {}
containers:
- name: app
image: <REPLACE WITH DOCKER PHP-FPM IMAGE URL>
imagePullPolicy : IfNotPresent
volumeMounts:
- name: shared-files
mountPath: /var/www/html
envFrom:
- configMapRef:
name: wordpress-configmap
- name: nginx
image: nginx
imagePullPolicy : IfNotPresent
volumeMounts:
- name: shared-files
mountPath: /var/www/html
- mountPath: /etc/nginx/conf.d
name: nginxconf
readOnly: true
instead of WordPress
image replace your php-fpm
image and test.代替WordPress
图像替换您的php-fpm
图像并进行测试。
You check this using Php-fpm WordPress with the Nginx pod and storing the config into the config map.您使用带有Nginx pod 的Php-fpm WordPress进行检查,并将配置存储到配置 map 中。
https://github.com/harsh4870/Kube.netes-wordpress-php-fpm-nginx https://github.com/harsh4870/Kube.netes-wordpress-php-fpm-nginx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.