kubeadmin快速安裝k8s集群


個人辦公環境,通過VM workstation NAT模式快速安裝k8s集群用於本地測試,本次安裝版本v1.15.0.

一、安裝kubeadm環境准備

主機 IP
k8s-master01 192.168.68.150
node01 192.168.68.151
node02 192.168.68.152

以下操作,在三台節點執行

  1. 關閉防火牆及selinux

systemctl stop firewalld && systemctl disable firewalld

sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && setenforce 0

  1. 關閉 swap 分區

swapoff -a 臨時

sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab 永久

  1. 分別在192.168.68.150/151/152上設置主機名及配置hosts
hostnamectl set-hostname k8s-master(192.168.68.150主機打命令)
hostnamectl set-hostname node01(192.168.68.151主機打命令)
hostnamectl set-hostname node02 (192.168.68.152主機打命令)
  1. 在所有主機上上添加如下命令
cat >> /etc/hosts << EOF
192.168.68.150 k8s-master
192.168.68.151 node01
192.168.68.152 node02
EOF
  1. 內核調整,將橋接的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
  1. 設置系統時區並同步時間服務器
    yum install -y ntpdate && ntpdate time.windows.com

  2. docker 安裝&配置

安裝依賴

yum install -y yum-utils device-mapper-persistent-data lvm2

設置yum源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

可以查看所有倉庫中所有docker版本,並選擇特定版本安裝`

yum list docker-ce --showduplicates | sort -r

yum -y install docker-ce-18.06.1.ce-3.el7

啟動並加入開機啟動

systemctl start docker && systemctl enable docker

配置鏡像加速器(阿里雲/可選)

  1. 添加kubernetes 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
  1. 安裝kubeadm,kubelet和kubectl

yum install -y kubelet-1.15.0-0 kubeadm-1.15.0-0 kubectl-1.15.0-0

二、部署Kubernetes Master

只需要在Master 節點執行,這里的apiserve需要修改成自己的master地址

kubeadm init \
--apiserver-advertise-address=192.168.68.150 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.17.0 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16

由於默認拉取鏡像地址k8s.gcr.io國內無法訪問,這里指定阿里雲鏡像倉庫地址,輸出信息:

[bootstrap-token] Using token: zh8qer.f6q4sixlsulxi2tp
[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
[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.68.129:6443 --token zh8qer.f6q4sixlsulxi2tp \
    --discovery-token-ca-cert-hash sha256:4fd045390a800653f081b0618d74e1d24c84a4dcf8baeb79878d3c7152bbe7ef 

根據輸出提示操作:

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

加入k8s node節點,分別在兩個Node節點 192.168.68.151/192.168.68.152執行
kubeadm join 192.168.68.129:6443 --token zh8qer.f6q4sixlsulxi2tp \ --discovery-token-ca-cert-hash sha256:4fd045390a800653f081b0618d74e1d24c84a4dcf8baeb79878d3c7152bbe7ef

輸出提示內容

[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.

三、安裝網絡插件

本次集群安裝采用flannel網絡插件

只需要在Master節點執行

wget https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

kubectl apply -f kube-flannel.yml

查看集群node狀態
查看集群的node狀態,安裝完網絡工具之后,只有顯示如下狀態,所有節點全部都Ready好了之后才能繼續后面的操作

kubectl get nodes

nodes

四、測試Kubernetes集群

在Kubernetes集群中創建一個pod,然后暴露端口,驗證是否正常訪問

kubectl create deployment nginx --image=nginx

kubectl expose deployment nginx --port=80 --type=NodePort

kubectl get pods,svc

訪問地址:http://NodeIP:Port ,此例就是:http://192.168.68.152:30667

visit


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM