一、硬件环境
- 准备3台Linux服务器,此处用Vmware虚拟机。
主机名 | CPU | 内存 |
---|---|---|
k8smaster | 2核 | 4G |
k8snode1 | 2核 | 4G |
k8snode2 | 2核 | 4G |
二、系统前置准备
- 配置三台主机的hosts文件
bash
cat << EOF > /etc/hosts
192.168.240.130 k8smaster
192.168.240.132 k8snode1
192.168.240.133 k8snode2
EOF
- 设置主机名
bash
hostnamectl set-hostname k8smaster
hostnamectl set-hostname k8snode1
hostnamectl set-hostname k8snode2
- 关闭selinux、禁用swap分区
bash
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
- ipv4流量转发
bash
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#让配置文件生效
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
- 关闭防火墙
bash
systemctl disable firewalld && systemctl stop firewalld
- 同步三台服务器的时间
bash
yum install ntpdate -y
ntpdate time.windows.com
三、环境安装
Docker 安装
- Docker卸载
bash
systemctl stop docker
yum remove -y docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
- Docker 安装
bash
yum -y install gcc
yum -y install gcc-c++
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce docker-ce-cli containerd.io
systemctl enable docker && systemctl start docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
k8s安装
- 配置k8s安装镜像
bash
cat <<EOF > kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
mv kubernetes.repo /etc/yum.repos.d/
- 安装kubeadm 、kubectl、kubelet
bash
yum remove -y kubelet kubeadm kubectl && yum install -y kubelet-1.16.2 kubectl-1.16.2 kubeadm-1.16.2 --disableexcludes=kubernetes
- 设置kubelet开机自启动
bash
systemctl enable kubelet
master节点配置
- 创建一个k8s的工作目录,生成一个kubeadm配置文件
bash
mkdir k8s-install && cd k8s-install
kubeadm config print init-defaults > kubeadm.yaml
- 修改配置文件
yaml
#修改生成好的配置文件镜像与版本
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.169.240.130
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: k8smaster
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers #修改镜像源
kind: ClusterConfiguration
kubernetesVersion: v1.16.2 #修改版本
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16 #pod网络
serviceSubnet: 10.96.0.0/12
scheduler: {}
- 下载kubeadm所需要的镜像
bash
kubeadm config images pull --config kubeadm.yaml
使用docker images 可查看下载的镜像信息
- 使用kubeadm 初始化master节点
bash
kubeadm init --config kubeadm.yaml
提示以下信息则表示初始化成功
bash
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.240.130:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:1877573f37b7fd0bc9d8e38cf8d68f17f51e64a55606f7b657f3cdba79469f16
- 按照提示创建目录
bash
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看集群节点状态
bash
kubectl get nodes
此时只有master节点是NotReady状态
- 将两台node节点 加入到master节点中去(kubeadm join 是在master节点上初始化配置时生成的)
bash
kubeadm join 192.168.240.130:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:1877573f37b7fd0bc9d8e38cf8d68f17f51e64a55606f7b657f3cdba79469f16
再次查看master节点信息
- 安装flannel网络插件
bash
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
编辑kube-flannel.yml
改成虚拟机的网卡名称
- 下载flannel镜像
bash
docker pull quay.io/coreos/flannel:v0.11.0-amd64
kubectl create -f kube-flannel.yml
- 查看集群节点状态
bash
kubectl get pods -A
- master节点的组件
- api-server: api-server是主节点对外开发api的组件,同时提供restful方式,也可以用kubectl方式。
- etcd: 数据存储组件,api-server会将收到的指令存储到etcd中。
- sched :负责接收指令,然后选举合适的node节点,通知节点进行容器的启停、负载均衡等。
- cm :主节点控制中心,controller-manager。
- node节点的组件
- kubelet : 用于管理节点中的容器启停,挂载等等。
- kube-proxy : 用于节点间的网络通信、流量转发等。
- pod : pod是k8s中的最小调度单位,一个pod中可以包含至少1个容器。多个容器组成了一个pod。