1. 程式人生 > 其它 >kubeadmin搭建k8s叢集

kubeadmin搭建k8s叢集

一、準備環境

準備環境:

角色 IP地址
k8s-master 192.168.56.21
k8s-node01 192.168.56.25
k8s-node02 192.168.56.26

所有節點環境初始化:

關閉防火牆:
# systemctl stop firewalld
# systemctl disable firewalld

關閉selinux:
# sed -i 's/enforcing/disabled/' /etc/selinux/config
# setenforce 0

關閉swap:
# swapoff -a
# sed -ri 's/.*swap.*/#&/
' /etc/fstab 按IP分別設定主機名: # hostnamectl set-hostname k8s-master # hostnamectl set-hostname k8s-node01 # hostnamectl set-hostname k8s-node02 僅在master新增hosts: # cat >> /etc/hosts << EOF 192.168.56.21 k8s-master 192.168.56.25 k8s-node01 192.168.56.26 k8s-node02 EOF 將橋接的IPv4流量傳遞到iptables的鏈: # cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge
-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF # sysctl --system 時間同步: # yum install ntpdate -y # ntpdate time.windows.com
# echo "*/5 * * * * /usr/sbin/ntpdate time.windows.com" >>/var/spool/cron/root

二、在所有節點安裝Docker、kubeadm、kubectl、kubelet

2.1 安裝docker

# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# yum install -y yum-utils device-mapper-persistent-data lvm2 # yum -y install docker-ce # systemctl enable docker && systemctl start docker

2.2 配置阿里雲yum源

# cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg 
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF

2.3 三個節點安裝kubeadmin、kubelet,僅在k8s-master節點上安裝kubectl

# yum install -y kubelet-1.19.0 kubeadm-1.19.0 kubectl-1.19.0
# systemctl enable kubelet

三、部署kubernetes master

# kubeadm init \
  --apiserver-advertise-address=192.168.56.21 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.19.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16 \
  --ignore-preflight-errors=all

# mkdir -p $HOME/.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# sudo chown $(id -u):$(id -g) $HOME/.kube/config
#  kubeadm init \
>   --apiserver-advertise-address=192.168.56.21 \
>   --image-repository registry.aliyuncs.com/google_containers \
>   --kubernetes-version v1.19.0 \
>   --service-cidr=10.96.0.0/12 \
>   --pod-network-cidr=10.244.0.0/16 \
>   --ignore-preflight-errors=all
W1005 15:41:47.137328   25574 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.19.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/
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.9. Latest validated version: 19.03
[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'
[certs] Using certificateDir folder "/etc/kubernetes/pki"  # 生成k8s和etcd證書
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.56.21]
[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/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.56.21 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.56.21 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"  # 生成kubeconfig檔案 
[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
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"  # 生成kubelet配置檔案 
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"  # 部署管理節點元件,用映象啟動 kubectl get pod -n kube-system
[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"  # 部署etcd資料庫用映象啟動
[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 14.006837 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace  # 上傳配置檔案到k8s中
[kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"  # 給管理節點打一個標籤
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]  # 將給管理節點打一個汙點
[bootstrap-token] Using token: 46x35t.q5wjyktnsjdeu7gk  # 自動為kubelet頒發證書
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[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
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS  # 部署外掛coredns和kube-proxy
[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:

# 接下來需要在master執行的命令
  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:

# 將node節點加入master的命令 
kubeadm join 192.168.56.21:6443 --token 46x35t.q5wjyktnsjdeu7gk \
    --discovery-token-ca-cert-hash sha256:8d44716e10fc9c16a45cb988cee767f0d4e59f1885e71914cc0ad94ba065aaf4 
kubeadm init 的部署過程及日誌

四、將node節點加入k8s叢集中

]# kubeadm join 192.168.56.21:6443 --token 46x35t.q5wjyktnsjdeu7gk \
>     --discovery-token-ca-cert-hash sha256:8d44716e10fc9c16a45cb988cee767f0d4e59f1885e71914cc0ad94ba065aaf4 
[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/
    [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.9. Latest validated version: 19.03
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

預設token有效期為24小時,當過期之後,該token就不可用了。這時就需要重新建立token,操作如下:

[root@k8s-master ~]# kubeadm token create
W1005 16:00:31.481430   31411 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
i376x3.lw9aepx6ldbzrj5c
[root@k8s-master ~]# kubeadm token list
TOKEN                     TTL         EXPIRES                     USAGES                   DESCRIPTION             EXTRA GROUPS
i376x3.lw9aepx6ldbzrj5c   23h         2021-10-06T16:00:31+08:00   authentication,signing   <none>                  system:bootstrappers:kubeadm:default-node-token
[root@k8s-master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
8d44716e10fc9c16a45cb988cee767f0d4e59f1885e71914cc0ad94ba065aaf4
[root@k8s-master ~]# kubeadm join 192.168.56.21:6443 --token i376x3.lw9aepx6ldbzrj5c \
--discovery-token-ca-cert-hash sha256:8d44716e10fc9c16a45cb988cee767f0d4e59f1885e71914cc0ad94ba065aaf4

或者直接用命令生成也可以

# kubeadm token create --print-join-command

五、部署網路元件(CNI)

此時檢視node節點是NotReady狀態

# kubectl get node
NAME         STATUS     ROLES    AGE   VERSION
k8s-master   NotReady   master   27m   v1.19.0
k8s-node01   NotReady   <none>   12m   v1.19.0
k8s-node02   NotReady   <none>   12m   v1.19.0

https://kubernetes.io/docs/concepts/cluster-administration/addons/

注意:只需要部署下面其中一個,推薦Calico。
Calico是一個純三層的資料中心網路方案,Calico支援廣泛的平臺,包括Kubernetes、OpenStack等。
Calico 在每一個計算節點利用 Linux Kernel 實現了一個高效的虛擬路由器( vRouter) 來負責資料轉發,而每個 vRouter 通過 BGP 協議負責把自己上執行的 workload 的路由資訊向整個 Calico 網路內傳播。

https://docs.projectcalico.org/getting-started/kubernetes/quickstart

# wget https://docs.projectcalico.org/manifests/calico.yaml --no-check-certificate

下載完後還需要修改裡面定義Pod網路(CALICO_IPV4POOL_CIDR),與前面kubeadm init指定的一樣

# vim calico.yaml

修改後

當calico的pod都啟動成功之後,node節點就會變成ready狀態

# kubectl get pod -n kube-system -l k8s-app=calico-node -o wide
NAME                READY   STATUS     RESTARTS   AGE     IP              NODE         NOMINATED NODE   READINESS GATES
calico-node-4dcl8   1/1     Running    0          12m     192.168.56.25   k8s-node01   <none>           <none>
calico-node-kjs86   1/1     Running    0          12m     192.168.56.26   k8s-node02   <none>           <none>

# kubectl get node
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   39m   v1.19.0
k8s-node01   Ready    <none>   24m   v1.19.0
k8s-node02   Ready    <none>   24m   v1.19.0