本文使用主机的系统皆为CentOS 7,所有机器之间网络连接正常。
确保3台主机的 /etc/hostname 和/etc/hosts文件中已经修改为正确的主机名。以及相应的主机名称解析,修改后,请重启系统。
注意:请确保CPU至少2核,内存2G
所有命令皆在root用户下运行
参考:
https://blog.csdn.net/u013469753/article/details/109239593
Hostnamer:k8s-singlenode
主机名 | IP地址 | 作用 | 配置 |
---|---|---|---|
k8s-singlenode | 192.168.2.107 | Master&Work | 2U4G64G |
配置文件/etc/hosts
# k8s-singlenode
hostnamectl set-hostname k8s-singlenode
exec bash
hostnamectl
hostname
cat /etc/hosts
echo 192.168.2.107 k8s-singlenode >> /etc/hosts
cat /etc/hosts
注:
①某些发行版本在/etc/hosts 文件中添加了多余的条目,该条目将实际的主机名解析为另一个回送IP地址,例如127.0.1.1。如果有的话,则必须将其注释或删除,以防止名称解析问题。
②不要删除127.0.0.1条目。
③每一个节点都需要对其他节点进行主机名的解析配置。
④主机名修改完成后,退出当前终端,重新登录服务器。
ping -c 4 k8s-singlenode
ping -c 4 qq.com
在实验环境中可以选择直接禁用防火墙,但是在生产环境中根据实际的需求进行规则的配置。
禁用防火墙
在CentOS7上面防火墙是firewalld服务,停止并且禁止开机启动firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
setenforce 0
getenforce
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
grep SELINUX=disabled /etc/sysconfig/selinux
grep SELINUX=disabled /etc/selinux/config
参考链接
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
执行如下命令使修改生效:
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
禁用swap
Linux swapoff命令用于关闭系统交换区(swap area)。swapoff实际上为swapon的符号连接,可用来关闭系统的交换区。
sed -i '/swap/ s/^/#/' /etc/fstab
swapoff -a
free -m
-a 将/etc/fstab文件中所有设置为swap的设备关闭
-h 帮助信息
-V 版本信息
Centos7换回国内源
cd
mkdir ori_repo-config
mv /etc/yum.repos.d/* ./ori_repo-config/
cd /etc/yum.repos.d/
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all
yum makecache
yum repolist
cd
yum install -y lsof vim net-tools wget git curl
yum install -y yum-utils device-mapper-persistent-data lvm2
所有节点都需要安装docker,
docker官方安装指南
菜鸟教程参考
阿里教程:
https://developer.aliyun.com/article/110806
https://yq.aliyun.com/articles/626118
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
手动安装,请参考菜鸟教程的安装指南
通过阿里云安装。
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
systemctl start docker
systemctl status docker
systemctl enable docker
systemctl list-unit-files |grep chronyd.service
docker version
快速配置
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors":["https://docker.mirrors.ustc.edu.cn/"]
}
EOF
or
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://ustc-edu-cn.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://registry.aliyuncs.com"
]
}
EOF
重新启动Docker并查看Docker信息
systemctl daemon-reload
docker info
systemctl restart docker
systemctl status docker
docker info
至此,docker安装完成。
安装kubectl、kubelet、kubeadm
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum clean all
yum makecache fast -y
yum install -y kubelet kubeadm kubectl
启动kubelet服务
systemctl start kubelet
systemctl status kubelet
systemctl enable kubelet
此时status应该是无法正常启动的,可以忽略,具体的解决措施请继续下一节的部署操作。
systemctl status kubelet
问题解释:kubelet服务启动失败,错误代码255
至此,所有节点,Kubernetes工具安装完成。
docker info |grep Cgroup
cat /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
sed -i 'N;4iEnvironment="KUBELET_CGROUP_ARGS=--cgroup-driver=cgroupfs"' /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload
systemctl restart kubelet
systemctl status kubelet
pwd
cd
kubeadm config images list
将输出结果的版本信息填入下方
images
touch k8s-pull-image.sh
vim k8s-pull-image.sh
#!/bin/bash
images=(
kube-apiserver:v1.21.1
kube-controller-manager:v1.21.1
kube-scheduler:v1.21.1
kube-proxy:v1.21.1
pause:3.4.1
etcd:3.4.13-0
coredns/coredns:v1.8.0
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName} k8s.gcr.io/${imageName}
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
done
执行脚本
bash ./k8s-pull-image.sh
执行过程中,无法找到 coredns:1.8.0
,手动安装coredns:1.8.0
,dockerhub地址
docker pull coredns/coredns:1.8.0
docker tag docker.io/coredns/coredns:1.8.0 k8s.gcr.io/coredns/coredns:v1.8.0
docker rmi docker.io/coredns/coredns:1.8.0
kubeadm config images list
docker images
注意:主机地址空间、kubernetes版本
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \
--kubernetes-version=v1.21.1 \
--ignore-preflight-errors=NumCPU \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 --v=6
如需重新初始化,则需要先进行reset
kubeadm reset
当前root
用户下
echo export KUBECONFIG=/etc/kubernetes/admin.conf >> ~/.bash_profile
source ~/.bash_profile
根据初始提示,普通用户centos
su centos
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
exit
export KUBECONFIG=/etc/kubernetes/admin.conf
yum install -y bash-completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc
单节点时,需要主节点添加污点taint,允许master节点运行pod
kubectl taint nodes --all node-role.kubernetes.io/master
kubectl taint nodes --all node-role.kubernetes.io/master-node/k8s-master untainted
kubectl taint node k8s-singlenode node-role.kubernetes.io/master-
kubectl taint nodes k8s-singlenode node-role.kubernetes.io/master=:NoSchedule
参考链接: 为k8s-master节点添加污点taints
查看节点信息
kubectl get cs
kubectl get csr
kubectl get nodes
修改yaml配置文件
k8s-master节点主节点,注释下面文件中相应的行。
vim /etc/kubernetes/manifests/kube-controller-manager.yaml +26
vim /etc/kubernetes/manifests/kube-scheduler.yaml +19
systemctl restart kubelet.service
主、从节点
kubectl get pods --all-namespaces
kubectl get cs
kubectl get csr
kubectl get nodes
接下来我们来安装flannel网络插件,很简单,和安装普通的 POD 没什么两样:
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
or
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
sleep 100s
安装完成后使用 kubectl get pods 命令可以查看到我们集群中的组件运行状态,如果都是Running 状态的话,那么恭喜你,你的 master 节点安装成功了。
kubectl get cs
kubectl get csr
kubectl get nodes
systemctl restart kubelet
systemctl restart docker
kubectl get pods --all-namespaces
kubectl get cs
kubectl get csr
kubectl get nodes
kubectl version
systemctl status kubelet
安装
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml
kubectl apply -f recommended.yaml
or
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml
sleep 100s
kubectl -n kubernetes-dashboard get pods
kubectl -n kubernetes-dashboard get svc
排错参考记录dial tcp 10.96.0.1:443: i/o timeout
主节点dashboard
https://zuozewei.blog.csdn.net/article/details/108193351
https://blog.csdn.net/l13568/article/details/113606606
https://github.com/rootsongjc/kubernetes-handbook/issues/92
kubectl cluster-info
kubectl -n kubernetes-dashboard get service kubernetes-dashboard
kubectl get pods --all-namespaces -o wide
kubectl patch svc kubernetes-dashboard \
-n kubernetes-dashboard \
-p '{"spec":{"type":"NodePort","ports":[{"port":443,"targetPort":8443,"nodePort":30443}]}}'
再给从节点开机
生成凭证
cat > dashboard-adminuser.yaml << EOF
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin
namespace: kube-system
EOF
kubectl apply -f dashboard-adminuser.yaml
查看密钥
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin | awk '{print $1}')
使用前一步输出的秘钥
https://192.168.2.107:30443/
参考链接
https://blog.csdn.net/cd_yourheart/article/details/108766912
https://blog.csdn.net/qq_45453266/article/details/109897843
https://blog.csdn.net/weixin_42182501/article/details/104347303
https://blog.csdn.net/happyzwh/article/details/86063807