本文檔介紹搭建Kubernetes集羣,版本爲1.18.5,以前安裝最新版1.18.8時發現Kubernetes安裝因此來的容器在國內沒法下載,而且切換使用阿里或騰訊的安裝源以後仍沒法正常下載,所以更換爲1.18.5版本。html
本文參考文章連接:https://www.cnblogs.com/hellxz/p/use-kubeadm-init-kubernetes-cluster.htmlnode
本文采用Centos 8做爲操做系統,使用虛擬機模擬部署。linux
IP | Hostname | CPU核數 | 內存 | 硬盤 | 說明 |
---|---|---|---|---|---|
192.168.43.130 | master | 2 | 2G | 20G | 控制節點 |
192.168.43.129 | node01 | 2 | 2G | 20G | 執行節點 |
軟件 | 版本號 |
---|---|
CentOS | 8 |
Kubernetes | 1.18.5 |
Docker | 19.03.12 |
說明 | 查看命令 | 修改命令 |
---|---|---|
集羣各節點互通 | ping 192.168.43.129 |
無 |
MAC地址惟一 | ip link 或 ifconfig -a |
請參考下面命令1 |
集羣內主機名惟一 | hostnamectl status |
hostnamectl set-hostname <hostname> |
系統產品uuid惟一 | dmidecode -s system-uuid |
請參考網上修改方法 |
# 1.修改MAC地址,本命令爲實際使用,待驗證 ifconfig eth0 down cd /etc/sysconfig/network-scripts vim ifcfg-eth0 # 修改其中的"HWADDR=xx:xx:xx:xx:xx:xx"爲"MACADDR=xx:xx:xx:xx:xx:xx" ifconfig eth0 up service network start # 注意:關鍵詞HWADDR和MACADDR是有區別的
協議 | 方向 | 端口 | 目的 |
---|---|---|---|
TCP | Inbound | 6443* | kube-api-server |
TCP | Inbound | 2379-2380 | etcd API |
TCP | Inbound | 10250 | Kubelet API |
TCP | Inbound | 10251 | kube-scheduler |
TCP | Inbound | 10252 | kube-controller-manager |
協議 | 方向 | 端口 | 目的 |
---|---|---|---|
TCP | Inbound | 10250 | Kubelet API |
TCP | Inbound | 30000-32767 | NodePort Services |
# 查看防火牆狀態 firewall-cmd --state # 查看防火牆開放的全部端口 firewall-cmd --zone=public --list-ports # 開放端口命令 firewall-cmd --zone=public --add-port=5672/tcp --permanent # 批量開放端口 firewall-cmd --permanent --zone=public --add-port=100-500/tcp # 從新加載防火牆,配置完端口必定要執行從新加載才能生效 firewall-cmd --reload
在全部節點配置hosts映射,後面爲對應主機名,與咱們上面設置的相同。git
若是後期增長節點須要在全部節點更新此文件。github
注意:修改成實際對應的IP地址。docker
# 全部節點執行 cat >> /etc/hosts <<EOF 192.168.43.130 master 192.168.43.129 node01 EOF
在master節點生成ssh密鑰,分發公鑰到各節點。shell
若是新加入其餘節點需分發此密鑰到新節點。json
# master節點執行 # 生成ssh密鑰,直接一路回車 ssh-keygen -t rsa # 複製剛剛生成的密鑰到各節點可信列表中,需分別輸入各主機密碼 ssh-copy-id root@master ssh-copy-id root@node01 # 配置完成後使用下面命令查看是否能夠登陸到目標服務器 ssh 'root@master' # 退出 exit
swap僅當在內存不足時使用硬盤空間充當額外內存,由於硬盤IO速度和內存差距較大,禁用swap能夠提高性能。bootstrap
# 全部節點服務器執行 swapoff -a sed -i 's/.*swap.*/#&/' /etc/fstab
若是開啓SELinux,在kubelet掛在目錄時可能會報錯 Permission denied
,能夠將SELinux設置爲permissive
或者diable
,使用permissive
會提示warn級別的錯誤信息。vim
# 全部節點服務器執行 setenforce 0 sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 全部節點服務器執行 # 設置時區 timedatectl set-timezone Asia/Shanghai systemctl enable --now chronyd # 驗證設置是否成功 date # 查看同步狀態 timedatectl status # 輸出結果中顯示下列屬性證實時鐘同步正常 System clock synchronized: yes NTP service: active # 將當前的UTC時間寫入硬件時鐘 timedatectl set-local-rtc 0 # 重啓依賴於系統時間的服務 systemctl restart rsyslog && systemctl restart crond
全部服務器節點均需安裝docker容器軟件。
# 全部節點服務器執行 # 安裝必要依賴 yum install -y yum-utils device-mapper-persistent-data lvm2 # 添加aliyum docker-ce yum源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 重建yum緩存 yum makecache
# 全部節點服務器執行 # 查看可用的docker版本 yum list docker-ce.x86_64 --showduplicates | sort -r # 結果以下: [root@localhost ~]# yum list docker-ce.x86_64 --showduplicates | sort -r Last metadata expiration check: 0:02:19 ago on Wed 26 Aug 2020 01:16:53 PM CST. docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable Available Packages
# 全部節點服務器執行 # 安裝指定版本docker,這裏以19.03.12爲例說明 yum install -y docker-ce-19.03.12-3.el7 # 執行此命令後可能會出現錯誤以下: Last metadata expiration check: 0:06:47 ago on Wed 26 Aug 2020 01:16:31 PM CST. Error: Problem: package docker-ce-3:19.03.12-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed - conflicting requests - package containerd.io-1.2.10-3.2.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.13-3.1.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.13-3.2.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.2-3.3.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.2-3.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.4-3.1.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.5-3.1.el7.x86_64 is filtered out by modular filtering - package containerd.io-1.2.6-3.3.el7.x86_64 is filtered out by modular filtering (try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages) # 錯誤緣由:須要安裝高版本的containerd.io # 解決措施:能夠安裝最新的containerd.io,可是使用官方的下載安裝時比較慢,可使用迅雷等下載工具將此安裝包下載後上傳至服務器安裝 # 這裏使用xshell鏈接服務器爲例執行,具體命令以下: yum install lrzsz mkdir software cd software/ rz yum localinstall -y containerd.io-1.2.6-3.3.el7.x86_64.rpm # 從新執行安裝docker命令,安裝成功 yum install -y docker-ce-19.03.12-3.el7
# 全部節點服務器執行 lsmod | grep overlay lsmod | grep br_netfilter
若上面的命令無返回值輸出或提示文件不存在,須要執行如下命令:
# 全部節點服務器執行 cat > /etc/modules-load.d/docker.conf <<EOF overlay br_netfilter EOF modprobe overlay modprobe br_netfilter
# 全部節點服務器執行 cat > /etc/sysctl.d/k8s.conf <<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system # 驗證是否生效,下面兩個命令結果需均返回 1 sysctl -n net.bridge.bridge-nf-call-iptables sysctl -n net.bridge.bridge-nf-call-ip6tables
# 全部節點服務器執行 mkdir /etc/docker # 修改cgroup驅動爲systemd[k8s官方推薦]、限制容器日誌量、修改存儲類型,最後的docker根目錄可修改 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" ], "registry-mirrors": ["https://7uuu3esz.mirror.aliyuncs.com"], "data-root": "/data/docker" } EOF # 添加開機自啓動,當即啓動 systemctl enable --now docker
# 全部節點服務器均執行 # 查看docker信息,判斷是否與配置一致 docker info # hello-docker測試 docker run --rm hello-world # 刪除測試的image docker rmi hello-world
非root用戶,無需sudo便可使用docker命令。
# 全部節點服務器均執行 # 添加用戶到docker組,此處zgs爲其餘帳號信息 usermod -aG docker zgs # 當前會話當即更新docker組 newgrp docker
如未特殊說明,全部節點服務器均需執行下面的命令。
# 全部節點服務器均執行 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=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF # 重建yum緩存,輸入y添加證書認證 yum makecache
# 全部節點服務器均執行 # 安裝 yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes # 配置開機啓動並當即啓動kubelet systemctl enable --now kubelet
# 全部節點服務器均執行 # 安裝bash自動補全插件 yum install bash-completion -y # 設置kubectl與kubeadm命令補全,下次login生效 kubectl completion bash >/etc/bash_completion.d/kubectl kubeadm completion bash > /etc/bash_completion.d/kubeadm
因爲國內網絡因素,kubernetes鏡像須要從mirrors站點或經過dockerhub用戶推送的鏡像拉取。
# 全部節點服務器均執行 # 查看執行kubernetes版本須要哪些鏡像 kubeadm config images list --kubernetes-version v1.18.5 # 結果以下 k8s.gcr.io/kube-apiserver:v1.18.5 k8s.gcr.io/kube-controller-manager:v1.18.5 k8s.gcr.io/kube-scheduler:v1.18.5 k8s.gcr.io/kube-proxy:v1.18.5 k8s.gcr.io/pause:3.2 k8s.gcr.io/etcd:3.4.3-0 k8s.gcr.io/coredns:1.6.7
在/root/k8s
目錄下新建腳本get-k8s-images.sh
,命令以下:
# 全部節點服務器均執行 cd /root/ mkdir k8s cd k8s/ # 建立腳本文件,文件內容以下一代碼段所示 vim get-k8s-images.sh
#!/bin/bash # Script For Quick Pull K8S Docker Images # by Hellxz Zhang <hellxz001@foxmail.com> KUBE_VERSION=v1.18.5 PAUSE_VERSION=3.2 CORE_DNS_VERSION=1.6.7 ETCD_VERSION=3.4.3-0 # pull kubernetes images from hub.docker.com docker pull kubeimage/kube-proxy-amd64:$KUBE_VERSION docker pull kubeimage/kube-controller-manager-amd64:$KUBE_VERSION docker pull kubeimage/kube-apiserver-amd64:$KUBE_VERSION docker pull kubeimage/kube-scheduler-amd64:$KUBE_VERSION # pull aliyuncs mirror docker images docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$PAUSE_VERSION docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:$CORE_DNS_VERSION docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:$ETCD_VERSION # retag to k8s.gcr.io prefix docker tag kubeimage/kube-proxy-amd64:$KUBE_VERSION k8s.gcr.io/kube-proxy:$KUBE_VERSION docker tag kubeimage/kube-controller-manager-amd64:$KUBE_VERSION k8s.gcr.io/kube-controller-manager:$KUBE_VERSION docker tag kubeimage/kube-apiserver-amd64:$KUBE_VERSION k8s.gcr.io/kube-apiserver:$KUBE_VERSION docker tag kubeimage/kube-scheduler-amd64:$KUBE_VERSION k8s.gcr.io/kube-scheduler:$KUBE_VERSION docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$PAUSE_VERSION k8s.gcr.io/pause:$PAUSE_VERSION docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:$CORE_DNS_VERSION k8s.gcr.io/coredns:$CORE_DNS_VERSION docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:$ETCD_VERSION k8s.gcr.io/etcd:$ETCD_VERSION # untag origin tag, the images won't be delete. docker rmi kubeimage/kube-proxy-amd64:$KUBE_VERSION docker rmi kubeimage/kube-controller-manager-amd64:$KUBE_VERSION docker rmi kubeimage/kube-apiserver-amd64:$KUBE_VERSION docker rmi kubeimage/kube-scheduler-amd64:$KUBE_VERSION docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$PAUSE_VERSION docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:$CORE_DNS_VERSION docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:$ETCD_VERSION
給腳本添加可執行權限,執行腳本拉去鏡像。
# 全部節點服務器均執行 # 添加腳本執行權限 chmod +x get-k8s-images.sh # 執行腳本 ./get-k8s-images.sh
腳本執行結束後,執行docker iamges
命令確認鏡像。
本小節中代碼僅需master節點服務器執行此步驟。
# master節點服務器執行 mkdir /var/lib/kubelet cat > /var/lib/kubelet/config.yaml <<EOF apiVersion: kubelet.config.k8s.io/v1beta1 kind: KubeletConfiguration cgroupDriver: systemd EOF systemctl restart kubelet
[可選],僅當須要自定義初始化配置時用,此時咱們應該在/root/k8s
目錄下。
# master節點服務器執行 kubeadm config print init-defaults > init.default.yaml
WARNING是正常的。
# master節點服務器執行 kubeadm init phase preflight # 原始命令:kubeadm init phase preflight [--config kubeadm-init.yaml] # 命令執行結束若是出現warning是正常的,通常會出現防火牆、沒法鏈接k8s站點的警告。 # 若是出現沒法從k8s拉去鏡像的錯誤屬於正常的,在執行初始化時優先使用咱們本地Docker中的鏡像,若是本地鏡像不存在纔會從k8s站點拉取。
10.244.0.0/16是flannel固定使用的IP段,設置取決於網絡組件要求。
# master節點服務器執行 # 原始命令:kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.18.5 [--config kubeadm-init.yaml] kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.18.5
初始化執行結果以下:
W0826 15:02:55.595805 40135 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io] [init] Using Kubernetes version: v1.18.5 [preflight] Running pre-flight checks [WARNING Firewalld]: firewalld is active, please ensure ports [6443 10250] are open or your cluster may not function correctly [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] Starting the kubelet [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 [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.43.130] [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 [master localhost] and IPs [192.168.43.130 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [192.168.43.130 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] 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" W0826 15:03:01.689893 40135 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" [control-plane] Creating static Pod manifest for "kube-scheduler" W0826 15:03:01.702934 40135 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" [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 18.034495 seconds [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.18" 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 master as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: 3wolsi.61tnffn49i0clcth [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 [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.43.130:6443 --token 3wolsi.61tnffn49i0clcth \ --discovery-token-ca-cert-hash sha256:fea2cc335b2f4b525bc71cc3f7fcbf68f19ced1efd43520710ad41f337ab6969
以服務器節點的另外一個用戶(zgs)爲例說明。
# master節點服務器執行 # 若是用戶不在管理員組,則須要添加管理員權限 [root@master k8s]# usermod -g root zgs [root@master k8s]# su zgs [zgs@master k8s]$ mkdir -p $HOME/.kube [zgs@master k8s]$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/admin.conf [sudo] password for zgs: [zgs@master k8s]$ sudo chown $(id -u):$(id -g) $HOME/.kube/admin.conf [zgs@master k8s]$ echo "export KUBECONFIG=$HOME/.kube/admin.conf" >> ~/.bashrc [zgs@master k8s]$ exit exit
若是在執行過程當中出現權限相關問題,可能時由於沒有將zgs用戶添加至sudo權限組中,執行下面命令。
執行時須要切換至root用戶下。
[root@master k8s]# su - Last login: Wed Aug 26 11:06:42 CST 2020 from 192.168.43.130 on pts/1 [root@master ~]# chmod u+w /etc/sudoers [root@master ~]# vim /etc/sudoers # 在文件內找到:"root ALL=(ALL) ALL"在起下面添加XXX ALL=(ALL) ALL" # (這裏的XXX是個人用戶名),而後保存退出。 [root@master ~]# chmod u-w /etc/sudoers [root@master ~]# exit
# master節點服務器執行 echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' >> /etc/profile . /etc/profile
若是不配置這個,會提示以下提示:
The connection to the server localhost:8080 was refused - did you specify the right host or port?
此時,mastaer節點已經初始化成功,可是尚未安裝網絡組件,還沒法與其餘節點通信。
以flannel
爲例。
# master節點服務器執行 cd ~/k8s/ yum install -y wget #下載最新的flannel配置文件 wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml
運行結果以下:
[root@master k8s]# kubectl apply -f kube-flannel.yml podsecuritypolicy.policy/psp.flannel.unprivileged created clusterrole.rbac.authorization.k8s.io/flannel created clusterrolebinding.rbac.authorization.k8s.io/flannel created serviceaccount/flannel created configmap/kube-flannel-cfg created daemonset.apps/kube-flannel-ds-amd64 created daemonset.apps/kube-flannel-ds-arm64 created daemonset.apps/kube-flannel-ds-arm created daemonset.apps/kube-flannel-ds-ppc64le created daemonset.apps/kube-flannel-ds-s390x created
# master節點服務器執行 kubectl get nodes
運行結果以下:
[root@master k8s]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 44m v1.18.8
在master節點將鏡像備份出來,便於後續傳輸給其餘node節點,固然有鏡像倉庫更好。
# master節點服務器執行 docker save k8s.gcr.io/kube-proxy:v1.18.5 \ k8s.gcr.io/kube-apiserver:v1.18.5 \ k8s.gcr.io/kube-controller-manager:v1.18.5 \ k8s.gcr.io/kube-scheduler:v1.18.5 \ k8s.gcr.io/pause:3.2 \ k8s.gcr.io/coredns:1.6.7 \ k8s.gcr.io/etcd:3.4.3-0 > k8s-imagesV1.18.5.tar
將會在/root/k8s
目錄下建立k8s-imagesV1.18.5.tar
文件,裏面爲k8s
所使用的docker image
。
[root@master k8s]# ll -h total 694M -rwxr-xr-x. 1 root docker 2.1K Aug 26 14:35 get-k8s-images.sh -rw-r--r--. 1 root docker 826 Aug 26 14:52 init.default.yaml -rw-r--r--. 1 root docker 694M Aug 26 15:52 k8s-imagesV1.18.5.tar -rw-r--r--. 1 root docker 15K Aug 26 15:45 kube-flannel.yml
每一個node節點都須要相關的docker鏡像,下面以其中一個爲例,其餘的能夠參考此法執行。
# node節點服務器執行 mkdir ~/k8s scp root@kube-master:/root/k8s/k8s-imagesV1.18.5.tar ~/k8s cd ~/k8s docker load < k8s-imagesV1.18.5.tar
剛纔執行完kubeadm init
命令後,最後幾行輸出的爲node節點加入集羣的命令,以下所示:
# master節點服務器kubeadm init執行結果 Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.43.130:6443 --token 3wolsi.61tnffn49i0clcth \ --discovery-token-ca-cert-hash sha256:fea2cc335b2f4b525bc71cc3f7fcbf68f19ced1efd43520710ad41f337ab6969
若是咱們沒有記錄下執行結果,能夠在master節點使用下面的命令建立新的接入token命令,以下所示:
# master節點服務器執行 kubeadm token create --print-join-command
執行結果以下:
W0826 16:12:44.007200 60971 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io] kubeadm join 192.168.43.130:6443 --token ad399n.rqut2l5e16azf0dv --discovery-token-ca-cert-hash sha256:fea2cc335b2f4b525bc71cc3f7fcbf68f19ced1efd43520710ad41f337ab6969
執行上面建立接入token命令時將會替換掉舊的接入命令,請注意。
輸出結果中有一個警告不用擔憂。
要使用root
用戶執行剛纔建立的接入token命令。
# 全部node節點服務器執行 kubeadm join 192.168.43.130:6443 --token ad399n.rqut2l5e16azf0dv --discovery-token-ca-cert-hash sha256:fea2cc335b2f4b525bc71cc3f7fcbf68f19ced1efd43520710ad41f337ab6969
執行結果以下:
W0826 16:17:45.106961 44268 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set. [preflight] Running pre-flight checks [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] Downloading configuration for the kubelet from the "kubelet-config-1.18" ConfigMap in the kube-system namespace [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.
當出現This node has joined the cluster:
時證實node節點成功加入kubernetes集羣。
在master節點上查看集羣中各節點狀態。
# master節點服務器執行 kubectl get nodes
執行結果以下:
NAME STATUS ROLES AGE VERSION master Ready master 77m v1.18.8 node01 NotReady <none> 2m50s v1.18.8
至此,kubernetes集羣已成功部署,能夠在該集羣上面執行業務層面的鏡像操做。