参考:
https://juejin.im/post/5d7fb46d5188253264365dcf
两台虚拟机 centos7.8版本
虚拟机初始化
/etc/sysconfig/network-scripts/ifcfg-ens33
yum install net-tools
vim /etc/hostname
一台叫:tom
一台叫:jack
vim /etc/hosts
192.168.233.132 tom
192.168.233.1 jack
修改后两台的配置
#tom
[root@tom ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.233.132 tom
192.168.233.1 jack
[root@tom ~]# cat /etc/hostname
tom
#jack
[root@jack ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.233.132 tom
192.168.233.1 jack
[root@jack ~]# cat /etc/hostname
jack
[root@tom ~]# systemctl stop firewalld
[root@tom ~]# systemctl disable firewalld
\3. 禁用SELinux
vi /etc/selinux/config
#设置SELINUX=disabled. 重启机器
#查看状态
[root@tom ~]# sestatus
SELinux status: disabled
swapoff -a # 临时
yum install ntpdate -y
ntpdate ntp.api.bz
参考:https://www.runoob.com/docker/centos-docker-install.html
设置仓库,在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,可以从仓库安装和更新 Docker。
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
设置国内源地址
阿里云源地址
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
列出可安装得版本
yum list docker-ce --showduplicates | sort -r
安装指定版本
yum install docker-ce-18.09.9-3.el7 docker-ce-cli-18.09.9-3.el7 containerd.io
启动docker
systemctl enable docker
systemctl start docker
由于国内网络原因, 官方文档中的地址不可用, 替换为阿里云镜像地址, 执行以下代码即可
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF
36 yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
37 systemctl enable kubelet && systemctl start kubelet
修改网络配置
cat < /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
以上所有操作在worker 和master 节点上都需要执行
初始化master
kubeadm config print init-defaults > kubeadm-init.yaml
该文件有两处需要修改:
advertiseAddress: 1.2.3.4
修改为本机地址imageRepository: k8s.gcr.io
修改为imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kubeadm config images pull --config kubeadm-init.yaml
kubeadm init --config kubeadm-init.yaml
执行完毕后会输出:
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.233.132:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:ae867e99b70f13173649a21cedc0a6cd2609186e263a51bd7dbee10e8cbdf9d3
最后两行保存下来是worker节点加入所需要执行的命令.
接下来配置环境, 让当前用户可以执行kubectl命令:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
测试一下: 此处的NotReady
是因为网络还没配置.
[root@tom ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
tom NotReady master 92s v1.18.4
配置网络
安装wget -y表示所有问题都设置yes
yum -y install wget
下载描述文件
[root@tom ~]# wget https://docs.projectcalico.org/v3.8/manifests/calico.yaml
[root@tom ~]# cat kubeadm-init.yaml | grep serviceSubnet:
serviceSubnet: 10.96.0.0/12
vi calico.yaml
, 将192.168.0.0/16
修改为10.96.0.0/12
需要注意的是, calico.yaml中的IP和kubeadm-init.yaml需要保持一致, 要么初始化前修改kubeadm-init.yaml, 要么初始化后修改calico.yaml.
执行
kubectl apply -f calico.yaml
初始化网络.
此时查看node信息, master的状态已经是Ready
了.
等个两三分钟才变化
[root@tom ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
tom Ready master 9m40s v1.18.4