-
创建虚拟机(KVM),桥接网络
virt-install --name=kmaster --description='k8s master' --ram=8192 --vcpus=4 --disk path=/home/yiran/vm/kmaster.qcow2,size=120 --cdrom /home/yiran/iso/ubuntu-22.04.2-live-server-amd64.iso --graphics vnc ......
-
Forwarding IPv4 and letting iptables see bridged traffic
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf overlay br_netfilter EOF sudo modprobe overlay sudo modprobe br_netfilter # sysctl params required by setup, params persist across reboots 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 # Apply sysctl params without reboot sudo sysctl --system
-
配置Docker镜像源和cgroup driver
vim /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://2qgjyg7c.mirror.aliyuncs.com"] } systemctl restart docker
-
安装cri-dockerd:https://github.com/Mirantis/cri-dockerd
dpkg -i cri-dockerd_0.3.4.3-0.ubuntu-jammy_amd64.deb
-
修改pause image
vim /usr/lib/systemd/system/cri-docker.service ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9 systemctl daemon-reload systemctl restart cri-docker.service
-
关闭swap分区
swapoff -a vim /etc/fstab
-
安装kubeadm, kubelet, kubectl:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
apt-get update apt-get install -y apt-transport-https ca-certificates curl apt-get install software-properties-common curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" apt-get update apt-get install -y kubelet kubeadm kubectl apt-mark hold kubelet kubeadm kubectl
-
master初始化集群
kubeadm init --apiserver-advertise-address=192.168.0.105 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --cri-socket=unix:///var/run/cri-dockerd.sock export KUBECONFIG=/etc/kubernetes/admin.conf 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 192.168.0.105:6443 --token crtr8j.hveq3vuqar8oy6jr \ --discovery-token-ca-cert-hash sha256:6aacb508259e6736343b6832db364193aeb631b7998d5c3bd423530ac0ae9f13
-
Deploy a pod network to the cluster:https://kubernetes.io/docs/concepts/cluster-administration/addons/
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
-
node加入集群
kubeadm join 192.168.0.105:6443 --token crtr8j.hveq3vuqar8oy6jr --discovery-token-ca-cert-hash sha256:6aacb508259e6736343b6832db364193aeb631b7998d5c3bd423530ac0ae9f13 --cri-socket=unix:///var/run/cri-dockerd.sock
-
测试
kubectl create deploy nginx --image=nginx kubectl expose deploy nginx --port=80 --type=NodePort
Ubuntu Server 22.04 家用k8s集群搭建
于 2023-08-03 18:42:00 首次发布