Let's start with docker...

# Create user for kubernetes
# Note that you would need to run as root if going with kvm2 instead of docker

sudo su
adduser k8s # enter password twice and confirm to create the user

# Download kubectl
su k8s
mkdir bin && cd bin
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x kubectl

# Check that you have docker
docker ps
# If got 'command not found' you should install docker

# Let's assume we are running debian based linux (ex. ubuntu):
sudo su

apt install docker -y
usermod k8s -G sudo,docker

# sudo allows k8s user to become the root, docker allows us to control docker from k8s user

# Let's log-out and log-in to k8s account
# It is not windows - no need to restart, just press ctrl+d on every session with k8s logged and log-in again

# Let's check docker if it is working... in my case there was some problem I had to solve
docker ps
docker ps -a
service docker status
journalctl -xe
dockerd --debug
vim /etc/docker/daemon.json
service docker status
docker ps
service kubelet status

... TODO: installation of kubelet

# Check hello world app within docker
docker run hello-world
# Should display some output... not errors ;)

# Lets create a simple REST app using... python3 at first
cd ~/ && mkdir service1 && cd service1
vim service.py
#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys
class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        SimpleHTTPRequestHandler.end_headers(self)
    def do_GET(self):
        #if self.path == '/':
        #    return SimpleHTTPRequestHanfler
        #else:
            return SimpleHTTPRequestHandler.do_GET(self)
if __name__ == '__main__':
    test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8888)
chmod +x service.py

mkdir www
echo "<html><body>hello" > www/index.html
vim Dockerfile
FROM python:3
ADD service.py
COPY www /
CMD ["python", "service.py"]

docker build . -f Dockerfile
...
# Docker will build image downloading from remote servers
Sending build context to Docker daemon 5.12kB
Step 1/4 : FROM python:3
3: Pulling from library/python
376057ac6fa1: Pull complete
5a63a0a859d8: Pull complete
496548a8c952: Pull complete
2adae3950d4d: Pull complete
0ed5a9824906: Pull complete
bb94ffe72389: Pull complete
241ada007777: Pull complete
be68aa7d1eeb: Pull complete
820ffc2e28ca: Pull complete # Docker downloads multiple images to get system and many other components... like lego blocks
Digest: sha256:6fcd27ebeb1a5b4fd289ff15cb666e619c060c7b76f5a1b1a99d7cddb6de337a
Status: Downloaded newer image for python:3
---> 659f826fabf4
Step 2/4 : ADD service.py /
---> e8af4262a5b3
Step 3/4 : COPY www /
---> dddc31839c4a
Step 4/4 : CMD ["python", "service.py"]
---> Running in 5e14b64c5700
Removing intermediate container 5e14b64c5700
---> 730009d328e2
Successfully built 730009d328e2

docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 730009d328e2 2 minutes ago 934MB

# Docker created an image which has almost 1GB and has fully working operating system with python3 and our script included

docker run --name service1 --publish 8888:8888 -d 730009d328e2 # I have not specified a tag name thus I need to provide this long (no... it is short really) ID
fe7ea67e59ffb59fc0e4440782ea466acb29004fbb31ce44938e988df6750db7
# What does it mean:
# run - to run a container using image
# --name service1 - only to ease work with the container (later you use this name instead of ID)
# --publish - make a port (or a range of ports) from the container visible on our PC
# 8888:8888 - internal port 8888 seen as 8888 on our PC
# you can specify --publish many times to publish more ports
# -d - run in background (return to console after container is started)
#
730009d328e2 - image to run

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe7ea67e59ff 730009d328e2 "python service.py" 29 seconds ago Up 27 seconds 0.0.0.0:8888->8888/tcp service1

curl http://127.0.0.1:8888
<html><body>Hello

# Lets login to the running container and mess a little bit with it
# Note: normally you won't do this
# Note: usually container would have a stripped-down OS without even `ls` working
docker exec -it
fe7ea67e59ff sh
# ls
bin boot dev etc home index.html lib lib64 media mnt opt proc root run sbin service.py srv sys tmp usr var
# If the shell would be started as user (not root) then there is an option to run shell as root (uid=0)
# exit

# Let's clean before next steps
docker stop service1
fe7ea67e59ffb59fc0e4440782ea466acb29004fbb31ce44938e988df6750db7
docker rm
service1
service1
docker rmi 730009d328e2
Deleted: sha256:730009d328e29592aaf3776347cc6aa9c525761da5a71ab280774a3c859c4a96
Deleted: sha256:dddc31839c4aad3f32d2110ebbe6fff5cf2b92095b0dafd39e58714b4d471fb1
Deleted: sha256:11c5c23810f356153495152992deff467738e0fbccdee870bdb2b05b31fd184e
Deleted: sha256:e8af4262a5b31a7374d8f9dd666d8d3be0492651171661af13706c37744295e8
Deleted: sha256:21e702a6f45965efe121aea58732af27314a7bbbc9a7a8947090aa6b856ababc

Comments