安裝 kubeadm(每臺(tái)機(jī)器上執(zhí)行)
前提準(zhǔn)備
- 一臺(tái)或多臺(tái)運(yùn)行著下列系統(tǒng)的機(jī)器:
- CentOS 7
- 每臺(tái)機(jī)器 2 GB 或更多的 RAM (如果少于這個(gè)數(shù)字將會(huì)影響您應(yīng)用的運(yùn)行內(nèi)存)
- 2 CPU 核心或更多
- 集群中的所有機(jī)器的網(wǎng)絡(luò)彼此均能相互連接(公網(wǎng)和內(nèi)網(wǎng)都可以)
節(jié)點(diǎn)之中不可以有重復(fù)的主機(jī)名往枣,MAC 地址各吨,product_uuid灶体。
- 您可以使用下列命令獲取網(wǎng)絡(luò)接口的 MAC 地址:ip link 或是 ifconfig -a
- 下列命令可以用來(lái)獲取 product_uuid sudo cat /sys/class/dmi/id/product_uuid
一般來(lái)講坞古,硬件設(shè)備會(huì)擁有獨(dú)一無(wú)二的地址送淆,但是有些虛擬機(jī)可能會(huì)雷同辫红。Kubernetes 使用這些值來(lái)唯一確定集群中的節(jié)點(diǎn)锐帜。如果這些值在集群中不唯一,可能會(huì)導(dǎo)致安裝失敗拍冠。
禁用 Swap 交換分區(qū)尿这。為了保證 kubelet 正確運(yùn)行
[root@asdff ~]# sysctl -w vm.swappiness=0
[root@asdff ~]# echo "vm.swappiness = 0">> /etc/sysctl.conf
[root@asdff ~]# swapoff -a
# 查看Swap是否關(guān)閉
[root@asdff ~]# free -m
total used free shared buff/cache available
Mem: 64265 487 62179 0 1598 63244
Swap: 0 0 0
檢查網(wǎng)絡(luò)適配器
如果您有一個(gè)以上的網(wǎng)絡(luò)適配器,同時(shí)您的 Kubernetes 組件通過(guò)默認(rèn)路由不可達(dá)庆杜,我們建議您預(yù)先添加 IP 路由規(guī)則妻味,這樣 Kubernetes 集群就可以通過(guò)對(duì)應(yīng)的適配器完成連接。
檢查所需端口是否被占用
Master 節(jié)點(diǎn)
規(guī)則 | 方向 | 端口范圍 | 作用 | 使用者 |
---|---|---|---|---|
TCP | Inbound | 6443* | Kubernetes API server | All |
TCP | Inbound | 2379-2380 | etcd server client API | kube-apiserver, etcd |
TCP | Inbound | 10250 | Kubelet API | Self, Control plane |
TCP | Inbound | 10251 | kube-scheduler | Self |
TCP | Inbound | 10252 | kube-controller-manager | Self |
Worker 節(jié)點(diǎn)
規(guī)則 | 方向 | 端口范圍 | 作用 | 使用者 |
---|---|---|---|---|
TCP | Inbound | 10250 | Kubelet API | Self, Control plane |
TCP | Inbound | 30000-32767 | NodePort Services** | All |
任何使用 * 標(biāo)記的端口號(hào)都有可能被覆蓋欣福,所以您需要保證您的自定義端口的狀態(tài)是開放的。
雖然主節(jié)點(diǎn)已經(jīng)包含了 etcd 的端口焦履,您也可以使用自定義的外部 etcd 集群拓劝,或是指定自定義端口。 您使用的 pod 網(wǎng)絡(luò)插件 (見下) 也可能需要某些特定端口開啟嘉裤。由于各個(gè) pod 網(wǎng)絡(luò)插件都有所不同郑临,請(qǐng)參閱他們各自文檔中對(duì)端口的要求。
安裝 runtime(每臺(tái)機(jī)器上執(zhí)行)
從 v1.6.0 起屑宠,Kubernetes 開始允許使用 CRI厢洞,容器運(yùn)行時(shí)接口。默認(rèn)的容器運(yùn)行時(shí)是 Docker,這是由 kubelet 內(nèi)置的 CRI 實(shí)現(xiàn) dockershim 開啟的躺翻。
其他的容器運(yùn)行時(shí)有:
- containerd (containerd 的內(nèi)置 CRI 插件)
- cri-o
- frakti
- rkt
CRI 安裝指南丧叽,以下安裝其中一個(gè)即可(Docker、CRI-O公你、Containerd)
Docker
在每臺(tái)機(jī)器上安裝Docker踊淳。建議使用版本18.06.2,但是1.11陕靠、1.12迂尝、1.13、17.03和18.09也可以使用剪芥。在Kubernetes發(fā)布說(shuō)明中跟蹤最新的經(jīng)過(guò)驗(yàn)證的Docker版本垄开。
使用以下命令在系統(tǒng)上安裝Docker:
# 安裝 required packages.
yum install yum-utils device-mapper-persistent-data lvm2
# 設(shè)置 yum repository
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 安裝 Docker CE
yum update && yum install docker-ce-18.06.2.ce
# 創(chuàng)建 /etc/docker 目錄.
mkdir /etc/docker
# 設(shè)置 daemon.
cat > /etc/docker/daemon.json <<EOF
{
"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
# 重啟 Docker
systemctl daemon-reload
systemctl restart docker
# 設(shè)置 Docker 開機(jī)啟動(dòng)
有關(guān)更多信息,請(qǐng)參閱官方Docker安裝指南税肪。
CRI-O
本節(jié)包含將crio安裝為CRI運(yùn)行時(shí)所需的步驟溉躲。
使用以下命令在你的系統(tǒng)上安裝crio:
先決條件
modprobe overlay
modprobe br_netfilter
# 設(shè)置需要的 sysctl 參數(shù),這些參數(shù)會(huì)在重啟時(shí)持續(xù)存在寸认。
cat > /etc/sysctl.d/99-kubernetes-cri.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl --system
安裝 CRI-O
# 安裝 yum 源
yum-config-manager --add-repo=https://cbs.centos.org/repos/paas7-crio-311-candidate/x86_64/os/
# 安裝 CRI-O
yum install --nogpgcheck cri-o
啟動(dòng) CRI-O
# 啟動(dòng) CRI-O
systemctl start crio
# 設(shè)置開機(jī)啟動(dòng)
systemctl enable crio
有關(guān)更多信息签财,請(qǐng)參閱crio安裝指南。
Containerd
本節(jié)包含使用containerd作為CRI運(yùn)行時(shí)的必要步驟偏塞。
使用以下命令在系統(tǒng)上安裝Containerd:
先決條件
modprobe overlay
modprobe br_netfilter
# Setup required sysctl params, these persist across reboots.
cat > /etc/sysctl.d/99-kubernetes-cri.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl --system
安裝 containerd
# 安裝 required packages
yum install yum-utils device-mapper-persistent-data lvm2
# 安裝 docker repository
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 安裝 containerd
yum update && yum install containerd.io
# 配置 containerd
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
# 重啟 containerd
systemctl restart containerd
# 設(shè)置開機(jī)啟動(dòng)
systemctl enable containerd
systemd
要使用 systemd
cgroup 驅(qū)動(dòng)程序, 在 /etc/containerd/config.toml
中設(shè)置 plugins.cri.systemd_cgroup = true
唱蒸。 當(dāng)使用kubeadm 時(shí),還需要為 kubelet 手動(dòng)配置 cgroup 驅(qū)動(dòng)程序:
安裝 kubeadm, kubelet 和 kubectl(每臺(tái)機(jī)器上執(zhí)行)
您需要在每臺(tái)機(jī)器上都安裝以下的軟件包:
- kubeadm: 用來(lái)初始化集群的指令灸叼。
- kubelet: 在集群中的每個(gè)節(jié)點(diǎn)上用來(lái)啟動(dòng) pod 和 container 等神汹。
- kubectl: 用來(lái)與集群通信的命令行工具。
kubeadm 不能 幫您安裝或管理 kubelet 或 kubectl 古今,所以您得保證他們滿足通過(guò) kubeadm 安裝的 Kubernetes 控制層對(duì)版本的要求屁魏。如果版本沒(méi)有滿足要求,就有可能導(dǎo)致一些難以想到的錯(cuò)誤或問(wèn)題捉腥。然而控制層與 kubelet 間的 小版本號(hào) 不一致無(wú)傷大雅氓拼,不過(guò)請(qǐng)記住 kubelet 的版本不可以超過(guò) API server 的版本。例如 1.8.0 的 API server 可以適配 1.7.0 的 kubelet抵碟,反之就不行了桃漾。
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
# 將 SELinux 設(shè)置為 permissive 模式(將其禁用)
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable kubelet && systemctl start kubelet
請(qǐng)注意:
- 通過(guò)命令 setenforce 0 和 sed ... 可以將 SELinux 設(shè)置為 permissive 模式(將其禁用)。 只有執(zhí)行這一操作之后拟逮,容器才能訪問(wèn)宿主的文件系統(tǒng)撬统,進(jìn)而能夠正常使用 Pod 網(wǎng)絡(luò)。您必須這么做敦迄,直到 kubelet 做出升級(jí)支持 SELinux 為止恋追。 - 一些 RHEL/CentOS 7 的用戶曾經(jīng)遇到過(guò):由于 iptables 被繞過(guò)導(dǎo)致網(wǎng)絡(luò)請(qǐng)求被錯(cuò)誤的路由凭迹。您得保證 在您的 sysctl 配置中 net.bridge.bridge-nf-call-iptables 被設(shè)為1。
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
kubelet 現(xiàn)在每隔幾秒就會(huì)重啟苦囱,因?yàn)樗萑肓艘粋€(gè)等待 kubeadm 指令的死循環(huán)嗅绸。
在 Master 節(jié)點(diǎn)上配置 kubelet 所需的 cgroup 驅(qū)動(dòng)(使用 CRI 為 Docker 的情況下,跳過(guò)此步驟)
使用 Docker 時(shí)沿彭,kubeadm 會(huì)自動(dòng)為其檢測(cè) cgroup 驅(qū)動(dòng)在運(yùn)行時(shí)對(duì) /var/lib/kubelet/kubeadm-flags.env 文件進(jìn)行配置朽砰。 如果您使用了不同的 CRI, 您得把 /etc/default/kubelet 文件中的 cgroup-driver 位置改為對(duì)應(yīng)的值喉刘,像這樣:
KUBELET_EXTRA_ARGS=--cgroup-driver=<value>
這個(gè)文件將會(huì)被 kubeadm init 和 kubeadm join 用于為 kubelet 獲取 額外的用戶參數(shù)瞧柔。
請(qǐng)注意,您只需要在您的 cgroup driver 不是 cgroupfs 時(shí)這么做睦裳,因?yàn)?cgroupfs 已經(jīng)是 kubelet 的默認(rèn)值了造锅。
需要重啟 kubelet:
systemctl daemon-reload
systemctl restart kubelet
初始化集群(Master 執(zhí)行)
# 安裝最新版本的 kubernetes
kubeadm init --image-repository registry.aliyuncs.com/google_containers
# 安裝指定版本
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.14.0
執(zhí)行 kubeadm init 命令,日志如下:
[init] Using Kubernetes version: v1.14.0
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[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 "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [izbp1dtcbwezldtyg8ygj7z localhost] and IPs [172.16.66.178 127.0.0.1 ::1]
[certs] Generating "apiserver-etcd-client" 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 [izbp1dtcbwezldtyg8ygj7z localhost] and IPs [172.16.66.178 127.0.0.1 ::1]
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [izbp1dtcbwezldtyg8ygj7z kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.16.66.178]
[certs] Generating "apiserver-kubelet-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
[apiclient] All control plane components are healthy after 15.001963 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 izbp1dtcbwezldtyg8ygj7z as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node izbp1dtcbwezldtyg8ygj7z as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 1zvfgx.nwsxi2lectpro9r6
[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 172.16.66.178:6443 --token 1zvfgx.nwsxi2lectpro9r6 \
--discovery-token-ca-cert-hash sha256:1b68e57e3bf11f4c4d310172a5fdef1863bde64d11f1fc0d1e13ce37147ed4a0
根據(jù)日志提示:“Your Kubernetes control-plane has initialized successfully!”廉邑,表示成功
按照以上提示哥蔚,執(zhí)行以下命令復(fù)制配置文件到普通用戶的 home 目錄下:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
若你是 root 用戶安裝的,執(zhí)行以下命令即可:
export KUBECONFIG=/etc/kubernetes/admin.conf
至此蛛蒙,Master 節(jié)點(diǎn)上 Kubernetes 安裝完畢糙箍。但集群內(nèi)還沒(méi)有可用的 Node 節(jié)點(diǎn),并缺乏容器網(wǎng)絡(luò)配置牵祟。
安裝 Node 節(jié)點(diǎn)深夯,并加入集群(Node 節(jié)點(diǎn)執(zhí)行)
根據(jù)上一步的日志提示執(zhí)行以下命令(Then you can join any number of worker nodes by running the following on each as root:):
# 此命令來(lái)自于 master 節(jié)點(diǎn)執(zhí)行 kubeadm init 命令的日志提示,復(fù)制即可
kubeadm join 172.16.66.178:6443 --token 1zvfgx.nwsxi2lectpro9r6 \
--discovery-token-ca-cert-hash sha256:1b68e57e3bf11f4c4d310172a5fdef1863bde64d11f1fc0d1e13ce37147ed4a0
kubeadm 在 Master 節(jié)點(diǎn)也安裝了 kubelet诺苹,默認(rèn)情況下并不參與工作負(fù)載咕晋。若希望安裝一個(gè)單機(jī) All-In-One 的 Kubernetes 環(huán)境,需刪除 Node 的 Label “node-role-kubernetes.io/master”收奔,執(zhí)行以下命令掌呜,讓 Master 節(jié)點(diǎn)變成 Node 節(jié)點(diǎn):
kubectl taint nodes --all node-role.kubernetes.io/master-
安裝網(wǎng)絡(luò)插件
根據(jù) kubeadm init 命令的日志提示:
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/
網(wǎng)絡(luò)插件有很多選擇,可以參考:https://kubernetes.io/docs/concepts/cluster-administration/addons/ 的說(shuō)明坪哄。
我這里選擇 Weave Net 插件质蕉,執(zhí)行以下命令即可安裝:
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
kubectl 命令自動(dòng)補(bǔ)全功能開啟
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
驗(yàn)證 Kubernetes 集群是否正常
在 Master 節(jié)點(diǎn)執(zhí)行以下命令,以驗(yàn)證 Kubernetes 集群相關(guān) Pod 是否正常創(chuàng)建并運(yùn)行:
# kubectl apply 之后翩肌,相關(guān)的 Pod 需要一定的時(shí)間重啟模暗,過(guò)一會(huì)執(zhí)行即可
kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-8686dcc4fd-bkwdp 1/1 Running 0 22m
kube-system coredns-8686dcc4fd-rt2pm 1/1 Running 0 22m
kube-system etcd-izbp1dtcbwezldtyg8ygj7z 1/1 Running 0 21m
kube-system kube-apiserver-izbp1dtcbwezldtyg8ygj7z 1/1 Running 0 20m
kube-system kube-controller-manager-izbp1dtcbwezldtyg8ygj7z 1/1 Running 0 20m
kube-system kube-proxy-btm7l 1/1 Running 0 8m20s
kube-system kube-proxy-cnd7h 1/1 Running 0 22m
kube-system kube-proxy-kxr6h 1/1 Running 0 8m29s
kube-system kube-scheduler-izbp1dtcbwezldtyg8ygj7z 1/1 Running 0 20m
kube-system weave-net-5f99p 2/2 Running 0 73s
kube-system weave-net-x2fhz 2/2 Running 0 73s
kube-system weave-net-zgsrk 2/2 Running 0 73s
以上 Pod 都為 Running 說(shuō)明成功水援,若發(fā)現(xiàn)有狀態(tài)錯(cuò)誤的 Pod 聋亡,可以執(zhí)行 kubectl --namespace=kube-system describe pod <pod_name> 來(lái)查看錯(cuò)誤原因灾部。
至此,通過(guò) kubeadm 搭建 kubernetes 就完成了棒卷。若安裝失敗顾孽,則可執(zhí)行 kubeadm reset 命令將主機(jī)恢復(fù)至原狀,重新執(zhí)行 kubeadm init 命令再次安裝比规。