Sign inSign up

neshkeev/k8s

By neshkeev

•Updated over 2 years ago

Kubernetes in Docker container with kubectl and SSH server

Image
0

547

neshkeev/k8s repository overview

⁠Kubernetes-In-Docker

Kubernetes-In-Docker⁠ is an image that allows running Kubernetes cluster in a docker container

The main differences of this image from the official kind image⁠ are:

  • kubectl can be executed from the docker container,
  • SSH server.

This image might come in handy in complex docker compose environments where kubectl commands are needed. Connect to the k8s container through SSH and and execute kubectl commands.

The Dockerfile⁠ can be found on GitHub⁠

⁠Quick Start

  1. Create a directory:
mkdir example
  1. Enter the created directory:
cd example
  1. Download docker-compose.yml:
curl -LO https://raw.githubusercontent.com/neshkeev/containers/master/k8s/example/single-node/docker-compose.yml
  1. Start the docker compose services:
docker compose up -d
  1. Wait until the k8s cluster starts:
docker compose logs k8s -f
  1. Run the hello-world pod from the k8s container:
docker compose exec -it k8s kubectl run hello-world --image=hello-world --image-pull-policy=Always
  1. Check if the pod is available:
docker compose exec -it k8s kubectl get pods
  1. Check the state of the pod:
docker compose exec -it k8s kubectl describe pod/hello-world
  1. Check the logs of the pod:
docker compose exec -it k8s kubectl logs pod/hello-world
  1. Remove the pod:
docker compose exec -it k8s kubectl delete pod/hello-world
  1. Check if the pod has gone:
docker compose exec -it k8s kubectl get pods
⁠Execute kubectl commands through SSH
  1. Run the hello-world pod from the k8s container:
docker compose exec -it k8s ssh root@localhost 'kubectl run hello-world --image=hello-world --image-pull-policy=Always'
  1. Check if the pod is available:
docker compose exec -it k8s ssh root@localhost 'kubectl get pods'
  1. Check the state of the pod:
docker compose exec -it k8s ssh root@localhost 'kubectl describe pod/hello-world'
  1. Check the logs of the pod:
docker compose exec -it k8s ssh root@localhost 'kubectl logs pod/hello-world'
  1. Remove the pod:
docker compose exec -it k8s ssh root@localhost 'kubectl delete pod/hello-world'
  1. Check if the pod's gone
docker compose exec -it k8s ssh root@localhost 'kubectl get pods'
⁠Stop docker compose services
  1. Check if the pod's gone
docker compose exec -it k8s kind delete cluster
  1. Stop the docker compose services
docker compose down

⁠Multi-node k8s cluster

In order to start a multi-cluster k8s setup execute the following commands:

  1. Create a directory:
mkdir -p example/conf
  1. Enter the created directory:
cd example
  1. Download docker-compose.yml:
curl -LO https://raw.githubusercontent.com/neshkeev/containers/master/k8s/example/multi-node/docker-compose.yml
  1. Download the config.yml kind (Kubernetes-In-Docker) config file:
curl -Lo conf/config.yml https://raw.githubusercontent.com/neshkeev/containers/master/k8s/example/multi-node/conf/config.yml
  1. Start the docker compose services:
docker compose up -d
  1. Wait until the k8s cluster starts:
docker compose logs k8s -f
  1. Check if the k8s cluster consists of three nodes:
docker compose exec -it k8s kubectl get nodes
⁠Stop docker compose services
  1. Check if the pod's gone
docker compose exec -it k8s kind delete cluster
  1. Stop the docker compose services
docker compose down

⁠Further configuration

⁠K8s configuration

This docker images spins kind⁠, so all the kind's configuration⁠ can be applied.

Please keep in mind that you need to:

  1. Mount⁠ your config.yaml⁠ file into the k8s container,
  2. (Optionally) point⁠ the K8S_CONFIG_FILE environment variable to the mounted file on the container's file system.

Setting the K8S_CONFIG_FILE environment variable can be omitted, if you mount the config file at the /etc/kubernetes/config.yml mount point.

⁠Known Issues

⁠SSH server crushes

Issue: a k8s container's SSH server crushes on restart

Solution: remove the container and start it again

⁠kubectl fails with ... no such host

Issue: When I execute kubectl get nodes or another commands I get:

$ kubectl get nodes

E1129 19:26:09.617992     758 memcache.go:265] couldn't get current server API group list: Get "https://kind-control-plane:6443/api?timeout=32s": dial tcp: lookup kind-control-plane on 127.0.0.11:53: no such host

Solution: Please make sure the network of the k8s container is named kind⁠

Tag summary

Content type

Image

Digest

sha256:473e48bf2…

Size

261.8 MB

Last updated

over 2 years ago

docker pull neshkeev/k8s