K8S集群架构
# K8S集群架构
## 创建三个虚拟机
> 别用自定义安装, 常规安装就可以了
![image.png](https://cos.easydoc.net/13568421/files/lmwsdbr5.png)
![image.png](https://cos.easydoc.net/13568421/files/lmwsffcp.png)
![image.png](https://cos.easydoc.net/13568421/files/lmwsrnoy.png)
![image.png](https://cos.easydoc.net/13568421/files/lmwskcib.png)
![image.png](https://cos.easydoc.net/13568421/files/lmwst87a.png)
![image.png](https://cos.easydoc.net/13568421/files/lmwtbwn7.png)
> **注意: Node节点才需要11G内存, Master节点4G就可以了**
> **创建两个完整克隆, 效果如下**
![image.png](https://cos.easydoc.net/13568421/files/lmwtqfj3.png)
![image.png](https://cos.easydoc.net/13568421/files/lmwtqkeo.png)
> 如果用的是VMWARE, 且按照我上面的步骤, 其实是不需要再配置网络的, 网络已经配置好了
> **原生的网关地址是不一样, MAC地址也是不一样的, 很智能的, 按照整个流程一定能Ping成功**
### 关闭防火墙(全部)
```shell
systemctl stop firewalld
systemctl disable firewalld
```
### 关闭selinux
```shell
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
```
### 关闭swap
```shell
swapoff -a #临时
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久
free -g #验证,swap 必须为 0;
```
### 设置映射关系
#### 更新每个主机的hostname
```shell
hostnamectl set-hostname [新的主机名]
```
![image.png](https://cos.easydoc.net/13568421/files/lmx3z1ph.png)
![image.png](https://cos.easydoc.net/13568421/files/lmx3zich.png)
![image.png](https://cos.easydoc.net/13568421/files/lmx3zxag.png)
```shell
vim /etc/hosts
```
```shell
192.168.10.139 k8s-master
192.168.10.143 k8s-node1
192.168.10.134 k8s-node2
```
> 到此为止, 我可以互相ping通
### 设置iptables
```shell
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
```
## 安装Docker
### 安装Docker-CE
```shell
yum install -y yum-utils device-mapper-persistent-data lvm2
```
### 配置docker repo的yum源
```shell
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
```
### 安装Docker以及Doocker-cli
```shell
sudo yum install -y docker-ce docker-ce-cli containerd.io
```
### 配置Docker加速器
```shell
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://y3iduo85.mirror.aliyuncs.com"]}
EOF
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
```
### 配置阿里yun源
```shell
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[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
```
## 安装 kubeadm,kubelet 和 kubectl
```shell
yum install -y kubelet-1.17.3 kubeadm-1.17.3 kubectl-1.17.3
```
```shell
systemctl enable kubelet
systemctl start kubelet # kubelet没有正常启动是正常的
```
### 初始化master节点
> 注意`--apiserver-advertise-address`后面的是你的主节点的网卡IP
```shell
kubeadm init --apiserver-advertise-address=192.168.10.139 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version v1.17.3 --service-cidr=10.96.0.0/16 --pod-network-cidr=10.244.0.0/16
```
#### 将结果中的令牌赋值下来, 如下所示
```shell
kubeadm join 192.168.10.139:6443 --token 94q34r.7zqr0ihe58xj5alr \
--discovery-token-ca-cert-hash sha256:a152a18d17a248ecfbe6bb98fb68b92339c30feb45d7f7231d8a0af6bd8c148e
```
#### 测试主节点运行
```shell
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
```
![image.png](https://cos.easydoc.net/13568421/files/lmx6u7gl.png)
#### 安装pod网络插件
拷贝kube-flannel.yaml, 并执行
```shell
kubectl apply -f kube-flannel.yml
```
![image.png](https://cos.easydoc.net/13568421/files/lmxhmsy6.png)
##### 问题解决
> 这是因为老师给的kube-flannel文件有问题, 不能用这个文件
> [**正确的flannel文件地址**](https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml), 亲测可行, 或者直接访问我的阿里云OSS
[**flannel文件**](https://bitmall-oss.oss-cn-guangzhou.aliyuncs.com/k8s/yaml/kube-flannel.yml)
#### Node节点加入Master节点
> 这个是之前复制过来的令牌, 在Node节点执行, 即可加入主节点
```shell
kubeadm join 192.168.10.139:6443 --token 94q34r.7zqr0ihe58xj5alr \
--discovery-token-ca-cert-hash sha256:a152a18d17a248ecfbe6bb98fb68b92339c30feb45d7f7231d8a0af6bd8c148e
```
查看pod状态
```shell
watch kubectl get pod -n kube-system -o wide
```
![image.png](https://cos.easydoc.net/13568421/files/lmxjkreg.png)
**集群搭建成功!!!**