# 日志

## 获取envoy的访问日志

最简单的Istio日志记录是 Envoy的访问日志记录。envoy代理将访问信息打印到其标准输出中。然后可以通过kubectl logs命令打印Envoy容器的标准输出。

## 先决条件

* 按照安装指南中的说明安装Istio 。

> 如果安装demo 配置概要文件,则将启用出口网关和访问日志记录。

* 部署sleep示例应用程序以用作发送请求的测试源。如果 启用了自动边车注入功能,请运行以下命令来部署示例应用程序:

```
$ kubectl apply -f samples/sleep/sleep.yaml
```

否则,请在sleep使用以下命令部署应用程序之前手动注入sidecar :

```
$ kubectl apply -f <(istioctl kube-inject -f samples/sleep/sleep.yaml)
```

> 您可以将curl已安装的任何Pod用作测试源。

* 将SOURCE\_POD环境变量设置为源容器的名称:

```
$ export SOURCE_POD=$(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name})
```

* 启动httpbin示例。

如果启用了自动边车注入,请部署httpbin服务:

```
$ kubectl apply -f samples/httpbin/httpbin.yaml
```

否则,您必须在部署httpbin应用程序之前手动注入sidecar :

```
$ kubectl apply -f <(istioctl kube-inject -f samples/httpbin/httpbin.yaml)
```

## 启用Envoy的访问日志

如果使用IstioOperatorCR安装Istio,请在配置中添加以下字段:

```
spec:
  meshConfig:
    accessLogFile: /dev/stdout
```

否则,将等效设置添加到原始istioctl install命令中,例如:

```
$ istioctl install <flags-you-used-to-install-Istio> --set meshConfig.accessLogFile=/dev/stdout
```

您还可以通过设置accessLogEncoding为JSON或在JSON和文本之间进行选择TEXT。

您可能还想通过编辑自定义访问日志的 格式。accessLogFormat

有关这三个设置的更多信息,请参考全局网格选项:

meshConfig.accessLogFile meshConfig.accessLogEncoding meshConfig.accessLogFormat

## 测试访问日志

1. 发送请求sleep至httpbin:

```
$ kubectl exec "$SOURCE_POD" -c sleep -- curl -v httpbin:8000/status/418
...
< HTTP/1.1 418 Unknown
< server: envoy
...
    -=[ teapot ]=-

       _...._
     .'  _ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`
```

1. 检查sleep的日志:

```
$ kubectl logs -l app=sleep -c istio-proxy
[2020-10-30T12:36:44.547Z] "GET /status/418 HTTP/1.1" 418 - "-" 0 135 25 24 "-" "curl/7.69.1" "f13c2118-3ef9-9ed9-91b7-5d21358029c3" "httpbin:8000" "10.244.0.30:80" outbound|8000||httpbin.default.svc.cluster.local 10.244.0.29:46348 10.96.148.56:8000 10.244.0.29:44678 - default
```

1. 检查httpbin的日志:

```
$ kubectl logs -l app=httpbin -c istio-proxy
[2020-10-30T12:36:44.553Z] "GET /status/418 HTTP/1.1" 418 - "-" 0 135 3 2 "-" "curl/7.69.1" "f13c2118-3ef9-9ed9-91b7-5d21358029c3" "httpbin:8000" "127.0.0.1:80" inbound|8000|| 127.0.0.1:42940 10.244.0.30:80 10.244.0.29:46348 outbound_.8000_._.httpbin.default.svc.cluster.local default
```

注意,对应于该请求的消息出现在源和目的地两者的Istio代理日志,sleep和httpbin分别。您可以在日志中看到HTTP动词(GET),HTTP路径(/status/418),响应代码(418)和其他与请求相关的信息。

## 清理

* 关闭sleep和httpbin服务:

```
$ kubectl delete -f samples/sleep/sleep.yaml
$ kubectl delete -f samples/httpbin/httpbin.yaml
```

* 禁用Envoy的访问日志

删除或设置为Istio安装配置中""的meshConfig.accessLogFile设置。

在下面的示例中,替换default为安装Istio时使用的配置文件的名称。

```
$ istioctl install --set profile=default
✔ Istio core installed
✔ Istiod installed
✔ Ingress gateways installed
✔ Installation complete
```
