一、环境设置
1.1、系统设置
OS :Centos 8
ip和域名设定
192.168.25.75 k8s-master.zhouyihua.com
192.168.25.76 k8s-node1.zhouyihua.com
192.168.25.77 k8s-node2.zhouyihua.com
1.2、卸载podman,系统默认安装了podman容器和docker有冲突
#yum remove podman -y
#yum erase podman buildah
1.3、关闭selinux :
#setenforce 0 //临时,即时生效
或#sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config //永久,重启生效
1.4、关闭swap分区 :
#swapoff -a //临时,即时生效
或 #sed -i 's/.*swap.*/#&/' /etc/fstab //永久,重启生效
1.5、关闭防火墙 :
#systemctl stop firewalld.service
#systemctl disable firewalld.service
二、安装docker
2.1、设置系统基本安装源
#sudo curl -o /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-8.repo
2.2、安装相关组件
#yum install -y yum-utils device-mapper-persistent-data lvm2 net-tools
#yum-config-manager --add-repohttps://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
wget https://download.docker.com/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
yum install containerd.io-1.2.6-3.3.el7.x86_64.rpm
设置阿里云加速
#sudo mkdir -p /etc/docker
#sudo vim /etc/docker/daemon.json
{
"registry-mirrors" : ["https://mj9kvemk.mirror.aliyuncs.com"]
}
2.3、安装docker
#yum -y install docker-ce
2.4、设置自动启动
#systemctl daemon-reload && systemctl start docker 启动docker
#systemctl enable docker
三、安装k8s组件,包括kubectl\kubelet\kubeadm
3.1、添加k8s安装源
将如下内容保存到:/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
3.2、安装各组件
#yum install -y kubectl-1.18.2 //安装指定版本
#yum install -y kubelet-1.18.2 //安装指定版本
#yum install -y kubeadm-1.18.2 //安装指定版本
#systemctl enable kubelet 设置开机自启
#systemctl start kubelet
#kubeadm version //查看版本
#kubectl version --client
#kubelet --version
3.3、初始化集群
#kubeadm init --apiserver-advertise-address=0.0.0.0 --apiserver-cert-extra-sans=127.0.0.1 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.5 --pod-network-cidr=10.18.0.0/16 //初始化集群,集群启动前需要启动docker
集群初始化后的信息最后是集群token,类似如下,建议复制保留:
kubeadm join 192.168.25.130:6443 --token 8rcheh.ncugoj65j7nqrevu \
--discovery-token-ca-cert-hash sha256:f0ec989d1fc472f44ea5dc2b38f2e303be753061247d67bc78c35aa7fcee55a8
#mkdir -p $HOME/.kube
#sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
#sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看k8s
#kubectl get node
#kubectl get pod --all-namespaces //查看发现coredns处于pending
need-to-insert-img
安装calico
#kubectl apply -fhttps://docs.projectcalico.org/manifests/calico.yaml
need-to-insert-img
#kubectl get pod --all-namespces 查看发现calico和coredns都处于running
need-to-insert-img
四、部署dashboard
4.1、部署dashboard
#wget https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended.yaml
添加以下两行
#vim recommended.yaml
need-to-insert-img
#kubectl create -f recommended.yaml //这里注意DNS一定要配置好,不然镜像拉不下来
#kubectl get svc -n kubernetes-dashboard
need-to-insert-img
#kubectl get pod --all-namespces //查看所有容器状态
need-to-insert-img
4.2、登录console
使用token登录
创建token
#kubectl create sa dashboard-admin -n kube-system
授权token访问权限
#kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
获取token
#ADMIN_SECRET=$(kubectl get secrets -n kube-system | grep dashboard-admin | awk '{print $1}')
#DASHBOARD_LOGIN_TOKEN=$(kubectl describe secret -n kube-system ${ADMIN_SECRET} | grep -E '^token' | awk '{print $2}')
#echo ${DASHBOARD_LOGIN_TOKEN} //通过该命令获取到登录的token
访问地址:https://192.168.25.75:30000/#/login//登录选择token,该token是上一步获取的token
need-to-insert-img
#kubectl apply -f kubernetes-dashboard.yaml //本命令保留,做参考
参考:https://blog.csdn.net/witton/article/details/107085155
报错1:
/proc/sys/net/bridge/bridge-nf-call-iptables does not exist
#modprobe br_netfilter
#echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
#echo 1 > /proc/sys/net/ipv4/ip_forward
报错2:
#kubectl get node /#kubectl get pod --all-namespaces
The connection to the server 192.168.25.75:6443 was refused - did you specify the right host or port
#systemctl start kubelet
报错3:dashboard已经存在
Error from server (AlreadyExists): error when creating "recommended.yaml": namespaces "kubernetes-da
#kubectl delete -f kubernetes-dashboard.yaml //根据模板先删除,删除后重新创建
查看pod状态
kubectl describe pods kubernetes-dashboard-577bd97bc-h42rr -n kubernetes-dashboard