在上游“ php”中找不到kubernetes主机

时间:2018-10-15 00:23:54

标签: php nginx kubernetes

如何在kubernetes中配置default.conf。此docker-compose在本地工作:

version: '3'

services:
  php:
    container_name: my_php
    build:
        context: .
        dockerfile: php/Dockerfile
    ports:
        - 9002:9000
    volumes:
        - ../:/var/www/some-service
        - ./logs/application:/var/www/some-service/var/logs:cached
    networks:
        - some-service
  nginx:
    container_name: my_nginx
    build:
        context: .
        dockerfile: nginx/Dockerfile
    ports:
        - 8080:80
    volumes:
        - ../:/var/www/some-service
        - ./logs/nginx/:/var/log/nginx:cached
    networks:
        - some-service
  db:
    image: mysql:5.7
    container_name: my_db
    environment:
        - MYSQL_ROOT_PASSWORD=root
    volumes:
        - some-service-db:/var/lib/mysql:cached
    ports:
        - 3311:3306
    networks:
        - some-service

volumes:
  some-service-db:

networks:
  some-service:
    driver: bridge

default.conf看起来像这样:

server {
  index index.php
  server_name  localhost;
  root /var/www/some-service/public;

  location / {
    try_files $uri @rewriteapp;
  }

  location @rewriteapp {
    rewrite ^(.*)$ /index.php/$1 last;
  }

  location ~ ^/index\.php(/|$) {
    fastcgi_pass php:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
  }

  error_log /var/log/nginx/some-service-error.log;
  access_log /var/log/nginx/some-service-access.log;
}

当我部署到kubernetes时,我从nginx容器中收到以下错误:

2018/10/14 22:51:14 [emerg] 1#1: host not found in upstream "php" in /etc/nginx/conf.d/default.conf:15
nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/default.conf:15

然后我放弃了fastcgi_pass php:9000;中的端口,但随后得到了no port in upstream "php"。因此,我将以下内容添加到default.conf:

upstream php {
  server php;
}

但是现在我得到了错误:

2018/10/14 23:32:23 [emerg] 1#1: upstream "php" may not have port 9000 in /etc/nginx/conf.d/default.conf:15
nginx: [emerg] upstream "php" may not have port 9000 in /etc/nginx/conf.d/default.conf:15

我也将端口更改为9002,但是得到了与9002相同的错误。在我的kubernetes部署yaml中,我将docker-compose中的容器端口用作containerPort值。 php和db容器可以正常启动。如何配置Nginx在php容器中的Kubernetes中正常工作?

更新 通过将fastcgi_pass php:9000;更改为fastcgi_pass 127.0.0.1:9002,nginx不会崩溃。但是,如果您只有任何IP或端口,它似乎仍然不会崩溃。只要我兼有,它就不会崩溃。

更新2:回复Matthews的评论 这是部署yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: some-service-deployment
  labels:
    app: some-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: some-service
  template:
    metadata:
      labels:
        app: some-service
    spec:
      containers:
      - name: php
        image: 1111111.dkr.ecr.us-east-1.amazonaws.com/some-service/php:latest
        ports:
        - containerPort: 9002
      - name: nginx
        image: 1111111.dkr.ecr.us-east-1.amazonaws.com/some-service/nginx:latest
        ports:
        - containerPort: 8089
        volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx/conf.d/
      - name: db
        image: mysql:5.7
        ports:
        - containerPort: 3311
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: somepassword
      volumes:
      - name: config-volume
        configMap:
          name: nginx-conf

1 个答案:

答案 0 :(得分:1)

我想参与进来,因为我在帮助同事解决此问题时遇到了同样的问题。

我查看了Pod中的kubernetes文档和容器到容器的通信,必须通过本地主机,没有别名,就像在桌面上使用docker-compose一样。

这里是kubernetes文档的链接和摘录:

从“集群网络”(https://kubernetes.io/docs/concepts/cluster-administration/networking/)页面

    Networking is a central part of Kubernetes, but it can be challenging 
    to understand exactly how it is expected to work. 
    There are 4 distinct networking problems to address:

    1. Highly-coupled container-to-container communications: 
       this is solved by Pods and localhost communications.
    2. Pod-to-Pod communications: this is the primary focus of this document.
    3. Pod-to-Service communications: this is covered by services.
    4. External-to-Service communications: this is covered by services.

在页面“ Pods”的“ Pods联网”(https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking)部分:

Inside a Pod (and only then), the containers that belong to the Pod can
communicate with one another using localhost. 
[cut]
Within a Pod, containers share an IP address and port space, and can find
each other via localhost.