安装初始环境
系统版本7.9 cpu 2c
主机节点 | 节点ip |
---|---|
master | 192.168.20.122 |
node1 | 192.168.20.123 |
node2 | 192.168.20.124 |
1.修改主机名 方便后续区分(3台机器都添加信息 并独自修改自己的名字)
master
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.20.122 master
192.168.20.123 node1
192.168.20.124 node2
[root@localhost ~]# hostname master
[root@localhost ~]# bash
[root@master ~]#
node1
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# hostname node1
[root@localhost ~]# bash
[root@node1 ~]#
node2
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# hostname node2
[root@localhost ~]# bash
[root@node2 ~]#
2.免密登录
master操作
ssh-keygen
进行三次回车
ssh-copy-id -i root@node2
ssh-copy-id -i root@node3
3.关闭防火墙
3台全部操作
systemctl stop firewalld && systemctl disable firewalld
4.配置slinux并关闭swap分区
3台全部操作
[root@node2 ~]# setenforce 0
[root@node2 ~]# sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@node2 ~]# getenforce
Permissive
[root@master ~]# swapoff -a
[root@master ~]# sed -i '/swap/ s/^\(.*\)$/#\1/g' /etc/fstab
[root@node2 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Sep 17 08:58:48 2024
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=5c0b82f2-5f15-4e41-ab54-2da67e588397 /boot xfs defaults 0 0
#/dev/mapper/centos-swap swap swap defaults 0 0
5.安装一些软件
3台全部操作
yum install -y device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxm12-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate telnet ipvsadm npdate
6.打开转发–导入模块
3台全部操作
[root@master ~]# modprobe ip_vs_rr
[root@master ~]# modprobe br_netfilter
[root@master ~]# lsmod | grep br_netfilter
br_netfilter 28672 0
[root@master ~]# vim /etc/modules-load.d/k8s.conf
[root@master ~]# cat >> /etc/modules-load.d/k8s.conf << EOF
> br_netfilter
> EOF
[root@master ~]# scp /etc/modules-load.d/k8s.conf root@node1:/etc/modules-load.d/
k8s.conf 100% 13 3.1KB/s 00:00
[root@master ~]# scp /etc/modules-load.d/k8s.conf root@node2:/etc/modules-load.d/
k8s.conf
7.修改内核参数
[root@master ~]# cat <<EOF >> /etc/sysctl.conf
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> net.ipv4.ip_nonlocal_bind = 1
> net.ipv4.ip_forward = 1
> vm.swappiness=0
> EOF
[root@master ~]# sysctl -p
全部执行 sysctl -p
[root@master ~]# scp /etc/sysctl.conf root@node1:/etc/
sysctl.conf 100% 598 165.0KB/s 00:00
[root@master ~]# scp /etc/sysctl.conf root@node2:/etc/
sysctl.conf
安装docker
1.使用阿里云的源
指定docker版本安装
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce-20.10.0 docker-ce-cli-20.10.0 containerd.io
配置加速地址使用docker
[root@master ~]# vim /etc/docker/daemon.json
[root@master ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://hub.atomgit.com"]
}
[root@master ~]# systemctl daemon-reload
[root@master ~]# systemctl start docker
传输文件 node节点重新加载 重启
[root@master ~]# scp /etc/docker/daemon.json root@node1:/etc/docker/
daemon.json 100% 54 13.2KB/s 00:00
[root@master ~]# scp /etc/docker/daemon.json root@node2:/etc/docker/
daemon.json
安装k8s
3台全部安装
使用阿里云的源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
安装指定版本的
yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
安装CoreDNS提供DNS解析服务
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.
改个名字
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4
安装tab键位
source <(kubeadm completion bash)
source <(kubectl completion bash)
vi .bashrc
末尾行添加
source <(kubeadm completion bash)
source <(kubectl completion bash)
初始化(master)
systemctl enable docker && systemctl enable kubelet
kubeadm init \
--apiserver-advertise-address=192.168.20.122 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--kubernetes-version v1.23.6 \
--image-repository registry.aliyuncs.com/google_containers
初始化成功
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 192.168.20.122:6443 --token 2p11ki.9joe3ada46h650jr \
--discovery-token-ca-cert-hash sha256:a514595d8b1af2d411a36f087343813ab88635d76cbac3eec6fcdd7b3bcf6f43
初始化完成后 配置命令环境
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
node节点操作
加入集群信息 master初始化成功最后一行复制
kubeadm join 192.168.20.122:6443 --token 2p11ki.9joe3ada46h650jr \
> --discovery-token-ca-cert-hash sha256:a514595d8b1af2d411a36f087343813ab88635d76cbac3eec6fcdd7b3bcf6f43
加入成功后显示
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
master
查看集群状态
[root@master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 11m v1.23.6
node1 NotReady <none> 4m49s v1.23.6
node2 NotReady <none> 4m42s v1.23.6
重要部分 网络插件的安装
calico安装
下载calico文件
curl -O https://docs.tigera.io/archive/v3.25/manifests/calico.yaml
修改一下文件
vim calico.yaml
找到以下内容 取消注释 修改ip
- name: CALICO_IPV4POOL_CIDR
value: "10.244.0.0/16"
注释文件中的外网地址拉取
sed -i 's#docker.io/##g' calico.yaml
[root@master ~]# grep image calico.yaml
image: docker.io/calico/cni:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/cni:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/node:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/node:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/kube-controllers:v3.25.0
imagePullPolicy: IfNotPresent
拉取calico镜像,所有节点都要执行\
文件pull不下来
从csdn站内搜索calico-image-v3.25.0.zip进行下载
解压后下载好通过docker进行导入
docker load -i calico-image-v3.25.0.tar
docker pull calico/cni:v3.25.0
docker pull calico/node:v3.25.0
docker pull calico/kube-controllers:v3.25.0
开始部署
kubectl apply -f calico.yaml
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 3h42m v1.23.6
node1 Ready <none> 3h41m v1.23.6
node2 Ready <none> 3h41m v1.23.6
[root@master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-64cc74d646-htbdb 1/1 Running 0 4m33s
calico-node-9v729 1/1 Running 0 4m33s
calico-node-jwjxk 1/1 Running 0 4m33s
calico-node-t6hhq 1/1 Running 0 4m33s
coredns-6d8c4cb4d-5kxp4 1/1 Running 0 3h43m
coredns-6d8c4cb4d-t2485 1/1 Running 0 3h43m
etcd-master 1/1 Running 7 (125m ago) 3h43m
kube-apiserver-master 1/1 Running 2 (125m ago) 3h43m
kube-controller-manager-master 1/1 Running 2 (125m ago) 3h43m
kube-proxy-jdq7z 1/1 Running 0 3h42m
kube-proxy-mzmfs 1/1 Running 0 3h42m
kube-proxy-wxkzh 1/1 Running 1 (125m ago) 3h43m
kube-scheduler-master 1/1 Running 12 (125m ago) 3h43m
k8s集群安装完成
参考文献 基于calico部署k8s集群k8s-1.23.6
原文链接:https://blog.csdn.net/shyboyandgirl/article/details/141460594
K8s安装部署–超级详细(无坑,v1.23)
原文链接:https://blog.csdn.net/m0_51720581/article/details/131153894