搭建k8s集群

方法一:利用官方工具kubeadm搭建

一、安装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用户组<br>

sudo gpasswd -a $USER docker #将登陆用户加入到docker用户组中<br>

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 <<EOF >/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.21.5-00 kubeadm=1.21.5-00 kubectl=1.21.5-00

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

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

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

kubeadm config images list

写脚本文件installks.sh,安装所需要的镜像

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.0

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.5 k8s.gcr.io/kube-apiserver:v1.21.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.5 k8s.gcr.io/kube-controller-manager:v1.21.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.5 k8s.gcr.io/kube-scheduler:v1.21.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.5 k8s.gcr.io/kube-proxy:v1.21.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 k8s.gcr.io/pause:3.4.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.0 k8s.gcr.io/coredns/coredns:v1.8.0

docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.0

执行脚本文件,镜像阿里云容器镜像服务的

sudo chmod -R 777 installks.sh

sudo ./installks.sh

五、设置配置文件

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

六、初始化k8s master节点

sudo kubeadm init  --kubernetes-version=v1.21.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/
sudo rm -rf /var/lib/etcd 

七、查看节点状态

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,未运行,因为缺少网络组件

八、配置网络

用calico:Project Calico



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

九、另外:

清理docker磁盘:

docker system prune -a --volumes

查看错误日志:

journalctl -xefu kubelet

方法二:利用kubekey搭建

步骤一:

1.硬件推荐配置

操作系统最低配置
Ubuntu 16.04, 18.04

2 核 CPU,4 GB 内存,40 GB 磁盘空间

2.节点要求

  • 节点必须能够通过 SSH 连接。
  • 节点上可以使用 sudo/curl/openssl 命令

3.容器运行时

您的集群必须有一个可用的容器运行时。如果您使用 KubeKey 搭建集群,KubeKey 会默认安装最新版本的 Docker。或者,您也可以在创建集群前手动安装 Docker 或其他容器运行时。

支持的容器运行时版本
Docker19.3.8 +
containerd(试验版,未经充分测试)最新版
CRI-O(试验版,未经充分测试)最新版
iSula(试验版,未经充分测试)最新版

4.依赖项要求

KubeKey 可以将 Kubernetes 和 KubeSphere 一同安装。针对不同的 Kubernetes 版本,需要安装的依赖项可能有所不同。您可以参考以下列表,查看是否需要提前在节点上安装相关的依赖项。

依赖项Kubernetes 版本 ≥ 1.18Kubernetes 版本 < 1.18
socat必须可选但建议
conntrack必须可选但建议
ebtables可选但建议可选但建议
ipset可选但建议可选但建议

5.网络和 DNS 要求

  • 请确保 /etc/resolv.conf 中的 DNS 地址可用,否则,可能会导致集群中的 DNS 出现问题。
  • 如果您的网络配置使用防火墙规则或安全组,请务必确保基础设施组件可以通过特定端口相互通信。建议您关闭防火墙。有关更多信息
  • 支持的 CNI 插件:Calico 和 Flannel。其他插件也适用(例如 Cilium 和 Kube-OVN 等),但请注意它们未经充分测试。

 步骤二:

先执行以下命令以确保您从正确的区域下载 KubeKey。

export KKZONE=cn 

执行以下命令下载 KubeKey。

curl -sfL https://get-kk.kubesphere.io | VERSION=v1.2.1 sh - 

kk 添加可执行权限:

chmod +x kk 

步骤三:

安装 Kubernetes ,可参考以下示例命令:

./kk create cluster --with-kubernetes v1.21.5  
  • KubeKey 会默认安装 OpenEBS 为开发和测试环境提供 LocalPV 以方便新用户
  • 执行该命令后,KubeKey 将检查您的安装环境,结果显示在一张表格中。输入 yes 继续安装流程。

新节点加入到旧节点上

1.查看 master 的节点的 token :

kubeadm token list


2.没有相应的 token 的话,则创建一个:

kubeadm token create

结果:

4g6vzo.50wx29nrpbaf8bjg


3.如果没有的值--discovery-token-ca-cert-hash,则可以通过在控制平面节点上运行以下命令链来获取它
运行指令:

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
   openssl dgst -sha256 -hex | sed 's/^.* //'

结果:

1de5d1cbf7b72263b993222acbb9dd5d25e3eddbcb346661c61581c179a0529f

最终在新的 node 节点上 运行 则会加入新的节点:
kubeadm join 10.3.172.47:6443 --token 4g6vzo.50wx29nrpbaf8bjg \
    --discovery-token-ca-cert-hash sha256:1de5d1cbf7b72263b993222acbb9dd5d25e3eddbcb346661c61581c179a0529f

如果新的节点down 机需要重新的连的话,需要:

清空上次连的一些缓存内容 :

rm -rf $HOME/.kube
kubeadm reset

 再进行添加节点 join 进去的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值