ubuntu之k8s集群安装实操
一主两从-腾讯云服务器
master节点
重命名:
hostnamectl set-hostname master
node1节点
重命名:
hostnamectl set-hostname node1
node2节点
重命名:
hostnamectl set-hostname node1
全部节点
执行vim /etc/hosts 并加入:
111.230.151.202 master
129.204.4.112 node1
122.51.200.178 node2
全部节点执行
#切换root用户
sudo su -
#检查更新
apt update
#更新安装
apt upgrade -y
#关闭swap
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
#添加加载的内核模块
tee /etc/modules-load.d/containerd.conf<<EOF
overlay
br_netfilter
EOF
#加载内核模块
modprobe overlay
modprobe br_netfilter
#设置内核参数
tee /etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#应用内核参数
sysctl --system
#安装curl gnupg2 software-properties-common apt-transport-https ca-certificates
apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
#安装GPG证书
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
#写入软件源信息
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
#检查更新
apt update
#安装containerd
apt install -y containerd.io
#生成containetd的配置文件
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
#修改cgroup Driver为systemd
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
#将镜像源设置为阿里云 google_containers 镜像源
sed -i 's/k8s.gcr.io/registry.aliyuncs.com\/google_containers/g' /etc/containerd/config.toml
#重新启动containerd
systemctl restart containerd
#启动containerd服务
systemctl enable containerd
#添加 apt key
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
#添加Kubernetes的apt源为阿里云的源
apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"
#检查更新
apt update
#kubelet kubeadm kubectl
apt install -y kubelet kubeadm kubectl
#锁定版本,不随 apt upgrade 更新
apt-mark hold kubelet kubeadm kubectl
#查看
kubeadm version
#开启路由功能
sysctl -w net.ipv4.ip_forward=1
master节点执行
#初始化kubernetes,指定版本号为最新版1.25.0,192.168.31.200为hep-k8s-lb-nginx负载均衡机器的IP
sudo kubeadm init --control-plane-endpoint=master --kubernetes-version=v1.25.0 --image-repository registry.aliyuncs.com/google_containers --v=5
#执行成功之后会打印以下信息,记得复制出来
#-------------------------------------------------------------------------------------------
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join master:6443 --token bqzhy7.xrt6vgc9jyegvjtk \
--discovery-token-ca-cert-hash sha256:c3ac98069b38a6daabc4a54020dca17299ea033f69c86d52ef4d1693ef75435e \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join master:6443 --token bqzhy7.xrt6vgc9jyegvjtk \
--discovery-token-ca-cert-hash sha256:c3ac98069b38a6daabc4a54020dca17299ea033f69c86d52ef4d1693ef75435e3f69c86d52ef4d1693ef75435e
#-------------------------------------------------------------------------------------------
#执行成功之后,执行下面操作
#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
#查看cluster-info
kubectl cluster-info
#查看node信息
kubectl get nodes
两个子节点执行
kubeadm join master:6443 --token bqzhy7.xrt6vgc9jyegvjtk \
--discovery-token-ca-cert-hash sha256:c3ac98069b38a6daabc4a54020dca17299ea033f69c86d52ef4d1693ef75435e3f69c86d52ef4d1693ef75435e
master执行
kubectl get nodes
#查看集群节点信息