DEV Community

Reene
Reene

Posted on

[k8s]After restarting the virtual machine, the kube-apiserver of k8s cannot start normally

After restarting the device:



kubectl get node
The connection to the server x.x.x.x:6443 was refused - did you specify the right host or port?
systemctl status kube-apiserver prompts:

Unit kube-apiserver.service could not be found
There is no such service, so it is judged that the configuration file is wrong
Enter fullscreen mode Exit fullscreen mode

Decided to reconstruct the K8S cluster

Solution:

# 1. Check if kubectl command is working
kubectl get node

# 2. Go to the home directory
cd ~

# 3. Check if the .kube directory exists
ll -a  # List all files to check if .kube directory exists

# 4. Remove the .kube directory
sudo rm -rf .kube/

# 5. Restart Docker service
sudo systemctl restart docker  # Restart Docker

# 6. Restart kubelet service
sudo systemctl restart kubelet  # Restart kubelet

# 7. Reset the Kubernetes setup
sudo kubeadm reset  # Reset Kubernetes cluster

# 8. Remove CNI configurations
sudo rm -rf /etc/cni/  # Delete CNI configurations

# 9. Reinitialize Kubernetes with specific settings
kubeadm init --kubernetes-version=1.23.5 --apiserver-advertise-address=192.168.1.21 --image-repository registry.aliyuncs.com/google_containers --service-cidr=192.1.0.0/16 --pod-network-cidr=192.244.0.0/16

# 10. After successful initialization, configure Kubernetes for the user
mkdir -p $HOME/.kube  # Create the .kube directory
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config  # Copy admin config
sudo chown $(id -u):$(id -u) $HOME/.kube/config  # Change ownership of the config file

# 11. Download the Flannel network configuration file
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml  # Download Flannel YAML file

# 12. Copy the kube-flannel.yml file to /opt and apply it
sudo cp kube-flannel.yml /opt/  # Copy the Flannel YAML file to /opt directory
kubectl apply -f /opt/kube-flannel.yml  # Apply the Flannel configuration

# After these steps, check the node status
kubectl get nodes  # Check if the node is ready

Enter fullscreen mode Exit fullscreen mode
  1. Issue with Flannel and CoreDNS Pods Not Starting: When checking the pods, Flannel and CoreDNS are not starting.


Error: Flannel is having an issue where it cannot obtain the necessary subnet lease. Specifically, the PodCIDR (assigned IP range for pods) 192.244.0.0/24 is not included within Flannel’s network configuration subnet 10.244.0.0/16.
Enter fullscreen mode Exit fullscreen mode
  1. Fixing the Subnet Configuration: Edit the Flannel ConfigMap: To resolve the subnet issue, the Flannel ConfigMap needs to be updated.

First, inspect the Flannel Pod:


kubectl describe pod kube-flannel-ds-99grr -n kube-flannel  # Describe the Flannel pod for detailed information
Enter fullscreen mode Exit fullscreen mode

Check the logs:


kubectl logs kube-flannel-ds-fqvvv -n kube-flannel  # Check the Flannel pod logs for errors
Error Explanation: The Flannel network plugin is unable to get a subnet lease because the PodCIDR 192.244.0.0/24 is not within the subnet 10.244.0.0/16 specified in Flannel’s configuration.
Enter fullscreen mode Exit fullscreen mode

Solution: You can resolve this by modifying the Flannel network configuration:

Edit the Flannel ConfigMap:

kubectl edit configmap kube-flannel-cfg -n kube-flannel  # Edit the Flannel ConfigMap in the kube-flannel namespace
Enter fullscreen mode Exit fullscreen mode

Change the subnet in the ConfigMap to match the required PodCIDR.

After modifying the subnet in the kube-flannel-cfg ConfigMap, Flannel should be able to allocate the necessary subnets and the pods should start successfully.

Top comments (0)