2021-11-14 15:52:46 +08:00

164 lines
4.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Kubeadm"
date: 2019-10-30T11:19:06+08:00
lastmod: 2019-10-30T11:19:06+08:00
tags: ["kubernetes", "k8s", "kubeadm"]
categories: ["container"]
---
## kubeadm 安装 kubernetes
### 全部服务器配置
IP 地址 | 主机名 | 操作系统 | 内存 | swap | 硬盘 | Internet | firewalld | selinux | /etc/hosts 增加行
--------- | -------- | -------- | ---- | ---- | ---- | -------- | --------- | ------- | ------------------
10.0.2.80 | master80 | CentOS7 | 4GB | 关闭 | 20GB | 可达 | 关闭 | 关闭 | 127.0.0.1 master80
10.0.2.81 | node81 | CentOS7 | 2GB | 关闭 | 20GB | 可达 | 关闭 | 关闭 | 127.0.0.1 node81
10.0.2.82 | node82 | CentOS7 | 2GB | 关闭 | 20GB | 可达 | 关闭 | 关闭 | 127.0.0.1 node82
- 确认各服务器工作网卡的 MAC 和 UUID 均不相同
```bash
#Ovirt 从模板创建虚拟机可忽略此步骤
#不确定时可参考如下命令
rm -rf /etc/udev/rules.d/70-*
sed -i -e '/HWADDR/d' -e '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-{eth,enp}*
```
- 配置好 IP确认各服务器网络互连且可连互联网
```bash
#10.0.2.80
sed -i 's/10.0.2.127/10.0.2.80/' /etc/sysconfig/network-scripts/ifcfg-eth0
#10.0.2.81
sed -i 's/10.0.2.127/10.0.2.81/' /etc/sysconfig/network-scripts/ifcfg-eth0
#10.0.2.82
sed -i 's/10.0.2.127/10.0.2.82/' /etc/sysconfig/network-scripts/ifcfg-eth0
```
- 关闭各服务器的防火墙
```bash
systemctl stop firewalld
systemctl disable firewalld
```
- 关闭各服务器的 selinux
```bash
setenforce 0
sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
```
- 关闭各服务器的 swap
```bash
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
```
- 安装 ebtables 和 ethtool
```bash
yum install ebtables ethtool
```
- 配置各服务器的 hostname
```bash
#10.0.2.80
hostnamectl set-hostname master80
echo '127.0.0.1 master80' >> /etc/hosts
#10.0.2.81
hostnamectl set-hostname node81
echo '127.0.0.1 node81' >> /etc/hosts
#10.0.2.82
hostnamectl set-hostname node82
echo '127.0.0.1 node82' >> /etc/hosts
```
- 重启各服务器
```bash
reboot
```
### 安装 docker
- 各服务器安装 docker
```bash
yum install docker
```
- 各服务器配置 iptables 转发
```bash
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
```
- 各服务器配置 docker 本地仓库(可选)
```bash
cat <<EOF > /etc/docker/daemon.json
{
"insecure-registries":["10.0.16.125:5080"]
}
EOF
```
- 各服务器启动 docker
```bash
systemctl enable docker && systemctl start docker
```
### 安装 kubernetes
- 各服务器配置 kubernetes yum 源
```bash
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
```
- 各服务器安装 kubeadm
```bash
yum install -y kubelet kubeadm kubectl
```
- 各服务器启动 kubelet
```bash
systemctl enable kubelet && systemctl start kubelet
```
### 在 master80 服务器上安装 kubernetes master 服务组件
- 初始化 kubeadm
```bash
kubeadm init --pod-network-cidr=192.168.0.0/16 --token-ttl 0
mkdir -p /root/.kube
cp -i /etc/kubernetes/admin.conf /root/.kube/config
#记录下输出的最后一行,类似如下
#kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>
```
- 安装 Calico 网络插件
```bash
kubectl apply -f http://docs.projectcalico.org/v2.4/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml
```
### 加入其他节点
- 在 node81 和 node82 服务器上执行以下命令,即 master80 服务器 'kuberadm init' 命令的最后一行输出
```bash
kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>
```
- 在 master80 服务器查看节点和 pod 情况
```bash
kubectl get pods --all-namespaces
kubectl get nodes
```