安装docker-ce
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg| sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-get -y install docker-ce
国内源安装kubelet kubeadm kubectl
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
sudo su
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
su 用户名
sudo cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
关闭swap
sudo swapoff -a
查看ip,选定一个机器作为master,然后将其ip复制到下面的apiserver-advertise-address的指
ifconfig
准备安装k8s镜像
# 服务器执行命令看需要什么镜像
sudo kubeadm config images list
# 本地翻墙网络下载镜像
docker pull k8s.gcr.io/kube-apiserver:v1.21.0
docker pull k8s.gcr.io/kube-controller-manager:v1.21.0
docker pull k8s.gcr.io/kube-scheduler:v1.21.0
docker pull k8s.gcr.io/kube-proxy:v1.21.0
docker pull k8s.gcr.io/pause:3.4.1
docker pull k8s.gcr.io/etcd:3.4.13-0
docker pull k8s.gcr.io/coredns/coredns:v1.8.0
# 本地保存镜像
docker save -o kube-apiserver k8s.gcr.io/kube-apiserver:v1.21.0
docker save -o kube-controller-manager k8s.gcr.io/kube-controller-manager:v1.21.0
docker save -o kube-scheduler k8s.gcr.io/kube-scheduler:v1.21.0
docker save -o kube-proxy k8s.gcr.io/kube-proxy:v1.21.0
docker save -o pause k8s.gcr.io/pause:3.4.1
docker save -o etcd k8s.gcr.io/etcd:3.4.13-0
docker save -o coredns k8s.gcr.io/coredns/coredns:v1.8.0
# 拷贝到服务器
scp -r install_images 用户名@服务器ip:/home/用户名
# 服务器加载
sudo docker load < kube-apiserver
sudo docker load < kube-controller-manager
sudo docker load < kube-scheduler
sudo docker load < kube-proxy
sudo docker load < pause
sudo docker load < etcd
sudo docker load < coredns
master执行,kubeadm初始化,master上执行
sudo kubeadm init --apiserver-advertise-address=192.168.3.54 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16 --kubernetes-version=1.22.0
init语句执行完毕会自动提示node节点join用到的token,类似下面所示,但不是下面的语句,从master节点复制出来并在node节点执行join命令
sudo kubeadm join 192.168.3.54:6443 --token u5od56.e4s74ce66r13a67q --discovery-token-ca-cert-hash sha256:2dc5c3e76afd8c34da6193f139b000186b45f87f627d653b992305ff30078f68
但是我们在当时如果没记下这个命令,可能后续加节点的时候就忘记了这个命令,此时需要执行如下命令即可
kubeadm token create --print-join-command
Master上执行,Master上执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Node上执行下列命令,复制文件到Node
mkdir -p $HOME/.kube
scp 用户名@192.168.3.54:/home/用户名/.kube/config $HOME/.kube/config
master执行,安装网络插件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
测试集群
kubectl get pods -A
kubectl get cs
kubectl get nodes
kubectl create deployment web --image=nginx
kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=nginx-web
kubectl get pod,svc
master执行,kubectl get cs,如果有Unhealthy异常解决方法
# master上,编辑下列两个文件,去掉'--port=0'这一行这个设置
sudo vim /etc/kubernetes/manifests/kube-scheduler.yaml
sudo vim /etc/kubernetes/manifests/kube-controller-manager.yaml
# master上,然后重启kubelet
sudo systemctl restart kubelet
如何重置集群,删除k8s相关网络和相关配置文件
sudo kubeadm reset
sudo rm -rf /etc/cni/*
sudo rm -rf $HOME/.kube
sudo su
systemctl stop kubelet
su 用户名
sudo ifconfig cni0 down
sudo ifconfig flannel.1 down
sudo ip link delete cni0
sudo ip link delete flannel.1
sudo su
systemctl start kubelet
su 用户名
sudo rm -rf /var/lib/rook
错误1:The connection to the server localhost:8080 was refused - did you specify the right host or port?
错误提示:NO_PUBKEY
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 6A030B21BA07F4FB NO_PUBKEY 8B57C5C2836F4BEB
sudo gpg --keyserver keyserver.ubuntu.com --recv 8B57C5C2836F4BEB
sudo gpg --export --armor 8B57C5C2836F4BEB | sudo apt-key add -