实操演示使⽤ kubeadm 搭建集群环境

简介: 本篇文章我将详细演示使用kubeadm搭建Kubernets集群的过程,希望对你有所帮助。使用 kubeadm,你能创建一个符合最佳实践的最小化 Kubernetes 集群。

0️⃣写在前面

本篇文章我将详细演示使用kubeadm搭建Kubernets集群的过程,希望对你有所帮助。使用 kubeadm,你能创建一个符合最佳实践的最小化 Kubernetes 集群。

1️⃣集群安装初始化

到这⾥我们的准备⼯作就完成了,接下来我们就可以在 master 节点上⽤ kubeadm 命令来初始化我们的集群了:

$ kubeadm init --kubernetes-version=v1.10.0 --pod-network-cidr=10.244.0.0/16 --apiserver-a dvertise-address=10.151.30.57

命令⾮常简单,就是 kubeadm init ,后⾯的参数是需要安装的集群版本,因为我们这⾥选 择 flannel 作为 Pod 的⽹络插件,所以需要指定 –pod-network-cidr=10.244.0.0/16 ,然后是 apiserver 的通信地址,这⾥就是我们 master 节点的 IP 地址。执⾏上⾯的命令,如果出现 running with swap on is not supported. Please disable swap 之类的错误,则我们还需要增加⼀个参数 – ignore-preflight-errors=Swap 来忽略 swap 的错误提示信息:

$ kubeadm init \ 
--kubernetes-version=v1.10.0 \ 
--pod-network-cidr=10.244.0.0/16 \ 
--apiserver-advertise-address=10.151.30.57 \ 
--ignore-preflight-errors=Swap 
[init] Using Kubernetes version: v1.10.0 
[init] Using Authorization modes: [Node RBAC] 
[preflight] Running pre-flight checks. 
[WARNING FileExisting-crictl]: crictl not found in system pathSuggestion: go get githu 
b.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 [ydzs-master1 kubernetes kub 
ernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.9 
6.0.1 10.151.30.57] 
[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 [ydzs-master1] and IPs [10.1 
51.30.57] 
[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/ku 
bernetes/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/e 
tcd.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 22.007661 seconds 
[uploadconfig] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-s 
ystem" Namespace 
[markmaster] Will mark node ydzs-master1 as master by adding a label and a taint 
[markmaster] Master ydzs-master1 tainted and labelled with key/value: node-role.kubernetes 
.io/master="" 
[bootstraptoken] Using token: 8xomlq.0cdf2pbvjs2gjho3 
[bootstraptoken] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in orde 
r for nodes to get long term certificate credentials 
[bootstraptoken] Configured RBAC rules to allow the csrapprover controller automatically a 
pprove CSRs from a Node Bootstrap Token 
[bootstraptoken] Configured RBAC rules to allow certificate rotation for all node client c 
ertificates 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.151.30.57:6443 --token 8xomlq.0cdf2pbvjs2gjho3 --discovery-token-ca-cert 
-hash sha256:92802317cb393682c1d1356c15e8b4ec8af2b8e5143ffd04d8be4eafb5fae368 

要注意将上⾯的加⼊集群的命令保存下⾯,如果忘记保存上⾯的 token 和 sha256 值的话也不⽤担 ⼼,我们可以使⽤下⾯的命令来查找:

$ kubeadm token list 
kubeadm token list 
TOKEN TTL EXPIRES USAGES D 
ESCRIPTION EXTRA GROUPS 
i5gbaw.os1iow5tdo17rwdu 23h 2018-05-18T01:32:55+08:00 authentication,signing T 
he default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:def 
ault-node-token 

要查看 CA 证书的 sha256 的值的话,我们可以使⽤ openssl 来读取证书获取 sha256 的值:

$ openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2> /dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' 
e9ca4d9550e698105f1d8fae7ecfd297dd9331ca7d50b5493fa0491b2b4df40c

另外还需要注意的是当前版本的 kubeadm ⽀持的docker版本最⼤是 17.03,所以要注意下。 上⾯的信 息记录了 kubeadm 初始化整个集群的过程,⽣成相关的各种证书、kubeconfig ⽂件、bootstraptoken 等等,后边是使⽤ kubeadm join 往集群中添加节点时⽤到的命令,下⾯的命令是配置如何使⽤kubectl 访问集群的⽅式:

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 10.151.30.57:6443 --token 8xomlq.0cdf2pbvjs2gjho3 --discovery-token-ca-cert-h 
ash sha256:92802317cb393682c1d1356c15e8b4ec8af2b8e5143ffd04d8be4eafb5fae368

我们根据上⾯的提示配置好 kubectl 后,就可以使⽤ kubectl 来查看集群的信息了:

$ kubectl get cs 
NAME STATUS MESSAGE ERROR 
scheduler Healthy ok 
controller-manager Healthy ok 
etcd-0 Healthy {"health": "true"} 
$ kubectl get csr 
NAME AGE REQUESTOR 
CONDITION 
node-csr-8qygb8Hjxj-byhbRHawropk81LHNPqZCTePeWoZs3-g 1h system:bootstrap:8xomlq 
Approved,Issued 
$ kubectl get nodes 
NAME STATUS ROLES AGE VERSION 
ydzs-master1 Ready master 3h v1.10.0 

如果你的集群安装过程中遇到了其他问题,我们可以使⽤下⾯的命令来进⾏重置:

$ kubeadm reset 
$ ifconfig cni0 down && ip link delete cni0 
$ ifconfig flannel.1 down && ip link delete flannel.1 
$ rm -rf /var/lib/cni/ 

2️⃣安装 Pod Network

接下来我们来安装 flannel ⽹络插件,很简单,和安装普通的 POD 没什么两样:

$ wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel. 
yml 
$ kubectl apply -f kube-flannel.yml 
clusterrole.rbac.authorization.k8s.io "flannel" created 
clusterrolebinding.rbac.authorization.k8s.io "flannel" created 
serviceaccount "flannel" created 
configmap "kube-flannel-cfg" created 
daemonset.extensions "kube-flannel-ds" created 

另外需要注意的是如果你的节点有多个⽹卡的话,需要在 kube-flannel.yml 中使⽤ --iface 参数指定 集群主机内⽹⽹卡的名称,否则可能会出现 dns ⽆法解析。flanneld 启动参数加上 --iface=

args: 
- --ip-masq 
- --kube-subnet-mgr 
- --iface=eth0 

安装完成后使⽤ kubectl get pods 命令可以查看到我们集群中的组件运⾏状态,如果都是Running 状 态的话,那么恭喜你,你的 master 节点安装成功了。

$ kubectl get pods --all-namespaces 
NAMESPACE NAME READY STATUS RESTARTS AGE 
kube-system etcd-ydzs-master1 1/1 Running 0 10m 
kube-system kube-apiserver-ydzs-master1 1/1 Running 0 10m 
kube-system kube-controller-manager-ydzs-master1 1/1 Running 0 10m 
kube-system kube-dns-86f4d74b45-f5595 3/3 Running 0 10m 
kube-system kube-flannel-ds-qxjs2 1/1 Running 0 1m 
kube-system kube-proxy-vf5fg 1/1 Running 0 10m 
kube-system kube-scheduler-ydzs-master1 1/1 Running 0 10m

3️⃣添加节点

同样的上⾯的环境配置、docker 安装、kubeadmin、kubelet、kubectl 这些都在Node(10.151.30.62)节 点安装配置好过后,我们就可以直接在 Node 节点上执⾏ kubeadm join 命令了(上⾯初始化的时候 有),同样加上参数 --ignore-preflight-errors=Swap :

$ kubeadm join 10.151.30.57:6443 --token 8xomlq.0cdf2pbvjs2gjho3 --discovery-token-ca-cert 
-hash sha256:92802317cb393682c1d1356c15e8b4ec8af2b8e5143ffd04d8be4eafb5fae368 --ignore-pre 
flight-errors=Swap 
[preflight] Running pre-flight checks. 
[WARNING Swap]: running with swap on is not supported. Please disable swap 
[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.151.30.57:6443" 
[discovery] Created cluster-info discovery client, requesting info from "https://10.151.30 
.57:6443" 
[discovery] Requesting info from "https://10.151.30.57:6443" again to validate TLS against 
the pinned public key 
[discovery] Cluster info signature and contents are valid and TLS certificate validates ag 
ainst pinned roots, will use API Server "10.151.30.57:6443" 
[discovery] Successfully established connection with API Server "10.151.30.57: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. 

我们可以看到该节点已经加⼊到集群中去了,然后我们把 master 节点的 ~/.kube/config ⽂件拷⻉到 当前节点对应的位置即可使⽤ kubectl 命令⾏⼯具了。

$ kubectl get nodes 
NAME STATUS ROLES AGE VERSION 
evjfaxic Ready <none> 1h v1.10.0 
ydzs-master1 Ready master 3h v1.10.0

到这⾥就算我们的集群部署成功了,接下来就可以根据我们的需要安装⼀些附加的插件,⽐如 Dashboard、Heapster、Ingress-Controller 等等,这些插件的安装⽅法就和我们之前⼿动安装集群的 ⽅式⽅法⼀样了,这⾥就不在重复了。
在这里插入图片描述


在这里插入图片描述

相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
11月前
|
Kubernetes Cloud Native Docker
云原生时代的容器化实践:Docker和Kubernetes入门
【10月更文挑战第37天】在数字化转型的浪潮中,云原生技术成为企业提升敏捷性和效率的关键。本篇文章将引导读者了解如何利用Docker进行容器化打包及部署,以及Kubernetes集群管理的基础操作,帮助初学者快速入门云原生的世界。通过实际案例分析,我们将深入探讨这些技术在现代IT架构中的应用与影响。
463 2
|
7月前
|
存储 Kubernetes 监控
K8s集群实战:使用kubeadm和kuboard部署Kubernetes集群
总之,使用kubeadm和kuboard部署K8s集群就像回归童年一样,简单又有趣。不要忘记,技术是为人服务的,用K8s集群操控云端资源,我们不过是想在复杂的世界找寻简单。尽管部署过程可能遇到困难,但朝着简化复杂的目标,我们就能找到意义和乐趣。希望你也能利用这些工具,找到你的乐趣,满足你的需求。
677 33
|
8月前
|
Cloud Native Serverless 数据中心
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
ACK One注册集群已正式支持ACS(容器计算服务)算力,为企业的容器化工作负载提供更多选择和更强大的计算能力。
|
8月前
|
Cloud Native Serverless 数据中心
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
255 10
|
10月前
|
存储 Kubernetes 开发者
容器化时代的领航者:Docker 和 Kubernetes 云原生时代的黄金搭档
Docker 是一种开源的应用容器引擎,允许开发者将应用程序及其依赖打包成可移植的镜像,并在任何支持 Docker 的平台上运行。其核心概念包括镜像、容器和仓库。镜像是只读的文件系统,容器是镜像的运行实例,仓库用于存储和分发镜像。Kubernetes(k8s)则是容器集群管理系统,提供自动化部署、扩展和维护等功能,支持服务发现、负载均衡、自动伸缩等特性。两者结合使用,可以实现高效的容器化应用管理和运维。Docker 主要用于单主机上的容器管理,而 Kubernetes 则专注于跨多主机的容器编排与调度。尽管 k8s 逐渐减少了对 Docker 作为容器运行时的支持,但 Doc
403 5
容器化时代的领航者:Docker 和 Kubernetes 云原生时代的黄金搭档
|
10月前
|
Kubernetes Ubuntu 网络安全
ubuntu使用kubeadm搭建k8s集群
通过以上步骤,您可以在 Ubuntu 系统上使用 kubeadm 成功搭建一个 Kubernetes 集群。本文详细介绍了从环境准备、安装 Kubernetes 组件、初始化集群到管理和使用集群的完整过程,希望对您有所帮助。在实际应用中,您可以根据具体需求调整配置,进一步优化集群性能和安全性。
773 13
|
11月前
|
Kubernetes Cloud Native 开发者
云原生入门:Kubernetes的简易指南
【10月更文挑战第41天】本文将带你进入云原生的世界,特别是Kubernetes——一个强大的容器编排平台。我们将一起探索它的基本概念和操作,让你能够轻松管理和部署应用。无论你是新手还是有经验的开发者,这篇文章都能让你对Kubernetes有更深入的理解。
|
11月前
|
Kubernetes Cloud Native 微服务
云原生入门与实践:Kubernetes的简易部署
云原生技术正改变着现代应用的开发和部署方式。本文将引导你了解云原生的基础概念,并重点介绍如何使用Kubernetes进行容器编排。我们将通过一个简易的示例来展示如何快速启动一个Kubernetes集群,并在其上运行一个简单的应用。无论你是云原生新手还是希望扩展现有知识,本文都将为你提供实用的信息和启发性的见解。
|
11月前
|
运维 Kubernetes Cloud Native
云原生技术入门:Kubernetes和Docker的协同工作
【10月更文挑战第43天】在云计算时代,云原生技术成为推动现代软件部署和运行的关键力量。本篇文章将带你了解云原生的基本概念,重点探讨Kubernetes和Docker如何协同工作以支持容器化应用的生命周期管理。通过实际代码示例,我们将展示如何在Kubernetes集群中部署和管理Docker容器,从而为初学者提供一条清晰的学习路径。
|
11月前
|
Kubernetes 负载均衡 Cloud Native
探索Kubernetes:云原生应用的基石
探索Kubernetes:云原生应用的基石

热门文章

最新文章

推荐镜像

更多