docker默认的日志驱动是json-file,启动容器时配置日志
#日志驱动
--log-driver json-file
#文件的大小
--log-opt max-size=[0-9+][k|m|g]
#文件数量
--log-opt max-file=[0-9+]
全局日志设置
/etc/docker/daemon.json
{
"log-driver":"json-file",
"log-opts": {"max-size":"100m", "max-file":"5"}
}
使用命令查看容器日志
root@k8s-201:~# docker logs -f e7aa88ddcba4
I1120 02:09:16.756192 1 server.go:625] external host was not specified, using 192.168.3.201
I1120 02:09:16.756603 1 server.go:163] Version: v1.19.3
I1120 02:09:17.584404 1 plugins.go:158] Loaded 12 mutating admission controller(s) successfully in the following order: NamespaceLifecycle,LimitRanger,ServiceAccount,NodeRestriction,TaintNodesByCondition,Priority,DefaultTolerationSeconds,DefaultStorageClass,StorageObjectInUseProtection,RuntimeClass,DefaultIngressClass,MutatingAdmissionWebhook.
I1120 02:09:17.584517 1 plugins.go:161] Loaded 10 validating admission controller(s) successfully in the following order: LimitRanger,ServiceAccount,Priority,PersistentVolumeClaimResize,RuntimeClass,CertificateApproval,CertificateSigning,CertificateSubjectRestriction,ValidatingAdmissionWebhook,ResourceQuota.
I1120 02:09:17.586224 1 plugins.go:158] Loaded 12 mutating admission controller(s) successfully in the following order: NamespaceLifecycle,LimitRanger,ServiceAccount,NodeRestriction,TaintNodesByCondition,Priority,DefaultTolerationSeconds,DefaultStorageClass,StorageObjectInUseProtection,RuntimeClass,DefaultIngressClass,MutatingAdmissionWebhook.
在宿主机上查看容器日志
docker inspect e7aa88ddcba4 #查看容器信息,确定日志路径
"Image": "sha256:a301be0cd44bb11162da49b9c55fc5d137f493bdefcf80226378204be403fa41",
"ResolvConfPath": "/var/lib/docker/containers/441e042ef45932d4cb242872ad4ce996777dce344a3ee9cc8ff9e1e81ed27f97/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/441e042ef45932d4cb242872ad4ce996777dce344a3ee9cc8ff9e1e81ed27f97/hostname",
"HostsPath": "/var/lib/kubelet/pods/8687149f58afed2eac19ee6af05f5594/etc-hosts",
"LogPath": "/var/lib/docker/containers/e7aa88ddcba41786963245267714302f7587296c3d2dd7fd62b3132ca8bc7c93/e7aa88ddcba41786963245267714302f7587296c3d2dd7fd62b3132ca8bc7c93-json.log",
"Name": "/k8s_kube-apiserver_kube-apiserver-k8s-201_kube-system_8687149f58afed2eac19ee6af05f5594_17",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
root@k8s-201:~# tail -5f /var/lib/docker/containers/e7aa88ddcba41786963245267714302f7587296c3d2dd7fd62b3132ca8bc7c93/e7aa88ddcba41786963245267714302f7587296c3d2dd7fd62b3132ca8bc7c93-json.log
{"log":"I1120 02:32:02.043064 1 passthrough.go:48] ccResolverWrapper: sending update to cc: {[{https://127.0.0.1:2379 \u003cnil\u003e 0 \u003cnil\u003e}] \u003cnil\u003e \u003cnil\u003e}\n","stream":"stderr","time":"2020-11-20T02:32:02.043328833Z"}
{"log":"I1120 02:32:02.043074 1 clientconn.go:948] ClientConn switching balancer to \"pick_first\"\n","stream":"stderr","time":"2020-11-20T02:32:02.04334572Z"}
{"log":"I1120 02:32:36.203547 1 client.go:360] parsed scheme: \"passthrough\"\n","stream":"stderr","time":"2020-11-20T02:32:36.203759769Z"}
{"log":"I1120 02:32:36.203937 1 passthrough.go:48] ccResolverWrapper: sending update to cc: {[{https://127.0.0.1:2379 \u003cnil\u003e 0 \u003cnil\u003e}] \u003cnil\u003e \u003cnil\u003e}\n","stream":"stderr","time":"2020-11-20T02:32:36.204023042Z"}
{"log":"I1120 02:32:36.204089 1 clientconn.go:948] ClientConn switching balancer to \"pick_first\"\n","stream":"stderr","time":"2020-11-20T02:32:36.204191905Z"}