# redhat 7.4部署K8s
[TOC]
## 1、安裝centos源
> 三臺機器初始化環境,參照文檔: [安裝centos源](http://note.youdao.com/noteshare?id=87b2c1bd5c0c7321632ca9fbaa2d6823&sub=A2C6411F449D42F98A6EFEB5345AD89C)
最后驗證結果:
```
[root@smart-bus-cloud yum.repos.d]# yum repolist enabled
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
* elrepo: mirror.rackspace.com
repo id repo name status
base CentOS-7 - Base - 163.com 10,072
elrepo ELRepo.org Community Enterprise Linux Repository - el7 105
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,490
extras CentOS-7 - Extras - 163.com 448
rhel7 rhel7 4,986
updates CentOS-7 - Updates - 163.com 1,155
repolist: 30,256
```
## 2、使用kubeadm安裝kubernetes_v1.19.x
### 2.1、 配置要求
- 至少2臺 2核4G 的服務器
- Cent OS 7.6 / 7.7 / 7.8
#### 安裝后的軟件版本為
- Kubernetes v1.19.x
- calico 3.13.1
- nginx-ingress 1.5.5
- Docker 19.03.11
### 2.2、 檢查 centos / hostname
```
# 在 master 節點和 worker 節點都要執行
cat /etc/redhat-release
# 此處 hostname 的輸出將會是該機器在 Kubernetes 集群中的節點名字
# 不能使用 localhost 作為節點的名字
hostname
# 請使用 lscpu 命令,核對 CPU 信息
# Architecture: x86_64 本安裝文檔不支持 arm 架構
# CPU(s): 2 CPU 內核數量不能低于 2
lscpu
```
>修改 hostname
如果您需要修改 hostname,可執行如下指令:
```
# 修改 hostname
hostnamectl set-hostname your-new-host-name
# 查看修改結果
hostnamectl status
# 設置 hostname 解析
echo "127.0.0.1 $(hostname)" >> /etc/hosts
```
### 2.3、 檢查網絡
>在所有節點執行命令
```
[root@smart-bus-cloud ~]# ip route show
default via 10.91.123.254 dev ens192 proto static metric 100
10.91.123.0/24 dev ens192 proto kernel scope link src 10.91.123.40 metric 100
[root@smart-bus-cloud ~]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:99:38:fb brd ff:ff:ff:ff:ff:ff
inet 10.91.123.40/24 brd 10.91.123.255 scope global ens192
valid_lft forever preferred_lft forever
```
> kubelet使用的IP地址
> - `ip route show` 命令中,可以知道機器的默認網卡,本機是 ` ens192` ,如 default via 10.91.123.254 dev ens192
> - ` ip address` 命令中,可顯示默認網卡的 IP 地址,Kubernetes 將使用此 IP 地址與集群內的其他節點通信,如 ` 10.91.123.40`
> - 所有節點上 Kubernetes 所使用的 IP 地址必須可以互通(無需 NAT 映射、無安全組或防火墻隔離)
### 2.4 安裝docker及kubelet
>使用 root 身份在所有節點執行如下代碼,以安裝軟件:
> - docker
> - nfs-utils
> - kubectl / kubeadm / kubelet
**請將腳本最后的 1.19.2 替換成您需要的版本號,** <font color='red'> 腳本中間的 v1.19.x 不要替換 </font>
#### 2.4.1快速安裝
> docker hub 鏡像請根據自己網絡的情況任選一個
> - 第四行為騰訊云 docker hub 鏡像
> - 第六行為DaoCloud docker hub 鏡像
> - 第八行為華為云 docker hub 鏡像
> - 第十行為阿里云 docker hub 鏡像
```
# 在 master 節點和 worker 節點都要執行
# 最后一個參數 1.19.5 用于指定 kubenetes 版本,支持所有 1.19.x 版本的安裝
# 騰訊云 docker hub 鏡像
# export REGISTRY_MIRROR="https://mirror.ccs.tencentyun.com"
# DaoCloud 鏡像
# export REGISTRY_MIRROR="http://f1361db2.m.daocloud.io"
# 華為云鏡像
# export REGISTRY_MIRROR="https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com"
# 阿里云 docker hub 鏡像
export REGISTRY_MIRROR=https://registry.cn-hangzhou.aliyuncs.com
curl -sSL https://kuboard.cn/install-script/v1.19.x/install_kubelet.sh | sh -s 1.19.2
```
> 安裝成功結果如下:

#### 2.4.2 手動安裝
**手動執行以下代碼,結果與快速安裝相同。** <font color='red'> 請將腳本)的 ${1} 替換成您需要的版本號,例如 1.19.2 </font>
> docker hub 鏡像請根據自己網絡的情況任選一個
> - 第四行為騰訊云 docker hub 鏡像
> - 第六行為DaoCloud docker hub 鏡像
> - 第八行為阿里云 docker hub 鏡像
```
# 在 master 節點和 worker 節點都要執行
# 最后一個參數 1.19.5 用于指定 kubenetes 版本,支持所有 1.19.x 版本的安裝
# 騰訊云 docker hub 鏡像
# export REGISTRY_MIRROR="https://mirror.ccs.tencentyun.com"
# DaoCloud 鏡像
# export REGISTRY_MIRROR="http://f1361db2.m.daocloud.io"
# 阿里云 docker hub 鏡像
export REGISTRY_MIRROR=https://registry.cn-hangzhou.aliyuncs.com
```
```
#!/bin/bash
# 在 master 節點和 worker 節點都要執行
# 安裝 docker
# 參考文檔如下
# https://docs.docker.com/install/linux/docker-ce/centos/
# https://docs.docker.com/install/linux/linux-postinstall/
# 卸載舊版本
yum remove -y docker \
docker-client \
docker-client-latest \
docker-ce-cli \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
# 設置 yum repository
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安裝并啟動 docker
yum install -y docker-ce-19.03.11 docker-ce-cli-19.03.11 containerd.io-1.2.13
mkdir /etc/docker || true
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["${REGISTRY_MIRROR}"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
mkdir -p /etc/systemd/system/docker.service.d
# Restart Docker
systemctl daemon-reload
systemctl enable docker
systemctl restart docker
# 安裝 nfs-utils
# 必須先安裝 nfs-utils 才能掛載 nfs 網絡存儲
yum install -y nfs-utils
yum install -y wget
# 關閉 防火墻
systemctl stop firewalld
systemctl disable firewalld
# 關閉 SeLinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
# 關閉 swap
swapoff -a
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap > /etc/fstab
# 修改 /etc/sysctl.conf
# 如果有配置,則修改
sed -i "s#^net.ipv4.ip_forward.*#net.ipv4.ip_forward=1#g" /etc/sysctl.conf
sed -i "s#^net.bridge.bridge-nf-call-ip6tables.*#net.bridge.bridge-nf-call-ip6tables=1#g" /etc/sysctl.conf
sed -i "s#^net.bridge.bridge-nf-call-iptables.*#net.bridge.bridge-nf-call-iptables=1#g" /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.all.disable_ipv6.*#net.ipv6.conf.all.disable_ipv6=1#g" /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.default.disable_ipv6.*#net.ipv6.conf.default.disable_ipv6=1#g" /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.lo.disable_ipv6.*#net.ipv6.conf.lo.disable_ipv6=1#g" /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.all.forwarding.*#net.ipv6.conf.all.forwarding=1#g" /etc/sysctl.conf
# 可能沒有,追加
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf
# 執行命令以應用
sysctl -p
# 配置K8S的yum源
cat <<EOF > /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 remove -y kubelet kubeadm kubectl
# 安裝kubelet、kubeadm、kubectl
# 將 ${1} 替換為 kubernetes 版本號,例如 1.19.0
yum install -y kubelet-${1} kubeadm-${1} kubectl-${1}
# 重啟 docker,并啟動 kubelet
systemctl daemon-reload
systemctl restart docker
systemctl enable kubelet && systemctl start kubelet
docker version
```
### 2.5 初始化 master 節點
> **關于初始化時用到的環境變量**
> - <font color='red'> APISERVER_NAME 不能是 master 的 hostname </font>
> - <font color='red'> APISERVER_NAME必須全為小寫字母、數字、小數點,不能包含減號 </font>
> - <font color='red'> POD_SUBNET 所使用的網段不能與 master節點/worker節點 所在的網段重疊。該字段的取值為一個 CIDR 值,如果您對 CIDR 這個概念還不熟悉,請仍然執行 export POD_SUBNET=10.100.0.1/16 命令,不做修改 </font>
#### 2.5.1 快速初始化
> **請將腳本最后的 1.19.5 替換成您需要的版本號,** <font color='red'> 腳本中間的 v1.19.2 不要替換 </font>
```
# 只在 master 節點執行
# 替換 x.x.x.x 為 master 節點實際 IP(請使用內網 IP)
# export 命令只在當前 shell 會話中有效,開啟新的 shell 窗口后,如果要繼續安裝過程,請重新執行此處的 export 命令
export MASTER_IP=10.91.123.40
# 替換 smartbuscloud.demo 為 您想要的 dnsName
export APISERVER_NAME=smartbuscloud.demo
# Kubernetes 容器組所在的網段,該網段安裝完成后,由 kubernetes 創建,事先并不存在于您的物理網絡中
export POD_SUBNET=10.100.1.1/16
echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts
curl -sSL https://kuboard.cn/install-script/v1.19.x/init_master.sh | sh -s 1.19.2
```
#### 2.5.2 手動初始化
#### 2.5.3 檢查 master 初始化結果
```
# 只在 master 節點執行
# 執行如下命令,等待 3-10 分鐘,直到所有的容器組處于 Running 狀態
watch kubectl get pod -n kube-system -o wide
# 查看 master 節點初始化結果
kubectl get nodes -o wide
```
### 2.6 初始化 worker節點
#### 2.6.1 獲得 join命令參數
> 在 **master 節點上執行**
```
# 只在 master 節點執行
kubeadm token create --print-join-command
```
> 可獲取kubeadm join 命令及參數,如下所示
```
kubeadm join smartbuscloud.demo:6443 --token v86kcm.9c6a0jo71jqc2iso --discovery-token-ca-cert-hash sha256:b5257fd9f93fd03290781e85a65d0540306f8b6a9e06be96323ffb0062a67859
```
> **有效時間**
> 該 token 的有效時間為 2 個小時,2小時內,您可以使用此 token 初始化任意數量的 worker 節點。
#### 2.6.2 初始化worker
> 針對所有的 worker 節點執行
```
# 只在 worker 節點執行
# 替換 x.x.x.x 為 master 節點的內網 IP
export MASTER_IP=10.91.123.40
# 替換 apiserver.demo 為初始化 master 節點時所使用的 APISERVER_NAME
export APISERVER_NAME=smartbuscloud.demo
echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts
# 替換為 master 節點上 kubeadm token create 命令的輸出
kubeadm join smartbuscloud.demo:6443 --token v86kcm.9c6a0jo71jqc2iso --discovery-token-ca-cert-hash sha256:b5257fd9f93fd03290781e85a65d0540306f8b6a9e06be96323ffb0062a67859
```
檢查work節點安裝情況
```
[root@smart-bus-cloud-1 ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.2", GitCommit:"f5743093fd1c663cb0cbc89748f730662345d44d", GitTreeState:"clean", BuildDate:"2020-09-16T13:41:02Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
```
> 碰到問題:
==**The** connection to the server localhost:8080 was refused - did you specify the right host or port?==
```
# 將主節點(master節點)中的【/etc/kubernetes/admin.conf】文件拷貝到從節點相同目錄下:
# 配置環境變量
export KUBECONFIG=/etc/kubernetes/admin.conf
```
### 2.7 安裝 Kuboard v2
#### 2.7.1 安裝
```
kubectl apply -f https://kuboard.cn/install-script/kuboard.yaml
kubectl apply -f https://addons.kuboard.cn/metrics-server/0.3.7/metrics-server.yaml
```
> 查看 Kuboard 運行狀態:
```
kubectl get pods -l k8s.kuboard.cn/name=kuboard -n kube-system
```
#### 2.7.2 獲取Token
> 擁有的權限
> - 此Token擁有 ClusterAdmin 的權限,可以執行所有操作
```
[root@smart-bus-cloud ~]# echo $(kubectl -n kube-system get secret $(kubectl -n kube-system get secret | grep kuboard-user | awk '{print $1}') -o go-template='{{.data.token}}' | base64 -d)
eyJhbGciOiJSUzI1NiIsImtpZCI6Ino0a0Y0a1VkUm44Z2dZUU5HVmZrRDlHNncxLW5DUGc4dFE4LS1EbmxSdEUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4tNnZsZ24iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNGQ1OGQ0YjctOTFhZi00YzhkLTk0ZWItOTRkOWNlMTE3NjQ2Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.umkDDtH4TYIaNakZJ7VVb28LllhufmZPA-ZN8U35iB7wAAC4a2ZelTuCws9xTCpqR1TudXg79dm0xsuAFqn9jsPXBP98zWeYE7T5nbAjSqt3SIfqI9WEIXrCHkygWSazfS_CO6YO2e4_XecIOysEVu6fkN-c5T1W_IlVPL6EKCkDhOaNFqvBdSzyf2GJLv6K6whRocwKOmjLhfBN-H-SnO_TT-PFacbwUT-ebkNKheCQvvMhBVfcGW-9CaSYHjLJYK7aQcaq6_IWCC0fRAmoSIVfvxIArske1XhpabNXOhAMSe5uaotj2YaPz0xu1OeKY-GrY0mXaZCUVqw-FKI_yQ
```
#### 2.7.3 訪問Kuboard
Kuboard Service 使用了 NodePort 的方式暴露服務,NodePort 為 32567;您可以按如下方式訪問 Kuboard。
> http://任意一個Worker節點的IP地址:32567/
> 如:http://10.91.123.40:32567/
### 問題
#### 工作節點不正常
```
[root@smart-bus-cloud ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
smart-bus-cloud Ready master 111m v1.19.2
smart-bus-cloud-1 NotReady <none> 72m v1.19.2
smart-bus-cloud-2 Ready <none> 83m v1.19.2
```
#### 重啟所有節點docker,問題解決
```
[root@smart-bus-cloud ~]# systemctl restart docker.service
```
鏡像私庫地址:http://10.91.123.5/harbor/projects/3/repository