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