kubernetes tips and tricks


  • prepare every node ( ubuntu 22.04 ) for running kubernetes

add in /etc/modules-load.d/modules.conf the following lines

overlay
br_netfilter

enable iptables for bridges and packet forwarding

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

configure containerd in every node for make in compatible with kubernetes ( generate default config and change SystemdCgroup from false to true )

mkdir /etc/containerd

sh -c "containerd config default > /etc/containerd/config.toml"

sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml

systemctl restart containerd.service

systemctl restart kubelet.service
  • initialize one of the machines as a control plane node:
kubeadm init --control-plane-endpoint $(hostname -i):6443 --pod-network-cidr=10.244.0.0/16 --service-cidr=172.18.0.24/16 --apiserver-advertise-address=$(hostname -i)
  • join every other worker node with the output provided by the last command, something like this:
kubeadm join 192.168.0.17:6443 --token jn7hpr.054iijynhdlgy9kq --discovery-token-ca-cert-hash sha256:blahblahblah
  • in the control plane, create the deployment for flannel with the following command ( is network plugin for a simple l2 network that connect the pods, adjust ip address KUBERNETES_SERVICE_HOST in yaml file )
  • following some example of kbnt commands

create a deplyment for deploy a container with the desiderd replica number:

kubectl create deployment --image httpd apache2 --replicas 2

list all pods in all namespaces and the nodes where are running:

kubectl get pods -A -o wide

attach to the console of a container:

kubectl exec --stdin --tty apache2-9b84587d7-6vjng -- bash

change the number of replicas that a container needs to have:

kubectl scale --replicas 2

get the stoutput of a pod

kubectl logs ubuntu

run a pod from an image in interactive mode

kubectl run --image busybox busy2 -it

add the role of worker to a node:

kubectl label node nodename node-role.kubernetes.io/worker=worker1

to check all pods in all namespaces ( coredns pods needs to be started after networking plugin is started correctly )

kubectl get pods -A