美文网首页集群技术
CentOS 7系统搭建k8s集群

CentOS 7系统搭建k8s集群

作者: zlzmy | 来源:发表于2019-07-18 23:56 被阅读22次

目标

以k8s官方文档为蓝本,搭建k8s单主集群,包含一个主节点,一个工作节点

准备工作

准备两台装有CentOS 7 64位操作系统的机器

以下是我用来安装的机器

IP 配置 规划
192.168.60.12 8C 16G 50G SAS 主节点
192.168.60.67 8C 16G 50G SAS 工作节点

关闭swap

# 查看已配置的swap
cat /proc/swaps
# 关闭swap
swapoff -a
# 永久关闭swap, 删除/etc/fstab文件中与swap相关的所有配置项

设置hostname

k8s以hostname为标识,区分主机节点,hostname需保证唯一,尤其不要使用localhost这种大众化的名称

# 查看主机名称
hostname
# 设置主机名称
hostnamectl set-hostname [your-host-name]

安装Docker

参考官方文档,简单快捷,5分钟搞定

安装k8s集群

在所有节点上安装以下三个组件

  • kubeadm the command to bootstrap the cluster
  • kubelet the component that runs on all of the machines in your cluster and does things like starting pods and containers
  • kubectl the component that runs on all of the machines in your cluster and does things like starting pods and containers
# 设置k8s yum源,以mirrors.aliyun.com/kubernetes代替packages.cloud.google.com防止网络和谐
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=[https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64](https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64)
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=[https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg](https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg) [https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg](https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg)
EOF
# Set SELinux in permissive mode (effectively disabling it)
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# 安装三个组件
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# 设置kubelet开机启动
systemctl enable --now kubelet

使用kubeadm初始化主节点

  • 以下命令在主节点执行
# --pod-network-cidr 用于后续使用calico网络插件;子网网段不要与主机已使用的网段重复
# --image-repository 使用阿里云镜像代替k8s.gcr.io拉取k8s基础镜像
kubeadm init --pod-network-cidr=172.18.0.0/24 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
  • 执行成功后会有类似以下输出
kubeadm join 192.168.60.12:6443 --token 3d4hxz.723se8pupr0evc2m \
    --discovery-token-ca-cert-hash sha256:bee41a92ab1f9d0bd3d4961eeb59bfbe5ab3681a93a84a55696e73d2c48817fe

该内容用于加入其它工作节点到k8s集群,保存该内容以便加入工作节点到主库时使用

安装网络插件

  • 以下命令在主节点执行
  • 此处安装的是当前流行的calico网络插件
kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml

使主节点可调度

  • 以下命令在主节点执行
  • k8s默认不会将用户服务调度到主节点,为了充分利用资源,此处使主节点可被调度
kubectl taint nodes --all node-role.kubernetes.io/master-

加入工作节点到主库

kubeadm join 192.168.60.12:6443 --token 3d4hxz.723se8pupr0evc2m \
    --discovery-token-ca-cert-hash sha256:bee41a92ab1f9d0bd3d4961eeb59bfbe5ab3681a93a84a55696e73d2c48817fe

验证集群成功

  • 以下命令在主节点执行
# 所有pod状态都为Running,说明集群安装成功
kubectl get pods --all-namepspaces

参考阅读

相关文章

网友评论

    本文标题:CentOS 7系统搭建k8s集群

    本文链接:https://www.haomeiwen.com/subject/hqoqlctx.html