apache - Apache Web 服务器自定义索引 kubernetes
问题描述
我已经使用来自 dockerhub 的标准 httpd 映像在 kubernetes 集群上部署了一个 apache Web 服务器。我想对索引文件进行更改,以便它打印容器 ID 而不是默认索引文件。我怎样才能做到这一点?
解决方案
回答问题:
如何
Apache
在 Kubernetes 中拥有一个容器,该容器将在该文件index.html
或其他.html
文件中输出容器的 ID。
处理它的一种方法是lifecycle hooks
(特别是postStart
):
PostStart
这个钩子在容器创建后立即执行。但是,不能保证钩子会在容器 ENTRYPOINT 之前执行。没有参数传递给处理程序。
至于如何实现设置的示例:
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
要检查每个是否有Pod
,hostname.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
其他资源:
推荐阅读
- python - 使用 Levi-Civita 的矢量产品
- python - Keras CNN 模型为所有输入预测相同的值,并且在训练期间不会提高准确性
- python - NLTK 语料库:IndexError:列表索引超出范围
- string - 计算相同的多组字符的算法
- java - Android管理API服务文件分配
- spring-boot - Mockito 抛出 NullPointer 异常测试服务层
- opencv - 如何使用 OpenCV 4.2 保存 v4l2 缓冲区图像
- android - android 5.1 中 viewPager.setAdapter(adapter) 上的应用程序关闭(仅 android 5.1)
- emacs - 有没有办法设置标记,然后将输入的字符包含在区域中?
- python - 在 pandas 中创建新列,该列将根据条件获取值