搭建环境最快的方法就是参照官方稳定进行搭建:https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/

一、 环境要求

  1. 一个或者多个兼容 deb 或者 rpm 软件包的操作系统,比如 Ubuntu 或者 CentOS
  2. 每台机器 2 GB 以上的内存,内存不足时应用会受限制,主节点上 2 CPU 以上核心
  3. 集群里所有的机器有完全的网络连接,公有网络或者私有网络都可以
  4. docker 1.9版本以上
  5. etcd 2.0版本以上

本次搭建测试环境

  • 虚拟机三台分别是:
    Master 一个,Node 二个

二、 安装k8s

  • 关闭swap和firewall
1
2
3
4
5
6
7
8
9
10
11
12
13
//临时关闭
swapoff -a

//永久关闭
vi /etc/fstab
//注释以下行
# /dev/mapper/centos-swap swap

//快捷执行方法
sed -i '/ swap / s/^/#/' /etc/fstab

//关闭防火墙
service firewalld stop
  • 安装docker

添加安装源

1
2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache

找不到执行(yum-config-manager):yum -y install yum-utils

安装docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
yum install docker-ce -y

//查看版本验证安装
docker --version
Docker version 18.09.4, build d14af54266

//开机启动
systemctl start docker & systemctl enable docker

//验证容器是否正常
docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

//说明docker没有启动,启动docker引擎
service docker start

//重新验证安装 下方为安装成功的情况
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete

Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/
  • 安装 kubelet kubeadm kubectl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cat <<EOF > /etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

EOF


# 将 SELinux 设置为 permissive 模式(将其禁用)
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

systemctl enable kubelet && systemctl start kubelet
  • 启动kuberlet

    1
    systemctl enable kubelet && systemctl start kubelet
  • 下载官网编译好的二进制k8s包

进入github下载地址:https://github.com/kubernetes/kubernetes/releases