欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kubeadm搭建K8s集群

程序员文章站 2022-07-13 21:10:35
...

准备

准备三台服务器。云服务器或者本地vagrant+VirtualBOX创建虚拟机都行。

三个节点安装’docker’,‘kubeadm’, ‘kubelet’ and ‘kubectl’

vi etc/yum.repos.d/kubernetes.repo
添加如下内容
[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

安装
yum install -y kubectl kubelet kubeadm
开机启动
systemctl enable kubelet
启动
systemctl start kubelet

[[email protected] ~]$ which kubeadm
/usr/bin/kubeadm
[[email protected] ~]$ which kubelet
/usr/bin/kubelet
[[email protected] ~]$ which kubectl
/usr/bin/kubectl
[[email protected] ~]$ sudo docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
 Go version:      go1.9.4
 Git commit:      94f4240/1.13.1
 Built:           Fri May 18 15:44:33 2018
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
 Go version:      go1.9.4
 Git commit:      94f4240/1.13.1
 Built:           Fri May 18 15:44:33 2018
 OS/Arch:         linux/amd64
 Experimental:    false
[[email protected] ~]$

初始化 master node

[[email protected] ~]$ sudo kubeadm init --pod-network-cidr 172.100.0.0/16 --apiserver-advertise-address 192.168.205.120
看到下面信息就表示成功了
......
Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.205.120:6443 --token snipoh.vxfykjsi7e7rbtna \
    --discovery-token-ca-cert-hash sha256:e202fbfa3eed1e1d6c646dd568285947d67e99b51e824c99aeb6f45080d284c1
[[email protected] ~]$

然后在master节点上运行

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

检查pod

[[email protected] ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGE
kube-system   coredns-5c98db65d4-f4kjf             0/1     Pending   0          58m
kube-system   coredns-5c98db65d4-xqpwd             0/1     Pending   0          58m
kube-system   etcd-k8s-master                      1/1     Running   0          57m
kube-system   kube-apiserver-k8s-master            1/1     Running   0          57m
kube-system   kube-controller-manager-k8s-master   1/1     Running   0          57m
kube-system   kube-proxy-9l9vr                     1/1     Running   0          58m
kube-system   kube-scheduler-k8s-master            1/1     Running   0          57m
[[email protected] ~]$

安装网络插件

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

检查pod

[[email protected] ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGE
kube-system   coredns-5c98db65d4-gpsvq             1/1     Running   0          7h31m
kube-system   coredns-5c98db65d4-st4pf             1/1     Running   0          7h31m
kube-system   etcd-k8s-master                      1/1     Running   0          7h30m
kube-system   kube-apiserver-k8s-master            1/1     Running   0          7h30m
kube-system   kube-controller-manager-k8s-master   1/1     Running   0          7h30m
kube-system   kube-proxy-kx5mv                     1/1     Running   0          7h31m
kube-system   kube-scheduler-k8s-master            1/1     Running   0          7h30m
kube-system   weave-net-57dtf                      2/2     Running   0          59s
[[email protected] ~]$

添加worker节点

[[email protected] ~]$ sudo kubeadm join 192.168.205.120:6443 --token tte278.145ozal6u6e26ypm --discovery-token-ca-cert-hash sha256:cbb168e0665fe1b14e96a87c2da5dc1eeda04c70932ac1913d989753703277bb
[preflight] Running pre-flight checks
	......
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.

[[email protected] ~]$ kubectl get nodes
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   25m     v1.15.3
k8s-node1    Ready    <none>   11m     v1.15.3
k8s-node2    Ready    <none>   5m39s   v1.15.3

[[email protected] ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGE
kube-system   coredns-5c98db65d4-72252             1/1     Running   0          25m
kube-system   coredns-5c98db65d4-x7xf5             1/1     Running   0          25m
kube-system   etcd-k8s-master                      1/1     Running   0          24m
kube-system   kube-apiserver-k8s-master            1/1     Running   0          24m

参考:https://blog.tekspace.io/setup-kubernetes-cluster-on-centos-7/