通过Kubeadm简单搭建Kubernetes的Master节点和Node节点

一、硬件配置:

两台服务器,IP地址分别为192.168.11.11,192.168.11.12

192.168.11.11做为Master,192.168.11.12做为Node。

操作系统Centos7.6
最低硬件配置:
CPU 2核
内存 2G

二、环境部署(所有节点)

修改hostname:

master上执行:

sudo su

hostnamectl set-hostname master1

worker上执行:

sudo su

hostnamectl set-hostname worker1

 

下面这些操作,所有服务器都需要执行。

vi /etc/hosts

192.168.11.11 master1
192.168.11.12 worker1

安装必要软件并禁用防火墙:

yum clean all
yum -y update 
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp ntpdate
setenforce 0
systemctl stop firewalld
systemctl disable firewalld


禁用虚拟内存,禁止swap并禁用开机启动:

swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

时间同步:

timedatectl set-timezone Asia/Shanghai
ntpdate cn.pool.ntp.org

修改内核参数:

cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
EOF

执行命令:

sysctl -p /etc/sysctl.d/kubernetes.conf

如果执行上面这个命令报下面的错误:

sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-iptables: 没有那个文件或目录
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: 没有那个文件或目录


可以执行一下:

modprobe br_netfilter


然后再执行命令:

sysctl -p /etc/sysctl.d/kubernetes.conf

 

三、安装docker(所有节点)

根据kubernetes对docker版本的兼容测试情况,我们选择18.09.9版本

由于大家都知道的原因,docker官网的访问速度可能极慢甚至无法访问,使用yum安装很难成功。

我们这里直接使用rpm方式安装:

下载rpm包:

mkdir -p /opt/kubernetes/docker && cd /opt/kubernetes/docker

wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.2-3.el7.x86_64.rpm
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-18.09.9-3.el7.x86_64.rpm
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-cli-18.09.9-3.el7.x86_64.rpm

清理原有版本:

yum remove -y docker* container-selinux

安装rpm包
yum localinstall -y *.rpm

开机启动
systemctl enable docker

 

四、安装kubernetes工具

配置yum源(如果有条件科学上网,可以把下面的"mirrors.aliyun.com"替换为"packages.cloud.google.com"):

cat <<EOF > /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

安装kubernetes工具:

yum list kubeadm --showduplicates | sort -r

yum install -y kubeadm-1.16.4-0 kubelet-1.16.4-0 kubectl-1.16.4-0 --disableexcludes=kubernetes

说明一下:kubelet版本是1.16.4,该版本支持的docker版本为1.13.1, 17.03, 17.06, 17.09, 18.06, 18.09

开机启动kubelet:

systemctl enable kubelet 

 

五、配置master节点


在master节点上生成配置文件:

kubeadm config print init-defaults ClusterConfiguration > kubeadm.conf

修改这个文件,有三个地方需要修改:

kubernetesVersion: v1.18.2

......

localAPIEndpoint:
  advertiseAddress: 192.168.11.11
  bindPort: 6443

......

imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers

......


拉取镜像:

kubeadm config images pull --config ./kubeadm.conf

检查镜像:

docker images

初始化:

kubeadm init --config ./kubeadm.conf

初始化完成以后会有类似如下的提示:

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

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/

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

kubeadm join 192.168.11.11:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:44ae14f61ea6d155d39e61616c8242e2a58ae4d01cfa847ecac7e573fc02d32a

 

按照上面的提示,我们执行这个命令:

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

 

初始化完成了,启动kubelet

systemctl enable kubelet && systemctl start kubelet

执行下面这个命令,查看节点状态,可以看到当前有一个master1的节点,并且STATUS是NotReady的状态:

[root@localhost ~]# kubectl get node
NAME      STATUS   ROLES    AGE     VERSION
master1   NotReady    master   3m30s   v1.16.4

 


获取并上传kube-flannel.yml

kube-flannel.yml下载地址:

https://download.csdn.net/download/fangfu123/12376844

下载下来以后,上传到master服务器上。

 

执行下面这个命令:

kubectl apply -f kube-flannel.yml 

稍等一会,就可以检查一下master的网络状态,可以看到已经状态已经变成Ready

[root@localhost ~]# kubectl get node
NAME      STATUS   ROLES    AGE     VERSION
master1   Ready    master   3m30s   v1.16.4

 

六、配置Worker Node

先在master上把相关文件传到worker1上

scp /etc/kubernetes/admin.conf root@worker1:~

scp kube-flannel.yml root@worker1:~

远程登录worker1,执行:

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

worker1 加入master集群:

kubeadm join 192.168.11.11:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:44ae14f61ea6d155d39e61616c8242e2a58ae4d01cfa847ecac7e573fc02d32a

启动flannel网络:

kubectl apply -f kube-flannel.yml

过一会,检查node3是否加入集群:

kubectl get nodes

集群节点状态查看:

kubectl get po -o wide -n kube-system 

Logo

开源、云原生的融合云平台

更多推荐