k8s快速安装部署

1.环境主备:
Vmware 虚拟机3台 配置2c 4g
主机名 节点IP 角色 部署组件
k8s-master01 10.0.0.21 Master01 etcd、kube-apiserver、kube-controller-manager、kubectl、kubeadm、kubelet、kube-proxy、flannel
k8s-node01 10.0.0.22 Node01 kubectl、kubelet、kube-proxy、annel
k8s-node02 10.0.0.23 Node02 kubectl、ubelet、ube-proxy、lannel

组件版本:
Centos 7.6
Kernel 3.10.0-862.el7.x86_64
etcd 3.3.15
coredns 1.6.2
kubeadmv 1.16.2
kubectlv 1.16.2
kubeletv 1.16.2
kube-proxyv 1.16.2
flannel v0.11.0

k8s安装部署分为两个角色,master和node,也就是管理节点和工作节点,可理解为master(老板发号指令)、node(打工崽干活的)。

2.修改主机名,添加hosts解析,所有机器执行。
10.0.0.21 hostnamectl set-hostname k8s-master01
10.0.0.22 hostnamectl set-hostname k8s-node01
10.0.0.23 hostnamectl set-hostname k8s-node02

cat >> /etc/hosts << EOF
10.0.0.21 k8s-master01
10.0.0.22 k8s-node01
10.0.0.23 k8s-node02
EOF

3.优化操作系统,所有机器执行。
iptables -P FORWARD ACCEPT
swapoff -a
防止开机自动挂载swap分区
sed -i '/ swap / s/^(.)$/#\1/g' /etc/fstab
关闭selinux
sed -ri 's#(SELINUX=).
#\1disabled#' /etc/selinux/config
setenforce 0
systemctl disable firewalld && systemctl stop firewalld

开启内核对流量转发
cat < /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
vm.max_map_count=262144
EOF

modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

下载阿里云源和docker源
curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

添加kubernetes.repo
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=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

Yum生成缓存
yum clean all && yum makecache

4.安装docker(所有节点执行)
yum list docker-ce --showduplicates | sort -r
yum install docker-ce -y

mkdir -p /etc/docker
配置docker镜像加速地址
vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://f1361db2.m.daocloud.io"]
}

启动docker
systemctl enable docker && systemctl start docker

验证是否启动
docker version

5.安装k8s-master01(所有机器执行)
安装1.16版本
yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 --disableexcludes=kubernetes

查看版本
kubeadm version

开机自启
systemctl enable kubelet

6.初始化配置文件,只在master01上执行
mkdir ~/k8s-install && cd ~/k8s-install

生成配置文件
kubeadm config print init-defaults > kubeadm.yaml

修改kubeadm.yml配置文件
[root@k8s-master01 k8s-install]# grep '#' kubeadm.yaml
advertiseAddress: 10.0.0.21 # 改为当前主机IP
imageRepository: registry.aliyuncs.com/google_containers # 改为阿里云镜像源
kubernetesVersion: v1.16.2 # 改为你自己安装的版本
podSubnet: 10.244.0.0/16 # 添加pod网段,设置容器内网络

提前下载镜像
kubeadm config images list --config kubeadm.yaml

检查镜像
[root@k8s-master01 k8s-install]# docker images|grep aliyun
registry.aliyuncs.com/google_containers/kube-proxy v1.16.2 8454cbe08dc9 2 years ago 86.1MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.16.2 6e4bffa46d70 2 years ago 163MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.16.2 ebac1ae204a2 2 years ago 87.3MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.16.2 c2c9a0406787 2 years ago 217MB
registry.aliyuncs.com/google_containers/etcd 3.3.15-0 b2756210eeab 2 years ago 247MB
registry.aliyuncs.com/google_containers/coredns 1.6.2 bf261d157914 2 years ago 44.1MB
registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 3 years ago 742kB

初始化k8s-master01节点
输入完之后注意查看提示信息
输入完:kubeadm init --config kubeadm.yaml
如下图所示:
k8s快速安装部署

执行如下命令,创建配置文件
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u)

上一篇:移动 APP 网络优化概述


下一篇:一道真实的面试题 | 如何保证消息队列的高可用