- 优势
- 简化了应用部署。k8s将所有的工作节点暴露为一个单独的部署平台,开发人员不需要知道服务器的细节
- 提高了硬件的利用率,让你从基础架构解耦。k8s会根据你描述的需求,从现有的节点中挑选出最合适的节点来运行应用程序。k8s能让应用移动,找到最优的节点。
- 健康检查和自愈。k8s监控应用,一旦节点发生故障,会自动把应用调度到其他节点。让运维团队可修复坏掉的节点
- 自动扩容,k8s监控应用,自动调整应用的实例数量。如果k8s运行在云架构上,弹性扩容会变得更加容易。
- 安装
-
三台虚拟机
- cptest001 172.17.142.153
- cptest002
- cptest003
-
在cptest001上创建集群
- 1.前期准备 root环境
lsmod | grep br_netfilter modprobe br_netfilter cat <<EOF | tee /etc/modules-load.d/k8s.conf br_netfilter EOF cat <<EOF | tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system
- 2.安装kubelet kubeadm kubectl
vim /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-$basearch 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 exclude=kubelet kubeadm kubectl yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes systemctl enable kubelet
- 3.安装docker
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install -y docker-ce mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://bz7tf7cs.mirror.aliyuncs.com"] } EOF # 添加普通用户work到用户组docker,没有work,执行useradd work gpasswd -a work docker systemctl enable docker systemctl start docker
- 4.初始化主节点
# 由于coredns阿里云镜像里没有,从docker.hub拉取,再tag kubeadm config images list docker pull coredns/coredns:1.8.0 docker tag coredns/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0 # 国外镜像上不去,设置阿里云 # --image-repository index.docker.io/mirrorgooglecontainers kubeadm init --kubernetes-version=1.21.0 --apiserver-advertise-address=172.17.142.153 --image-repository index.docker.io/mirrorgooglecontainers --service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.0.0/16 # 普通用户work下执行 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config kubeadm join 172.17.142.153:6443 --token 7cm7no.nd80gvapv1j3nv72 \ --discovery-token-ca-cert-hash sha256:fc60b0cda22b23aa300b038e1c9024162b6d0f14d61ae46b120687752352b233
- 查看安装效果
> kubectl get nodes NAME STATUS ROLES AGE VERSION cptest001 Ready control-plane,master 98m v1.21.1 cptest002 Ready <none> 70m v1.21.1 cptest003 Ready <none> 23m v1.21.1
- 安装kubernetes-dashboard
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc7/aio/deploy/recommended.yaml kubectl apply -f recommended.yaml
-
node节点
- 重复上述步骤1、2、3
- 执行主节点的kubeadm join
kubeadm join 172.17.142.153:6443 --token 7cm7no.nd80gvapv1j3nv72 \ --discovery-token-ca-cert-hash sha256:fc60b0cda22b23aa300b038e1c9024162b6d0f14d61ae46b120687752352b233
-
问题
- swap分区不能打开。 swapoff -a
- docker版本不同导致初始化报错,升级就可以了
[kubelet-check] The HTTP call equal to ‘curl -sSL http://localhost:10248/healthz‘ fa
- docker 升级 1.13–>1.20
# 查找当前包 rpm -qa | grep docker # 卸载 yum remove docker-1.13.1-208.git7d71120.el7_9.x86_64 docker-common-1.13.1-208.git7d71120.el7_9.x86_64 docker-client-1.13.1-208.git7d71120.el7_9.x86_64 # 升级 curl -fsSL https://get.docker.com/ | sh # 重启 > systemctl restart docker # 开机自启动 > systemctl enable docker > docker -v Docker version 20.10.7, build f0df350
- 报错docker 升级后或者重装后,启动容器提示:Error response from daemon: Unknown runtime specified docker-runc。版本不兼容导致
# 更改/var/lib/docker/containers目录中的文件参数,把docker-runc替换为runc grep -rl 'docker-runc' /var/lib/docker/containers/ | xargs sed -i 's/docker-runc/runc/g' # 重启 systemctl restart docker
-
参考
- https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
- https://www.kubernetes.org.cn/7189.html
- https://stackoverflow.com/questions/65803204/k8sget-http-localhost10248-healthz-dial-tcp-110248-connect-connect
- https://www.cnblogs.com/yaun1498078591/p/11969632.html
- https://www.cnblogs.com/dongweizhen/p/13800883.html
- dashboard https://www.cnblogs.com/tylerzhou/p/11117956.html