k8s lab 01

Another day, another lab, this time something with k8s. I haven’t done anything with it, just some hello-world examples and the ocassional ranting of “we don’t need this here”. First I started with a simple python service (forget the bad code, we’ll fix it later)

from flask import Flask
import socket

app = Flask(__name__)

@app.route('/')
def hello():
    docker_short_id = socket.gethostname()
    return 'Hello, World! ' + docker_short_id

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

To expose the (flask) service with docker I had to add app.run(host='0.0.0.0')

Dockerfile:

FROM alpine:3.14

RUN apk add python3
RUN apk add py3-pip

RUN pip3 install flask

WORKDIR /opt/hello

ENV FLASK_APP=hello
ENV FLASK_ENV=development
CMD ["flask", "run", "--host=0.0.0.0"]

EXPOSE 5000

And a Makefile

build:
    docker build -t k8s-lab .

run:
    docker run -it -p 5000:5000 k8s-lab

shell:
    docker run -it -p 5000:5000 k8s-lab /bin/sh

For initial tests, I’m going with minikube

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
minikube kubectl -- get po -A
alias kubectl="minikube kubectl --"

Then, read some docs and finish the Learn Kubernetes Basic Tutorial

I also had to point the docker daemon to the minikube internal registry (and build the image again)

> minikube docker-env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.17.0.2:2376"
export DOCKER_CERT_PATH="/home/user/.minikube/certs"
export MINIKUBE_ACTIVE_DOCKERD="minikube"

# To point your shell to minikube’s docker-daemon, run:
# eval $(minikube -p minikube docker-env)

Finally, the deployment file (hello.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-lab-deployment
  labels:
    app: k8s-lab
spec:
  replicas: 3
  selector:
    matchLabels:
      app: k8s-lab
  template:
    metadata:
      labels:
        app: k8s-lab
    spec:
      containers:
      - name: k8s-lab
        image: k8s-lab:1
        ports:
        - containerPort: 80

Create and tag a new build (because we’re using minikube’s registry), start the deployment and create a service

make build
docker tag k8s-lab k8s-lab:1
kubectl apply -f hello.yaml
kubectl expose deployment k8s-lab --type=LoadBalancer --port=5000

Stocks plugin for sopel

Good ol' irc is the best way to keep in contact with my friends, since ‘98 or around. We use sopel for some stuff, for example, to track all the stocks we own.

This is a simple stocks plugin for sopel.

import sopel
import requests

@sopel.module.commands('dji','stocks','stock')
def stocks(bot,trigger):
    if trigger.group(2):
        url = "https://rest.yahoofinanceapi.com/v6/finance/quote"
        querystring = {"symbols": trigger.group(2)}
        headers = {'x-api-key': "FAKE_API_KEY"}
        try:
            response = requests.request("GET", url, headers=headers, params=querystring)
            stock = response.json()['quoteResponse']['result'][0]
        except requests.exceptions.RequestException as e:
            return bot.say("An error ocurred")
    else:
        return bot.say("Please use a valid symbol")

    change = '{0:.2f}'.format(float(stock['regularMarketChange']))
    percent = '{0:.2f}'.format(float(stock['regularMarketChangePercent']))

    return bot.say("{0}: {1} ({2}/{3})".format(stock['longName'], stock['regularMarketPrice'],"\x0304"+change+"\x0F" if float(change) < 0 else "\x0303"+change+"\x0F","\x0304"+percent+"%\x0F" if float(percent) < 0 else "\x0303+"+percent+"%\x0F",str(stock['regularMarketOpen'])))

Pwnagotchi 01

So, I decided to start with a simple (outside from comfort zone) project during my “funemployment” time and found pwnagotchi on my todo list (which is quite huge). After some reading I bought an rpi zero and a waveshare 2.1 display, along with one SD card. First mistake was to select the Vilros kit in amazon because it doesn’t have the 40 pins solded. Also Amazon suggests to add the display and it ended became a v3, instead of the v2 that was on the images.

Hence, as a side project, I had to soldier the pins, which was somehow easy, given my lack of expertise. And I ended with a new soldering iron and some equipment.

rpi zero soldering

Setting up the pwnagotchi was simple, just follow the instructions, install, configure and power on. However the display didnt work, and after some googling I just found that the v3 version is not supported.

The display (and soldering) was ok, I could check that it was working with the example located on the github repo:

Just clone the repo, and run python3 epd_2in13_V2_test.py

Later, I found some reddit thread with instructions to use that display (copying them here, just for reference):

Then:

  • Use "waveshare213d" in config.toml
  • Modify /usr/local/lib/python3.7/dist-packages/pwnagotchi/plugins/default/ups_lite.py
 def on_ui_setup(self, ui):
        ui.add_element('ups', LabeledValue(color=BLACK, label='UPS', value='0%/0V', position=(ui.width() / 2, 0),
                                           label_font=fonts.Bold, text_font=fonts.Medium))
  • Modify /usr/local/lib/python3.7/dist-packages/pwnagotchi/plugins/default/memtemp.py
    def on_ui_setup(self, ui):
        if ui.is_waveshare_v2():
            h_pos = (180, 80)
            v_pos = (180, 61)
        elif ui.is_waveshare_v1():
            h_pos = (170, 80)
            v_pos = (170, 61)
        elif ui.is_waveshare144lcd():
            h_pos = (53, 77)
            v_pos = (78, 67)
        elif ui.is_inky():
            h_pos = (140, 68)
            v_pos = (165, 54)
        elif ui.is_waveshare27inch():
            h_pos = (192, 138)
            v_pos = (216, 122)
        else:
            h_pos = (140, 68)
            v_pos = (175, 54)


        if self.options['orientation'] == "vertical":
            ui.set('memtemp',
                   " mem:%s%%\n cpu:%s%%\n temp:%s%s" % (self.mem_usage(), self.cpu_load(), temp, symbol))
        else:
            # default to horizontal
            ui.set('memtemp',
                   " mem cpu temp\n %s%% %s%% %s%s" % (self.mem_usage(), self.cpu_load(),

And voilá

pwnagotchi running

Vim Setup

Simple vim configuration

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Sets how many lines of history VIM has to remember
set history=700

" Enable filetype plugins
filetype plugin on
filetype indent on

" Set to auto read when a file is changed from the outside
set autoread

""use keys for navigating between tabs
nnoremap <C-Up> gT
nnoremap <C-Down> gt
nnoremap <C-n>   :tabnext<CR>
nnoremap <C-t>     :tabnew<CR>
nnoremap <C-b>     :tabprev<CR>

" Fast saving
nmap <leader>w :w!<cr>

"TAB settings.
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

" More Common Settings.
set encoding=utf-8
set scrolloff=3
set autoindent
set showmode
set showcmd
set hidden
set wildmenu
set wildmode=list:longest
set visualbell

"set cursorline
set ttyfast
set ruler
set backspace=indent,eol,start
set laststatus=2

"set relativenumber
set number
set norelativenumber

"set undofile
set shell=/bin/bash
set lazyredraw
set matchtime=3

syntax on

set term=builtin_ansi
if $TERM == 'screen'
    set term=xterm
endif