mkdir ~/k3d

curl -s https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
CLUSTER=cnbc
IP4=$(/sbin/ip -o -4 addr list br2 | awk '{print $4}' | cut -d/ -f1)

# Get from TRAINER :)
MIRROR_IP=192.168.1.246
cat >registries.yml <<EOF 
mirrors:
  docker.io:
    endpoint:
      - "http://$MIRROR_IP:5001"
      - "https://mirror.gcr.io"
      - "https://docker.io"
EOF

# WARNING Check IP
k3d cluster create $CLUSTER \
  --api-port 8545 \
  -p "8580:80@loadbalancer" \
  -p "8543:443@loadbalancer" \
  --agents=2 \
  --k3s-arg "--tls-san=$IP4@server:0" \
  --k3s-arg "--disable=traefik@server:0" \
  --registry-create $CLUSTER-registry:0.0.0.0:5000 \
  --registry-config=./registries.yml
echo | \
    openssl s_client -connect $IP4:8545 2>/dev/null | \
    openssl x509 -text

PORT=$(docker container inspect \
    --format '{{ (index (index .NetworkSettings.Ports "5000/tcp") 0).HostPort }}' \
    $CLUSTER-registry)
sudo sh -c "echo 127.0.0.1 $CLUSTER-registry >>/etc/hosts"
curl cnbc-registry:5000/v2/_catalog
source <(k3d completion bash)


sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg  \
  | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" \
  | sudo tee -a /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update
sudo apt-get install -y kubectl

KUBE_VERSION=v1.22.13
curl -LO https://storage.googleapis.com/kubernetes-release/release/$KUBE_VERSION/bin/linux/amd64/kubectl
chmod +x kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version

kubectl cluster-info
kubectl get nodes

docker run --privileged -d -p 127.0.0.1:12375:2375 -e DOCKER_TLS_CERTDIR='' docker:dind
DOCKER_HOST=127.0.0.1:12375 docker container ls
docker --host=127.0.0.1:12375 container ls

Use Docker context remote

# start ssh-agent
DOCKER_SSH_IP=142.132.190.1
# allow ssh access!
# cat ~/.ssh/id_cnbc_ed25519.pub >>~/.ssh/authorized_keys
eval `ssh-agent`
ssh-add ~/.ssh/id_cnbc_ed25519


# password see chat
docker context create cnbc \
  --docker "host=ssh://cnbc@${DOCKER_SSH_IP},key=/home/cnbc/.ssh/id_cnbc_ed25519"

# accept host verification
ssh -i ~/.ssh/id_cnbc_ed25519 cnbc@${DOCKER_SSH_IP}
 
# use context 
docker context use cnbc
# remote docker access
docker container ls
# access local default context
docker --context=default container version
# switch back default
docker context use default

