前言
Kubernetes是用于自动部署、扩展和管理“容器化应用程序”的开源系统。该系统由Google设计并捐赠给Cloud Native Computing Foundation来使用。 它旨在提供“跨主机集群的自动部署、扩展以及运行应用程序容器的平台”。 它支持一系列容器工具,包括Docker等。
必须条件
不少于2台的拥有公网ip的云服务器(强烈建议使用海外服务器),服务器最低配置:CPU2核心,内存2G,硬盘20G
系统为CentOS 7.9
部署Master节点
设置Master节点主机名
1
| hostnamectl set-hostname k8s-master
|
修改Master节点hosts文件
1 2 3 4
| vim /etc/hosts
192.168.1.10 k8s-master 192.168.1.11 k8s-node1
|
关闭Master节点selinux
1 2
| setenforce 0 sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
|
关闭防火墙
1 2
| systemctl stop firewalld systemctl disable firewalld
|
关闭swap
1 2 3
| swapoff /dev/mapper/centos-swap rm /dev/mapper/centos-swap /dev/mapper/centos-swap swap swap default 0 0
|
在Master节点添加安装源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| cat <<EOF > kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
mv kubernetes.repo /etc/yum.repos.d/
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install yum-utils
|
在Master节点安装k8s和docker组件
1 2 3 4 5
|
yum install -y kubelet-1.22.4 kubectl-1.22.4 kubeadm-1.22.4 docker-ce.20.10.11-3.e17
|
启动Master节点的kubelet和docker服务
1 2 3 4
| systemctl enable docker systemctl enable kubelet systemctl start docker systemctl start kubelet
|
修改Master节点 daemon.json 配置
1 2 3 4 5 6 7 8 9 10 11
| cat <<EOF > daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://rdy406dj.mirror.aliyuncs.com"] } EOF mv daemon.json /etc/docker/
systemctl daemon-reload systemctl restart docker
|
初始化k8s集群
1 2 3 4 5 6 7 8
| kubeadm init --image-repository=registry.aliyuncs.com/google_containers
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config
|
在Master节点安装flannel网络插件
1
| kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
|
部署Worker节点
设置Worker节点主机名
1
| hostnamectl set-hostname k8s-node1
|
修改Worker节点hosts文件
1 2 3 4
| vim /etc/hosts
192.168.1.10 k8s-master 192.168.1.11 k8s-node1
|
关闭Worker节点selinux
1 2
| setenforce 0 sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
|
关闭防火墙
1 2
| systemctl stop firewalld systemctl disable firewalld
|
关闭swap
1 2 3
| swapoff /dev/mapper/centos-swap rm /dev/mapper/centos-swap /dev/mapper/centos-swap swap swap default 0 0
|
在Worker节点添加安装源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| cat <<EOF > kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
mv kubernetes.repo /etc/yum.repos.d/
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install yum-utils
|
在Worker节点安装k8s和docker组件
1 2 3 4 5
|
yum install -y kubelet-1.22.4 kubectl-1.22.4 kubeadm-1.22.4 docker-ce.20.10.11-3.e17
|
启动Worker节点的kubelet和docker服务
1 2 3 4
| systemctl enable docker systemctl enable kubelet systemctl start docker systemctl start kubelet
|
修改Worker节点 daemon.json 配置
1 2 3 4 5 6 7 8 9 10 11
| cat <<EOF > daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://rdy406dj.mirror.aliyuncs.com"] } EOF mv daemon.json /etc/docker/
systemctl daemon-reload systemctl restart docker
|
将Worker节点加入k8s集群
1
| kubeadm join 192.168.1.10:6443 --token xxx --discovery-token-ca-cert-hash xxx
|
初步使用
查看所有节点