背景
服务器配置
节点 | 内网IP | 公网IP | 配置 |
---|---|---|---|
ds-2 | 10.80.239.78 | 无 | 4C16G |
ds-8 | 10.80.239.84 | 无 | 4C16G |
ds-9 | 10.80.239.85 | 无 | 4C16G |
ds-10 | 10.80.239.86 | 无 | 4C16G |
ds-11 | 10.80.239.87 | 无 | 4C16G |
软件版本
软件 | 版本 |
---|---|
centos | 7.6 |
docker | 20.10.7 |
kubelet | 1.20.9 |
kubeadm | 1.20.9 |
kubectl | 1.20.9 |
镜像版本
镜像 | 版本 |
---|---|
k8s.gcr.io/kube-apiserver | 1.20.9 |
k8s.gcr.io/kube-controller-manager | 1.20.9 |
k8s.gcr.io/kube-scheduler | 1.20.9 |
k8s.gcr.io/kube-proxy | 1.20.9 |
k8s.gcr.io/pause | 3.2 |
k8s.gcr.io/etcd | 3.4.13-0 |
k8s.gcr.io/coredns | 1.7.0 |
创建初始文件夹
#/Users/keyboardone/同步空间/software
mkdir -p /opt/software
cd /opt/software/k8s/
chmod 755 /opt/software/k8s/*.sh
配置ssh免密
ds-2
ds-2.sh
cd /opt/software/k8s/
vi ds-2.sh
#修改主机名
sudo hostnamectl set-hostname ds-2
sudo hostnamectl set-hostname "ds-2" --pretty
sudo hostnamectl set-hostname ds-2 --static
sudo hostnamectl set-hostname ds-2 --transient
#标识其他主机名
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.80.239.78 ds-2
10.80.239.84 ds-8
10.80.239.85 ds-9
10.80.239.86 ds-10
10.80.239.87 ds-11
EOF
#清空密钥
cd ~/.ssh/
rm -rf *
#用户目录下生成公钥、私钥文件
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
#分发公钥到其他主机
for ip in ds-8 ds-9 ds-10 ds-11; # 请将此处主机名替换为自己要部署的机器的 hostname
do
ssh-copy-id $ip # 该操作执行过程中需要手动输入用户的密码
done
ds-8
ds-8.sh
cd /opt/software/k8s/
vi ds-8.sh
#修改主机名
sudo hostnamectl set-hostname ds-8
sudo hostnamectl set-hostname "ds-8" --pretty
sudo hostnamectl set-hostname ds-8 --static
sudo hostnamectl set-hostname ds-8 --transient
#标识其他主机名
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.80.239.78 ds-2
10.80.239.84 ds-8
10.80.239.85 ds-9
10.80.239.86 ds-10
10.80.239.87 ds-11
EOF
#清空密钥
cd ~/.ssh/
rm -rf *
#用户目录下生成公钥、私钥文件
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
#分发公钥到其他主机
for ip in ds-2 ds-9 ds-10 ds-11; # 请将此处主机名替换为自己要部署的机器的 hostname
do
ssh-copy-id $ip # 该操作执行过程中需要手动输入用户的密码
done
ds-9
ds-9.sh
cd /opt/software/k8s/
vi ds-9.sh
#修改主机名
sudo hostnamectl set-hostname ds-9
sudo hostnamectl set-hostname "ds-9" --pretty
sudo hostnamectl set-hostname ds-9 --static
sudo hostnamectl set-hostname ds-9 --transient
#标识其他主机名
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.80.239.78 ds-2
10.80.239.84 ds-8
10.80.239.85 ds-9
10.80.239.86 ds-10
10.80.239.87 ds-11
EOF
#清空密钥
cd ~/.ssh/
rm -rf *
#用户目录下生成公钥、私钥文件
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
#分发公钥到其他主机
for ip in ds-2 ds-8 ds-10 ds-11; # 请将此处主机名替换为自己要部署的机器的 hostname
do
ssh-copy-id $ip # 该操作执行过程中需要手动输入用户的密码
done
ds-10
ds-10.sh
cd /opt/software/k8s/
vi ds-10.sh
#修改主机名
sudo hostnamectl set-hostname ds-10
sudo hostnamectl set-hostname "ds-10" --pretty
sudo hostnamectl set-hostname ds-10 --static
sudo hostnamectl set-hostname ds-10 --transient
#标识其他主机名
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.80.239.78 ds-2
10.80.239.84 ds-8
10.80.239.85 ds-9
10.80.239.86 ds-10
10.80.239.87 ds-11
EOF
#清空密钥
cd ~/.ssh/
rm -rf *
#用户目录下生成公钥、私钥文件
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
#分发公钥到其他主机
for ip in ds-2 ds-8 ds-9 ds-11; # 请将此处主机名替换为自己要部署的机器的 hostname
do
ssh-copy-id $ip # 该操作执行过程中需要手动输入用户的密码
done
ds-11
ds-11.sh
cd /opt/software/k8s/
vi ds-11.sh
#修改主机名
sudo hostnamectl set-hostname ds-11
sudo hostnamectl set-hostname "ds-11" --pretty
sudo hostnamectl set-hostname ds-11 --static
sudo hostnamectl set-hostname ds-11 --transient
#标识其他主机名
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.80.239.78 ds-2
10.80.239.84 ds-8
10.80.239.85 ds-9
10.80.239.86 ds-10
10.80.239.87 ds-11
EOF
#清空密钥
cd ~/.ssh/
rm -rf *
#用户目录下生成公钥、私钥文件
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
#分发公钥到其他主机
for ip in ds-2 ds-8 ds-9 ds-10; # 请将此处主机名替换为自己要部署的机器的 hostname
do
ssh-copy-id $ip # 该操作执行过程中需要手动输入用户的密码
done
文件准备
上传相关文件到 /opt/software/
分发
scp -r /opt/software/ ds-8:/opt/
scp -r /opt/software/ ds-9:/opt/
scp -r /opt/software/ ds-10:/opt/
scp -r /opt/software/ ds-11:/opt/
安装docker
rpm版(依赖epel)
#安装yum存储库
yum install -y epel-release.noarch
#安装Docker Yum源
yum -y install yum-utils
yum-config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
#rpm离线安装
cd /opt/software/offline_package/docker
rpm -ivh --replacefiles --replacepkgs *.rpm
#启动Docker
systemctl enable docker.service
systemctl start docker.service
tar版
#安装docker
cd /opt/software/docker
tar xzvf docker-20.10.7.tgz
chmod +x docker/*
mv docker/* /usr/local/bin/
#创建docker配置文件
echo '[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io
After=network.target
[Service]
Environment="PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin"
ExecStart=/usr/local/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
RestartSec=5
TimeoutSec=0
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
' >> /etc/systemd/system/docker.service
#重新加载docker配置文件
cd /usr/local/bin
#重新加载配置文件
systemctl daemon-reload
#设置开机启动
systemctl enable docker.service
#启动
systemctl start docker.service
#重启
systemctl daemon-reload
systemctl restart docker
#等待
sleep 30s
#添加docker源
mkdir -p /etc/docker/
touch /etc/docker/daemon.json
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors":["https://docker.mirrors.ustc.edu.cn/"],
"exec-opts": ["native.cgroupdriver=systemd"],
"insecure-registries": ["http://ren:8088"]
}
EOF
#重启docker
systemctl daemon-reload
systemctl restart docker
#等待
sleep 1m
#查看验证docker
docker info
安装Kubernetes
加载Kubernetes镜像
#加载Kubernetes镜像
docker load -i /opt/software/images/k8simages.tar
#恢复镜像名称及tag
while read REPOSITORY TAG IMAGE_ID
do
echo "== Tagging $REPOSITORY $TAG $IMAGE_ID =="
docker tag "$IMAGE_ID" "$REPOSITORY:$TAG"
done < /opt/software/images/k8simages.list
设置Kubernetes对应内核参数
#设置Kubernetes对应内核参数
cat > /etc/sysctl.d/kubernetes.conf << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
重新加载参数
#重新加载参数
modprobe br_netfilter
sysctl --system
关闭swap区
#关闭swap区
swapoff -a
sed -e '/swap/s/^/#/g' -i /etc/fstab
关闭SELinux
#关闭SELinux
getenforce
sestatus
setenforce 0
getenforce
sestatus
rpm离线安装
#添加yum源
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
#安装离线包
systemctl stop firewalld
cd /opt/software/offline_package/k8s
rpm -ivh --replacefiles --replacepkgs /opt/software/offline_package/k8s/*.rpm
bash中启用kubectl
#bash中启用kubectl
kubectl completion bash > /etc/bash_completion.d/kubectl
确认主节点的kubelet服务状态
#初始化Kubernetes主节点
systemctl status kubelet
systemctl daemon-reload
sudo systemctl enable --now kubelet
systemctl status kubelet
使用 kubeadm 引导集群
所有节点执行
#所有机器添加master域名映射,以下需要修改为自己的
echo "10.80.239.78 cluster-endpoint" >> /etc/hosts
主节点执行
kubeadm init \
--apiserver-advertise-address=10.80.239.78 \
--control-plane-endpoint=cluster-endpoint \
--image-repository k8s.gcr.io \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16
得到
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join cluster-endpoint:6443 --token yee1e9.8nrqge5yc5xsftbi \
--discovery-token-ca-cert-hash sha256:2767e227965196fef7ac8831c12b67ac6d9babbc6e25f41b6560f48e5651eab4 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join cluster-endpoint:6443 --token yee1e9.8nrqge5yc5xsftbi \
--discovery-token-ca-cert-hash sha256:2767e227965196fef7ac8831c12b67ac6d9babbc6e25f41b6560f48e5651eab4
主节点执行建议脚本
#执行建议脚本
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
确认从节点的kubelet服务状态
#初始化Kubernetes从节点
systemctl status kubelet
systemctl daemon-reload
sudo systemctl enable --now kubelet
systemctl status kubelet
从节点执行命令加入k8s集群
kubeadm join cluster-endpoint:6443 --token yee1e9.8nrqge5yc5xsftbi \
--discovery-token-ca-cert-hash sha256:2767e227965196fef7ac8831c12b67ac6d9babbc6e25f41b6560f48e5651eab4
安装calico
上传文件
kubectl apply -f calico.yaml
查看初始化进度
#监听应用启动情况
kubectl get pod -A -w
#或者
watch -n 1 kubectl get pod -A
#检查各节点连接状态
kubectl get pods -o wide --all-namespaces
#或者
watch -n 1 kubectl get pods -o wide --all-namespaces