一 介绍

        Kubeadm 是一个 K8s 部署工具,提供 kubeadm init 和 kubeadm join,用于快速部 署 Kubernetes 集群。
        是官方社区推出的一个用于快速部署 kubernetes 集群的工具,这个工具能通过两条指令完成一个 kubernetes 集群的部署:
        第一、创建一个 Master 节点 kubeadm init
        第二, 将 Node 节点加入到当前集群中 $ kubeadm join <Master 节点的 IP 和端口 >
        官方地址:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/

二 安装要求

        部署 Kubernetes 集群机器需要满足以下几个条件:
- 一台或多台机器,操作系统 CentOS7.x-86_x64
- 硬件配置:2GB 或更多 RAM,2 个 CPU 或更多 CPU,硬盘 30GB 或更多
- 集群中所有机器之间网络互通
- 可以访问外网,需要拉取镜像
- 禁止 swap 分区

三 安装目标

  1. 在所有节点上安装 Docker 和 kubeadm
  2. 部署 Kubernetes Master
  3. 部署容器网络插件
  4. 部署 Kubernetes Node,将节点加入 Kubernetes 集群中
  5. 部署 Dashboard Web 页面,可视化查看 Kubernetes 资源

 四 安装

1 系统初始化(所有机器)

# 关闭防火墙:
[root@192 ~]# systemctl stop firewalld
[root@192 ~]# systemctl disable firewalld

# 关闭 selinux:
[root@192 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
[root@192 ~]# setenforce 0 # 临时

# 关闭swap(你都集群了,还怕个毛线内存不足)
[root@192 ~]# swapoff -a # 临时
[root@192 ~]# vim /etc/fstab
# 把swap的那一行注释掉即可永久关闭
#
# /etc/fstab
# Created by anaconda on Mon Dec 27 15:36:24 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=8c69334a-17bb-4043-85c9-b8f3788dfc7c /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0


# 设置主机名,分别为master,slave1,slave2
[root@192 ~]# hostnamectl set-hostname master


# 在master添加hosts
[root@192 ~]# cat >> /etc/hosts << EOF
> 192.168.247.136 k8s-master
> 192.168.247.137 k8s-node1
> 192.168.247.138 k8s-node2
> EOF

# 将桥接的 IPv4 流量传递到 iptables 的链
[root@192 ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@192 ~]# sysctl --system

2 时间同步

# 安装chrony,设置开机启动
[root@192 ~]# yum install chrony
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Package chrony-3.4-1.el7.x86_64 already installed and latest version
Nothing to do
[root@192 ~]# systemctl start chronyd
[root@192 ~]# systemctl enable chronyd

# server配置,vim /etc/chrony.conf 添加以下两项
# Allow NTP client access from local network.
allow all # 允许所有主机从server端同步时间

# Serve time even if not synchronized to a time source.
local stratum 10 # 即使server端无法从互联网同步时间,也同步本机时间至client

# client配置,vim /etc/chrony.conf 注释原server配置,改为prod1机器IP地址
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.247.136 iburst


# 都重启服务
[root@192 ~]# systemctl restart chronyd
# 服务端查看server端在线情况和时间同步情况
[root@192 ~]# chronyc activity
200 OK
0 sources online
0 sources offline
1 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
[root@192 ~]# chronyc tracking
Reference ID    : B65C0C0B (time5.aliyun.com)
Stratum         : 3
Ref time (UTC)  : Tue Dec 28 08:56:41 2021
System time     : 0.000352393 seconds fast of NTP time
Last offset     : +0.000411336 seconds
RMS offset      : 0.000915787 seconds
Frequency       : 1.113 ppm slow
Residual freq   : -0.518 ppm
Skew            : 55.244 ppm
Root delay      : 0.024603928 seconds
Root dispersion : 0.003826973 seconds
Update interval : 64.2 seconds
Leap status     : Normal

# 客户端查看server端(也就是本地服务器)在线情况和时间同步情况
[root@192 ~]# chronyc activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
[root@192 ~]# chronyc tracking
Reference ID    : C0A8F788 (192.168.247.136)
Stratum         : 4
Ref time (UTC)  : Tue Dec 28 08:55:38 2021
System time     : 0.000000145 seconds slow of NTP time
Last offset     : +0.000075000 seconds
RMS offset      : 0.000075000 seconds
Frequency       : 31.362 ppm slow
Residual freq   : +0.119 ppm
Skew            : 198.082 ppm
Root delay      : 0.024361094 seconds
Root dispersion : 0.004769796 seconds
Update interval : 2.0 seconds
Leap status     : Normal

3 所有节点安装docker


# 确定是centos7及以上版本
[root@192 docker]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
# yum安装gcc相关注意centos7系统主机要能上外网
[root@192 docker]# yum -y install gcc
[root@192 docker]# yum -y install gcc-c++
# 卸载旧版本
[root@192 docker]# yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# 安装需要的软件包(初次安装需要 这个命令)
[root@192 docker]# yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
# 设置stable镜像仓库 ,这个大坑!
# 注意!!!!其实官网没错,但是由于国内有防火墙,官网链接会 很慢 ,会导致Err14,Err12等错误 
# 建议使用国内的镜像仓库,阿里云大法好 
[root@192 docker]# yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 更新yum软件包索引,这个英文手册没有,不过还是加一下
[root@192 docker]# yum makecache fast
# 安装docker ce
[root@192 docker]# yum -y install docker-ce
# 启动docker
[root@192 docker]# systemctl start docker
# 设置开机启动
[root@192 ~]# systemctl enable docker

# 测试
[root@192 docker]# docker version 

4 配置镜像加速

如图获取自己的阿里云加速链接

[root@192 docker]# mkdir -p /etc/docker
[root@192 docker]# vim /etc/docker/daemon.json
# 注意与centos6不同,字段内容如下:
# 网易云
 
{"registry-mirrors":["http://hub-mirror.c.163.com"]}
# 阿里云
 
{
"registry-mirrors":["https://【自己的编码】.mirror.aliyuncs.com"]
}
 
[root@192 docker]# systemctl daemon-reload
 
[root@192 docker]# systemctl restart docker

5 安装kubeadm、kubelet和kubectl

下面的有可能有问题,我使用的是rpm包导出安装

# 注意,这个很多文档可能有问题,有可能不能进行正常安装,错误的地方是baseurl中应该是mirrors而不是mirror
# 由于kubernetes的镜像在国外,速度⽐较慢,这⾥切换成国内的镜像源
# 编辑/etc/yum.repos.d/kubernetes.repo,添加下⾯的配置
[root@192 ~]# vim /etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgchech=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
 http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

也可以本地导出安装

我使用的rpm包是1.17.0版本的

[root@master ~]# yum install -y conntrack socat
# 按照顺序进行安装,否则会有依赖问题
[root@master ~]# rpm -ivh /tmp/cri-tools-1.13.0-0.x86_64.rpm
warning: /tmp/cri-tools-1.13.0-0.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 3e1ba8d5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:cri-tools-1.13.0-0               ################################# [100%]

[root@master ~]# rpm -ivh /tmp/kubectl-1.17.0-0.x86_64.rpm 
warning: /tmp/kubectl-1.17.0-0.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 3e1ba8d5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:kubectl-1.17.0-0                 ################################# [100%]

[root@master ~]# rpm -ivh /tmp/kubelet-1.17.0-0.x86_64.rpm /tmp/kubernetes-cni-0.7.5-0.x86_64.rpm 
warning: /tmp/kubelet-1.17.0-0.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 3e1ba8d5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:kubernetes-cni-0.7.5-0           ################################# [ 50%]
   2:kubelet-1.17.0-0                 ################################# [100%]
[root@master ~]# rpm -ivh /tmp/kubeadm-1.17.0-0.x86_64.rpm 
warning: /tmp/kubeadm-1.17.0-0.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 3e1ba8d5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:kubeadm-1.17.0-0                 ################################# [100%]

# 启动kubelet,添加开机启动
[root@slave1 ~]# systemctl start kubelet
[root@slave1 ~]# systemctl enable kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.

6  集群初始化

[root@master ~]# kubeadm init
 --apiserver-advertise-address=192.168.247.136  # 这个是master主机地址
 --image-repository registry.aliyuncs.com/google_containers
 --kubernetes-version=v1.17.4
 --service-cidr=10.96.0.0/12
 --pod-network-cidr=10.244.0.0/16

# 结果就是这个
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.247.136:6443 --token 4d6492.rpd2zll8rb7c0u1l \
    --discovery-token-ca-cert-hash sha256:bd23534d635b46f5316f0d388bd88853a6ddb47b1c04129bf25ea31cdbbfba4a 
# 最后这个就是只需要在node节点上执行的加入集群指令
# 创建必要⽂件,所有节点执行,否则会报错The connection to the server localhost:8080 was refused
[root@master ~]# mkdir -p $HOME/.kube
[root@master ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# 如果提示文件不存在,就从master节点拷过来
[root@master ~]# chown $(id -u):$(id -g) $HOME/.kube/config

在node上节点执行

[root@slave1 ~]# kubeadm join 192.168.247.136:6443 --token 4d6492.rpd2zll8rb7c0u1l \
>     --discovery-token-ca-cert-hash sha256:bd23534d635b46f5316f0d388bd88853a6ddb47b1c04129bf25ea31cdbbfba4a
~~~~
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.

在master上查看节点信息

[root@master ~]# kubectl get nodes
NAME     STATUS     ROLES    AGE    VERSION
master   NotReady   master   11h    v1.17.0
slave1   NotReady   <none>   2m2s   v1.17.0
slave2   NotReady   <none>   25s    v1.17.0

安装网络插件,只在master节点操作即可

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kubeflannel.yml


** 由于外⽹不好访问,如果出现⽆法访问的情况,可以直接⽤下⾯的 记得⽂件名是kube-flannel.yml,位置:/root/kube-flannel.yml内容

 可以用这个直接复制粘贴过来使用

# 重启kubelet
[root@slave2 ~]# systemctl restart kubelet
# 重启docker
[root@slave2 ~]# systemctl restart docker

# 执行使⽤配置⽂件启动fannel
[root@master ~]# kubectl apply -f kube-flannel.yml 
[root@master ~]# kubectl get nodes
NAME     STATUS   ROLES    AGE   VERSION
master   Ready    master   11h   v1.17.0
slave1   Ready    <none>   18m   v1.17.0
slave2   Ready    <none>   16m   v1.17.0

五 验证

1 创建⼀个nginx服务

# 创建
[root@master ~]# kubectl create deployment nginx --image=nginx:1.14-alpine
deployment.apps/nginx created
# 开放端口
[root@master ~]# kubectl expose deploy nginx --port=80 --target-port=80 --type=NodePort
service/nginx exposed

2 查看服务

[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        11h
nginx        NodePort    10.96.154.42   <none>        80:30912/TCP   15s
[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6867cdf567-vkf99   1/1     Running   0          34s

3 浏览器查看三个地址

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐