栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

搭建k8s集群 初始化master 和 nodes

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

搭建k8s集群 初始化master 和 nodes

一、安装docker-ce

sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

二、将docker加入docker群组,使用普通用户也可以使用docker
 

sudo groupadd docker #添加docker用户组
sudo gpasswd -a $USER docker #将登陆用户加入到docker用户组中
newgrp docker #更新用户组

三、配置Ubuntu 的kubectl、kubeadm、kubelet

apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 
cat </etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF  
apt-get update
apt-get install -y kubelet=1.22.5-00 kubeadm=1.22.5-00 kubectl=1.22.5-00

如果安装过其他版本,先删除软件及其配置文件
 

apt-get --purge remove kubelet
apt-get --purge remove kubectl
apt-get --purge remove kubeadm

四、查看对应的k8s安装组件版本,在阿里云下载对应镜像

kubeadm config images list

五、设置配置文件

1.

sudo vim /etc/docker/daemon.json

在文件中添加代码段

{
  "exec-opts": ["native.cgroupdriver=systemd"] #Kubernetes 推荐使用 systemd 来代替 cgroupfs
}

2.

vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

添加

Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true --fail-swap-on=false"

3.

sudo vim /etc/fstab   #swap那行 前面加#

保存配置

systemctl daemon-reload
systemctl restart docker 
systemctl status docker

systemctl restart kubectl
systemctl status kubectl

六、初始化k8s master节点

sudo kubeadm init  --kubernetes-version=v1.22.5  --service-cidr=10.96.0.0/12 --pod-network-cidr=10.96.0.0/16 

出现下面提示就成功了,然后按提示操作

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 10.3.172.47:6443 --token eul8eh.jhok8q7mj6mgijun 
	

当初始化失败,重新初始化时,需要清理环境

kubeadm reset
sudo rm -rf /etc/cni/net.d
sudo rm -rf $HOME/.kube/config
sudo  rm -rf /etc/kubernetes/

七、查看节点状态

scarlettzhao@scarlettzhao-master:~$  kubectl get nodes
NAME                  STATUS     ROLES                  AGE    VERSION
scarlettzhao-master   NotReady   control-plane,master   100s   v1.22.2

节点是notready,检查pod

scarlettzhao@scarlettzhao-master:~$ kubectl get pod --all-namespaces
NAMESPACE     NAME                                          READY   STATUS     RESTARTS        AGE
kube-system   coredns-78fcd69978-8ps67                      0/1     Pending    0               122m
kube-system   coredns-78fcd69978-ng7ln                      0/1     Pending    0               122m
kube-system   etcd-scarlettzhao-master                      1/1     Running    2 (4m32s ago)   122m
kube-system   kube-apiserver-scarlettzhao-master            1/1     Running    2 (4m31s ago)   122m
kube-system   kube-controller-manager-scarlettzhao-master   1/1     Running    2 (4m32s ago)   122m
kube-system   kube-flannel-ds-ntchz                         0/1     Init:1/2   0               25s
kube-system   kube-proxy-9mh9h                              1/1     Running    1 (4m32s ago)   122m
kube-system   kube-scheduler-scarlettzhao-master            1/1     Running    2 (4m32s ago)   122m

coredns 状态pending,未运行,因为缺少网络组件

八、配置网络


1.Install the Tigera Calico operator and custom resource definitions.

kubectl create -f https://docs.projectcalico.org/manifests/tigera-operator.yaml


2.Install Calico by creating the necessary custom resource. For more information on configuration options available in this manifest, see the installation reference.

kubectl create -f https://docs.projectcalico.org/manifests/custom-resources.yaml

  #  Note: Before creating this manifest, read its contents and make sure its settings are correct for your environment. For example, you may need to change the default IP pool CIDR to match your pod network CIDR.


3.Confirm that all of the pods are running with the following command.

watch kubectl get pods -n calico-system

#Wait until each pod has the STATUS of Running.

#    Note: The Tigera operator installs resources in the calico-system namespace. Other install methods may use the kube-system namespace instead.


4.Remove the taints on the master so that you can schedule pods on it.

kubectl taint nodes --all node-role.kubernetes.io/master-


5.kubectl get nodes -o wide

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/680333.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号