提问人:Dolphin 提问时间:3/4/2020 最后编辑:PatricioDolphin 更新时间:9/6/2023 访问量:93882
如何从 Kubernetes 获取部署日志?
How to get logs of deployment from Kubernetes?
问:
我正在 Kubernetes 集群 (v1.15.2) 中创建一个 InfluxDB 部署,这是我的 yaml 文件:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: monitoring-influxdb
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
task: monitoring
k8s-app: influxdb
spec:
containers:
- name: influxdb
image: registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-influxdb-amd64:v1.5.2
volumeMounts:
- mountPath: /data
name: influxdb-storage
volumes:
- name: influxdb-storage
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
task: monitoring
# For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
# If you are NOT using this as an addon, you should comment out this line.
kubernetes.io/cluster-service: 'true'
kubernetes.io/name: monitoring-influxdb
name: monitoring-influxdb
namespace: kube-system
spec:
ports:
- port: 8086
targetPort: 8086
selector:
k8s-app: influxdb
这是 pod 状态:
$ kubectl get deployment -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
coredns 1/1 1 1 163d
kubernetes-dashboard 1/1 1 1 164d
monitoring-grafana 0/1 0 0 12m
monitoring-influxdb 0/1 0 0 11m
现在,我已经等待了 30 分钟,仍然没有可用的 pod,如何从命令行检查部署日志?我现在无法访问 Kubernetes 仪表板。我正在搜索一个命令来获取 pod 日志,但现在没有可用的 pod。我已经尝试在节点中添加标签:
kubectl label nodes azshara-k8s03 k8s-app=influxdb
这是我的部署描述内容:
$ kubectl describe deployments monitoring-influxdb -n kube-system
Name: monitoring-influxdb
Namespace: kube-system
CreationTimestamp: Wed, 04 Mar 2020 11:15:52 +0800
Labels: k8s-app=influxdb
task=monitoring
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"extensions/v1beta1","kind":"Deployment","metadata":{"annotations":{},"name":"monitoring-influxdb","namespace":"kube-system"...
Selector: k8s-app=influxdb,task=monitoring
Replicas: 1 desired | 0 updated | 0 total | 0 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: k8s-app=influxdb
task=monitoring
Containers:
influxdb:
Image: registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-influxdb-amd64:v1.5.2
Port: <none>
Host Port: <none>
Environment: <none>
Mounts:
/data from influxdb-storage (rw)
Volumes:
influxdb-storage:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
SizeLimit: <unset>
OldReplicaSets: <none>
NewReplicaSet: <none>
Events: <none>
这是获取日志的另一种方法:
$ kubectl -n kube-system logs -f deployment/monitoring-influxdb
error: timed out waiting for the condition
此命令没有输出:
kubectl logs --selector k8s-app=influxdb
kube-system 命名空间中有我所有的 pod:
~/Library/Mobile Documents/com~apple~CloudDocs/Document/k8s/work/heapster/heapster-deployment ⌚ 11:57:40
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-569fd64d84-5q5pj 1/1 Running 0 46h
kubernetes-dashboard-6466b68b-z6z78 1/1 Running 0 11h
traefik-ingress-controller-hx4xd 1/1 Running 0 11h
答:
9赞
Tim O'Connell
3/4/2020
#1
您可以尝试获取部署的一些高级视图,也许可以在此处获取一些信息。kubectl describe deploy monitoring-influxdb
有关更详细的日志,请先获取 Pod: 然后,请求 Pod 日志:kubectl get po
kubectl logs <pod-name>
评论
0赞
Eduard Florinescu
10/5/2021
您能否告诉我如何查看其凭据已完成部署的 kubernetes 用户
150赞
Florian Ludewig
7/10/2020
#2
kubectl logs deployment/<name-of-deployment> # logs of deployment
kubectl logs -f deployment/<name-of-deployment> # follow logs
评论
3赞
Matteo
3/23/2022
有没有办法在日志中显示 pod-id?
1赞
Rotem jackoby
1/24/2021
#3
添加两个可能帮助您查看群集日志的出色工具的引用:
如果您希望在不使用“繁重”的第三方日志记录解决方案的情况下从终端查看日志,我会考虑使用 K9S,这是一个很棒的 CLI 工具,可以帮助您控制集群。
如果您不只绑定到 CLI,并且仍然希望在本地运行,我建议您在 Lens 上运行。
1赞
Petrisor Petrisor
9/6/2023
#4
您可以尝试:
kubectl get events -n <your_namespace>
评论
kubectl logs
没有给你所有细节?kubectl logs --selector app=yourappname
请尝试此操作以获取更多信息kubectl describe pod -n kube-system <influxdb-podName>