Kubernetes集群快速部署

目前很多企业都将自己的服务上云了,有些使用自建云,但大部分使用公有云服务。服务上公有云除了费用高以外,其他都很方便高效。在云技术中,Kubernetes无疑是其中的佼佼者,其稳定的组件设计,丰富的插件选择和活跃的社区开发使得其成为资源池化的首选。熟悉并掌握Kubernetes使用方法已经成为企业对运维、DevOps工程师的基本要求。
文章对Kubernetes集群的部署做介绍。
环境介绍
为了可以轻松在8G内存的笔记本上运行集群,使用两台虚拟机,安装CentOS 7
Master节点
主机名:master.k8s.xyz,IP:192.168.52.131,内存:2G,CPU:2核,硬盘:30G
Node节点
主机名:node.k8s.xyz,IP:192.168.52.132,内存:2G,CPU:2核,硬盘:20G
两台服务器网络互通并可以访问Internet,修改/etc/hosts使得主机名可以在任一台主机上解析。为防止不必要的麻烦,建议禁用主机防火墙并关闭SELinux。
注意两点,服务器需禁用swap;master节点最低2核,不然使用kubeadm集群安装工具会报错。
安装
1,安装Docker(两台服务器都需要)
安装Docker源

yum install -y wget && wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

安装Docker并配置自启动

yum -y install docker-ce
systemctl enable docker && systemctl start docker

2,安装kubeadm,kubelet和kubectl(前两个组件两台服务器都需要,后一个可以仅在master节点安装,实际会依赖安装)
(1)添加阿里云YUM源

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

(2)安装kubeadm、kubelet和kubectl

yum install -y kubelet kubeadm kubectl

kubernetes更新迭代速度很快,如果需要安装特定版本可人为指定包的版本号。
设置kubelet开机自启动

systemctl enable kubelet

简单介绍下三个组件。kubeadm是官方推出的快速安装Kubernetes集群的命令行工具,它只关注启动引导,而非配置机器。安装各种扩展,例如 Kubernetes Dashboard、 监控方案、以及特定云平台的扩展,kubeadm都不负责。kubelet就像docker的经理(当然你可以根据需要自由选择容器运行时,不一定非是docker),kubelet从API Server接收到指令,会操作docker下载容器镜像,启动容器等工作,当然它还有别的功能,kubelet是集群中默认唯一通过systemd管理的组件,集群组件中它需要第一个安装。kubectl是管理集群的命令行工具。
3,安装Kubernetes控制平台组件(仅在master节点操作)
输入下面的命令,

kubeadm init  \
--apiserver-advertise-address=192.168.52.131  \
--image-repository registry.aliyuncs.com/google_containers  \
--kubernetes-version v1.23.4  \
--service-cidr=10.1.0.0/16  \
--pod-network-cidr=10.244.0.0/16

命令选项和参数都是自解释的,如有不熟悉的,可以输入“kubeadm help”查看每个子命令的详细解释。注意一点,指定阿里云镜像仓库用来拉取镜像,因为默认集群组件镜像从“k8s.gcr.io”拉取,不知什么原因,拉不下来。
kubeadm命令的工作过程,从上面命令的执行输出可以看出来,为了让大家有个直观认识,这里全部贴出来,

[root@master ]# kubeadm init --apiserver-advertise-address=192.168.52.131 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.4 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.23.4
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master.k8s.xyz] and IPs [10.1.0.1 192.168.52.131]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master.k8s.xyz] and IPs [192.168.52.131 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master.k8s.xyz] and IPs [192.168.52.131 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 30.522586 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kubelet-config". Kubeadm upgrade will handle this transition transparently.
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master.k8s.xyz as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node master.k8s.xyz as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 3mhq29.jrwbs2kgiytddqsb
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane 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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.52.131:6443 --token 3mhq29.jrwbs24giytdabec \
    --discovery-token-ca-cert-hash sha256:80a5e825a4c5f0fd123277505f166f7e16ddd78f8b256d627b6d834342e746e9 

kubectl命令查看控制平台组件已经就绪了,

[root@master ]# kubectl get pods -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-6d8c4cb4d-l6nbp                      1/1     Running   0          14m
coredns-6d8c4cb4d-pnvz8                      1/1     Running   0          14m
etcd-master.k8s.xyz                      1/1     Running   3          14m
kube-apiserver-master.k8s.xyz            1/1     Running   3          14m
kube-controller-manager-master.k8s.xyz   1/1     Running   3          14m
kube-proxy-dwvsx                             1/1     Running   0          14m
kube-scheduler-master.k8s.xyz            1/1     Running   3          14m

4,部署Pod网络插件
兼容CNI的网络插件市面上有很多,根据实际需要选择,这里安装易维护的flannel pod网络插件。输入下面命令安装,直截了当(详情可登录github搜索flannel-io了解)

kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

5,将node节点加入集群

[root@node ~]# kubeadm join 192.168.52.131:6443 --token 3mhq29.jrwbs24giytdabec \
> --discovery-token-ca-cert-hash sha256:80a5e825a4c5f0fd123277505f166f7e16ddd78f8b256d627b6d834342e746e9 
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

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

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

查看集群节点,

[root@master ]# kb get nodes
NAME                 STATUS   ROLES                  AGE     VERSION
master.k8s.xyz   Ready    control-plane,master   22m     v1.23.4
node.k8s.xyz     Ready    <none>                 3m32s   v1.23.4

至此,一个可供测试使用的kubernetes集群就部署完了,但这个离生产环境部署还差很远。生产环境要部署监控、日志收集、APM、Dashboard等,最重要是集群组件高可用保障。
总结
文章介绍依赖命令行工具kubeadm安装kubernetes集群的步骤和方法,从上面的过程看,并不复杂,在虚拟机就绪情况下,半个小时就可以部署完成,但维护一个kubernetes生产集群远比这个复杂,需要考虑的东西很多。
希望这篇文章能够帮助到读者!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容