Use awslogs with kubernetes 'natively'

我的梦境 提交于 2020-01-24 10:50:05

问题


  1. I came up with a way of configuring k8s to use aws logs without any 3rd party service/application. All you have to do is to add the following lines in your master.yaml file:
spec:
  additionalPolicies:
    master: |
      [
        {
          "Effect": "Allow",
          "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"],
          "Resource": ["*"]
        }
      ]
    node: |
      [
        {
          "Effect": "Allow",
          "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"],
          "Resource": ["*"]
        }
      ]
  docker:
    logDriver: awslogs
    logOpt:
    - awslogs-region=eu-west-1
    - awslogs-group=<group-name> # make sure that this group already exist (create it manually)
    - tag={{.Name}}
  1. The last line is the most important one, and it will rename the log-stream for each pod to something readable instead of the docker hash.

  2. goes without saying that you have to update the cluster in order for the changes to take affect. (kops update cluster ${CLUSTER-NAME} --yes)

  3. That's it. Open AWS Cloudwatch and enjoy your logs :-)

  4. With that said, I have one problem. The log stream name contains much more info than what I would have wanted. Any idea how to trim the log stream name into simply the pod nice name?

  5. I have tried several ways of manipulating the 'tag' value (e.g. tag={{ with split .Name "_" }}{{ index . 2 }}{{end}} ), but it has failed the update operation.

  6. logstream name example: k8s_POD-NICE-NAME_POD-NICE-NAME-67c77758bf-8knn8_mind_24ed4160-5b5e-11e9-b53a-0a02b6d80d7c_1


回答1:


In this case you are using the Docker awslogs driver for logging. In which case you have to specify awslogs-stream or tag options to change the stream name from default. The tag is a bit more flexible and I think it will adjust to your requirements better, since it interprets Go template markup. This way you can have a more friendly stream name instead of the container ID.

From docker documentation:

Specify tag as an alternative to the awslogs-stream option. tag interprets Go template markup, such as {{.ID}}, {{.FullID}} or {{.Name}} docker.{{.ID}}. See the tag option documentation for details on all supported template substitutions.

When both awslogs-stream and tag are specified, the value supplied for awslogs-stream overrides the template specified with tag.

If not specified, the container ID is used as the log stream.

See tag and awslogs-stream options here: https://docs.docker.com/config/containers/logging/awslogs/



来源:https://stackoverflow.com/questions/55609398/use-awslogs-with-kubernetes-natively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!