欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

centos系统安装Kubernetes集群步骤

程序员文章站 2022-03-02 17:34:37
目录前言1、安装docker2、安装kubernetes1、基本环境2、安装kubelet、kubeadm、kubectl(三台机器全部都要设置)3、初始化master节点3、安装calico网络插件...

前言

安装前请准备
选择4核8g(master)、8核16g(node1)、8核16g(node2) 三台机器,按量付费进行实验,centos7.9

这里的机器默认都是干净的,建议租云上的机器,差不多一个小时3块钱,自己搭虚拟机也行

云上机器
vpc网络: 172.31.0.0/16
私有网络:172.31.0.0/24

安全组端口开放:22、30000~32767

1、安装docker

请在三台机器上安装docker,执行下面的命令即可

sudo yum remove docker*
sudo yum install -y yum-utils

#配置docker的yum地址
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo


#安装指定版本
sudo yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6

#	启动&开机启动docker
systemctl enable docker --now

# docker加速配置
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'eof'
{
  "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
eof
sudo systemctl daemon-reload
sudo systemctl restart docker

2、安装kubernetes

1、基本环境

每个机器使用内网ip互通
每个机器配置自己的hostname,不能用localhost

三台机器设置hostname

#设置每个机器自己的hostname
hostnamectl set-hostname xxx

例如:
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2

基础设置(三台机器全部都要设置)

# 将 selinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^selinux=enforcing$/selinux=permissive/' /etc/selinux/config

#关闭swap关闭分区
swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab

#允许 iptables 检查桥接流量
cat <<eof | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
eof

cat <<eof | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
eof
sudo sysctl --system

2、安装kubelet、kubeadm、kubectl(三台机器全部都要设置)

#配置k8s的yum源地址
cat <<eof | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=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
eof


#安装 kubelet,kubeadm,kubectl
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9

#启动kubelet
sudo systemctl enable --now kubelet

⚠️ 注意⚠️ : 三台机器同时写一个域名来配置master节点的ip地址
#所有机器配置master域名
echo "172.31.0.4  k8s-master" >> /etc/hosts

3、初始化master节点

1、初始化

⚠️ - apiserver-advertise-address = 你master节点的ip
⚠️ - control-plane-endpoint = 你上面配置的域名

kubeadm init \
--apiserver-advertise-address=172.31.0.4 \
--control-plane-endpoint=k8s-master \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16

2、记录关键信息

当前初始化完成之后将会输出下面的信息,记得保存到本地哦

⚠️下面的操作将用到这个输出的内容,下面的操作请不要复制我这里的命令,以自己的实际输出为准

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/

you can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240 \
    --control-plane 

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

kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240
保存后执行输出中的内容
mkdir -p $home/.kube
sudo cp -i /etc/kubernetes/admin.conf $home/.kube/config
sudo chown $(id -u):$(id -g) $home/.kube/config

3、安装calico网络插件

先下载calico.yaml ,然后使用kubectl来部署

curl https://docs.projectcalico.org/manifests/calico.yaml -o

kubectl apply -f calico.yaml

4、加入worker节点

上面成功部署之后输出的内容注意,最后面

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

kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240
将命令复制到其他的node节点上执行,如果master上也执行了,则master也当作成一个工作节点了

扩展:加入master节点(多个master节点)

上面成功部署之后输出的内容注意,最后面

you can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s-master:6443 --token 3vckmv.lvrl05xpyftbs177 \
    --discovery-token-ca-cert-hash sha256:1dc274fed24778f5c284229d9fcba44a5df11efba018f9664cf5e8ff77907240 \
    --control-plane 
在其他你需要成为master节点的机器上执行即可

5、验证

kubectl get node

至此安装成功

总结

到此这篇关于centos系统安装kubernetes集群步骤的文章就介绍到这了,更多相关centos安装kubernetes内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: CentOS kubernetes