安装docker

配置yum源

yum install  -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安装docker
由于要配合使用k8s 所以要指定版本

yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6

把docker加入开机自启动 现在并启动

systemctl enable docker --now

加入阿里云加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://lforptrg.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装kubeadm

官网地址
必须满足一下要求
在这里插入图片描述

设置每一主机的名字

hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2

设置Linux自带的安全模式禁用

setenforce 0  #临时禁用
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config  #修改配置文件禁用

关闭交换分区

swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

允许 iptables 检查桥接流量

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

安装kubelet、kubeadm、kubectl

#设置kubernetes的下载源地址
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
   http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

#下载指定版本
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
#加入开机自启动
sudo systemctl enable --now kubelet

下载各个机器需要的镜像

使用脚本进行下载

sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
#赋予脚本权限
chmod +x ./images.sh && ./images.sh

设置主节点

要给每台机器设置主节点的域名

#所有机器添加master域名映射,以下需要修改为自己的 主节点的内网地址
echo "172.31.0.141 cluster-endpoint" >> /etc/hosts

#所有网络范围不重叠

#主节点初始化
kubeadm init \
--apiserver-advertise-address=172.31.0.141 \ #映射域名地址
--control-plane-endpoint=cluster-endpoint \ #映射的域名
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.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/

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 cluster-endpoint:6443 --token 4hq48k.rvcrk1v4tda3l7pe \
    --discovery-token-ca-cert-hash sha256:ebb924fb60659d334dcfb0ab82f368da21089056e69987883ffe4f6831f58a8f \
    --control-plane 

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join cluster-endpoint:6443 --token 4hq48k.rvcrk1v4tda3l7pe \
    --discovery-token-ca-cert-hash sha256:ebb924fb60659d334dcfb0ab82f368da21089056e69987883ffe4f6831f58a8f 

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

简单命令

#查看集群所有节点
kubectl get nodes

#根据配置文件,给集群创建资源
kubectl apply -f xxxx.yaml

#查看集群部署了哪些应用?
docker ps   ===   kubectl get pods -A
# 运行中的应用在docker里面叫容器,在k8s里面叫Pod
kubectl get pods -A

安装网络组件

calico官网
You should now deploy a pod network to the cluster.
你应该再集群上部署一个网络 主节点上

curl https://docs.projectcalico.org/manifests/calico.yaml -O

kubectl apply -f calico.yaml

在这里插入图片描述
Then you can join any number of worker nodes by running the following on each as root:
再每个子节点中执行添加令牌 默认时间是24小时

kubeadm join cluster-endpoint:6443 --token 4hq48k.rvcrk1v4tda3l7pe \
    --discovery-token-ca-cert-hash sha256:ebb924fb60659d334dcfb0ab82f368da21089056e69987883ffe4f6831f58a8f 

再主节点会发现新添加的节点
在这里插入图片描述
令牌过期
将新生成的令牌添加到每一个子节点中

kubeadm token create --print-join-command
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐