Published on

ubuntu k8s集群安装

Authors

https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/ 官方教程被墙了,无法安装。

前置条件

  • 2 CPUs 2G RAM
  • docker
  • curl
  • master和node hostname不能一样
  • master和nodes之间网络互通

配置环境

1.关闭swap

sudo swapoff -a

2.设置hostname

sudo hostnamectl set-hostname HOSTNAME #(用master,node1,node2替换)

3.检查各个节点间的mac和product_uuid 要求这两个值唯一

ip link
sudo cat /sys/class/dmi/id/product_uuid

4.允许 iptables 检查桥接流量

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

安装

1.添加k8s gpgkey

sudo curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add

2.添加安装源

sudo apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"

3.安装

sudo apt-get install kubeadm kubelet kubectl -y
# 锁定版本
sudo apt-mark hold kubelet kubeadm kubectl

启动master

kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.21.3

报错:[ERROR ImagePull]: failed to pull image registry.aliyuncs.com/google_containers/coredns:v1.8.0: output: Error response from daemon: manifest for registry.aliyuncs.com/google_containers/coredns:v1.8.0 not found: manifest unknown: manifest unknown 从其他地方拉去镜像,再重新tag名字,再次运行上面的kubeadm init xxxm命令

docker pull coredns/coredns:1.8.0
docker tag coredns/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns:v1.8.0
  1. 普通用户获得执行kubectl的权限
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
  1. 安装Pod网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  1. 查看部署情况
# -n指定namespace, 默认的namespace是default
# 查看系统的pods
kubectl get pods -n kube-system

etcd-master-k8s                      1/1     Running   0          165m
kube-apiserver-master-k8s            1/1     Running   0          164m
kube-controller-manager-master-k8s   1/1     Running   0          165m

三个Pod都启动了,已经正常运行起来了 4. 查看node状态

kubectl get nodes

状态变成Ready 即成功

node加入

输入master初始化后的加入命令

kubeadm join 192.169.0.126:6443 --token bmedqs.92sv7np6w25v8zr6         --discovery-token-ca-cert-hash sha256:8d2f8cdf347b427a99f2d47853b864749e10b1a505faa690959f68d8533513a7

在master查看node状态,是Ready即正常

root@fabric:~ kubectl get nodes
NAME         STATUS   ROLES                  AGE    VERSION
master-k8s   Ready    control-plane,master   172m   v1.21.3
node1-k8s    Ready    <none>                 126m   v1.21.3