在单机上用kubeadm安装K8s
起因
最近(其实已经好几个月了)搞了一台配置比较高的机器,想用来跑多一些应用,当然直接用docker也不是不行,或者加上portainer,但还是觉得k8s更好玩一些。
因为之前是按照常规的组集群的方式安装的,这回只有单机,又不想用minikube之类的,所以还是按kubeadm的方式来装。加上新版本有一些变化,就懒得再去改旧文,还是重新水一篇吧。
基本的系统环境是:3C19G的ARM平台,系统是ubuntu 24.04LTS
基本安装
首先,新版的k8s已经不再使用docker,改用其它容器实现方案,我这边选择的是containerd。操作接近原来的docker,大部分情况下把docker命令换成crictl即可。
Ansible剧本
用的还是之前那个剧本,稍作修改,比如docker代理不需要了,因为服务器本身在国外。
---
- name: Install packages
apt:
pkg:
- arptables
- ebtables
- containerd
update_cache: no
install_recommends: no
autoremove: yes
autoclean: yes
- name: Update alternatives
alternatives:
name: "{{ item.name }}"
path: "{{ item.path }}"
with_items:
- { name: 'iptables', path: '/usr/sbin/iptables-legacy' }
- { name: 'ip6tables', path: '/usr/sbin/ip6tables-legacy' }
- { name: 'arptables', path: '/usr/sbin/arptables-legacy' }
- { name: 'ebtables', path: '/usr/sbin/ebtables-legacy' }
- name: Turn off swap
command: swapoff -a
- name: Turn off swap forever
lineinfile:
path: /etc/fstab
regexp: "^([^#].*none\\s+swap\\s+sw.*)$"
line: "#\\g<1>"
backrefs: yes
- name: Ensure directory for apt keyrings exists
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
- name: Download Kubernetes GPG key
ansible.builtin.get_url:
url: https://pkgs.k8s.io/core:/stable:/v1.32/deb/Release.key
dest: /tmp/kubernetes-key.gpg
mode: '0644'
force: no # 幂等性
- name: Convert and install the key
community.gpg.gpg_convert:
executable: gpg
key: /tmp/kubernetes-key.gpg
keyring: /etc/apt/keyrings/kubernetes-apt-keyring.gpg
state: present
- name: Clean up temporary key file
ansible.builtin.file:
path: /tmp/kubernetes-key.gpg
state: absent
- name: Add repository of kubernetes
apt_repository:
repo: "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.32/deb/ /"
state: presen
- name: Actually install kubernetes
apt:
pkg:
- kubelet
- kubeadm
- kubectl
state: latest
update_cache: yes
install_recommends: no
- name: Hold kubernetes version
dpkg_selections:
name: "{{ item.name }}"
selection: hold
with_items:
- { name: 'kubelet' }
- { name: 'kubeadm' }
- { name: 'kubectl' }
- name: Get kubernetes images
shell: kubeadm config images pull
安装后配置
以下都是在root用户下操作,故略去sudo。
配置containerd和kubelet
先检查安装是否成功:
kubectl cluster-info
然后确保containerd和kubelet服务已经正常启动
systemctl status containerd
systemctl status kubelet
检查containerd的配置文件,启用systemd的cgroup:
# 如无则创建containerd配置文件,如有则略过
sudo mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml > /dev/null
# 编辑配置文件,找到 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] 部分
# 将 SystemdCgroup = false 修改为 SystemdCgroup = true
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
# 重启 containerd
systemctl restart containerd
配置modules-load和sysctl参数:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# 设置所需的 sysctl 参数,这些参数在重启后仍然有效
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# 应用 sysctl 参数设置,无需重启
sudo sysctl --system
初始化Control Plane
开始安装Control Plane
kubeadm init --pod-network-cidr 10.244.0.0/16 --apiserver-advertise-address=10.0.0.123 --kubernetes-version=v1.32.2
其中的pod-network-cidr是Flannel的网段,如果使用不同的网络插件,要就指定不同的网段。
apiserver-advertise-address是服务器的地址,不指定的话可能会导致服务启动失败。
kubernetes-version是指定k8s的版本,以确保版本正确。
初始化完成后返回这么一段内容:
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
其中包含几个重要信息:
第一:如果要用非root用户操作kubectl,则需要在用户目录下创建.kube/config
第二:如果要在集群里部署应用,则使用kubectl apply命令
第三:如果有其它节点要加入这个集群,则使用kubeadm join命令,参数中的token和hash要注意安全保存,如果忘记记录token,可以用kubeadm token list
查看
第四:token的有效期只有24小时,过期后需要重新创建kubeadm token create
初始化完成后可以看看当前的状态:
kubectl get nodes
kubectl get pods -A
正常情况下是刚开始的状态为Ready但很快变成NotReady,因为还没有安装Flannel网络。
安装网络
还是安装常用的Flannel,路径与原来有所不同:
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
现在再查看状态应该就是Ready了。
将control plane作为worker node
默认情况下control plane不作为worker node,所以k8s集群至少需要2台机器,但实际上也可以用一台机器跑,那就是将control plane也同时配置为worker node。
首先查看一下control plane节点的污点状态:
kubectl describe node <control-plane-node-name> | grep Taint
可以看到类似‘NoSchedule’这样的污点,表示不能在这里调试普通pod。将其移除:
kubectl taint nodes <control-plane-node-name> node-role.kubernetes.io/control-plane:NoSchedule-
之后就可以在这个节点上部署pod了。
推送到[go4pro.org]