如何使用Kubernetes运行一个简单的PHP Hello World应用程序

巴布卢·艾哈迈德(Bablu Ahmed)

我正在尝试部署和运行一个简单的PHP应用程序,该应用程序只能Hello World通过我的Kubernetes集群显示消息,该集群只是一个主节点集群,不幸的是,我无法做到这一点。

我正在描述我的项目结构-我有一个根项目目录kubernetes-test,在该目录下,我有3个yaml文件,code在该目录下有一个目录,我有一个PHP文件,index.php

hello-world-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    tier: backend
spec:
  selector:
    app: nginx
    tier: backend
  type: NodePort
  ports:
  - nodePort: 30500
    port: 80
    targetPort: 80

nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      tier: backend
  template:
    metadata:
      labels:
        app: nginx
        tier: backend
    spec:
      volumes:
      - name: code
        hostPath:
          path: /code
      - name: config
        configMap:
          name: nginx-config
          items:
          - key: config
            path: site.conf
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        volumeMounts:
        - name: code
          mountPath: /var/www/html
        - name: config
          mountPath: /etc/nginx/conf.d

php-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php
  labels:
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php
      tier: backend
  template:
    metadata:
      labels:
        app: php
        tier: backend
    spec:
      volumes:
      - name: code
        hostPath:
          path: /code
      containers:
      - name: php
        image: php:7-fpm
        volumeMounts:
        - name: code
          mountPath: /var/www/html

代码/ index.php

<?php
  echo 'Hello World';

最重要的是,我通过互联网发现了这些东西。

当我运行此命令时,kubectl get pods这样的状态ContainerCreating对于Nginx部署将永远显示

NAME                    READY   STATUS              RESTARTS   AGE
nginx-64c9df788f-jxwzx   0/1     ContainerCreating    0          12h
php-55f974bb4-qvv9x      1/1     Running              0          25s

命令: kubectl describe pod nginx-64c9df788f-jxwzx

输出:

Name:           nginx-64c9df788f-jxwzx
Namespace:      default
Priority:       0
Node:           bablu-node/192.168.43.123
Start Time:     Mon, 11 May 2020 03:20:58 +0600
Labels:         app=nginx
                pod-template-hash=64c9df788f
                tier=backend
Annotations:    <none>
Status:         Pending
IP:             
IPs:            <none>
Controlled By:  ReplicaSet/nginx-64c9df788f
Containers:
  nginx:
    Container ID:   
    Image:          nginx
    Image ID:       
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /etc/nginx/conf.d from config (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-l2zp2 (ro)
      /var/www/html from code (rw)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  code:
    Type:          HostPath (bare host directory volume)
    Path:          /code
    HostPathType:  
  config:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      nginx-config
    Optional:  false
  default-token-l2zp2:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-l2zp2
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason       Age                    From                 Message
  ----     ------       ----                   ----                 -------
  Warning  FailedMount  31m (x14 over 147m)    kubelet, bablu-node  Unable to attach or mount volumes: unmounted volumes=[config], unattached volumes=[default-token-l2zp2 code config]: timed out waiting for the condition
  Warning  FailedMount  16m (x82 over 167m)    kubelet, bablu-node  MountVolume.SetUp failed for volume "config" : configmap "nginx-config" not found
  Warning  FailedMount  6m53s (x44 over 165m)  kubelet, bablu-node  Unable to attach or mount volumes: unmounted volumes=[config], unattached volumes=[code config default-token-l2zp2]: timed out waiting for the condition
  Warning  FailedMount  2m23s (x10 over 163m)  kubelet, bablu-node  Unable to attach or mount volumes: unmounted volumes=[config], unattached volumes=[config default-token-l2zp2 code]: timed out waiting for the condition

命令: kubectl get events -n default

输出:

LAST SEEN   TYPE      REASON              OBJECT                       MESSAGE
18m         Warning   FailedMount         pod/nginx-64c9df788f-jxwzx   MountVolume.SetUp failed for volume "config" : configmap "nginx-config" not found
8m45s       Warning   FailedMount         pod/nginx-64c9df788f-jxwzx   Unable to attach or mount volumes: unmounted volumes=[config], unattached volumes=[code config default-token-l2zp2]: timed out waiting for the condition
4m15s       Warning   FailedMount         pod/nginx-64c9df788f-jxwzx   Unable to attach or mount volumes: unmounted volumes=[config], unattached volumes=[config default-token-l2zp2 code]: timed out waiting for the condition
33m         Warning   FailedMount         pod/nginx-64c9df788f-jxwzx   Unable to attach or mount volumes: unmounted volumes=[config], unattached volumes=[default-token-l2zp2 code config]: timed out waiting for the condition
18m         Normal    Scheduled           pod/php-55f974bb4-qvv9x      Successfully assigned default/php-55f974bb4-qvv9x to bablu-node
18m         Normal    Pulled              pod/php-55f974bb4-qvv9x      Container image "php:7-fpm" already present on machine
18m         Normal    Created             pod/php-55f974bb4-qvv9x      Created container php
18m         Normal    Started             pod/php-55f974bb4-qvv9x      Started container php
18m         Normal    SuccessfulCreate    replicaset/php-55f974bb4     Created pod: php-55f974bb4-qvv9x
18m         Normal    ScalingReplicaSet   deployment/php               Scaled up replica set php-55f974bb4 to 1

谁能帮帮我吗?提前致谢!!

请问ROF

我运行了您的环境,这是我发现的主要问题:

  • 首先,您没有部署nginx-config,但这只是您的第一个问题,很容易解决(在下面的示例中有更多信息)。
  • 第二个问题(我认为是主要问题)是HostPath
    • 正如我在这里 所解释的HostPath要求容器进程以root身份运行。
    • php-fpm运行,www-data因此/code如果通过挂载此文件夹,他将无法使用挂载文件hostPath

从这里,我们的选择是:

  • 烘烤的图像内的php文件(或作为configmap)和运行在相同的吊舱都nginx的和PHP(共享一个emptydir文件夹),更有关本手册中这一过程:PHP-FPM,Nginx的,Kubernetes,和泊坞-而一方面,它涉及创建一个新的Docker映像,另一方面,如果您还没有存储映像,则无需配置它。
  • 使用外部存储将文件挂载到永久卷中,然后从外部存储库下载php文件。这种方法要求nginx和php在同一节点上运行-因为存储是RWO,这意味着只能以读写方式安装在一个节点上。由于您的设置位于单个节点上,因此在本示例中将使用此方法。

我试图重现您的示例,但是我必须进行一些更改。这些是文件:

  • cm-nginx.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
  labels:
    tier: backend
data:
  config : |
    server {
      index index.php index.html;
      error_log  /var/log/nginx/error.log;
      access_log /var/log/nginx/access.log;

      root /code;

      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }

      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass php:9000;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
  • root /code 指向要查找的目录 index.php
  • fastcgi_pass php:9000指向称为php服务监听的服务port 9000

  • 存储:

这是可变的,具体取决于您使用的存储类型。Minikube带有开箱即用配置的存储提供程序和storageclass。而且,尽管称为minikube存储提供程序,但minikube-hostpath它是一种CSI,它不需要在容器级别上的root访问权即可运行。

  • 话虽如此,这是pvc.yaml:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: code
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: standard

请注意,这standard是内置在minikube中的动态存储提供程序的名称。我们在这里要做的是创建一个PVC,以code供我们的应用程序运行。

  • php.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php
  labels:
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php
      tier: backend
  template:
    metadata:
      labels:
        app: php
        tier: backend
    spec:
      volumes:
      - name: code
        persistentVolumeClaim:
          claimName: code
      containers:
      - name: php
        image: php:7-fpm
        volumeMounts:
        - name: code
          mountPath: /code
      initContainers:
      - name: install
        image: busybox
        volumeMounts:
        - name: code
          mountPath: /code
        command:
        - wget
        - "-O"
        - "/code/index.php"
        - https://raw.githubusercontent.com/videofalls/demo/master/index.php
  • 在这里,我们使用busyboxinitContainer来获取此php文件(与您使用的php文件相同)并将其保存在已安装的卷中/code

  • PHP服务svc-php.yaml

apiVersion: v1
kind: Service
metadata:
  name: php
  labels:
    tier: backend
spec:
  selector:
    app: php
    tier: backend
  ports:
  - protocol: TCP
    port: 9000
  • Nginx部署nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      tier: backend
  template:
    metadata:
      labels:
        app: nginx
        tier: backend
    spec:
      volumes:
      - name: code
        persistentVolumeClaim:
          claimName: code
      - name: config
        configMap:
          name: nginx-config
          items:
          - key: config
            path: site.conf
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        volumeMounts:
        - name: code
          mountPath: /code
        - name: config
          mountPath: /etc/nginx/conf.d

这里的关键点,是安装PVC叫codemountPath /code和configmap我们正在创建作为monted一个名为文件site.conf夹内/etc/nginx/conf.d

  • Nginx服务svc-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    tier: backend
spec:
  type: NodePort
  selector:
    app: nginx
    tier: backend
  ports:
  - protocol: TCP
    port: 80

我正在使用NodePort简化输出测试。


再生产:

  • 让我们创建文件:首先是configmappvc,因为Pod正确启动需要它们,然后是服务和部署:
$ ls
cm-nginx.yaml  nginx.yaml  php.yaml  pvc.yaml  svc-nginx.yaml  svc-php.yaml

$ kubectl apply -f cm-nginx.yaml 
configmap/nginx-config created

$ kubectl apply -f pvc.yaml 
persistentvolumeclaim/code created

$ kubectl get cm
NAME           DATA   AGE
nginx-config   1      52s

$ kubectl get pvc
NAME   STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
code   Bound    pvc-b63559a0-a306-46f2-942b-0a063bc4ab6b   1Gi        RWO            standard       17s

$ kubectl apply -f svc-php.yaml 
service/php created

$ kubectl apply -f svc-nginx.yaml 
service/nginx created

$ kubectl apply -f php.yaml 
deployment.apps/php created

$ kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
php-69d5c956ff-8tjfn   1/1     Running   0          5s

$ kubectl apply -f nginx.yaml 
deployment.apps/nginx created

$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6854dcb7db-75zxt   1/1     Running   0          4s
php-69d5c956ff-8tjfn     1/1     Running   0          22s

$ kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx        NodePort    10.107.16.212   <none>        80:31017/TCP   41s
php          ClusterIP   10.97.237.214   <none>        9000/TCP       44s

$ minikube service nginx --url
http://172.17.0.2:31017

$ curl -i http://172.17.0.2:31017
HTTP/1.1 200 OK
Server: nginx/1.7.9
Date: Thu, 28 May 2020 19:04:48 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.4.6

Demo Test

在这里,我们可以看到由PHP 7驱动的nginx服务器curl返回200 OK的信息以及index.php文件的内容

我希望它可以帮助您对这种情况有更清晰的了解。

如有任何疑问,请在评论中告诉我。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular4&Webpack-300kb(经过优化)用于一个简单的“ hello world”应用程序?

配置流口水和Maven并使用它们编写Hello World应用程序

无法在Windows上使用本机映像编译简单的“ Hello World” Java应用程序

简单的VueJS Hello world应用程序无法正常工作

Hello World Android应用程序的大小

如何编译Scala Hello World应用程序

在OS X Swift应用程序中单击按钮时,Xcode如何打印“ Hello World”?

科尔多瓦“ Hello World”应用程序不会显示

如何用IntelliJ IDEA创建Hello World Web应用程序?

如何构建在.NET CoreCLR上运行的hello-world控制台应用程序?

为什么我不能启动这个简单的FXML hello world应用程序?

使用Web组件的Vanilla Hello World应用程序

基本Hello World应用程序未使用ndk-build构建(.mk文件)

OpenWRT的简单Hello World程序

在“ Hello World” Cordova应用程序中无法单击按钮

无法调试AWS SAM Hello World应用程序

无法在Android中运行简单的Hello World应用程序

对于简单的Hello World,应用程序输出仅显示任何内容

为什么Android应用RAM使用量会永远稳定增加?甚至Hello World应用程序

我编译的任何Android应用程序都需要一些权限,即使是Hello World应用程序

使用Servlet 4.0(Java EE 8)和HTTP / 2创建Hello World应用程序

Aurelia 快速入门或我的第一个应用程序 Hello World - No view model found 错误,2017

如何制作一个没有 HTML 标签的 Hello World 应用程序?

无法开始使用 Hello World Electron 应用程序示例

使用 spacemacs 和 clojure 运行 Hello World 应用程序时出错

如何使用 CLion 和 MSVC 编译器工具链编译 wxWidgets Hello World 应用程序而不会出错?

reactjs 应用程序 - 无法显示我的 hello world

在 hello world 应用程序中使用 docker CMD 而不是 application.properties

一个简单的 Hello World 应用程序 Spring Boot 上的 Whitelabel 错误