Kubernetes

K8S基础版搭建

  • 准备至少两台以上服务器
  • 服务器 >2CPU >2GB Ram
  • Centos 7.x
    • 192.168.1.1 master
    • 192.168.1.2 node1
    • 192.168.1.3 node2


基础环境配置

关闭防火墙

systemctl stop firewalld && systemctl disable firewalld

关闭SELINUX

sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

关闭SWAP

临时关闭 swapoff -a
永久关闭 vim /etc/fstab 注释swap行

设置主机名

hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2

配置hosts文件

echo "192.168.1.1 master" >> /etc/hosts
echo "192.168.1.2 node1" >> /etc/hosts
echo "192.168.1.3 node2" >> /etc/hosts

配置iptables链

cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

重载

sysctl --system

准备容器运行时环境(Docker)

添加阿里云镜像源

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

安装Docker(任选其一)

yum -y install docker-ce-18.06.1.ce-3.el7
yum -y install docker-ce-19.03.5

准备Kubernetes环境

添加阿里云 kubernetes镜像源

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=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/rpm-package-key.gpg
EOF

安装相关组件

yum install -y --nogpgcheck kubelet-1.20.6 kubeadm-1.20.6 kubectl-1.20.6

设置kubelet开机自启

systemctl enable kubelet

配置Docker的cgroup-driver为systemd

cat > /etc/docker/daemon.json <<EOF
{
 "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

重启Docker

systemctl restart docker

以上操作步骤在集群的所有节点都需要执行

配置K8S Master节点

apiserver-advertise-address需要替换Master服务器IP

kubernetes-version 需要和kubeadm kubelet组件版本保持一致

kubeadm init \
--apiserver-advertise-address = 172.16.3.40 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.14.0 \
--service-cidr = 10.1.0.0/16 \
--pod-network-cidr = 10.244.0.0/16

配置使用kubectl工具

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

查看节点

kubectl get node


[root@master ~]# kubectl get node
NAME     STATUS                     ROLES                  AGE   VERSION
master   Ready,SchedulingDisabled   control-plane,master   18d   v1.20.6

安装CNI插件

安装pod网络插件,该步骤必须成功

测试拉取镜像地址:

docker pull quay.io/coreos/flannel:v0.11.0-amd64
kubectl apply -f  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Node节点加入集群

在Node节点执行

集群master集群在kubeadm init初始化好时会打印join命令,如果忘记则可以使用

kubeadm token create --print-join-command

Node节点加入集群

kubeadm join 10.0.4.4:6443 --token 1mc4wy.rcagp875vcv6c1i     --discovery-token-ca-cert-hash sha256:2af6c8488f56d450b815485066fc8ba79507965a6f9f49edhjde00a439e949b 

查看加入的集群

等到node节点的status都变为Ready即为成功

kubectl get node


NAME     STATUS                     ROLES                  AGE   VERSION
master   Ready,SchedulingDisabled   control-plane,master   18d   v1.20.6
n1       Ready                      <none>                 27h   v1.20.6
n2       Ready                      <none>                 27h   v1.20.6

回复

This is just a placeholder img.