1. 结构规划

角色ip
test-k8s-0110.0.0.1
test-k8s-0210.0.0.2
test-k8s-0310.0.0.3

2. 操作系统初始化配置(所有k8s节点操作)

# 1、关闭防火墙 
systemctl stop firewalld 
systemctl disable firewalld 
 
# 2、关闭selinux 
sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久 
setenforce 0  # 临时 
 
# 3、关闭swap 
swapoff -a  # 临时 
sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久 
 
# 4、在master添加hosts 
cat >> /etc/hosts << EOF 
10.0.0.1 test-k8s-01
10.0.0.2 test-k8s-02 
10.0.0.3 test-k8s-03
EOF
 
# 5、将桥接的IPv4流量传递到iptables的链 
cat > /etc/sysctl.d/k8s.conf << EOF 
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF

cat >/etc/modules-load.d/k8s.conf<<EOF
overlay
br_netfilter
EOF

# 生效 
modprobe overlay
modprobe br_netfilter
sysctl --system
 
# 6、时间同步 
yum install ntpdate -y 
ntpdate time.windows.com

3. 获取包含containerd的docker源(所有k8s节点操作)

wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. 安装containerd并启动(所有k8s节点操作)

yum -y install containerd.io
systemctl start containerd
systemctl enable containerd

5. 切换containerd镜像国内源(所有k8s节点操作)

# 读取默认配置文件
containerd config default > /etc/containerd/config.toml

# 切换为国内源
sed -i 's/registry.k8s.io/registry.aliyuncs.com\/google_containers/' /etc/containerd/config.toml

# 重启
systemctl daemon-reload
systemctl restart containerd

6. 添加k8s组件源(所有节点)

cat  >>/etc/yum.repos.d/kubernetes.repo<<EOF
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/
enabled=1
gpgcheck=0
EOF

7. 安装kubeadm、kubelet、kubectl 启动kubelet(所有节点)

yum -y install kubeadm-1.30.0 kubelet-1.30.0 kubectl-1.30.0
systemctl start kubelet 
systemctl enable kubelet
# 目前查看kubelet状态为err,稍后创建集群就好了

8. 获取集群所需要的镜像

kubeadm config images pull --image-repository registry.aliyuncs.com/google_containers

9. 创建修改初始化配置文件

# 创建初始化配置文件
kubeadm config print init-defaults > /etc/kubernetes/init-default.yaml

# 修改为国内阿里源
sed -i 's/registry.k8s.io/registry.aliyuncs.com\/google_containers/' /etc/kubernetes/init-default.yaml

# 设置 apiServerIP 地址. 请自行替换10.0.0.1为自己服务器ip
sed -i 's#1.2.3.4#10.0.0.1#' /etc/kubernetes/init-default.yaml

10. 初始化

kubeadm init --image-repository registry.aliyuncs.com/google_containers --control-plane-endpoint "10.0.0.1:6443" --upload-certs

11. 设置环境变量

cat >>/etc/profile<<EOF
export KUBECONFIG=/etc/kubernetes/admin.conf
EOF

source /etc/profile 

12. 安装网络插件

cd /etc/kubernetes/ && wget https://docs.projectcalico.org/manifests/calico.yaml
kubectl apply -f /etc/kubernetes/calico.yaml
#创建网络插件前有些pod为pending状态
#由于墙的原因calico的镜像不一定能拉下来 需要的可以加我Logout-y获取

13.添加node节点

注:如果加入时忘记token,重新生成命令为 kubeadm token create --print-join-command

kubeadm join 10.0.0.1:6443 --token XXXxxxxxx \
>         --discovery-token-ca-cert-hash sha256:e04398ab10692f24b0f956fbfc0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

mkdir ~/.kube
cp /etc/kubernetes/kubelet.conf  ~/.kube/config

14. 查看Node状态以及Pod

[root@test-k8s-01 ~]#  kubectl get node
NAME          STATUS   ROLES           AGE   VERSION
test-k8s-01   Ready    control-plane   28m   v1.30.0
test-k8s-02   Ready    <none>          10m   v1.30.0
test-k8s-03   Ready    <none>          10m   v1.30.0
[root@test-k8s-01 ~]#  kubectl get pod -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-5b9b456c66-r7rwh   1/1     Running   0          20m
kube-system   calico-node-4fdpz                          1/1     Running   0          10m
kube-system   calico-node-c2pdl                          1/1     Running   0          10m
kube-system   calico-node-sv25d                          1/1     Running   0          20m
kube-system   coredns-7b5944fdcf-ctl88                   1/1     Running   0          28m
kube-system   coredns-7b5944fdcf-xh8c2                   1/1     Running   0          28m
kube-system   etcd-test-k8s-01                           1/1     Running   0          28m
kube-system   kube-apiserver-test-k8s-01                 1/1     Running   0          28m
kube-system   kube-controller-manager-test-k8s-01        1/1     Running   0          28m
kube-system   kube-proxy-lsfkt                           1/1     Running   0          10m
kube-system   kube-proxy-m965j                           1/1     Running   0          10m
kube-system   kube-proxy-r9p4m                           1/1     Running   0          28m
kube-system   kube-scheduler-test-k8s-01                 1/1     Running   0          28m
Logo

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

更多推荐