这里是k8s节点篇的搭建,如果需要搭建master,请看 Centos7 K8S Master搭建
一、禁用SELINUX:
setenforce 0
vi /etc/selinux/config
SELINUX=disabled
二、创建/etc/sysctl.d/k8s.conf文件,添加如下内容:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
执行命令使修改生效。
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
三、安装Docker
1、设置docker的yum源(采用阿里云):
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2、下载docker-ce,这里采用18.06.1.ce版本
yum makecache fast
yum install -y --setopt=obsoletes=0 docker-ce-18.06.1.ce-3.el7
3、启动docker
systemctl start docker
systemctl enable docker
4、确认一下iptables filter表中FOWARD链的默认策略(pllicy)为ACCEPT
iptables -nvL
Chain INPUT (policy ACCEPT 263 packets, 19209 bytes) pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
如果不是ACCEPT,则修改
iptables -P FORWARD ACCEPT
四、使用kubeadm部署Kubernetes
1、安装kubeadm和kubelet(这里也采用阿里云镜像)
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=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
EOF
yum makecache fast && yum install -y kubelet kubeadm kubectl
这里会从google下载镜像,如果无法翻墙请执行下面命令后在执行上面名
docker pull mirrorgooglecontainers/kube-apiserver-amd64:v1.12.2
docker pull mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.2
docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.12.2
docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.12.2
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd-amd64:3.2.24
docker pull coredns/coredns:1.2.2
docker tag docker.io/mirrorgooglecontainers/kube-proxy-amd64:v1.12.2 k8s.gcr.io/kube-proxy:v1.12.2
docker tag docker.io/mirrorgooglecontainers/kube-scheduler-amd64:v1.12.2 k8s.gcr.io/kube-scheduler:v1.12.2
docker tag docker.io/mirrorgooglecontainers/kube-apiserver-amd64:v1.12.2 k8s.gcr.io/kube-apiserver:v1.12.2
docker tag docker.io/mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.2 k8s.gcr.io/kube-controller-manager:v1.12.2
docker tag docker.io/mirrorgooglecontainers/etcd-amd64:3.2.24 k8s.gcr.io/etcd:3.2.24
docker tag docker.io/mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag docker.io/coredns/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2
Kubernetes 1.8开始要求关闭系统的Swap,默认配置下kubelet将无法启动。
swapoff -a
修改/etc/sysctl.d/k8s.conf,增加下面一行。然后执行sysctl -p /etc/sysctl.d/k8s.conf使修改生效。
vm.swappiness=0
sysctl -p /etc/sysctl.d/k8s.conf
修改/etc/sysconfig/kubelet,增加--fail-swap-on=false
KUBELET_EXTRA_ARGS=--fail-swap-on=false
2 使用kubeadm init初始化集群
在各节点开机启动kubelet服务:
systemctl enable kubelet.service
四、安装pod
mkdir -p ~/k8s/ && cd ~/k8s
五、用命令加入集群
在master生成的join命令后面加入 --ignore-preflight-errors=Swap
kubeadm join 172.20.38.10:6443 --token m6fr83.uo3449ytbzyg73r6 --discovery-token-ca-cert-hash sha256:e9dc2bc1461e75a74638759f2609f3ea6166c894743b572cd07c06c3f45ebc13 --ignore-preflight-errors=Swap
查看集群成功了结果:kubectl get nodes
NAME STATUS ROLES AGE VERSION
test-chentianhui--devms--1 Ready master 6h43m v1.12.2
test-chentianhui--devms--2 Ready <none> 128m v1.12.2
test-chentianhui--devms--3 Ready <none> 84m v1.12.2
test-chentianhui--devms--4 Ready <none> 72s v1.12.2