【K8S】为了考试而学习01:如何搭建一个K8S集群

1.环境准备

1.操作系统环境

Ubuntu-18.04.6

2.地址分配情况

节点名称 地址 备注 部署内容
Master 172.168.0.129 主节点 docker kubeadm kubelet kubectl
Node01 172.168.0.130 Node01节点 docker kubeadm kubelet kubectl
Node02 172.168.0.131 Node02节点 docker kubeadm kubelet kubectl

3.系统级别配置

  • 禁用操作系统SWAP

    swapoff -a
    vim /etc/fstab 注释掉SWAP那一行

  • 修改Ubuntu操作系统镜像源(ubuntu 18.04配置)
    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse  
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse  
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse  
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse  
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse  
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse  
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse  
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse  
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse  
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse  
    
  • 更新操作系统软件,安装vim编辑器便于配置文件修改

    apt-get update && apt-get upgrade -y
    apt-get install -y vim

2.部署K8S集群

1.安装docker

apt-get install -y docker.io

2.配置dokcer镜像加速

vim /etc/docker/daemon.json  

{
    "registry-mirrors": ["https://***(加速镜像源地址)"],
    "live-restore": true,
    "exec-opts": ["native.cgroupdriver=systemd"]
}

systemctl daemon-reload
systemctl restart docker
systemctl enable docker 
  • 注:"exec-opts": ["native.cgroupdriver=systemd"]
    配置一定要加,让docker以systemd的服务形式运行,不然会和k8s冲突,后面kubeadm init的时候会报错

3.配置K8S镜像源加速

vim /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main

4.配置K8S镜像源key并更新

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

5.安装kubeadm kubelet kubectl软件(1.20.1-00版本)

apt-get install -y kubeadm=1.20.1-00 kubelet=1.20.1-00 kubectl=1.20.1-00
apt-mark hold kubelet kubeadm kubectl

6.下载配置calico文件(master节点)

wget https://docs.projectcalico.org/manifests/calico.yaml

7.hosts文件中添加本机host解析记录

主机名配置的时候不能带下划线,不然等kubeadm初始化的时候会报错
172.168.0.129 k8smaster
172.168.0.130 k8snode01
172.168.0.131 k8snode02

8.配置kubeadm-config.yaml配置文件

vim kubeadm-config.yaml

apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: 1.20.1
controlPlaneEndpoint: "172.168.0.129:6443"
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
networking:
    podSubnet: 192.168.0.0/16

9.使用kubeadm部署master节点

kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.out

10.配置kube config文件

  • 以上配置以sudo -i配置
  • 以下切换到普通用户
mkdir -p $HOME/.kube
sudo cp -i /etc/kubenetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
less /.kube/config

11.通过calico安装网络

cp /root/calico.yaml .
kubectl apply -f calico.yaml

12.安装bash-completion(为了支持kubectl子命令的补全)

sudo apt-get install bash-completion -y
<退出当前命令窗口,重新登陆>
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> $HOME/.bashrc
<可以通过tab补全子命令检查是否正确配置>

13.Node节点基础配置

  • 在node节点中重复2-1至2-7(2-6不做)的各个步骤,配置基础环境

14.生成token(针对master节点token过期后加入master节点在master节点操作)

  1. 查看master节点token情况
    sudo kubeadm token list
  2. 创建一个新的token
    sudo kubeadm token create
  3. 使用新创建的token更新CA
    openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt |openssl rsa -pubin -outform der 2>/dev/null |openssl dgst -sha256 -hex | sed 's/^.* //'

15.将Node节点加入到集群中

  1. 查看之前保存到文件中的kubeadm init的输出kubeadm-init.out
    sudo cat /root/kubeadm-init.out
  2. 以root权限,将Node节点加入集群中(注意核对token及ca-cert-hash)
    kubeadm join 172.168.0.129:6443 --token 4rtqbq.qm26ch2d6hi1b6yx --discovery-token-ca-cert-hash sha256:af6fd593f604caf95fd331905216a06f52e9be9da0eee3e2dbef1d556c1e95f7
  3. 在master节点中,查看当前节点状态
    kubectl get nodes
  4. 查看节点状态为ready即可

你可能感兴趣的:(【K8S】为了考试而学习01:如何搭建一个K8S集群)