K8s集群搭建
本文记录了自己搭建K8s集群的过程,第二部分是普通的搭建方式,第三部分是kubekey一键安装方式
目录
1 基础概念
部署方式:传统部署,虚拟机部署,容器化部署
大量容器的编排系统:
Kubernetes Cluster = N Master Node + N Worker Node:N主节点+N工作节点; N>=1
组件信息:Kubernetes 组件 | Kubernetes
API server :负责集群组件间的交互
kube proxy :负责网络访问的
kubelet :监控应用
2 集群安装
2.1 安装前置环境
4核8G(master 10.198.198.28)、8核16G(node1 10.198.198.27)、docker已安装
1)更改主机名
hostnamectl set-hostname hostname
cat >>/etc/hosts<<EOF
10.198.198.28 master
10.198.198.27 node-CICD
EOF
2)关闭SELinux和防火墙
systemctl stop firewalld
systemctl disable firewalld
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
3)关闭交换分区
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
4)配置流量桥接
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
net.ipv4.ip_forward = 1
vm.swappiness = 0
EOF
sudo sysctl --system
2.2 安装三大件
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
exclude=kubelet kubeadm kubectl
EOF
yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
systemctl enable --now kubelet
2.3 kubeadm创建集群
主节点下载所有的镜像
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
chmod +x ./images.sh && ./images.sh
其他节点就下载kube-proxy就行
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-proxy:v1.20.9
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
chmod +x ./images.sh && ./images.sh
所有Node节点都要配置master IP,命令如下
echo "10.198.198.28 cluster-endpoint" >> /etc/hosts
master主节点初始化
kubeadm init \
--apiserver-advertise-address=10.198.198.28 \
--control-plane-endpoint=cluster-endpoint \
--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
安装成功会出现下面这段话:
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 cluster-endpoint:6443 --token s9kvwn.feuihd3ezux4hy7u \
--discovery-token-ca-cert-hash sha256:dbc89d4391253fd7c2cf1ac80fd7b12e1da773647725b2bcba7362c80771b08a \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join cluster-endpoint:6443 --token s9kvwn.feuihd3ezux4hy7u \
--discovery-token-ca-cert-hash sha256:dbc89d4391253fd7c2cf1ac80fd7b12e1da773647725b2bcba7362c80771b08a
2.4 安装收尾
1)主节点按照要求执行如下三条命令:
2)网络组件选一个安装:
curl https://docs.projectcalico.org/manifests/calico.yaml -O
kubectl apply -f calico.yaml
3)其他节点加入集群
根据安装结束的提示命令,将Node节点加入集群
4)令牌过期处理
kubeadm token create --print-join-command
3 Kubekey一键安装
1)设置域名
#各个机器设置自己的域名
hostnamectl set-hostname xxxx
2)master节点配置
export KKZONE=cn
curl -sfL https://get-kk.kubesphere.io | VERSION=v1.1.1 sh -
chmod +x kk
3)创建集群配置文件
./kk create config --with-kubernetes v1.20.4 --with-kubesphere v3.1.1
修改config-sample.yaml
再就根据需要把需要的组件false改为true
4)创建集群
./kk create cluster -f config-sample.yaml
首先检查发现conntrack没有安装,使用yum安装一下,然后再执行上面的命令
5) http://10.198.198.28:30880
更多推荐
所有评论(0)