K8S集群搭建

一、 环境和版本

1. 操作系统

干干静静的centos7系统,选取的是mini的iso最小化安装

# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
# cat /proc/version
Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016

2. K8S版本

K8S使用的当前(现在是2021-04-07)最新版本:v1.20。官方教程:https://kubernetes.io/zh/docs/home/。安装方式采用kubeadm安装。

3. 集群环境

最少2个节点,其中1个节点作为master节点,其他节点作为node节点,本例中:

  1. 10.10.2.251,master节点
  2. 10.10.2.252,node节点
  3. 10.10.2.253,node节点

二、 安装

1. 设置host

# ssh root@10.10.2.251
# hostnamectl set-hostname k8s-1
# exit

# ssh root@10.10.2.252
# hostnamectl set-hostname k8s-2
# exit

# ssh root@10.10.2.252
# hostnamectl set-hostname k8s-3
# exit

2. 所有节点执行准备

2.1 关闭防火墙和selinux

# setenforce 0
# sed -i 's|SELINUX=\(.*\)|SELINUX=disabled|g' /etc/selinux/config
# systemctl stop firewalld
# systemctl disable firewalld

2.2 配置docker的yum源

# curl -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
# sudo sed -i 's+download.docker.com+opentuna.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo

2.3 安装docker-ce

# yum install -y docker-ce

2.4 启动docker服务

# systemctl start docker
# systemctl enable docker

2.5 关闭swap

# swapoff -a
# vim /etc/fstab 
将有swap那行注释掉

2.6 配置K8S的yum源

# vim /etc/yum.repos.d/kubernetes.repo
输入一下内容保存 [kubernetes]
name=kubernetes
baseurl=https://opentuna.cn/kubernetes/yum/repos/kubernetes-el7-$basearch
enabled=1

2.7 安装kubelet、kubeadm、kubectl

# yum install -y kubelet kubeadm kubectl --nogpgcheck

2.8 启动kubelet服务

# systemctl start kubelet
# systemctl enable kubelet

2.9 获取K8S所需的docker镜像

执行kubeadm config images list查看K8S需要的镜像,并参照如下方式获取镜像

# docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.20.5
# docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.20.5
# docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.20.5
# docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.20.5
# docker pull registry.aliyuncs.com/google_containers/pause:3.2
# docker pull registry.aliyuncs.com/google_containers/etcd:3.4.13-0
# docker pull registry.aliyuncs.com/google_containers/coredns:1.7.0

# docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.20.5           k8s.gcr.io/kube-apiserver:v1.20.5
# docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.20.5  k8s.gcr.io/kube-controller-manager:v1.20.5
# docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.20.5           k8s.gcr.io/kube-scheduler:v1.20.5
# docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.20.5               k8s.gcr.io/kube-proxy:v1.20.5
# docker tag registry.aliyuncs.com/google_containers/pause:3.2                        k8s.gcr.io/pause:3.2
# docker tag registry.aliyuncs.com/google_containers/etcd:3.4.13-0                    k8s.gcr.io/etcd:3.4.13-0
# docker tag registry.aliyuncs.com/google_containers/coredns:1.7.0                    k8s.gcr.io/coredns:1.7.0

3. master节点执行创建集群

3.1 初始化集群

# kubeadm init --pod-network-cidr=10.244.0.0/16

注意执行成功后类似如下输出,复制下来

kubeadm join 10.10.2.251:6443 --token 2dhvds.4yd881szk64kqxoo --discovery-token-ca-cert-hash sha256:5435c5cbfbef0e00f5fd7b96c325771adc5a8f5591a7799939861bde6d0f557f

3.2 执行命令

# vi /etc/profile
增加如下一行,并保存
export KUBECONFIG=/etc/kubernetes/admin.conf
# source /etc/profile

3.3 安装网络插件

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

4. 将node节点加入集群

执行3.1步输出内容

# ssh root@10.10.2.252
# kubeadm join 10.10.2.251:6443 --token 2dhvds.4yd881szk64kqxoo --discovery-token-ca-cert-hash sha256:5435c5cbfbef0e00f5fd7b96c325771adc5a8f5591a7799939861bde6d0f557f
# exit

# ssh root@10.10.2.253
# kubeadm join 10.10.2.251:6443 --token 2dhvds.4yd881szk64kqxoo --discovery-token-ca-cert-hash sha256:5435c5cbfbef0e00f5fd7b96c325771adc5a8f5591a7799939861bde6d0f557f
# exit

5. 查看集群状态

# kubectl get nodes

NAME    STATUS     ROLES                  AGE     VERSION
k8s-1   Ready        control-plane,master   15m    v1.20.5
k8s-2   Ready        <none>                 6m48s   v1.20.5
k8s-3   Ready        <none>                 7m35s   v1.20.5

 

posted @ 2021-04-07 18:16  柚子苹果果  阅读(338)  评论(1编辑  收藏  举报