使用kubeadm在Cent 7安裝K8s Cluster

kubeadm是 Kubernetes 官方推出的部署工具,用來快速安装Kubernetes集群

  • 新用戶可以 kubeadm 開始來試用 Kubernetes。
  • 熟悉 Kubernetes 的用戶可以使用 kubeadm 快速搭建集群並測試他們的應用。

這次使用kubeadm透過初始化 Master 節點來提供給 Node加入
1.準備好VM(這裡使用Centos)
2.安裝Container runtimes(這裡使用Docker )
3.安装 kubeadm
4.加入節點

  1. 我在GCP上準備兩台VM
    jeff-test-k8s-tw-m01 ->master
    jeff-test-k8s-tw-p01 -> Node

2.安裝Container runtimes
照著官方文件操作

# Install Docker CE
## Set up the repository
### Install required packages.
yum install yum-utils device-mapper-persistent-data lvm2

### Add Docker repository.
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

## Install Docker CE.
yum update && yum install docker-ce-18.06.2.ce

## Create /etc/docker directory.
mkdir /etc/docker

# Setup 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

# Restart Docker
systemctl daemon-reload
systemctl restart docker

兩台都安裝好後可以用docker version檢查版本

3.安装 kubeadm
照著官方文件操作
先準備repo檔,然後取消SELinux

#prepare the repo filecat <<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

# Set SELinux in permissive mode (effectively disabling it)
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

systemctl enable --now kubelet

3-1.master node

[root@jeff-test-k8s-tw-m01 yum.repos.d]# kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[root@jeff-test-k8s-tw-m01 yum.repos.d]# kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.1", GitCommit:"d647ddbd755faf07169599a625faf302ffc34458", GitTreeState:"clean", BuildDate:"2019-10-02T17:01:15Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?

由於還沒有設定要連到哪個 k8s 叢集,所以這裡會有 “localhost:8080 was refused …” 的錯誤訊息,可以先忽略它。
https://ithelp.ithome.com.tw/articles/10192748

3–2初始化

[root@jeff-test-k8s-tw-m01 ~]# kubeadm init

3–3執行初始化配置

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

使用docker ps && docker images, 會看到一堆 container 已經在跑還有自己載了一些images

確認node狀態

[root@jeff-test-k8s-tw-m01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
jeff-test-k8s-tw-m01 NotReady master 69s v1.16.1

取得 kube-system pods 狀態,可以看到 coredns 還沒 ready
詳細確認jeff-test-k8s-tw-m01節點資訊

[root@jeff-test-k8s-tw-m01 ~]# kubectl get pods -n kube-system[root@jeff-test-k8s-tw-m01 ~]# kubectl describe node jeff-test-k8s-tw-m01

3–4我們必須自行設定Pod 與 Pod 之間的網路架構,我們需要安裝CNI Plugin
在這裡我是使用Weave Net

$ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

coredns 狀態會變成 running,node 的狀態也變Ready

在master node產生token

[root@jeff-test-k8s-tw-m01 ~]# kubeadm token create --print-join-commandkubeadm join 10.20.0.13:6443 --token 3y9caf.cfzzxrbtdrulmudi     --discovery-token-ca-cert-hash sha256:0c41a17320a39ef31e28b8721e418247a622475b887dc0fa12d09ce543977afd

4 –1
在另一個node上安裝 kubeadm, kubectl, kubelet
然後使用剛剛產生的token加入節點

[root@jeff-test-k8s-tw-p01 ~]# kubeadm join 10.20.0.13:6443 --token 3y9caf.cfzzxrbtdrulmudi     --discovery-token-ca-cert-hash sha256:0c41a17320a39ef31e28b8721e418247a622475b887dc0fa12d09ce543977afd

回到master node確認

[root@jeff-test-k8s-tw-m01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
jeff-test-k8s-tw-m01 Ready master 39m v1.16.1
jeff-test-k8s-tw-p01 Ready <none> 56s v1.16.1

#在安裝的過程中有遇到CoreDNS Pending,運行不起來的狀況
請跑一次kubeadm reset,再初始化一次

Ref:
https://kubernetes.io/zh/docs/setup/independent/install-kubeadm/
https://rickhw.github.io/2019/03/17/Container/Install-K8s-with-Kubeadm
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
https://kubernetes.io/docs/setup/production-environment/container-runtimes/

DevOps engineer