1 在 CentOS7 上 使 用 kubeadm 搭 建
k8s-v1.13.5 集群
1.1 系统初始化
1.1.1设置永久主机名称
hostnamectl set-hostname m01
hostnamectl set-hostname s01
hostnamectl set-hostname s02
1.1.2添加主机名和 IP 的对应关系
cat >> /etc/hosts <
1.1.5关闭 swap 分区
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
1.1.6设置时区
# 调整系统 TimeZone
timedatectl set-timezone Asia/Shanghai
# 将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond
[root@m01 ~]# timedatectl set-timezone Asia/Shanghai
[root@m01 ~]# timedatectl set-local-rtc 0
[root@m01 ~]#
1.1.7更新系统时间
ntpdate cn.pool.ntp.org
[root@m01 ~]# ntpdate cn.pool.ntp.org
19 Apr 14:31:13 ntpdate[16848]: the NTP socket is in use, exiting
[root@m01 ~]#
1.2 install docker
1.2.1卸载旧版 docker 并手动删除/var/lib/docker 目录
[root@m01 ~]# sudo yum remove docker \
>
>
>
>
>
>
>
>
>
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
Loaded plugins: fastestmirror, langpacks
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-selinux
No Match for argument: docker-engine-selinux
No Match for argument: docker-engine
No Packages marked for removal
[root@m01 ~]#
[root@m01 ~]# rm -rf /var/lib/docker/
[root@m01 ~]#
1.2.2set up docker repository
1.2.2.1.Install required packages.
yum-utils
provides
the
yum-config-managerutility,
and
device-mapper-persistent-data
and
lvm2
are
required
by
the devicemapper storage driver.
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
1.2.2.2.Use the following command to set up the stable repository.
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
1.2.3安装 docker
[root@s02 ~]# sudo yum install -y docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package 1:docker-ce-cli-18.09.5-3.el7.x86_64 already installed and latest version
Package containerd.io-1.2.5-3.1.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:18.09.5-3.el7 will be installed
-->
>=
3:docker-ce-18.09.5-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.74-1.el7 will be installed
--> Finished Dependency Resolution
container-selinux
Dependency:
Processing
2.9
for
package:
Dependencies Resolved
===========================================================
===========================================================
===========================================================
Arch
Package
Version
Repository
Size
===========================================================
===========================================================
===========================================================
Installing:
docker-ce
3:18.09.5-3.el7
19 M
Installing for dependencies:
container-selinux
2:2.74-1.el7
38 k
x86_64
docker-ce-stable
noarch
extras
Transaction Summary
===========================================================
===========================================================
===========================================================
Install 1 Package (+1 Dependent package)
Total size: 19 M
Total download size: 19 M
Installed size: 81 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
docker-ce-18.09.5-3.el7.x86_64.rpm
| 19 MB 00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing
1/2
setsebool: SELinux is disabled.
Installing
2/2
Verifying
1/2
Verifying
2/2
Installed:
:
:
2:container-selinux-2.74-1.el7.noarch
3:docker-ce-18.09.5-3.el7.x86_64
:
:
2:container-selinux-2.74-1.el7.noarch
3:docker-ce-18.09.5-3.el7.x86_64
docker-ce.x86_64 3:18.09.5-3.el7
Dependency Installed:
container-selinux.noarch 2:2.74-1.el7
from /etc/systemd/system/multi-user.target.wants/docker.service
Complete!
[root@s02 ~]# sudo systemctl start docker
[root@s02 ~]# sudo systemctl enable docker
Created symlink
/usr/lib/systemd/system/docker.service.
[root@s02 ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Status: Downloaded newer image for hello-world:latest
to
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/
[root@s02 ~]#
1.3 installing kubeadm
1.3.1前置要求
2 GB or more of RAM per machine (any less will leave little room for your apps)
2 CPUs or more
Full network connectivity between all machines in the cluster (public or private
network is fine)
Unique hostname, MAC address, and product_uuid for every node. See here for
more details.
Certain ports are open on your machines. See here for more details.
Swap disabled. You MUST disable swap in order for the kubelet to work properly.
1.3.2检查要求的端口
Master node(s)
Protocol Direction
Port Range
Purpose
Used By
TCP
Inbound
6443*
Kubernetes API server
All
Protocol Direction
Port Range
Purpose
Used By
TCP
TCP
TCP
TCP
Inbound
2379-2380
etcd server client API
kube-apiserver, etcd
Inbound
10250
Kubelet API
Self, Control plane
Inbound
10251
kube-scheduler
Self
Inbound
10252
kube-controller-manager
Self
Worker node(s)
Protocol
Direction
Port Range
Purpose
Used By
TCP
TCP
Inbound
10250
Kubelet API
Self, Control plane
Inbound
30000-32767
NodePort Services**
All
** Default port range for NodePort Services.
Any port numbers marked with * are overridable, so you will need to ensure any
custom ports you provide are also open.
Although etcd ports are included in master nodes, you can also host your own
etcd cluster externally or on custom ports.
The pod network plugin you use (see below) may also require certain ports to be
open. Since this differs with each pod network plugin, please see the
documentation for the plugins about what port(s) those need.
1.3.3关闭 SELinux
[root@redis-test2 ~]# clear
[root@redis-test2 ~]# setenforce 0
setenforce: SELinux is disabled
[root@redis-test2
/etc/selinux/config
~]#
sed
-i
's/^SELINUX=enforcing$/SELINUX=permissive/'
[root@redis-test2 ~]#
1.3.4开启 CentOS 网桥
[root@redis-test2 ~]# cat < /etc/sysctl.d/k8s.conf
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@redis-test2 ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
* Applying /etc/sysctl.conf ...
[root@redis-test2 ~]#
1.3.5准备 k8s 的国内 yum 源
[root@redis-test2 ~]# cat < /etc/yum.repos.d/kubernetes.repo
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=0
> EOF