三台服务器
master 192.168.3.101
node1 192.168.3.102
node2 192.168.3.104
#将192.168.3.101的主机名改为master
hostnamectl set-hostname master && bash
#将192.168.3.102的主机名改为node1
hostnamectl set-hostname node1 && bash
#将192.168..3.104的主机名改为node2
hostnamectl set-hostname node2 && bash
#添加hosts
cat >> /etc/hosts << EOF
192.168.3.101 master
192.168.3.102 node1
192.168.3.104 node2
EOF
#查看
cat /etc/hosts
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
firewall-cmd --state
#selinux永久关闭
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
cat /etc/selinux/config
#swap永久关闭
swapoff --all
sed -ri 's/.*swap.*/#&/' /etc/fstab
cat /etc/fstab
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#查看
cat /etc/sysctl.d/k8s.conf
#加载br_netfilter模块
modprobe br_netfilter
#查看是否加载
lsmod | grep br_netfilter
#加载网桥过滤及内核转发配置文件
sysctl -p /etc/sysctl.d/k8s.conf
yum -y install ntpdate
echo "0 */1 * * * /usr/sbin/ntpdate time1.aliyun.com" > /var/spool/cron/root
crontab -l
#安装ipset及ipvsadm
yum -y install ipset ipvsadm
配置ipvsadm模块加载方式
#添加需要加载的模块
echo ' #!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
' > /etc/sysconfig/modules/ipvs.modules
#查看
cat /etc/sysconfig/modules/ipvs.modules
#授权、运行、检查是否加载
chmod 755 /etc/sysconfig/modules/ipvs.modules
bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep -e ip_vs -e nf_conntrack
#重启
reboot
配置准备完成后,所有节点都需重启
打开docker-ce.repo的文件,并将以下内容复制到文件中:
echo '
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
' > /etc/yum.repos.d/docker-ce.repo
保存并退出文件。
运行以下命令来安装Docker CE:
yum install -y docker-ce docker-ce-cli containerd.io
#启动docker并设置开机自启
systemctl start docker
systemctl enable docker
#查看版本
docker -v
docker compose version
配置修改为如下:
#将配置写入daemon.json文件
echo '
{
"exec-opts": ["native.cgroupdriver=systemd"]
}' > /etc/docker/daemon.json
#查看
cat /etc/docker/daemon.json
systemctl daemon-reload
systemctl restart docker
docker info
#下载cri-dockerd安装包
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.1/cri-dockerd-0.3.1-3.el7.x86_64.rpm
#安装cri-dockerd
rpm -ivh cri-dockerd-0.3.1-3.el7.x86_64.rpm
#修改镜像地址为国内,否则kubelet拉取不了镜像导致启动失败
vi /usr/lib/systemd/system/cri-docker.service
###########################修改内容######################
ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9
###########################修改内容######################
#启动cri-dockerd
systemctl daemon-reload
systemctl start cri-docker.service
systemctl enable cri-docker.service
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#安装kubelet、kubeadm、kubectl
yum install -y kubelet-1.28.0 kubeadm-1.28.0 kubectl--1.28.0 --disableexcludes=kubernetes
#将cgroup改为systemd
echo 'KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"' > /etc/sysconfig/kubelet
#查看
cat /etc/sysconfig/kubelet
#设置开机启动
systemctl restart kubelet.service
systemctl enable kubelet.service
kubeadm init \
--apiserver-advertise-address=192.168.3.101\
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.28.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--cri-socket=unix:///var/run/cri-dockerd.sock \
--ignore-preflight-errors=all
–apiserver-advertise-address=192.168.3.101 :集群的主节点IP地址
–image-repository registry.aliyuncs.com/google_containers:指定容器镜像的仓库地址。
–kubernetes-version v1.28.0:指定要使用的Kubernetes版本。
–service-cidr=10.90.0.0/16:指定服务网络的CIDR范围。
–pod-network-cidr=10.240.0.0/16:指定Pod网络的CIDR范围。
–cri-socket=unix:///var/run/cri-dockerd.sock:不可缺少
执行成功后会产生如下的命令参数(需要分别在本机和各node节点执行):
[addons] Applied essential addon: kube-proxy
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.3.101:6443 --token xrnhnh.37cn3fcsltg7e90h --discovery-token-ca-cert-hash sha256:fb68fac112fd55ddb45c691a5b9db98a9bf15ce86a0244641cd787654aa3cf40
在本机(master)执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubeadm join 192.168.3.101:6443 --token xrnhnh.37cn3fcsltg7e90h \
--discovery-token-ca-cert-hash sha256:fb68fac112fd55ddb45c691a5b9db98a9bf15ce86a0244641cd787654aa3cf40 \
--cri-socket=unix:///var/run/cri-dockerd.sock #此行不可缺少
wget https://docs.projectcalico.org/manifests/calico.yaml
#修改calico.yaml找到CALICO_IPV4POOL_CIDR
vi calico.yaml
##############修改内容###################
value: "10.244.0.0/16"
##############修改内容###################
#在master节点上安装calico
kubectl apply -f calico.yaml
#查看所有的节点
kubectl get nodes
kubectl get nodes -o wide
#查看集群健康情况
kubectl get cs
#创建Nginx程序
kubectl create deployment nginx --image=nginx
#开放80端口
kubectl expose deployment nginx --port=80 --type=NodePort
#查看pod状态
kubectl get pod
#查看service状态
kubectl get service
##########################################################################
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 5d1h
nginx NodePort 10.98.221.224 80:32743/TCP 23s
##########################################################################
#访问网页测试(端口号以查看service状态得到的为准)
http://192.168.3.101:32743/
成功界面如下
点击链接查看Kubernetes Dashboard
安装部署教程
https://blog.csdn.net/AMCUL/article/details/132865544