Centos搭建kubenetes集群

准备

  • 至少2台机器,1台作为master,其他机器作为node
  • 机器最低配置:2cpu,2g内存

安装docker

配置docker安装包下载源

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

下载和安装docker

 sudo yum install docker-ce docker-ce-cli containerd.io

配置docker镜像下载加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://gvfjy25r.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

下载kubeadm,kubectl,kubelet安装包

配置安装源

创建/etc/yum.repos.d/kubernetes.repo文件

/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 kubelet kubeadm kubectl

搭建kubenetes集群

检查需要安装镜像

kubeadm config images list

启动kubeneters cluster

kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository=registry.aliyuncs.com/google_containers

option介绍

--image-repository Choose a container registry to pull control plane images from (default "k8s.gcr.io")

--pod-network-cidr Specify range of IP addresses for the pod network. If set, the control plane will automatically allocate CIDRs for every node.

执行上述命令后,可以看到如下信息

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 172.16.158.50:6443 --token q3dg40.lr0suvqa23s0h3if \
    --discovery-token-ca-cert-hash sha256:ffe2ec1194c6a6acfc2bb46ccb37a8d5e129aa26812b68c2a81f19be98f5a313
    

安装kube-flannel插件

保证pod之间可以通信

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml

也可以将kube-flannel.yml下载(wget工具)到本地,然后再用kubectl apply启动。

node加入集群

节点加入集群

命令:

kubeadm join 172.16.158.50:6443 --token q3dg40.lr0suvqa23s0h3if \
    --discovery-token-ca-cert-hash sha256:ffe2ec1194c6a6acfc2bb46ccb37a8d5e129aa26812b68c2a81f19be98f5a313

在同一个账号下同一个区域内,会收到如下信息:

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

查看节点运行情况

kubectl get nodes --all-namespace
NAME                      STATUS   ROLES    AGE   VERSION
izbp17m6hlxv4d1w6eelqoz   Ready    <none>   42h   v1.17.3
izbp1htc4i0js572re75azz   Ready    master   47h   v1.17.3
上一篇:Java api 1.8 中文 帮助文档


下一篇:Class 2 快速搭建Docker环境