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

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 本篇文章我将详细演示使用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 等等,这些插件的安装⽅法就和我们之前⼿动安装集群的 ⽅式⽅法⼀样了,这⾥就不在重复了。
在这里插入图片描述


在这里插入图片描述

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
1天前
|
Kubernetes Cloud Native 持续交付
云原生之旅:Docker容器化与Kubernetes集群管理
【9月更文挑战第33天】在数字化转型的浪潮中,云原生技术如同一艘航船,带领企业乘风破浪。本篇文章将作为你的航海指南,从Docker容器化的基础讲起,直至Kubernetes集群的高级管理,我们将一起探索云原生的奥秘。你将学习到如何封装应用、实现环境隔离,以及如何在Kubernetes集群中部署、监控和扩展你的服务。让我们启航,驶向灵活、可伸缩的云原生未来。
|
4天前
|
Kubernetes Cloud Native Docker
云原生时代的容器化实践:Docker与Kubernetes入门
【9月更文挑战第30天】在云计算的浪潮中,云原生技术正以前所未有的速度重塑着软件开发和运维领域。本文将通过深入浅出的方式,带你了解云原生的核心组件——Docker容器和Kubernetes集群,并探索它们如何助力现代应用的构建、部署和管理。从Docker的基本命令到Kubernetes的资源调度,我们将一起开启云原生技术的奇妙之旅。
|
1天前
|
Kubernetes Cloud Native Ubuntu
云原生之旅:Kubernetes集群搭建与应用部署
【8月更文挑战第65天】本文将带你进入云原生的世界,通过一步步指导如何在本地环境中搭建Kubernetes集群,并部署一个简单的应用。我们将使用Minikube和Docker作为工具,探索云原生技术的魅力所在。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和实践技巧。
|
1月前
|
Kubernetes 网络虚拟化 Docker
K8S镜像下载报错解决方案(使用阿里云镜像去下载kubeadm需要的镜像文件)
文章提供了一个解决方案,用于在无法直接访问Google镜像仓库的情况下,通过使用阿里云镜像来下载kubeadm所需的Kubernetes镜像。
81 3
K8S镜像下载报错解决方案(使用阿里云镜像去下载kubeadm需要的镜像文件)
|
18天前
|
Kubernetes Cloud Native 开发者
云原生入门:从Docker到Kubernetes的旅程
【9月更文挑战第16天】 本文将带你进入云原生的世界,从理解Docker容器的基础开始,逐步深入到Kubernetes集群管理。我们将通过简单的代码示例和实际操作,探索这两个关键技术如何协同工作,以实现更高效、灵活的应用程序部署和管理。无论你是云原生新手还是希望深化理解,这篇文章都将为你提供清晰的指导和实用的知识。
51 11
|
10天前
|
Kubernetes 负载均衡 Cloud Native
探索云原生技术:Kubernetes的魔法
【9月更文挑战第24天】 在数字化浪潮中,云原生技术如同现代航海的罗盘,指引着企业航向灵活、高效的未来。本文将深入剖析云原生世界的璀璨明星——Kubernetes,揭秘其如何在容器化的基础上,实现复杂应用的自动化部署、扩展和管理。从概念到实践,我们将一同领略Kubernetes如何简化运维、提高资源利用率,并推动微服务架构的发展。通过实际的代码示例,我们将手把手教你如何在云上构建和运行第一个Kubernetes集群,让理论与实践相结合,开启云原生之旅。
|
17天前
|
Kubernetes Cloud Native Linux
云原生入门:Kubernetes的简易部署与应用
【8月更文挑战第49天】在云原生的世界里,Kubernetes(K8s)是一颗璀璨的星。本文将带你走进K8s的世界,从安装到简单应用,轻松驾驭这个强大的容器编排工具。让我们一起探索云原生的奥秘,解锁新技能!
|
17天前
|
Kubernetes 负载均衡 监控
深入云原生技术:Kubernetes集群部署与管理
【9月更文挑战第17天】在数字化转型的浪潮中,云原生技术以其灵活性和可扩展性成为企业新宠。本文将引导读者探索云原生的核心组件——Kubernetes,通过实际案例分析其部署与管理流程,旨在帮助技术从业者和企业决策者理解如何利用Kubernetes提升应用的可用性和性能。从基础概念到操作实践,我们将一同见证云原生技术的变革力量。
|
28天前
|
Kubernetes 监控 Cloud Native
云原生入门:Kubernetes 集群部署与管理
【8月更文挑战第38天】在数字化浪潮中,云原生技术如同翱翔的雄鹰,引领着企业飞向灵活高效的未来。本文将带你一探究竟,从Kubernetes的基础概念到实际操作,深入浅出地介绍如何在云端构建和管理你的容器化应用。我们将一步步搭建起一个小型的Kubernetes集群,并通过代码示例和图解,让你轻松掌握云原生世界的钥匙。让我们一起开启这趟技术之旅,探索云原生的秘密花园,找到那把打开创新之门的金钥匙。
|
5天前
|
Kubernetes Cloud Native Docker
云原生之旅:深入理解容器化与Kubernetes
【9月更文挑战第29天】在云计算的海洋中,云原生技术如同一艘航船带领着企业乘风破浪。本文将作为你的航海图,带你探索云原生的核心——容器化和Kubernetes。我们将从容器的基本概念出发,逐步深入到如何在Kubernetes集群中部署应用,最后探讨这些技术如何助力现代软件开发。文章旨在为读者提供清晰的云原生入门知识,并展示实际操作的步骤,让你能够自信地启航。
下一篇
无影云桌面