Skip to content

Instantly share code, notes, and snippets.

@mbodo
Last active November 25, 2024 22:19
Show Gist options
  • Select an option

  • Save mbodo/e72ebe037bb96d7be93040979a4368a1 to your computer and use it in GitHub Desktop.

Select an option

Save mbodo/e72ebe037bb96d7be93040979a4368a1 to your computer and use it in GitHub Desktop.
k8s.md

Kubernetes (K8s)

Installation Minikube KVM (WIP)

Installation of Minikube in KVM Centos7 VM image with --vm-driver=none

Installation KVM image

Create Centos 7 KVM Image

  1. List available images
$ sudo virt-builder --list | grep -i --color centos

centos-6                 x86_64     CentOS 6.6
centos-7.0               x86_64     CentOS 7.0
centos-7.1               x86_64     CentOS 7.1
centos-7.2               aarch64    CentOS 7.2 (aarch64)
centos-7.2               x86_64     CentOS 7.2
centos-7.3               x86_64     CentOS 7.3
centos-7.4               x86_64     CentOS 7.4
centos-7.5               x86_64     CentOS 7.5
centos-7.6               x86_64     CentOS 7.6
centos-7.7               x86_64     CentOS 7.7
centos-8.0               x86_64     CentOS 8.0

Pick centos-7.7

  1. Get default pool-list
$ sudo virsh pool-list | grep -i --color default
default              active     yes
  1. Get default pool path
$ sudo virsh pool-dumpxml default | grep -oP "<path>(.*)</path>" | cut -d ">" -f 2 | cut -d "<" -f 1
/opt/user/kvm
  1. Change directory to default pool path
$ cd /opt/user/kvm
  1. Create qcow2 image
<your_password> - default password to created centos image

$ sudo virt-builder centos-7.7 --arch x86_64 --size 100G -m 8192 --root-password password:<your_password> --format qcow2
  1. Create minikube KVM Centos virtual machine instance
$ sudo virt-install --name minikube --ram 8192 --vcpus=4 --disk path=/opt/user/kvm/centos-7.7.qcow2 --rng /dev/urandom --os-variant centos7.0 --import

Setup Centos 7 KVM Image environment

  1. Switch off selinux for current session
Check status first:

$ setstatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

if SELinux status: enabled than:

sudo setenforce 0
  1. Switch off selinux permanently
$vi /etc/selinux/config

SELINUX=disabled
  1. Disable firewalld systemd service
$ sudo systemctl stop firewalld
$ sudo systemctl disable firewalld
  1. Reboot VM
$ sudo reboot

Note: We switch off selinux and firewalld service, because we expect that Minikube installation will be used only for test/development purposes

Install Docker daemon

  1. Find Kubernetes required Docker version Kubernetes - CHANGELOG-1.16

  2. Install required packages see Docker

$ sudo yum install iptables git procps-ng xz
  1. Download the Docker binaries package
$ curl -L https://download.docker.com/linux/static/stable/x86_64/docker-18.09.9.tgz -o docker-18.09.9.tgz
  1. Install binaries
$ sudo tar -xvzf docker-18.09.9.tgz -C /usr/local/ && sudo ln -s /usr/local/docker/* /usr/local/bin
  1. Create systemd Docker service docker.service configuration
$ sudo vi /etc/systemd/system/docker.service
# /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/local/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target
  1. Create systemd Docker service socket docker.socket configuration
$ sudo vi /etc/systemd/system/docker.socket
[Unit]
Description=Docker Socket for the API
PartOf=docker.service

[Socket]
# If /var/run is not implemented as a symlink to /run, you may need to
# specify ListenStream=/var/run/docker.sock instead.
ListenStream=/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker

[Install]
WantedBy=sockets.target
  1. Add docker group
$ sudo groupadd -g 1001 docker
  1. Add docker group to developer user
$ sudo usermod -a -G docker developer
  1. Create containerd systemd service containerd.service configuration
sudo vi /etc/systemd/system/containerd.service
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd
KillMode=process
Delegate=yes
LimitNOFILE=1048576
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity

[Install]
WantedBy=multi-user.target
  1. Create /run/containerd directory
$ sudo mkdir -p /run/containerd && sudo chmod -v 0711 /run/containerd
  1. Start services
$ sudo systemctl daemon-reload && sudo systemctl start containerd.service && systemctl start docker.service

Installing minikube

  1. yum install -y socat
  2. Documentation - Getting Started - Linux - None (bare-metal)

Configuring minikube

Minikube rootless

  1. Move /root/.kube to /home/developer
sudo mv /root/.kube /home/developer/.kube # this will write over any previous configuration
sudo chown -R developer:users /home/developer/.kube
  1. Move /root/.kube to /home/developer
sudo mv /root/.minikube /home/developer/.minikube # this will write over any previous configuration
sudo chown -R developer:users /home/developer/.minikube
  1. Modify paths in `/home/developer/.kube/config
apiVersion: v1
clusters:
- cluster:
    certificate-authority: ../.minikube/ca.crt
    server: https://192.168.122.142:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: ../.minikube/client.crt
    client-key: ../.minikube/client.key

Minikube kubectl bash completion

  1. kubectl bash completion

Minikube bash completion

  1. Add to ~/.bashrc
source <(minikube completion bash)
  1. Relogin as current user or
source ~/.bashrc

Troubleshooting

  • Failed to get system container stats for "/system.slice/docker.service"
systemctl status -l kubelet
...
Jan 04 20:51:28 localhost.localdomain kubelet[808]: E0104 14:51:28.377869     808 summary_sys_containers.go:47] Failed to get system container stats for "/system.slice/docker.service": failed to get cgroup stats for "/system.slice/docker.s
ervice": failed to get container info for "/system.slice/docker.service": unknown container "/system.slice/docker.service"
...
  • Add to kubelet.service
--runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice

See: kubelet-fails-to-get-cgroup-stats-for-docker-and-kubelet-services

  • ! VM may be unable to resolve external DNS records
yum install bind-utils
  • Purge minikube generated files

see minikube failed to start on Ubuntu 18.04 with VirtualBox

Links

@new-php

new-php commented Nov 25, 2024

Copy link
Copy Markdown

When I was trying to install Minikube with Docker on a CentOS 7 KVM virtual machine, I encountered several challenges related to SELinux settings, Docker configuration, and troubleshooting Kubernetes services. The configuration for Docker and containerd services was tricky, and I ran into several errors related to systemd, as well as issues with external DNS resolution.

After spending hours trying to resolve these, I turned to Vultr’s documentation, and it was a game-changer. The Minikube setup guide on CentOS 7 provided detailed, step-by-step instructions that helped me set up Minikube with Docker quickly and correctly. It covered the installation of required dependencies, setting up Docker as a service, and configuring systemd for Docker and containerd.

For example, I found that the command for disabling SELinux temporarily (sudo setenforce 0) was essential in getting the Minikube environment to work without conflicts. I also had issues with system container stats, which the Vultr Kubernetes guide helped resolve. It explained how to adjust the kubelet settings by adding --runtime-cgroups=/systemd/system.slice to prevent the error about missing cgroup stats from Docker services.

Additionally, the Docker installation guide for CentOS 7 from Vultr How to Install Docker on CentOS 7provided a clear path for downloading and installing the Docker binaries directly, ensuring compatibility with Kubernetes. The guide also helped me set up Docker as a systemd service, which was critical for ensuring smooth operations in my virtualized environment.

Vultr’s troubleshooting tips, such as resolving DNS issues by installing bind-utils and checking the status of kubelet services, were also crucial. Their Kubernetes guide for CentOS 7 helped me get everything running without unnecessary delays How to Install Kubernetes on CentOS 7
Here is the command I used to install Docker, based on the guide:

$ sudo yum install iptables git procps-ng xz
$ curl -L https://download.docker.com/linux/static/stable/x86_64/docker-18.09.9.tgz -o docker-18.09.9.tgz
$ sudo tar -xvzf docker-18.09.9.tgz -C /usr/local/ && sudo ln -s /usr/local/docker/* /usr/local/bin

Thanks to Vultr's detailed guides, I was able to smoothly configure Docker, Minikube, and Kubernetes on my CentOS 7 KVM setup. If you're facing similar issues, I highly recommend referring to Vultr’s Minikube on CentOS 7 and Docker installation resources—they made a complex setup process much simpler!
```

Lessons Learned

  • The importance of using the right Docker version for Kubernetes cannot be overstated.
  • Configuring system services like SELinux, firewalls, and groups upfront saves a lot of debugging time later.
  • Always consult the documentation for tools like virt-builder, Docker, and Kubernetes, as these resources provide invaluable insights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment