gitlab-cicd+docker+harbor+k8s实现spring项目部署
公网IP | 私网IP | 配置 | 角色 |
---|---|---|---|
39.98.160.204 | 172.22.128.38 | 2c 8g(阿里云) | k8s-master |
101.42.166.142 | 10.0.8.11 | 8c 16g(腾讯云) | k8s-node1、gitlab、gitlab-runner |
39.98.49.122 | 172.19.194.168 | 2c 8g(阿里云) | k8s-node2、harbor |
k8s v1.23.1搭建
因为条件有限,只能使用公网的云主机来搭建k8s集群。一般实际使用内网搭建就可以了,而且相比于下面内网搭建要更容易一些。
# 云主机的安全组全放开
# 分别给三个云主机创建虚拟网卡
cat > /etc/sysconfig/network-scripts/ifcfg-eth0:1 << EOF
BOOTPROTO=static
DEVICE=eth0:1
IPADDR=39.98.160.204/IPADDR=101.42.166.142/IPADDR=39.98.49.122
PREFIX=32
TYPE=Ethernet
USERCTL=no
ONBOOT=yes
EOF
# 配置完重启网络
service network restart
# 我的云主机下面的部分本身就是关闭的,所以注了。没关的话都关上。
# 关闭防火墙
# systemctl stop firewalld && systemctl disable firewalld
# 关闭selinux
# setenforce 0 && getenforce
# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
# 关闭swap
# swapoff -a
# sed -ri 's/.*swap.*/#&/' /etc/fstab
# 分别给三个云主机设置主机名
hostnamectl set-hostname k8s-master/k8s-node1/k8s-node2
# 分别在三个云主机上修改hosts文件
cat >> /etc/hosts << EOF
39.98.160.204 k8s-master
101.42.166.142 k8s-node1
39.98.49.122 k8s-node2
EOF
# 修改内核参数
cat > /etc/modules-load.d/k8s.conf << EOF
br_netfilter
EOF
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
# 据说要配置时间同步,我没配,暂时还没受到影响
# yum -y install ntpdate
# ntpdate time.windows.com
# 安装docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-20.10.9 docker-ce-cli-20.10.9 containerd.io
# 修改docker的驱动 追加以下内容
mkdir /etc/docker && touch /etc/docker/daemon.json
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"insecure-registries":["39.98.49.122:5001"],
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
] ,
# 下面的地址换成自己的
"registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"]
}
EOF
systemctl enable docker && systemctl start docker
# 配置k8s的yum源
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
yum install -y kubeadm-1.23.1 kubelet-1.23.1 kubectl-1.23.1 kubernetes-cni
systemctl enable kubelet
# 配置kubelet启动文件
# cat >> /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf << EOF
# 在最后一行末尾添加
--node-ip=39.98.160.204
# EOF
# cat >> /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf << EOF
--node-ip=101.42.166.142
# EOF
# cat >> /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf << EOF
--node-ip=39.98.49.122
# EOF
# 初始化k8s集群
kubeadm init \
--apiserver-advertise-address=39.98.160.204 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.1 \
--control-plane-endpoint=39.98.160.204 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--v=5
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config