此处只做学习使用,配置单master环境。
1、ip+主机规划(准备五台新机)==>修改各个节点的主机名
注意:关闭防火墙与selinux
节点 | 主机名 | ip | 身份 |
joshua1 | kubernetes-master.openlab.cn |
192.168.134.151 | master |
joshua2 | kubernetes-work1.openlab.cn | 192.168.134.152 | work1 |
joshua3 | kubernetes-work2.openlab.cn | 192.168.134.153 | work2 |
joshua4 | kubernetes-work3.openlab.cn | 192.168.134.154 | work3 |
joshua5 | kubernetes-register.openlab.cn | 192.168.134.155 | register |
2、设置hosts解析
tail -n 5 /etc/hosts
192.168.134.151 kubernetes-master.openlab.cn kubernetes-master
192.168.134.152 kubernetes-work1.openlab.cn kubernetes-work1
192.168.134.153 kubernetes-work2.openlab.cn kubernetes-work2
192.168.134.154 kubernetes-work3.openlab.cn kubernetes-work3
192.168.134.155 kubernetes-register.openlab.cn kubernetes-register
3、时间同步
yum install chrony -y
vim /etc/chrony.conf
修改处如下:
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp1.aliyun.com iburst
启动
systemctl enable --now chronyd
chronyc sources
4、禁用swap分区
sed -i 's/.*swap.*/#&/' /etc/fstab
临时关闭
swapoff -a # 非常重要,如果没有设置,则报错
5、修改其内核参数
cat >> /etc/sysctl.d/k8s.conf << EOF
vm.swappiness=0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
执行以下几个命令
modprobe br_netfilter
modprobe overlay
sysctl -p /etc/sysctl.d/k8s.conf
6、配置ipvs功能
安装ipset和ipvsadm:yum install ipset ipvsadm -y
添加需要加载的模块写入脚本文件
cat < /etc/sysconfig/modules/ipvs.modules
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
为脚本文件添加执行权限
chmod +x /etc/sysconfig/modules/ipvs.modules
执行脚本文件
/bin/bash /etc/sysconfig/modules/ipvs.modules
查看对应的模块是否加载成功
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
1、定制软件源并且安装最新版docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
2、配置docker加速器并且启动
cat >> /etc/docker/daemon.json <<-EOF
{
"registry-mirrors": [
"http://74f21445.m.daocloud.io",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": ["kubernetes-register.openlab.cn"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl enable --now docker
解压包并且放到指定位置
tar xf cri-dockerd-0.3.4.amd64.tgz -C /usr/local/
mv /usr/local/cri-dockerd/cri-dockerd /usr/local/bin/
检查软件版本
cri-dockerd --version
cri-dockerd 0.3.4 (e88b1605)
配置文件的修改
cat > /etc/systemd/system/cri-dockerd.service<<-EOF
[Unit]
Description=CRI Interface for Docker Application Container Engine
Documentation=https://docs.mirantis.com
After=network-online.target firewalld.service docker.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/local/bin/cri-dockerd --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9 --network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin --container-runtime endpoint=unix:///var/run/cri-dockerd.sock --cri-dockerd-root-directory=/var/lib/dockershim --docker-endpoint=unix:///var/run/docker.sock --
cri-dockerd-root-directory=/var/lib/docker
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/cri-dockerd.socket <<-EOF
[Unit]
Description=CRI Docker Socket for the API
PartOf=cri-docker.service
[Socket]
ListenStream=/var/run/cri-dockerd.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
EOF
启动服务
systemctl daemon-reload
systemctl enable --now cri-dockerd.service
1、安装docker-compose
install -m 755 docker-compose-linux-x86_64 /usr/local/bin/docker-compose
查看版本
docker-compose version
Docker Compose version v2.20.3
2、安装harbor软件
mkdir -p /data/server/
tar xf harbor-offline-installer-v2.8.4.tgz -C /data/server/
cd /data/server/harbor/
docker load -i harbor.v2.8.4.tar.gz # 导入镜像
cp harbor.yml.tmpl harbor.yml
修改配置文件(以下只列出修改过的地方)
hostname: kubernetes-register.openlab.cn
# https related config
#https:
# https port for harbor, default is 443
# port: 443
# The path of cert and key files for nginx
# certificate: /your/certificate/path
# private_key: /your/private/key/path
data_volume: /data/server/harbor/data
./install.sh # 启动
定制一个服务启动文件
vim /etc/systemd/system/harbor.service
[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor
[Service]
Type=simple
Restart=on-failure
RestartSec=5
#需要注意harbor的安装位置
ExecStart=/usr/local/bin/docker-compose --file /data/server/harbor/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose --file /data/server/harbor/docker-compose.yml down
[Install]
WantedBy=multi-user.target
启动脚本
systemctl daemon-reload
systemctl enable --now harbor.service
测试:在除仓库外集群的任意一个节点测试
docker pull busybox
docker login kubernetes-register.openlab.cn -u admin -p Harbor12345
docker tag busybox:latest kubernetes-register.openlab.cn/library/busybox:latest
docker push kubernetes-register.openlab.cn/library/busybox:latest
1、定制软件源
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
2、安装软件,然后设置自启动
yum install kubeadm kubectl kubelet -y
systemctl enable --now kubelet
3、记得在仓库中创建项目,google_containers ,然后编写脚本(此处我们使用的kubernetes版本为1.28.1),最后要执行脚本
cat images.sh
#!/bin/bash
images=$(kubeadm config images list --kubernetes-version=1.28.1 | awk -F'/' '{print $NF}')
for i in ${images}
do
docker pull registry.aliyuncs.com/google_containers/$i
docker tag registry.aliyuncs.com/google_containers/$i kubernetes-register.openlab.cn/google_containers/$i
docker push kubernetes-register.openlab.cn/google_containers/$i
docker rmi registry.aliyuncs.com/google_containers/$i
done
sh images.sh # 执行脚本
4、初始化master节点
kubeadm init --kubernetes-version=1.28.1 \ # 自己使用的版本
--apiserver-advertise-address=192.168.134.151 \ # 自己的master节点ip
--image-repository kubernetes-register.openlab.cn/google_containers \ # 自己的主机名加项目名
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=Swap \
--cri-socket=unix:///var/run/cri-dockerd.sock # 非常重要,不可删除
5、初始化成功后悔提示下一步安装步骤(根据自己初始化后提示的命令来安装)
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
6、加入work节点(先在master节点上执行) ==> 接着在多个work节点执行
kubeadm join 192.168.134.151:6443 --token hgxtuj.gehto17fynyiglxy --cri-socket=unix:///var/run/cri-dockerd.sock --discovery-token-ca-cert-hash sha256:00e2bac5c90162ac991e9eff77fdceaa331cb0c1b9cd170186be3587dd5e964d # 一切按照自己的ip和初始化之后给定的值来加入
7、在master的环境文件中加入以下内容
echo "source <(kubectl completion bash)" >> ~/.bashrc
echo "source <(kubeadm completion bash)" >> ~/.bashrc
source ~/.bashrc
8、配置网络环境
下载kube-flannel.yml
# cat flannel.sh # 写脚本
#!/bin/bash
for i in $(grep image kube-flannel.yml | grep -v '#' | awk -F '/' '{print $NF}')
do
docker pull flannel/$i
docker tag flannel/$i kubernetes-register.openlab.cn/google_containers/$i
docker push kubernetes-register.openlab.cn/google_containers/$i
docker rmi flannel/$i
done
sh flannel.sh # 脚本启动
sed -i '/ image:/s#docker.io/flannel#kubernetes-register.openlab.cn/google_containers#' kube-flannel.yml # 修改内容
应用配置文件
kubectl apply -f kube-flannel.yml # 应用配置文件
9、如果上面的步骤都没有出问题,那么整体部署基本成功
kubectl get pod -n kube-system
scp -r $HOME/.kube kubernetes-work1.openlab.cn:$HOME/
然后使用kubectl get pod -n kube-system查看是否状态全部running,如果全部running即可以运行。