【发布时间】:2019-07-15 17:40:11
【问题描述】:
我正在尝试在 Pod 终止之前将日志传输到 S3。为此,我们需要
将我们的容器配置为具有 AWS-CLI。我成功地做到了 在 postStart 挂钩中使用脚本。
执行 AWS S3 命令将文件从 hostPath 传输到 S3 桶。差点有这个!!!
这是我的 Kube 部署(在 minikube 上运行):
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: logtransfer-poc
spec:
replicas: 1
template:
metadata:
labels:
app: logs
spec:
volumes:
- name: secret-resources
secret:
secretName: local-secrets
- name: testdata
hostPath:
path: /data/testdata
containers:
- name: logtransfer-poc
image: someImage
ports:
- name: https-port
containerPort: 8443
command: ["/bin/bash","-c","--"]
args: ["while true; do sleep 30; done;"]
volumeMounts:
- name: secret-resources
mountPath: "/data/apache-tomcat/tomcat/resources"
- name: testdata
mountPath: "/data/testdata"
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "cd /data/testdata/ && chmod u+x installS3Script.sh && ./installS3Script.sh > postInstall.logs"]
preStop:
exec:
command: ["/bin/sh", "-c", "cd /data/testdata/ && chmod u+x transferFilesToS3.sh && ./transferFilesToS3.sh > postTransfer.logs"]
terminationMessagePath: /data/testdata/termination-log
terminationGracePeriodSeconds: 30
imagePullSecrets:
- name: my-docker-credentials
installS3Script.sh
#!/bin/bash
apt-get update
curl -O https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py --user
chmod u+x get-pip.py
echo "PATH=$PATH:/root/.local/bin" >> ~/.bashrc && echo "Path Exported !!"
source ~/.bashrc && echo "Refreshed profile !"
pip3 install awscli --upgrade --user
mkdir -p ~/.aws
cp /data/testdata/config/config ~/.aws
cp /data/testdata/config/credentials ~/.aws
transferFilesToS3.sh
#!/bin/bash
# export AWS_DEFAULT_PROFILE=admin
echo "Transfering files to S3.."
aws s3 cp /data/testdata/data s3://testpratham --recursive --profile admin
aws s3 ls s3://testpratham --profile admin
echo "Transfer to S3 successfull !!"
失败的原因:transferFilesToS3.sh 运行成功但它不执行 AWS 命令..
什么有效:我创建了测试日志文件并将 aws 命令放在 postStart 挂钩 (installS3Script.sh) 中,它工作正常!!
我想我可能会以不同的方式研究 preStop 挂钩。我读了几篇关于lifecycle 和preStop 钩子的文章。还有一个关于使用preStop hook with grace period的相关问题。
感谢任何关于我可能遗漏的建议/帮助。
【问题讨论】:
标签: bash amazon-web-services kubernetes minikube kubernetes-deployment