kubernetes官網(wǎng)
:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#installing-kubeadm-kubelet-and-kubectl
GitHub
:https://github.com/kubernetes/kubeadm
本文
:使用kubeadm搭建一個3臺機器組成的k8s集群,1臺master節(jié)點,2臺worker節(jié)點。
一、環(huán)境資源準備
1.1、版本統(tǒng)一
由于k8s安裝較麻煩,為防止出現(xiàn)其他異常,特此統(tǒng)一下安裝的軟件版本。
Docker 18.09.0
---
kubeadm-1.14.0-0
kubelet-1.14.0-0
kubectl-1.14.0-0
---
k8s.gcr.io/kube-apiserver:v1.14.0
k8s.gcr.io/kube-controller-manager:v1.14.0
k8s.gcr.io/kube-scheduler:v1.14.0
k8s.gcr.io/kube-proxy:v1.14.0
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
---
calico:v3.9
1.2、k8s環(huán)境系統(tǒng)要求
配置要求
:
一臺或多臺機器運行其中之一
- K8S需要運行在以下一臺或多臺機器中:
- Ubuntu 16.04+
- Debian 9+
- CentOS 7 【本文使用】
- Red Hat Enterprise Linux (RHEL) 7
- Fedora 25+
- HypriotOS v1.0.1+
- Container Linux (tested with 1800.6.0)
- 每臺機器至少有2GB或更多的內(nèi)存(少于2GB的空間,其他應用就無法使用了)
- 2個或更多cpu
- 所有網(wǎng)絡節(jié)點互聯(lián)(互相ping通)
- 每個節(jié)點具有唯一主機名、MAC地址和product_uuid
- 關閉防火墻或開放某些互聯(lián)端口
- 為了使kubelet正常工作,必須禁用swap.
1.3、準備三臺Centos7虛擬機
(1)保證3臺虛擬機的Mac地址唯一
需同時啟動3臺虛擬機,故應保證3臺虛擬機的Mac地址唯一。
(2)虛擬機配置
節(jié)點角色 | IP | 處理器內(nèi)核總數(shù) | 內(nèi)存(GB) |
---|---|---|---|
K8S_master(Centos7) | 192.168.116.170 | 2 | 2 |
K8S_worker01(Centos7) | 192.168.116.171 | 2 | 2 |
K8S_worker02(Centos7) | 192.168.116.172 | 2 | 2 |
可參考下圖:
(3)保證各節(jié)點互相Ping通
二、集群搭建
2.1、更新yum,并安裝依賴包
注意:(3臺機器均要執(zhí)行
)
注: 需聯(lián)網(wǎng),時間較長。
(1)更新yum
[root@localhost ~]# yum -y update
(2)安裝依賴包
[root@localhost ~]# yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
2.2、安裝Docker
注意:(3臺機器均要執(zhí)行
)
在每一臺機器上都安裝好Docker,版本為18.09.0
(0)卸載之前安裝的docker
[root@localhost /]# sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
(1)安裝必要的依賴
[root@localhost ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
(2)設置docker倉庫,添加軟件源(阿里源)
[root@localhost ~]# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(3)設置ustc鏡像加速器(強烈建議
)
注: 如果沒有 /etc/docker/daemon.json,需要手動創(chuàng)建。
① 創(chuàng)建文件夾(/etc/docker)
[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]#
[root@localhost ~]# vi /etc/docker/daemon.json
② 創(chuàng)建并編輯daemon.json(內(nèi)容如下)
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
③ 重新加載生效
[root@localhost ~]# sudo systemctl daemon-reload
也可參考博文:Docker設置ustc的鏡像源(鏡像加速器:修改/etc/docker/daemon.json文件)
(4)安裝指定版本docker(v18.09.0)
① 列出可用的docker版本(排序:從高到低)
[root@localhost /]# yum list docker-ce --showduplicates | sort -r
② 安裝指定版本(這里安裝版本v18.09.0
),需聯(lián)網(wǎng)
[root@localhost /]# sudo yum -y install docker-ce-18.09.0 docker-ce-cli-18.09.0 containerd.io
(5)啟動Docker
提示: 安裝完成后,Docker默認創(chuàng)建好了docker用戶組,但該用戶組下沒有用戶。
啟動命令:
[root@localhost /]# sudo systemctl start docker
[root@localhost ~]#
[root@localhost ~]# ps -ef | grep docker
[root@localhost ~]#
或
啟動docker并設置開機啟動(推薦
):
[root@localhost /]# sudo systemctl start docker && sudo systemctl enable docker
[root@localhost ~]#
[root@localhost ~]# ps -ef | grep docker
[root@localhost ~]#
附:停止docker命令
systemctl stop docker
(6)測試Docker是否安裝成功
提示: 通過運行 hello-world映像來驗證是否正確安裝了 Docker Engine-Community。
運行“hello-world”容器測試:
[root@localhost /]# sudo docker run hello-world
如上圖即已安裝成功。
2.3、設置hostname,修改hosts文件
注意:(3臺機器均要執(zhí)行
)
hosts文件:用于解析計算機名稱和IP地址的映射關系,功能相當于windows下面的c:\windows\system32\drivers\etc\hosts文件,如果想使用計算機名稱來訪問對方的主機,需要把對方計算機的名稱和IP地址寫到本機的hosts文件中,或使用配置DNS。
(1)對master
節(jié)點主機操作
① 設置master虛擬機的hostname為m
[root@localhost /]# sudo hostnamectl set-hostname m
注: 使用
hostname
或uname -n
命令,可查看當前主機名:如:[root@localhost ~]# hostname
② 修改hosts文件
在/etc/hosts文件內(nèi)容末尾 追加 IP和主機名映射。
[root@localhost /]# vi /etc/hosts
附:/etc/hosts中追加內(nèi)容如下:
192.168.116.170 m
192.168.116.171 w1
192.168.116.172 w2
(2)對worker01
節(jié)點主機操作
① 設置worker01虛擬機的hostname為w1
[root@localhost /]# sudo hostnamectl set-hostname w1
注: 使用
hostname
或uname -n
命令,可查看當前主機名:如:[root@localhost ~]# hostname
② 修改hosts文件
在/etc/hosts文件內(nèi)容末尾 追加 IP和主機名映射。
[root@localhost /]# vi /etc/hosts
附:/etc/hosts中追加內(nèi)容如下:
192.168.116.170 m
192.168.116.171 w1
192.168.116.172 w2
(3)對worker02
節(jié)點主機操作
① 設置worker02虛擬機的hostname為w2
[root@localhost /]# sudo hostnamectl set-hostname w2
注: 使用
hostname
或uname -n
命令,可查看當前主機名:如:[root@localhost ~]# hostname
② 修改hosts文件
在/etc/hosts文件內(nèi)容末尾 追加 IP和主機名映射。
[root@localhost /]# vi /etc/hosts
附:/etc/hosts中追加內(nèi)容如下:
192.168.116.170 m
192.168.116.171 w1
192.168.116.172 w2
(3)使用ping命令互測三臺虛擬機的網(wǎng)絡聯(lián)通狀態(tài)
[root@localhost ~]# ping m
[root@localhost ~]# ping w1
[root@localhost ~]# ping w2
2.4、設置k8s的系統(tǒng)要求
注意:(3臺機器均要執(zhí)行
)
(1)關閉防火墻(永久關閉)
[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld
(2)關閉selinux
SELinux,Security Enhanced Linux 的縮寫,也就是安全強化的 Linux,是由美國國家安全局(NSA)聯(lián)合其他安全機構(比如 SCC 公司)共同開發(fā)的,旨在增強傳統(tǒng) Linux 操作系統(tǒng)的安全性,解決傳統(tǒng) Linux 系統(tǒng)中自主訪問控制(DAC)系統(tǒng)中的各種權限問題(如 root 權限過高等)。
依次執(zhí)行命令:
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
(3)關閉swap
Swap分區(qū)在系統(tǒng)的物理內(nèi)存不夠用的時候,把硬盤內(nèi)存中的一部分空間釋放出來,以供當前運行的程序使用。那些被釋放的空間可能來自一些很長時間沒有什么操作的程序,這些被釋放的空間被臨時保存到Swap分區(qū)中,等到那些程序要運行時,再從Swap分區(qū)中恢復保存的數(shù)據(jù)到內(nèi)存中。
依次執(zhí)行命令:
[root@localhost ~]# swapoff -a
[root@localhost ~]# sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
(4)配置iptables的ACCEPT規(guī)則
[root@localhost ~]# iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
(5)設置系統(tǒng)參數(shù)
注:
cat<<EOF
,以EOF輸入字符作為標準輸入結束。
① 執(zhí)行以下命令
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
② 重新加載配置
[root@localhost ~]# sysctl --system
操作如下圖:
2.5、安裝kubeadm, kubelet and kubectl
注意:(3臺機器均要執(zhí)行
)
(1)配置yum源
注:
cat<<EOF
,以EOF輸入字符作為標準輸入結束。
執(zhí)行以下命令:
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
(2)安裝kubeadm&kubelet&kubectl
依次執(zhí)行命令:
#(1)查看源上的kubeadm版本列表
[root@localhost ~]# yum list kubeadm --showduplicates | sort -r
#(2)安裝kubelet(會安裝依賴kubernetes-cni.x86_64 0:0.7.5-0),切記以下第一步必須
#先安裝kubelet-1.14.0-0,否則先安裝其他會出現(xiàn)各種問題。
#(本人測試:先安裝其他,會默認安裝依賴kubelet-1.20.0-0版本,與我們的要求不符,否則后面初始化master節(jié)點的時候會報錯)
[root@localhost ~]# yum install -y kubelet-1.14.0-0
#(3)安裝kubeadm、kubectl
[root@localhost ~]# yum install -y kubeadm-1.14.0-0 kubectl-1.14.0-0
#(4)查看kubelet版本(保證版本是v1.14.0即完成安裝)
[root@localhost ~]# kubelet --version
Kubernetes v1.14.0
注:卸載安裝或安裝其他版本方法
(1)檢查yum方式已安裝軟件:yum list installed
如檢測到已安裝:kubeadm.x86_64、kubectl.x86_64、kubelet.x86_64、kubernetes-cni.x86_64
(2)移除(卸載)軟件
yum remove kubeadm.x86_64
yum remove kubectl.x86_64
yum remove kubelet.x86_64
yum remove kubernetes-cni.x86_64
(3)docker和k8s設置同一個cgroup
① 編輯/etc/docker/daemon.json內(nèi)容:
[root@localhost ~]# vi /etc/docker/daemon.json
添加內(nèi)容如下:
“exec-opts”: [“native.cgroupdriver=systemd”]
附: daemon.json全部內(nèi)容
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
② 重啟docker
# systemctl daemon-reload
# systemctl restart docker
③ 配置kubelet,啟動kubelet,并設置開機啟動kubelet
Ⅰ、配置kubelet
[root@localhost ~]# sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
報:“
sed:無法讀取 /etc/systemd/system/kubelet.service.d/10-kubeadm.conf:沒有那個文件或目錄
”說明: 報如上問題不用管,屬于正?,F(xiàn)象,請繼續(xù)進行下一步操作。
如果上面路徑不存在,大概率在此路徑:/usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
Ⅱ、啟動kubelet,并設置開機啟動kubelet
[root@localhost ~]# systemctl enable kubelet && systemctl start kubelet
注: 此時使用ps -ef|grep kubelet,查不到任何進程(請先忽略)
2.6、安裝k8s核心組件(proxy、pause、scheduler)
注意:(3臺機器均要執(zhí)行
)
(1)查看kubeadm鏡像,發(fā)現(xiàn)都是國外鏡像
[root@localhost ~]# kubeadm config images list
I0106 02:06:13.785323 101920 version.go:96] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://storage.googleapis.com/kubernetes-release/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
I0106 02:06:13.785667 101920 version.go:97] falling back to the local client version: v1.14.0
k8s.gcr.io/kube-apiserver:v1.14.0
k8s.gcr.io/kube-controller-manager:v1.14.0
k8s.gcr.io/kube-scheduler:v1.14.0
k8s.gcr.io/kube-proxy:v1.14.0
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
[root@localhost ~]#
(2)解決國外鏡像不能訪問的問題
注: 由于國內(nèi)大陸網(wǎng)絡限制,無法獲取國外鏡像;但是如果是香港的服務器,就可以直接獲取。
解決: 由于使用國內(nèi)地址“registry.cn-hangzhou.aliyuncs.com/google_containers ”獲取所有鏡像過程(pull鏡像—>打tag—>刪除原有鏡像),每個鏡像依次操作過程較麻煩,故此
本節(jié)提供以腳本方式運行,從國內(nèi)地址獲取鏡像(如下):
① 創(chuàng)建并編輯kubeadm.sh腳本
說明: 在當前目錄創(chuàng)建腳本文件即可,目的只為執(zhí)行下載鏡像而已。
執(zhí)行命令:
[root@localhost ~]# vi kubeadm.sh
腳本內(nèi)容:
#!/bin/bash
set -e
KUBE_VERSION=v1.14.0
KUBE_PAUSE_VERSION=3.1
ETCD_VERSION=3.3.10
CORE_DNS_VERSION=1.3.1
GCR_URL=k8s.gcr.io
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION})
for imageName in ${images[@]} ; do
docker pull $ALIYUN_URL/$imageName
docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done
② 執(zhí)行腳本kubeadm.sh
[root@localhost ~]# sh ./kubeadm.sh
說明:
(1)執(zhí)行腳本命令“sh ./kubeadm.sh”過程中可能報錯:
Error response from daemon: Get https://registry.cn-hangzhou.aliyuncs.com/v2/: dial tcp: lookup registry.cn-hangzhou.aliyuncs.com on 8.8.8.8:53: read udp 192.168.116.170:51212->8.8.8.8:53: i/o timeout
(2)執(zhí)行腳本命令“sh ./kubeadm.sh”過程中語法或符號報錯:
[root@m xiao]# sh ./kubeadm.sh
./kubeadm.sh: line 2: $‘\r’: command not found
: invalid optionne 3: set: -
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …]
[root@m xiao]#【解決方法】去除Shell腳本的\r字符,執(zhí)行命令:
[root@m xiao]# sed -i ‘s/\r//’ kubeadm.sh
個人測試:
(1)先執(zhí)行下載其中一個鏡像進行測試,如下載kube-proxy,執(zhí)行:dockerpull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.14.0
,能下載即可。
(2)再執(zhí)行腳本:sh ./kubeadm.sh
實際執(zhí)行過程:
[root@m xiao]# sh kubeadm.sh
v1.24.0: Pulling from google_containers/kube-proxy
Digest: sha256:c957d602267fa61082ab8847914b2118955d0739d592cc7b01e278513478d6a8
Status: Image is up to date for registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.24.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy@sha256:c957d602267fa61082ab8847914b2118955d0739d592cc7b01e278513478d6a8
v1.24.0: Pulling from google_containers/kube-scheduler
36698cfa5275: Pull complete
fe7d6916a3e1: Pull complete
a47c83d2acf4: Pull complete
Digest: sha256:db842a7c431fd51db7e1911f6d1df27a7b6b6963ceda24852b654d2cd535b776
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.24.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler@sha256:db842a7c431fd51db7e1911f6d1df27a7b6b6963ceda24852b654d2cd535b776
v1.24.0: Pulling from google_containers/kube-controller-manager
36698cfa5275: Already exists
fe7d6916a3e1: Already exists
45a9a072021e: Pull complete
Digest: sha256:df044a154e79a18f749d3cd9d958c3edde2b6a00c815176472002b7bbf956637
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.24.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager@sha256:df044a154e79a18f749d3cd9d958c3edde2b6a00c815176472002b7bbf956637
v1.24.0: Pulling from google_containers/kube-apiserver
36698cfa5275: Already exists
fe7d6916a3e1: Already exists
8ebfef20cea7: Pull complete
Digest: sha256:a04522b882e919de6141b47d72393fb01226c78e7388400f966198222558c955
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.24.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.24.0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver@sha256:a04522b882e919de6141b47d72393fb01226c78e7388400f966198222558c955
3.7: Pulling from google_containers/pause
7582c2cc65ef: Pull complete
Digest: sha256:bb6ed397957e9ca7c65ada0db5c5d1c707c9c8afc80a94acbe69f3ae76988f0c
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.7
registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.7
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.7
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/pause@sha256:bb6ed397957e9ca7c65ada0db5c5d1c707c9c8afc80a94acbe69f3ae76988f0c
3.5.3-0: Pulling from google_containers/etcd
36698cfa5275: Already exists
924f6cbb1ab3: Pull complete
11ade7be2717: Pull complete
8c6339f7974a: Pull complete
d846fbeccd2d: Pull complete
Digest: sha256:13f53ed1d91e2e11aac476ee9a0269fdda6cc4874eba903efd40daf50c55eee5
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.3-0
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.3-0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.3-0
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/etcd@sha256:13f53ed1d91e2e11aac476ee9a0269fdda6cc4874eba903efd40daf50c55eee5
v1.8.6: Pulling from google_containers/coredns
d92bdee79785: Pull complete
6e1b7c06e42d: Pull complete
Digest: sha256:5b6ec0d6de9baaf3e92d0f66cd96a25b9edbce8716f5f15dcd1a616b3abd590e
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.8.6
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.8.6
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.8.6
Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/coredns@sha256:5b6ec0d6de9baaf3e92d0f66cd96a25b9edbce8716f5f15dcd1a616b3abd590e
[root@m xiao]#
③ 查看docker鏡像
[root@localhost ~]# docker images
2.7、kube init 初始化 master
注意:(只在Master節(jié)點執(zhí)行
)
官網(wǎng): https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
(1)初始化集群狀態(tài)(可跳過
)
說明:
1>
正常情況下,請?zhí)^此步驟
。
2> 如果在后續(xù)的步驟中出現(xiàn)錯誤,可進行此步驟操作,然后重新接著下一步執(zhí)行。
3> 執(zhí)行命令:[root@m ~]# kubeadm reset
(2)初始化master節(jié)點
如果出現(xiàn)如下問題,解決方法請參考博文(
3臺機器均要執(zhí)行
):Centos7安裝K8S報錯:[ERROR KubeletVersion]: the kubelet version is higher than the control plane version.執(zhí)行初始化master節(jié)點命令時,我的機器報了一個錯誤:
[ERROR KubeletVersion]: the kubelet version is higher than the control plane version. This is not a supported version skew and may lead to a malfunctional cluster. Kubelet version: "1.20.1" Control plane version: "1.14.0"
執(zhí)行命令(只在Master節(jié)點執(zhí)行
):
[root@m ~]# kubeadm init --v=5 \
--kubernetes-version=1.14.0 \
--apiserver-advertise-address=192.168.116.170 \
--pod-network-cidr=10.244.0.0/16
參數(shù)說明:
–apiserver-advertise-address:
master節(jié)點ip
–v=5會打出詳細檢查日志
–pod-network-cidr=10.244.0.0/16保持不做修改
注意: 修改命令中的ip。
【說明】:如果init過程中還去拉取鏡像,請仔細之前步驟是否已經(jīng)拉取了該鏡像,如果已拉取,只是鏡像名稱不正確,則重新打下tag即可:
格式:docker tag 源鏡像名:版本 新鏡像名:版本
打tag: docker tag k8s.gcr.io/coredns:v1.8.6 k8s.gcr.io/coredns/coredns:v1.8.6
刪除原鏡像: docker rmi k8s.gcr.io/coredns:v1.8.6
當看到如下界面,即初始化成功!??!仔細觀察
初始化的全部過程如下(仔細閱讀,里面有需要繼續(xù)執(zhí)行的命令
):
[root@m ~]# kubeadm init --v=5 \
> --kubernetes-version=1.14.0 \
> --apiserver-advertise-address=192.168.116.170 \
> --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.14.0
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [m kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.116.170]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [m localhost] and IPs [192.168.116.170 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [m localhost] and IPs [192.168.116.170 127.0.0.1 ::1]
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 108.532279 seconds
[upload-config] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.14" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --experimental-upload-certs
[mark-control-plane] Marking the node m as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node m as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: qzgakq.brqfhysglgh1z285
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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.116.170:6443 --token qzgakq.brqfhysglgh1z285 \
--discovery-token-ca-cert-hash sha256:ec5a689ac221ad489452c96730e27814f0faa798b058fbe843f8a47388fe910f
[root@m ~]#
從上述日志中可以看出,etcd、controller、scheduler等組件都以pod的方式安裝成功了。
切記:要留意上述過程中的最后部分(如下圖),后面要用
接著,依次執(zhí)行命令(只在Master節(jié)點執(zhí)行
):
[root@m ~]# mkdir -p $HOME/.kube
[root@m ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@m ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看pod(Master節(jié)點
):
[root@m ~]# kubectl get pods -n kube-system
注意: 如上圖,發(fā)現(xiàn)必須coredns處于
Pending(等待)
狀態(tài),這是由于缺少各節(jié)點互聯(lián)的網(wǎng)絡插件導致,安裝網(wǎng)絡插件參考下一小節(jié)。
(3)安裝calico網(wǎng)絡插件(只在Master節(jié)點
)
選擇網(wǎng)絡插件網(wǎng)址:https://kubernetes.io/docs/concepts/cluster-administration/addons/
這里使用calico網(wǎng)絡插件:https://docs.projectcalico.org/v3.9/getting-started/kubernetes/
提示:最新我安裝的版本為:calico-v3.21
# (1)下載calico.yaml到當前目錄
[root@m ~]# wget https://docs.projectcalico.org/v3.9/manifests/calico.yaml
# (2)查看下calico.yaml中所需的鏡像image
[root@m ~]# cat calico.yaml | grep image
image: calico/cni:v3.9.6
image: calico/cni:v3.9.6
image: calico/pod2daemon-flexvol:v3.9.6
image: calico/node:v3.9.6
image: calico/kube-controllers:v3.9.6
[root@m ~]#
# (3)為后續(xù)操作方便,我們這里先手動pull下所有鏡像
[root@m ~]# docker pull calico/cni:v3.9.3
[root@m ~]# docker pull calico/pod2daemon-flexvol:v3.9.3
[root@m ~]# docker pull calico/node:v3.9.3
[root@m ~]# docker pull calico/kube-controllers:v3.9.3
# (4)應用calico.yaml
[root@m ~]# kubectl apply -f calico.yaml
# (5)不斷查看pod(Master節(jié)點),等會,所有pod會處于Running狀態(tài)即安裝成功(如下圖)
[root@m ~]# kubectl get pods -n kube-system
(4)將worker01節(jié)點和worker02節(jié)點加入master節(jié)點
# (1)在master節(jié)點查看所有node信息
[root@m ~]# kubectl get nodes
# (2)在worker01節(jié)點執(zhí)行(命令來自初始化master節(jié)點的過程日志中)
[root@m ~]# kubeadm join 192.168.116.170:6443 --token qzgakq.brqfhysglgh1z285 \
--discovery-token-ca-cert-hash sha256:ec5a689ac221ad489452c96730e27814f0faa798b058fbe843f8a47388fe910f
# (3)在worker02節(jié)點執(zhí)行(命令來自初始化master節(jié)點的過程日志中)
[root@m ~]# kubeadm join 192.168.116.170:6443 --token qzgakq.brqfhysglgh1z285 \
--discovery-token-ca-cert-hash sha256:ec5a689ac221ad489452c96730e27814f0faa798b058fbe843f8a47388fe910f
# (4)再在master節(jié)點查看所有node信息(等待所有狀態(tài)變?yōu)镽eady,如下圖)-大概需要等待3~5分鐘
[root@m ~]# kubectl get nodes
注:如果join報錯,請看后續(xù)處理異常環(huán)節(jié)。
常用命令:
(1)在master節(jié)點實時查看所有的Pods(前臺監(jiān)控)
kubectl get pods --all-namespaces -w
(2)在master節(jié)點查看所有node信息
kubectl get nodes
.
join異常處理:
.異常詳情:
accepts at most 1 arg(s), received 2
問題分析: 最多接收1個參數(shù),實際2個參數(shù)。問題大概率是join命令格式問題,我是將命令復制到windows上了,又復制粘貼去執(zhí)行(里面換行符導致多一個參數(shù))
解決方法: 刪除換行即可再次執(zhí)行異常詳情:
[preflight] Running pre-flight checks error execution phase preflight: couldn't validate the identity of the API Server: invalid discovery token CA certificate hash: invalid hash "sha256:9f89085f6bfbf60a35935b502a57532204c12a3da3d6412", expected a 32 byte SHA-256 hash, found 23 bytes
問題分析: master節(jié)點的token、sha256均過期了(我是隔天才去執(zhí)行join命令的)
解決方法:
(1)在master節(jié)點重新生成token:
[root@m ~]# kubeadm token create(2)在master節(jié)點重新生成sha256:
[root@m ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed ‘s/^.* //’
(3)在work01、work02節(jié)點重新執(zhí)行join命令:
kubeadm join 192.168.116.201:6443 --tokenfsyl1v.i4cglmc2i0b2d86p
--discovery-token-ca-cert-hash sha256:9f89085f6bfbf60a35935b502a57532204c12a3da3d6412443b7b2c700aa6b3c
(4)再在master節(jié)點查看所有node信息(等待所有狀態(tài)變?yōu)镽eady,如下圖)-大概需要等待3~5分鐘
[root@m ~]# kubectl get nodes
安裝完成?。?!
三、測試(體驗)Pod
(1)定義/創(chuàng)建pod.yml文件
執(zhí)行以下創(chuàng)建pod_nginx_rs.yaml的命令(會在當前目錄創(chuàng)建):文章來源:http://www.zghlxwxcb.cn/news/detail-845724.html
cat > pod_nginx_rs.yaml <<EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
labels:
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
name: nginx
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
EOF
(2)創(chuàng)建pod(根據(jù)pod_nginx_rs.yml創(chuàng)建)
[root@m ~]# kubectl apply -f pod_nginx_rs.yaml
replicaset.apps/nginx created
[root@m ~]#
(3)查看pod
注意: 創(chuàng)建pod需要一定的時間,等待所有pod狀態(tài)均變?yōu)镽unning狀態(tài)即可。文章來源地址http://www.zghlxwxcb.cn/news/detail-845724.html
# 概覽所有pod
[root@m ~]# kubectl get pods
# 概覽所有pod(可看到分布到的集群節(jié)點node)
[root@m ~]# kubectl get pods -o wide
# 查看指定pod詳細信息(這里查看pod:nginx)
[root@m ~]# kubectl describe pod nginx
(4)感受通過rs將pod擴容
#(1)通過rs將pod擴容
[root@m ~]# kubectl scale rs nginx --replicas=5
replicaset.extensions/nginx scaled
[root@m ~]#
#(2)概覽pod(等待所有pod狀態(tài)均變?yōu)镽unning狀態(tài))
[root@m ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-7b465 1/1 Running 0 16m 192.168.190.67 w1 <none> <none>
nginx-fn8rx 1/1 Running 0 16m 192.168.80.194 w2 <none> <none>
nginx-gp44d 1/1 Running 0 26m 192.168.190.65 w1 <none> <none>
nginx-q2cbz 1/1 Running 0 26m 192.168.80.193 w2 <none> <none>
nginx-xw85v 1/1 Running 0 26m 192.168.190.66 w1 <none> <none>
[root@m ~]#
(5)刪除測試pod
[root@m ~]# kubectl delete -f pod_nginx_rs.yaml
replicaset.apps "nginx" deleted
[root@m ~]#
[root@m ~]#
# 查看pod(正在終止中...)
[root@m ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-7b465 1/1 Terminating 0 21m 192.168.190.67 w1 <none> <none>
nginx-fn8rx 1/1 Terminating 0 21m 192.168.80.194 w2 <none> <none>
nginx-gp44d 1/1 Terminating 0 31m 192.168.190.65 w1 <none> <none>
nginx-q2cbz 1/1 Terminating 0 31m 192.168.80.193 w2 <none> <none>
nginx-xw85v 1/1 Terminating 0 31m 192.168.190.66 w1 <none> <none>
# 查看pod(無法查到)
[root@m ~]# kubectl get pods
No resources found in default namespace.
[root@m ~]#
到了這里,關于kubernetes(K8S)學習(一):K8S集群搭建(1 master 2 worker)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!