一、软件环境
操作系统 centos7
docker:20+
k8s:1.23.6
二、初始化操作(所有节点)
1.yum 换源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
2.关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
3.关闭 selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
4.关闭 swap
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
swapoff -a # 临时
5.重启虚拟机
6.根据规划设置主机名
hostnamectl set-hostname
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
7.在 master 添加 hosts
cat >> /etc/hosts << EOF
192.168.2.190 k8s-master
192.168.2.126 k8s-node1
192.168.2.135 k8s-node2
EOF
8.将桥接的 IPv4 流量传递到 iptables 的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
9.时间同步
yum install ntpdate -y
(分开执行不要全复制)
ntpdate time.windows.com
三、安装基础软件(所有节点)
1.安装 docker
step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
错误信息
Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
Could not fetch/save url https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 14] curl#60 - “Peer’s Certificate issuer is not recognized.”
# 编辑 /etc/yum.conf 文件, 在 [main] 下面添加 sslverify=0 参数
vi /etc/yum.conf
# 配置如下----------------------
[main]
sslverify=0
# -----------------------------
Step 3: 更新并安装 Docker-CE
yum makecache fast
yum -y install docker-ce
Step 4: 开启 Docker 服务
service docker start
注意:
# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。
# vim /etc/yum.repos.d/docker-ee.repo
# 将[docker-ce-test]下方的 enabled=0 修改为 enabled=1
#
# 安装指定版本的 Docker-CE:
# Step 1: 查找 Docker-CE 的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
# Loading mirror speeds from cached hostfile
# Loaded plugins: branch, fastestmirror, langpacks
# docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
# docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable
# docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
# Available Packages
# Step2: 安装指定版本的 Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]
Step 5: 安装校验
docker version
Step 6: docker 换源
vi /etc/docker/daemon.json
{
"registry-mirrors" : [
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com",
"https://cr.console.aliyun.com/"
],
"exec-opts": ["native.cgroupdriver=systemd"]
}
Step 7:重启 docker
systemctl daemon-reload
systemctl restart docker
2.添加阿里云 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
3.安装 kubeadm、kubelet、kubectl
yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
systemctl enable kubelet
配置关闭 Docker 的 cgroups,修改 /etc/docker/daemon.json,加入以下内容
"exec-opts": ["native.cgroupdriver=systemd"]
重启 docker
systemctl daemon-reload
systemctl restart docker
配置关闭 Docker 的 cgroups,修改 /etc/docker/daemon.json,加入以下内容
"exec-opts": ["native.cgroupdriver=systemd"]
重启 docker
systemctl daemon-reload
systemctl restart docker
四、部署 k8s master(仅在 master 节点执行)
在 Master 节点下执行
kubeadm 初始化有可能出现得问题
1、swap 分区注释后需要重启
2、docker 的驱动与 kubelet 驱动不一致
docker info | grep Cgroup
docker info | grep Driver
cat /var/lib/kubelet/config.yaml | grep cgroup
初始化
kubeadm init \
--apiserver-advertise-address=192.168.2.190 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.6 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
安装成功后,复制如下配置并执行
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
查询 nodes
kubectl get nodes
五、加入 k8s node (仅在非 master 节点执行)
分别在 k8s-node1 和 k8s-node2 执行
下方命令可以在 k8s master 控制台初始化成功后复制 join 命令
kubeadm join 192.168.2.190:6443 --token 4iqq92.30npytadpu42tero --discovery-token-ca-cert-hash sha256:31e48846230ff0543d28e1dba76b4d1835dd578906f214f20910a3ad8b61191e
如果初始化的 token 不小心清空了,可以通过如下命令获取或者重新申请
# 如果 token 已经过期,就重新申请
kubeadm token create
# token 没有过期可以通过如下命令获取
kubeadm token list
# 获取 --discovery-token-ca-cert-hash 值,得到值后需要在前面拼接上 sha256:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
六、部署 CNI 网络插件(在 master 节点上执行)
下载 calico 配置文件,可能会网络超时
mkdir /opt/k8s
cd /opt/k8s
curl https://calico-v3-25.netlify.app/archive/v3.25/manifests/calico.yaml -O
修改 calico.yaml 文件中的 CALICO_IPV4POOL_CIDR 配置,修改为与初始化的 cidr 相同,即为 10.244.0.0/16
修改 IP_AUTODETECTION_METHOD 下的网卡名称。没找到,没改。
删除镜像 docker.io/ 前缀,避免下载过慢导致失败
sed -i ‘s#docker.io/##g’ calico.yaml
执行部署命令
kubectl apply -f calico.yaml
执行之后,看 pod 都启动起来没
kubectl get po -n kube-system
查看具体 pod
kubectl describe pod calico-node-lk6fx -n kube-system
删除 pod
kubectl delete pod calico-node-hh975 -n kube-system
错误:
会遇到
Normal BackOff 2m11s (x2 over 5m31s) kubelet Back-off pulling image "calico/cni:v3.25.0"
Warning Failed 2m11s (x2 over 5m31s) kubelet Error: ImagePullBackOff
Normal Pulling 119s (x3 over 8m28s) kubelet Pulling image "calico/cni:v3.25.0"
因为 docker 无法拉取镜像导致
手动到所有节点拉取
docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/calico/node:v3.25.0
#docker tag swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/calico/node:v3.25.0 calico/node:v3.25.0
docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/calico/kube-controllers:v3.25.0
#docker tag swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/calico/kube-controllers:v3.25.0 calico/kube-controllers:v3.25.0
docker pull docker.m.daocloud.io/calico/cni:v3.25.0
#docker tag docker.m.daocloud.io/calico/cni:v3.25.0 calico/cni:v3.25.0
kubectl get nodes
部署结束。
七、测试 k8s 集群
在 master 节点创建部署
kubectl create deployment nginx --image=nginx:v1.27.0
kubectl describe pod nginx
暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
查看 pod 以及服务信息
kubectl get pod,svc
错误:nginx 镜像无法拉取
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 20m default-scheduler Successfully assigned default/nginx-69655d54b-bpxjk to k8s-node1
Normal Pulling 18m (x4 over 20m) kubelet Pulling image "nginx:v1.27.0"
Warning Failed 17m (x4 over 20m) kubelet Failed to pull image "nginx:v1.27.0": rpc error: code = Unknown desc = Error response from daemon: manifest for nginx:v1.27.0 not found: manifest unknown: manifest unknown
Warning Failed 17m (x4 over 20m) kubelet Error: ErrImagePull
Warning Failed 17m (x6 over 20m) kubelet Error: ImagePullBackOff
Normal BackOff 23s (x75 over 20m) kubelet Back-off pulling image "nginx:v1.27.0"
换源。
{
"registry-mirrors": 【
"https://docker.m.daocloud.io",
"https://docker.1panel.live"
任意节点任用 kubectl
将 master 节点中
/etc/kubernetes/admin.conf 拷贝到 node 到/etc/kubernetes
scp /etc/kubernetes/admin.conf root@192.168.2.126:/etc/kubernetes
echo "export KUBECONFIG=/etc/kubernetes/admin.conf">>~/.bash_peofile
source ~/.bash_peofile
错误提示:
由于断电停机,kubernetes 集群挂掉,使用任意 kubectl 命令会报错:
The connection to the server ip:6443 was refused - did you specify the right host or port,重启 kubelet 也不能恢复,etcd 读取数据报错,数据文件损坏
解决办法:
systemctl status kubelet
journalctl -u kubelet
docker ps -a| grep kube-apiserver
docker ps -a| grep etcd
只能重新安装。
kubeadm reset
rm -rf $HOME/.kube
在 master 节点执行即可
kubeadm init --config=kubeadm.yaml --ignore-preflight-errors=SystemVerification
创建必要文件
先初始化
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown (id−u):(id−u):(id -g) $HOME/.kube/config
查看将工作节点加入集群的命令
kubeadm token create --print-join-command
工作节点 node1 集群机器
查看 docker 状态,重启 docker。通过日志可以查询
kubeadm reset
kubeadm join 192.168.5.132:6443 --token abcdef.0123456789abcdef
–discovery-token-ca-cert-hash sha256:843aae90033aa0f5a3bff1fc8fc977aeea2f423e50b5b991dfb0f5c9971a3c1b
工作节点 node2 集群机器
kubeadm reset
kubeadm join 192.168.5.132:6443 --token abcdef.0123456789abcdef
–discovery-token-ca-cert-hash sha256:843aae90033aa0f5a3bff1fc8fc977aeea2f423e50b5b991dfb0f5c9971a3c1b
查看 node 状态
kubectl get node
calico 也需要重新安装
vim 写 yaml 文件容易出错。kind:Pod
手写 yaml 配置 nginx,因为 pull nginx 容易出错。
Normal Created 4m14s (x5 over 5m38s) kubelet Created container nginx
Normal Started 4m14s (x5 over 5m38s) kubelet Started container nginx
Normal Pulled 4m14s (x4 over 5m38s) kubelet Container image "nginx:latest" already present on machine
Warning BackOff 2m56s (x14 over 5m37s) kubelet Back-off restarting failed container
kubectl logs podname
用于查询部署时出错错误查询日志。