楼主环境:笔记本一台,内存16G, 硬盘500G,Oracle VM VirtualBox新建两个centos7虚拟机(2核cpu 内存4G 硬盘20G)
本文命令可直接粘贴复制
环境准备
虚拟机联网配置:
-
设置网卡连接方式 (master/node)
-
静态ip配置(master/node)
#vim 没安装时使用vi代替(蓝色部分为修改或新增部分)
$ vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=1532ae46-52e7-4f1e-8032-1a9bbc55d393
DEVICE=enp0s3
ONBOOT=yes
#以下是静态ip配置时新增内容
IPADDR=192.168.100.215 #此处给自己设置一个静态ip
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=119.29.29.29
DNS2=223.5.5.5
$ service network restart
详细步骤
-
虚拟机两台(ip按自己的网络环境相应配置)(master/node)
机器IP | hostname |
---|---|
192.168.100.215 | k8s-master |
192.168.100.216 | k8s-node1 |
-
关闭防火墙(master/node)
systemctl stop firewalld
systemctl disable firewalld
-
关闭selinux(master/node)
setenforce 0 # 临时关闭
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 永久关闭
-
关闭swap(master/node)
swapoff -a # 临时关闭;关闭swap主要是为了性能考虑
free # 可以通过这个命令查看swap是否关闭了
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久关闭
-
添加主机名与IP对应的关系(master/node)
$ vim /etc/hosts
#添加如下内容:
192.168.100.215 k8s-master
192.168.100.216 k8s-node1
#保存退出
-
修改主机名(master/node)
k8s-master:
[root@localhost ~] hostname
localhost.localdomain
[root@localhost ~] hostname k8s-master ##临时生效
[root@localhost ~] hostnamectl set-hostname k8s-master ##重启后永久生效
k8s-node1:
[root@localhost ~] hostname
localhost.localdomain
[root@localhost ~] hostname k8s-node1 ##临时生效
[root@localhost ~] hostnamectl set-hostname k8s-node1 ##重启后永久生效
-
桥接设置(master/node)
$ 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(master/node)
$ yum -y install wget
# 添加docker yum源
$ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O/etc/yum.repos.d/docker-ce.repo
# 安装
$ yum -y install docker-ce
# 设置开机启动
$ systemctl enable docker
# 启动docker
$ systemctl start docker
-
添加阿里云YUM软件源(master/node)
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[k8s]
name=k8s
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
EOF
-
安装kubeadm,kubelet和kubectl(master/node)
$ yum install -y kubelet-1.17.0 kubectl-1.17.0 kubeadm-1.17.0
# 此时,还不能启动kubelet,因为此时配置还不能,现在仅仅可以设置开机自启动
$ systemctl enable kubelet
-
部署Kubernetes (master)
- 11.1 kubeadm init
kubeadm init \
--apiserver-advertise-address=192.168.100.215 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.17.0 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
成功后末尾输出信息如下:
kubeadm join 192.168.100.215:6443 --token 5l6xl0.qwifh1s0hrxdss3f \
--discovery-token-ca-cert-hash sha256:cfd498dd8bb0e0d88cc45a795941906911dabc65f9251ae5479e5fb6ac85a472
查看安装的镜像
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.aliyuncs.com/google_containers/kube-proxy v1.17.0 7d54289267dc 7 months ago 116MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.17.0 0cae8d5cc64c 7 months ago 171MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.17.0 5eb3b7486872 7 months ago 161MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.17.0 78c190f736b1 7 months ago 94.4MB
registry.aliyuncs.com/google_containers/coredns 1.6.5 70f311871ae1 8 months ago 41.6MB
registry.aliyuncs.com/google_containers/etcd 3.4.3-0 303ce5db0e90 9 months ago 288MB
registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 2 years ago 742kB
- 11.2 使用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命令
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 23m v1.17.0
k8s-node1 Ready <none> 19m v1.17.0
-
安装Pod网络插件(master/node)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
报错:The connection to the server raw.githubusercontent.com was refused - did you specify the right host or port?
原因:外网不可访问
解决办法:
sudo vim /etc/hosts199.232.28.133 raw.githubusercontent.com
重新执行上面命令,便可成功安装!
-
node节点加入master(node)
kubeadm join 192.168.100.215:6443 --token 5l6xl0.qwifh1s0hrxdss3f \
--discovery-token-ca-cert-hash sha256:cfd498dd8bb0e0d88cc45a795941906911dabc65f9251ae5479e5fb6ac85a472
在master查看
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady master 22m v1.17.0
k8s-node1 Ready <none> 18m v1.17.0
至此,主从双节点集群搭建完毕,后续部署相关,敬请期待!
觉得有用的,请动动手指点个赞,以赞鼓励!!!