之前一直有个疑问,如果要在同一个Kubernetes的POD中部署两个系统镜像的容器,两个容器岂不是会有资源(网络端口号,文件等)冲突。在尝试在Kubernetes挂载不同的存储时就遇到同一个nginx镜像部署在同一个POD中,第二个POD始终失败的问题,查看日志也提示尝试多次终止了容器。猜测应该是在同一个POD中端口冲突导致第二个容器启动失败,于是修改官方nginx镜像的端口号成功在一个POD中启动了两个nginx容器。
具体操作如下:
从dokcer
hub官方下载nginx:alpine镜像
docker pull nginx:alpine
查看镜像中的默认配置
1 2
| docker run -it --name nginx nginx:alpine /bin/sh cat /etc/nginx/nginx.conf
|
修改官方镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| user nginx; worker_processes auto;
error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; #tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server { listen 8080; listen [::]:8080; server_name localhost;
#charset koi8-r; #access_log /var/log/nginx/host.access.log main;
location / { root /usr/share/nginx/log; # index index.html index.htm; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
}
|
1 2 3 4
| FROM nginx:alpine RUN mkdir -p /usr/share/nginx/log ADD index.html /usr/share/nginx/log/index.html COPY nginx.conf /etc/nginx/nginx.conf
|
docker build -t nginx:log .
本地测试成功以后就可以推送到自己的镜像仓库了:)。