K8s安装

K8S安装

Kubernetes支持在物理服务器或虚拟机中运行,本次使用虚拟机准备测试环境,硬件配置信息如表所示

IP地址 节点角色 Hostname
172.24.2.69 master k8s-master
172.24.2.71 worker k8s-node-1
172.24.2.70 worker k8s-node-2

环境准备

1、设置主机名hostname,管理节点设置主机名为 k8s-master 。

1
hostnamectl set-hostname k8s-master

2、关闭防火墙、selinux和swap

1
2
3
4
5
6
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab

3、配置内核参数,将桥接的IPv4流量传递到iptables的链

1
2
3
4
5
6
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system

4、配置国内yum源

1
2
3
4
5
6
7
8
9
yum install -y wget

mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum clean all && yum makecache

5、配置国内Kubernetes源

1
2
3
4
5
6
7
8
9
10
cat <<EOF > /etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

6、配置 docker 源

1
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

软件安装

1、安装docker

1
2
3
4
5
yum install docker-ce-18.06.1.ce-3.el7

systemctl enable docker && systemctl start docker

docker --version

2、安装kubeadm、kubelet、kubectl

1
2
3
yum install kubelet-1.14.3-0 kubeadm-1.14.3-0 kubectl-1.14.3-0

systemctl enable kubelet

部署启动

1、在master进行Kubernetes集群初始化

1
kubeadm init --kubernetes-version=1.14.3 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16

2、配置kubectl工具

1
2
3
4
5
mkdir -p /root/.kube
cp /etc/kubernetes/admin.conf /root/.kube/config

kubectl get nodes
kubectl get cs

3、部署flannel网络

1
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

子节点部署

1、设置子节点的名称

1
hostnamectl set-hostname k8s-node-1

2、子节点可以继续执行主节点中(环境准备、软件安装)

3、将子节点加入到主节点中

1
2
kubeadm join 172.24.2.69:6443 --token 94zry6.lzob4h452am02f0d \
--discovery-token-ca-cert-hash sha256:bf8e4fcdbf4001cafe5ab5325e429dcb3f8a52a836d0a6b80f683db506611043

4、如果当token过期了,可以使用下面命令重新生成token

1
kubeadm token create

集群状态检查

在master节点输入命令检查集群状态,返回如下结果则集群状态正常

1
2
3
4
5
6
kubectl get nodes

NAME STATUS ROLES AGE VERSION
k8s-master Ready master 7d6h v1.14.3
k8s-node-1 Ready <none> 7d5h v1.14.3
k8s-node-2 Ready <none> 100s v1.14.3

参考文献:https://www.kubernetes.org.cn/5462.html