首页 > 解决方案 > Apache Web 服务器自定义索引 kubernetes

问题描述

我已经使用来自 dockerhub 的标准 httpd 映像在 kubernetes 集群上部署了一个 apache Web 服务器。我想对索引文件进行更改,以便它打印容器 ID 而不是默认索引文件。我怎样才能做到这一点?

标签: apachekubernetes

解决方案


回答问题:

如何Apache在 Kubernetes 中拥有一个容器,该容器将在该文件index.html或其他.html文件中输出容器的 ID。

处理它的一种方法是lifecycle hooks(特别是postStart):

PostStart

这个钩子在容器创建后立即执行。但是,不能保证钩子会在容器 ENTRYPOINT 之前执行。没有参数传递给处理程序。

-- Kubernetes.io:文档:概念:容器:容器生命周期钩子:容器钩子


至于如何实现设置的示例:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apache
  labels:
    app: apache
spec:
  replicas: 3
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - name: apache
        image: httpd # <-- APACHE IMAGE
        # LIFECYCLE DEFINITION START 
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "echo $HOSTNAME > htdocs/hostname.html"]
        # LIFECYCLE DEFINITION END
        ports:
        - containerPort: 80

具体来看:

  • command: ["/bin/sh", "-c", "echo $HOSTNAME > htdocs/hostname.html"]

这部分会将容器的主机名写入/保存到hostname.html

要检查每个是否Podhostname.html您可以创建一个Service并运行:

  • $ kubectl port-forward svc/apache 8080:80->curl localhost:8080/hostname.html
  • $ kubectl run -it --rm nginx --image=nginx -- /bin/bash->curl apache/hostname.html

其他资源:


推荐阅读