Skip to content

Instantly share code, notes, and snippets.

@lalyos
Created June 26, 2018 15:06
Show Gist options
  • Select an option

  • Save lalyos/9de0afa65976749806be8ad600102d8d to your computer and use it in GitHub Desktop.

Select an option

Save lalyos/9de0afa65976749806be8ad600102d8d to your computer and use it in GitHub Desktop.
k8s nginx expose logs to a browser as http ws via sidecar

Install

apt-get -qq update
apt-get install -y curl unzip # jq neovim procps dnsutils
curl -LO https://github.com/joewalnes/websocketd/releases/download/v0.3.0/websocketd-0.3.0-linux_amd64.zip
cp ./websocketd /usr/local/bin/
unzip websocketd-0.3.0-linux_amd64.zip

loger script

mylog.sh

#!/bin/bash

curl \
  -s \
  -k \
  --no-buffer \
  -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"  \
  "https://kubernetes.default.svc.cluster.local/api/v1/namespaces/default/pods/$(hostname)/log?follow=true&tailLines=1"

Start websocket server

 websocketd --port 8765 ./mylogs.sh

ws expose port

kubectl port-forward web-7cff45f7bc-r72dc 8765:8765

open browser

open logs.html

html

content of logs.html

<!DOCTYPE html>
<pre id="log"></pre>
<script>
 // helper function: log message to screen
 function log(msg) {
   document.getElementById('log').textContent += msg + '\n';
 }

 // setup websocket with callbacks
 var ws = new WebSocket('ws://localhost:8765/');
 ws.onopen = function() {
   log('CONNECT');
 };
 ws.onclose = function() {
   log('DISCONNECT');
 };
 ws.onmessage = function(event) {
   log('MESSAGE: ' + event.data);
 };
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment