FlorianOtel
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # EKS logging with EFK stack __Goal__: Set up logging of EKS clusters using EFK stack consisting of: - AWS hosted ElasticSearch - FluentD - Kibana. The logging should be for _BOTH_ the EKS control plane and pods (via FluentD) __Problem__: Logging for _EITHER_ control plane _OR_ pods work fine. Logging for _BOTH_ in the same EFK stack does not work. Note: I obfuscated things like account Id and IP addresses in case this needs to be shared further. I'm sure you understand. #### Setup description My environment consists of: - AWS EC2 Ubuntu instance, acting as a jump server. This is where most of the admin tasks are executed from. The EC2 instance has an IAM role with extensive privileges (though _not_ a blanket "AdminAccess"). This IAM role is called `fo20190822k8s` and has these extensive policies attached: ![](https://i.imgur.com/kvMwEFE.png) - An EKS cluster -- named `eksctl-mixed-asgs-20200301-1520`. The cluster has two ASGs (auto-scaling groups): One ASG with on-demand instances, and one ASG with spot instances. In this screenshot, it's the top two ASGs (selected). ![](https://i.imgur.com/CQjF2nm.png) ### Create a hosted ElasticSearch domain Instructions adapted from from [EKS Workshop - Logging with EFK](https://eksworkshop.com/intermediate/230_logging/): For ES (ElasticSearch) domain name I used same name as the K8S cluster (domain name has to be shorter than 28 characters) ```` aws es create-elasticsearch-domain \ --domain-name mixed-asgs-20200301-1520 \ --elasticsearch-version 6.3 \ --elasticsearch-cluster-config \ InstanceType=m5.large.elasticsearch,InstanceCount=3 \ --ebs-options EBSEnabled=true,VolumeType=standard,VolumeSize=100 \ --access-policies '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["es:*"],"Resource":"*"}]}' ```` __Important notes__: 1) The domain was created with fully open access. Creating this in an Isengard account created a flurry of tickets flagging this as a security issue. As such access to the ES domain was limited (below). 3) Regardless, the EFK logging problem can be replicated with fully open access, so it does does not _SEEM_ to be an access issue. Locking down the access to the ES domain is done like this (example adapted from https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html . Account IDs and Source IP obfuscated) ```` { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": [ "arn:aws:iam::123123123123:role/eksctl-mixed-asgs-20200301-1520-n-NodeInstanceRole-HM3STYCGR6YY", "arn:aws:iam::123123123123:role/eksctl-mixed-asgs-20200301-1520-n-NodeInstanceRole-1363S87DRY84T", "arn:aws:iam::123123123:role/fo20190822k8s" ] }, "Action": "es:*", "Resource": "*" }, { "Effect": "Allow", "Principal": { "AWS": ["*"] }, "Action": "es:*", "Condition": { "IpAddress": { "aws:SourceIp": [ "123.123.123.123/32" ] } }, "Resource": "*" } ] } ```` Notes: - The `fo20190822k8s` IAM is the "Administrator-like" role assumed by the jump server (and others, below). - The two other IAM roles are the `NodeInstanceRole` that were automatically created when the EKS cluster was created. These IAM roles are attached, respectively, to the EKS worker nodes in the two ASGs for the cluster (above). - The "Source IP" condition was to allow web acces from my web browser (remote, off-AWS) directly to the Kibana dashboard. ### Enabling EKS cluster control plane logging to CloudWatch Used the instructions at [EKS docs -- Control plane logging](https://docs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html) and at [EKS user guide on GitHub -- Control plane logging](https://github.com/awsdocs/amazon-eks-user-guide/blob/master/doc_source/control-plane-logs.md). Result: In CloudWatch, a log group with the name `/aws/eks/mixed-asgs-20200301-1520/cluster` was successfully created. ### Installing FluentD for pod log forwarding Instructions adapted from [EKS workshop - Deploy FluentD](https://eksworkshop.com/intermediate/230_logging/deploy/) ```` curl https://eksworkshop.com/intermediate/230_logging/deploy.files/fluentd.yml -o eksworkshop-fluentd.yaml sed -i".bak" -e "s#us-west-2#eu-north-1#g" -e "s#eksworkshop-eksctl#mixed-asgs-20200301-1520#g" -e "s#/eks/#/aws/eks/#g" eksworkshop-fluentd.yaml ```` __Notes__: - The YAML config file in the EKS workshop example is _very_ opaque... - Changing the region from `use-west-2` to `eu-north-1` (where my cluster is located). - (Matter of cosmetic preference): The default configuration file uses as log prefix `/eks/<cluster-name>`. However, to be consistent with the control plane logging -- which uses `/aws/eks/<cluster-name>` prefix -- adjusting the log prefix as well. Install it and check status ```` kubectl apply -f eksworkshop-fluentd.yaml serviceaccount/fluentd created clusterrole.rbac.authorization.k8s.io/fluentd created clusterrolebinding.rbac.authorization.k8s.io/fluentd created configmap/fluentd-config created daemonset.apps/fluentd-cloudwatch created kubectl describe daemonset.apps/fluentd-cloudwatch -n kube-system .... .... ```` __Result__: A second log group was created in CloudWatch, called `/aws/eks/mixed-asgs-20200301-1520/containers`. So overall, I have TWO log groups in Cloud Watch -- one for control plane, and one for pods: ![](https://i.imgur.com/IyKw8hH.png) ### Forwarding TWO log groups from CloudWatch __This is the tricky part since it involves Lambda functions__ Following instructions from [EKS workshop -- Configure CloudWatch](https://eksworkshop.com/intermediate/230_logging/configurecwl/) and [AWS Docs -- Streaming CloudWatch Logs Data to Amazon Elasticsearch Service](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_ES_Stream.html) Following those instructions: - From the CloudWatch console, select one of the two log groups. - "Actions" -> "Stream to Amazon Elastic Service" - In my case select as "Amazon ES cluster" the ES domain created above. - As Lambda IAM Execution Role select `fo20190822k8s` (the Administrator-like role. As per above, this role has full access to CloudWatch policy attached). - Select Common Log format in next screen. (Note: The log format pattern is inadequate for BOTH control plane logs and pod logs, but it can be adjusted -- somewhat. More on this, later). - Create the filter, and start streaming logs. Result: - A new log group (3rd log group) is created in CloudWatch for the Lambda function thus created. - The CloudWatch logs are sent to ES domain, and can be seen in Kibana (after configuring Kibana, as per the instructions at [EKS workshop -- Configure CloudWatch](https://eksworkshop.com/intermediate/230_logging/configurecwl/)) #### Problem Doing those steps for _any_ of the two log groups -- for the control plane OR for pods, doesn't matter -- works. CloudWatch logs are in streamed to ES and can be seen in Kibana. __HOWEVER__, trying to do the same for both log groups does __NOT__ work. Note, the result is the same no matter if I create the function for control plane log group and then do the same for the pod log group, or the other way around. That's why below "first log group" or "second log group" can be either of the two log groups. Things I attempted #### Alternative 1: Trying repeat the steps above for "the other" log group Instead of creating another lambda function for the second log group, the following message appears: ![](https://i.imgur.com/R7cZ1ly.png) Following the same steps as before, it does create a subscription filter for the second log group. However, the Lambda function -- which work fine for the fist log group -- issues only errors for the second log group. From the CloudWatch log for the Lambda function: ![](https://i.imgur.com/17IwzD4.png) #### Alternative 2: In the Lambda GUI try to add another trigger for the second log group. In the GUI console for the Lambda function that was created, in the "Configuration" tab, under "Designer", it is possible to "Add trigger" and create a forwarder for the 2nd log group (see screenshot below). Also, in this Lambda GUI it is possible: - To Enable / Disable any of the two forwarders. - Adjust the filter pattern (not possible in the "Alternative 1" above). Result: The function issues the same error as above for the second log group (the one for which the trigger was added after creation). Disabling - in the "Designer" -- the trigger for the second log group makes the function work fine again (no errors). Disabling the original trigger / the trigger for the first log group (the one automatically created when the function was created via CloudWatch) and leaving only the manually added trigger issues same error. This latter case is in this screenshot -- function initially created for the control plane log group, then a 2nd trigger attemped for the pod logs, then the first one disabled to isolate only errors: ![](https://i.imgur.com/REkihoc.png) __Note__: Disabling / Enabling trigger requires saving / updating the lambda function ### Alternative 3: Try to manually create a separate Lambda function No matter which log group was chosen as "first log group" -- log group for control plane or pods -- the resulting lambda function code was identical (Node.js code). Only the triggers (???) and possibly something else that I'm missing (???) was different between the two situations. As such, after creating -- from the CloudWatch GUI -- the lambda function for the first log group, I tried to manually create a Lambda function for the 2nd log group: - Downloaded the (working) Lambda function for the first log group as a file - From Lambda GUI -> "Create Function" - "Use blueprint" - Search "cloudwatch" in the "Blueprints" search box - Used "cloudwatch-logs-process-data" as blueprint - In the "Configure" GUI: - Provide name - Use existing IAM role -- same as above - As "CloudWatch trigger" select the second log group - Select fiter pattern and name - Enable filter checkbox __Result__: - The Lambda function is created and a CloudWatch log group (4th log group) is created for this function. - All log entries for the function issue the same error as above. - This is even if the first function -- the one created via CloudWatch -- has the trigger disabled, or the function is deleted. Also, manually creating the function using this steps (i.e. not from the CloudWatch GUI) issues the same error even in the absence of any other Lambda function / log streamer. Questions: 1) What does the message stem from: > You already have a Lambda function associated with your .. <name>.. Amazon ElasticSearch cluster and this setup will be reusing the Lambda function... <>... to get your log data delivered to the cluster ? 2) How can this be done when "manually" creating a Lambda function. When doing so, are there any other steps needed to successfully stream logs from CloudWatch to ES ? 3) If creating two Lambda functions "associated with the ES cluster" (whatever that means...) is unnecessary and/or impossible, how can the first function be re-used for the 2nd log group 4) (Optional) The Common Log format -- with filter pattern ` [host, ident, authuser, date, request, status, bytes]` seems inadequate for either control plane logs and FluentD logs (slightly different), as fields are not mapped entirely correct. How can that be adjusted so that fields are mapped correctly ? Specify a JSON unmarshalling struct ? Other ? *** Author: Nick Brandaleone Date: March 14, 2020 (also known as PI day!) I was able to get two streams of logs into the ElasticSearch cluster. However, I had to make a few changes for it to work. 1. I created a new lambda function. It had a different name, but nearly the same code as the AWS provided function. I copied the original function into the code editor, and made only minor changes. 2. I manually created a trigger for this new function. The trigger was the second CloudWatch *log_group* we wanted to observe inside the ES cluster. I had to create this **manually**, as it was not possible to use the CW *subscription* feature. The subscription feature only can handle a single lambda function for streaming - since I needed two functions, it was not suitable (although I did use it for the first function/stream). 3. I altered the **index** name of the log stream, inside the lambda function. I found during debugging the second stream was being denied by the ElasticSearch cluster due to an index clash. I do not know enough about ES to determine if there is an easier way to solve this problem. However, by hard-coding the steam into a new index - it worked. 4. I had to go into the ES cluster, and identify the new **index**. Once done, I was able to toggle between the two different indexes, and see the two different *log_groups*. ## Altered lambda function - index name changed > // index name format: cwl-YYYY.MM.DD var indexName = [ 'kubernetes-' + timestamp.getUTCFullYear(), // year ('0' + (timestamp.getUTCMonth() + 1)).slice(-2), // month ('0' + timestamp.getUTCDate()).slice(-2) // day ].join('.'); ## Lambda debugging. Fortunately, there is a toggle in the provided function which helped > // Set this to true if you want to debug why data isn't making it to // your Elasticsearch cluster. This will enable logging of failed items // to CloudWatch Logs. var logFailedResponses = true; // Set to false normally. It was by turning on debugging on both the lambda function and the ES cluster, I was able to determine that CloudWatch was sending the logs to the ES cluster. The cluster was responding with a 400 error. By examining the error and some Googling, I was able to confirm the index clash *error*. ![](https://i.imgur.com/kMMsFdr.png) Finally, I should admit that while I did install fluentd for log-shipping, I used the simplified *container insights* installation process. This created several log streams, but not the same ones you used from the **eksworkshop**. I am sure our root cause is the same, but our *log_groups* are slightly different. ### Container Insights installation > curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/daemonset/container-insights-monitoring/quickstart/cwagent-fluentd-quickstart.yaml | sed "s/{{cluster_name}}/cluster-name/;s/{{region_name}}/cluster-region/" | kubectl apply -f - The above command will create 5 *log groups*, which populate the CloudWatch Dashboards, and provide various logging of the control plane and worker nodes. I have not played around with it enough to know all the details yet. **Good night**. I hope this is useful. I personally find the *IAM* credentials to be a huge burden to some of our products. However, security first is not a bad motto. If you find easier methods please send them my way. I think that *Prometheus* is the superior solution for many reasons - however, ElasticSearch is a powerful product. I am sure many customers will certainly be using it. This error was really an ES configuration issue (i.e. customer responsibility!!!!) :-)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully