使用kubeadm安装Kubernete以及常见问题解答

关于K8S:

Kubernetes是Google开源的容器集群管理系统。它构建于docker技术之上,为容器化的应用提供资源调度、部署运行、服务发现、扩 容缩容等整一套功能,本质上可看作是基于容器技术的mini-PaaS平台。

相信看过我博客的童鞋应该知道,我在14年的时候就发表了一篇名为Docker容器管理之Kubernetes当时国内Docker刚刚兴起,对于Docker的兴起我很有感触,仿佛一瞬间就火了,当时也是一个偶然的机会了解到K8S,所以当时就写文简单的介绍了下K8S以及如何采用源码部署。今时不同往日K8S在容器界已经是翘首,再读旧文有感而发,索性来研究下kubeadm安装K8S以及Dashboard功能预览。


环境描述:

采用CentOS7.4 minimual,docker 1.13,kubeadm 1.10.0,etcd 3.0, k8s 1.10.0

我们这里选用三个节点搭建一个实验环境。

10.0.100.202 k8smaster

10.0.100.203 k8snode1

10.0.100.204 k8snode2

准备环境:

1.配置好各节点hosts文件

2.关闭系统防火墙

3.关闭SElinux

4.关闭swap

5.配置系统内核参数使流过网桥的流量也进入iptables/netfilter框架中,在/etc/sysctl.conf中添加以下配置:

net.bridge.bridge-nf-call-iptables = 1

net.bridge.bridge-nf-call-ip6tables = 1

sysctl -p

使用kubeadm安装:

1.首先配置阿里K8S YUM源

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=0

EOF

yum -y install epel-release

yum clean all

yum makecache

2.安装kubeadm和相关工具包

yum -y install docker kubelet kubeadm kubectl kubernetes-cni

3.启动Docker与kubelet服务

systemctl enable docker && systemctl start docker

systemctl enable kubelet && systemctl start kubelet

提示:此时kubelet的服务运行状态是异常的,因为缺少主配置文件kubelet.conf。但可以暂不处理,因为在完成Master节点的初始化后才会生成这个配置文件。

4.下载K8S相关镜像

因为无法直接访问gcr.io下载镜像,所以需要配置一个国内的容器镜像加速器

配置一个阿里云的加速器:

登录 https://cr.console.aliyun.com/

在页面中找到并点击镜像加速按钮,即可看到属于自己的专属加速链接,选择Centos版本后即可看到配置方法。

提示:在阿里云上使用 Docker 并配置阿里云镜像加速器,可能会遇到 daemon.json 导致 docker daemon 无法启动的问题,可以通过以下方法解决。

你需要的是编辑 

vim /etc/sysconfig/docker 

然后 

OPTIONS='--selinux-enabled --log-driver=journald --registry-mirror=http://xxxx.mirror.aliyuncs.com' 
registry-mirror 输入你的镜像地址 

最后 service docker restart 重启 daemon 

然后 ps aux | grep docker 然后你就会发现带有镜像的启动参数了。

5.下载K8S相关镜像

OK,解决完加速器的问题之后,开始下载k8s相关镜像,下载后将镜像名改为k8s.gcr.io/开头的名字,以便kubeadm识别使用。

#!/bin/bash
images=(kube-proxy-amd64:v1.10.0 kube-scheduler-amd64:v1.10.0 kube-controller-manager-amd64:v1.10.0 kube-apiserver-amd64:v1.10.0
etcd-amd64:3.1.12 pause-amd64:3.1 kubernetes-dashboard-amd64:v1.8.3 k8s-dns-sidecar-amd64:1.14.8 k8s-dns-kube-dns-amd64:1.14.8
k8s-dns-dnsmasq-nanny-amd64:1.14.8)
for imageName in ${images[@]} ; do
  docker pull keveon/$imageName
  docker tag keveon/$imageName k8s.gcr.io/$imageName
  docker rmi keveon/$imageName
done

上面的shell脚本主要做了3件事,下载各种需要用到的容器镜像、重新打标记为符合k8s命令规范的版本名称、清除旧的容器镜像。

提示:镜像版本一定要和kubeadm安装的版本一致,否则会出现time out问题。

6.初始化安装K8S Master

执行上述shell脚本,等待下载完成后,执行kubeadm init

[[email protected] ~]# kubeadm init --kubernetes-version=v1.10.0 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.10.0
[init] Using Authorization modes: [Node RBAC]
[preflight] Running pre-flight checks.
	[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
	[WARNING FileExisting-crictl]: crictl not found in system path
Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
[preflight] Starting the kubelet service
[certificates] Generated ca certificate and key.
[certificates] Generated apiserver certificate and key.
[certificates] apiserver serving cert is signed for DNS names [k8smaster kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.0.100.202]
[certificates] Generated apiserver-kubelet-client certificate and key.
[certificates] Generated etcd/ca certificate and key.
[certificates] Generated etcd/server certificate and key.
[certificates] etcd/server serving cert is signed for DNS names [localhost] and IPs [127.0.0.1]
[certificates] Generated etcd/peer certificate and key.
[certificates] etcd/peer serving cert is signed for DNS names [k8smaster] and IPs [10.0.100.202]
[certificates] Generated etcd/healthcheck-client certificate and key.
[certificates] Generated apiserver-etcd-client certificate and key.
[certificates] Generated sa key and public key.
[certificates] Generated front-proxy-ca certificate and key.
[certificates] Generated front-proxy-client certificate and key.
[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
[controlplane] Wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml"
[controlplane] Wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager.yaml"
[controlplane] Wrote Static Pod manifest for component kube-scheduler to "/etc/kubernetes/manifests/kube-scheduler.yaml"
[etcd] Wrote Static Pod manifest for a local etcd instance to "/etc/kubernetes/manifests/etcd.yaml"
[init] Waiting for the kubelet to boot up the control plane as Static Pods from directory "/etc/kubernetes/manifests".
[init] This might take a minute or longer if the control plane images have to be pulled.
[apiclient] All control plane components are healthy after 21.001790 seconds
[uploadconfig] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[markmaster] Will mark node k8smaster as master by adding a label and a taint
[markmaster] Master k8smaster tainted and labelled with key/value: node-role.kubernetes.io/master=""
[bootstraptoken] Using token: thczis.64adx0imeuhu23xv
[bootstraptoken] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: kube-dns
[addons] Applied essential addon: kube-proxy

Your Kubernetes master 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

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 machines by running the following on each node
as root:

  kubeadm join 10.0.100.202:6443 --token thczis.64adx0imeuhu23xv --discovery-token-ca-cert-hash sha256:fa7b11bb569493fd44554aab0afe55a4c051cccc492dbdfafae6efeb6ffa80e6

提示:选项--kubernetes-version=v1.10.0是必须的,否则会因为访问google网站被墙而无法执行命令。这里使用v1.10.0版本,刚才前面也说到了下载的容器镜像版本必须与K8S版本一致否则会出现time out。

上面的命令大约需要1分钟的过程,期间可以观察下tail -f /var/log/message日志文件的输出,掌握该配置过程和进度。上面最后一段的输出信息保存一份,后续添加工作节点还要用到。

7.配置kubectl认证信息

# 对于非root用户
mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 对于root用户
export KUBECONFIG=/etc/kubernetes/admin.conf

也可以直接放到~/.bash_profile

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile

8.安装flannel网络

mkdir -p /etc/cni/net.d/

cat <<EOF> /etc/cni/net.d/10-flannel.conf
{
“name”: “cbr0”,
“type”: “flannel”,
“delegate”: {
“isDefaultGateway”: true
}
}

EOF

mkdir /usr/share/oci-umount/oci-umount.d -p

mkdir /run/flannel/

cat <<EOF> /run/flannel/subnet.env
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.1.0/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true

EOF

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml

9.让node1、node2加入集群

在node1和node2节点上分别执行kubeadm join命令,加入集群:

[[email protected] ~]# kubeadm join 10.0.100.202:6443 --token thczis.64adx0imeuhu23xv --discovery-token-ca-cert-hash sha256:fa7b11bb569493fd44554aab0afe55a4c051cccc492dbdfafae6efeb6ffa80e6
[preflight] Running pre-flight checks.
	[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
	[WARNING FileExisting-crictl]: crictl not found in system path
Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
[discovery] Trying to connect to API Server "10.0.100.202:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://10.0.100.202:6443"
[discovery] Requesting info from "https://10.0.100.202:6443" again to validate TLS against the pinned public key
[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "10.0.100.202:6443"
[discovery] Successfully established connection with API Server "10.0.100.202:6443"

This node has joined the cluster:
* Certificate signing request was sent to master and a response
  was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the master to see this node join the cluster.

提示:细心的童鞋应该会发现,这段命令其实就是前面K8S Matser安装成功后我让你们保存的那段命令。

默认情况下,Master节点不参与工作负载,但如果希望安装出一个All-In-One的k8s环境,则可以执行以下命令,让Master节点也成为一个Node节点:

kubectl taint nodes --all node-role.kubernetes.io/master-

10.验证K8S Master是否搭建成功

# 查看节点状态
kubectl get nodes

# 查看pods状态
kubectl get pods --all-namespaces

# 查看K8S集群状态
kubectl get cs

常见错误解析

安装时候最常见的就是time out,因为K8S镜像在国外,所以我们在前面就说到了提前把他下载下来,可以用一个国外机器采用habor搭建一个私有仓库把镜像都download下来。

[[email protected] ~]# kubeadm init
[init] Using Kubernetes version: v1.10.0
[init] Using Authorization modes: [Node RBAC]
[preflight] Running pre-flight checks.
	[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
	[WARNING FileExisting-crictl]: crictl not found in system path
Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
[preflight] Starting the kubelet service
[certificates] Generated ca certificate and key.
[certificates] Generated apiserver certificate and key.
[certificates] apiserver serving cert is signed for DNS names [k8smaster kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.0.100.202]
[certificates] Generated apiserver-kubelet-client certificate and key.
[certificates] Generated etcd/ca certificate and key.
[certificates] Generated etcd/server certificate and key.
[certificates] etcd/server serving cert is signed for DNS names [localhost] and IPs [127.0.0.1]
[certificates] Generated etcd/peer certificate and key.
[certificates] etcd/peer serving cert is signed for DNS names [k8smaster] and IPs [10.0.100.202]
[certificates] Generated etcd/healthcheck-client certificate and key.
[certificates] Generated apiserver-etcd-client certificate and key.
[certificates] Generated sa key and public key.
[certificates] Generated front-proxy-ca certificate and key.
[certificates] Generated front-proxy-client certificate and key.
[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
[controlplane] Wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml"
[controlplane] Wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager.yaml"
[controlplane] Wrote Static Pod manifest for component kube-scheduler to "/etc/kubernetes/manifests/kube-scheduler.yaml"
[etcd] Wrote Static Pod manifest for a local etcd instance to "/etc/kubernetes/manifests/etcd.yaml"
[init] Waiting for the kubelet to boot up the control plane as Static Pods from directory "/etc/kubernetes/manifests".
[init] This might take a minute or longer if the control plane images have to be pulled.

Unfortunately, an error has occurred:
	timed out waiting for the condition

This error is likely caused by:
	- The kubelet is not running
	- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
	- Either there is no internet connection, or imagePullPolicy is set to "Never",
	  so the kubelet cannot pull or find the following control plane images:
		- k8s.gcr.io/kube-apiserver-amd64:v1.10.0
		- k8s.gcr.io/kube-controller-manager-amd64:v1.10.0
		- k8s.gcr.io/kube-scheduler-amd64:v1.10.0
		- k8s.gcr.io/etcd-amd64:3.1.12 (only if no external etcd endpoints are configured)

If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
	- 'systemctl status kubelet'
	- 'journalctl -xeu kubelet'
couldn't initialize a Kubernetes cluster

那出现这个问题大部分原因是因为安装的K8S版本和依赖的K8S相关镜像版本不符导致的,关于这部分排错可以查看/var/log/message我们在文章开始安装的时候也提到了要多看日志。

还有些童鞋可能会说,那我安装失败了,怎么清理环境重新安装啊?下面教大家一条命令:

kubeadm reset

好了,至此就完成了K8S三节点集群的安装部署。

原文地址:http://blog.51cto.com/devingeng/2096495

时间: 2024-08-29 14:22:31

使用kubeadm安装Kubernete以及常见问题解答的相关文章

下载中心常见问题解答【Q&A帮助】

公告 1.下载中心不支持迅雷以及各个浏览器(如360.猎豹等)自带的加速模块下载,如果您正在使用,请关闭后再下载. 2. 本文内容较多,可利用"Ctrl+F"进行搜寻,如果没有找到您想知道的问题,请留言提出,我们会及时答复您.如果您的问题具有代表性,将会采纳入正文中.本文中已做出解答的问题,请不要重复提问. 下载常见问题 1.Q:为什么下载的资料提示解压错误?而且只有7KB? A:如果无法打开附件并提示解压报错,可以已下载的附件大小和资料原本标注的大小是否一致,如果资料下载完只有7KB

U盘启动盘常见问题解答

主要为大家详细说明U当家U盘装系统,U盘启动盘制作常见问题解答,希望对你有所帮助. 1.U盘启动盘制作工具是什么? U盘启动盘制作工具是将U盘变成为带有winpe系统的启动盘.方便用户维护和安装系统!(例如:系统不能启动时,我们使用已经制作好的U当家U盘启动盘就可以将C盘的重要文件复制出来,还可以保证数据的完整性.) 2.为什么我用U盘装系统工具制作好的U盘里面是空的? U盘装系统工具采用的是隐藏分区技术,看不到里面的内容是正常现象,你可以对比制作前的U盘容量,看容量是否少了 400兆,如果是就

ecshop模板开发制作教程及常见问题解答

ecshop模板开发制作教程 下列章节的适用于ECshop程序. 同时这里许多内容和一些 Smarty相关. 假如您已经熟悉这些内容可跳过不阅读.假如您是ECshop新手并且想diy一下自己的店铺, 那您应该认真详细地从头到尾读一遍这些章节.( ps: 大家不要紧张,我会尽量用人类的语言和大家交流,实在万不得已才会用机器语言展示给大家 ) 希望大家能够通过本教程,想要什么模板都能自己做出来.哈哈!一起加油吧! 第一章节: 读取这些内容,您将了解: 每个前台页面所对应的模板页面,模板文件的目录结构

组态王常见问题解答

一.在XP系统安装完组态王软件和驱动在打开运行时信息栏出现"您没有正确安装KVCOM.SYS驱动程序". [答:] (1)建议在安装前先把电脑上的杀毒软件先关闭,以免安装时对部分程序的影响. (2)若还出现此类问题,可以尝试把装好的组态软件在电脑上卸载掉,然后重新 启动安装,这个有时是系统造成的,建议多装几次.(我的就是这样后来装 好的)若实在还是不行,建议换个XP系统,应该是系统环境的问题. 二.打开组态王软件,在切换到view界面时弹出"历史库:打开工程映射区内存失败&q

SSL/TLS 高强度加密: 常见问题解答

关于这个模块 mod_ssl 简史 mod_ssl会受到Wassenaar Arrangement(瓦森纳协议)的影响吗? mod_ssl 简史 mod_ssl v1 最早在1998年4月由Ralf S. Engelschall基于Ben Laurie的Apache-SSL 1.17 源代码补丁(Apache 1.2.6 to Apache 1.3b6)开发.因为和Ben Laurie的开发周期冲突,所以就在合并了旧有代码之后,为Apache 1.3.0重新开发.从此之后,mod_ssl就按照自

使用LNMP常见问题解答

使用LNMP常见问题解答 一.LNMP的安装过程详解,注:绿色文字为注释,实际使用过程中没有. ##先要下载,并根据不同系统进行安装: wget -c http://soft.vpser.net/lnmp/lnmp1.0.tar.gz tar zxvf lnmp1.0.tar.gz cd lnmp1.0 ./centos.sh #如果您是其它系统,则命令改为:./ubuntu.sh 或 debian.sh 以上为下载及进入安装过程,以下为安装前简易设置,切记输入个其它域名及牢记mysql密码:

DNS 服务器三种模式搭建和常见问题解答

DNS 服务器三种模式搭建和常见问题解答                               作者浩浩哥来了   主DNS服务器搭建   DNS是域名服务器(DomainNameServer)的缩写,在TCP/IP网络中是一种很重要的网络服务,它用于将易于记忆的域名和不易记忆的IP地址进行转化   Vim /etc/hosts 修改主机名 127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomai

Delphi XE5 常见问题解答

Delphi XE5 常见问题解答 有关于新即时试用的问题吗?请看看 RAD Studio 即时试用常见问答. 常见问题 什么是 Delphi? Embarcadero? Delphi? XE5 是易于学习的应用开发,适合构建针对Android 和iOS 的真正原生应用.并将它们快速应用到应用商店和企业的团队.使用相同的源代码库构建应用,无需牺牲应用质量.连通性或性能.通过原生Android 和iOS 支持,延伸到世界上最大规模可访问的移动市场. 有哪些版本可以使用,并且版本之间有什么区别? D

Apache 的修改端口,虚拟主机搭建基于网站加密和常见问题解答

Apache 的修改端口,虚拟主机搭建基于网站加密和常见问题解答 作者  浩浩哥来了 首先需要确定apache这个服务存在 /etc/init.d/httpd restart 如果启动起来了说明你已经安装成功 如果启动不起来就需要自己手动安装,因为添加了本地源,所以可以直接安装 Yum install http* -y /etc/init.d/httpd restart 在一次检测下,服务安装成功与否 Cd /var/www/html 目录下是存放网页的路径 Vim index.html 因为当