2.2centos7系统配置

centos下载地址:推荐大家使用centos7.6以上版本

http://mirrors.aliyun.com/centos/7/isos/x86_64/

查看centos系统版本命令:

cat /etc/centos-release

配置阿里云yum源

1.下载安装wget
yum install -y wget
2.备份默认的yum
mv /etc/yum.repos.d /etc/yum.repos.d.backup
3.设置新的yum目录
mkdir -p /etc/yum.repos.d
4.下载阿里yum配置到该目录中,选择对应版本
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
5.更新epel源为阿里云epel源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

升级系统内核

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-lt
grep initrd16 /boot/grub2/grub.cfg
grub2-set-default 0

查看centos系统内核命令:

uname -r
uname -a

查看CPU命令:

lscpu

查看内存命令:

free
free -h

查看硬盘信息

fdisk -l

2.2centos7系统配置

2.2.1关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

2.2.2关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
setenforce 0

2.2.3网桥过滤

vi /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0
生效命令
sysctl --system
查看效果
sysctl -a|grep "ip_forward"

2.2.4开启IPVS

安装IPVS
yum -y install ipset ipvsdm
编译ipvs.modules文件
vi /etc/sysconfig/modules/ipvs.modules
文件内容如下
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
赋予权限并执行
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules &&lsmod | grep -e ip_vs -e nf_conntrack
重启电脑,检查是否生效
reboot
lsmod | grep ip_vs_rr

2.2.5同步时间

安装软件
yum -y install ntpdate
向阿里云服务器同步时间
ntpdate time1.aliyun.com
删除本地时间并设置时区为上海
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
查看时间
date -R || date

2.2.6命令补全

安装bash-completion
yum -y install bash-completion bash-completion-extras
使用bash-completion
source /etc/profile.d/bash_completion.sh

2.2.7关闭swap分区

临时关闭:
swapoff -a
永久关闭:
vi /etc/fstab
将文件中的/dev/mapper/centos-swap这行代码注释掉
#/dev/mapper/centos-swap swap swap defaults 0 0
确认swap已经关闭:若swap行都显示 0 则表示关闭成功
free -m

2.2.8hosts配置

vi /etc/hosts
文件内容如下:
cat <<EOF >>/etc/hosts
192.168.198.186 k8s-master01
192.168.198.187 k8s-node01
192.168.198.188 k8s-node02
192.168.198.189 k8s-node03
EOF

2.3安装docker

2.3.0阿里云开发者平台 开发者平台官网地址:可以参考阿里云官网提供的docker安装教程进行安装。

阿里云-计算,为了无法计算的价值

2.3.1安装docker前置条件

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

2.3.2添加源

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

2.3.3查看docker更新版本

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

2.3.4安装docker最新版本

yum -y install docker-ce
安装指定版本:
yum -y install docker-ce-18.09.8
可以通过docker version命令查看
docker-client版本:当前最新版本
docker-server版本为:18.09.8

2.3.5开启dock而服务

systemctl start docker

systemctl status docker

2.3.6安装阿里云镜像加速器

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

2.3.7设置docker开启启动服务

systemctl  enable docker

2.3.8修改Cgroup Driver

修改daemon.json,新增:
"exec-opts": ["native.cgroupdriver=systemd"]
重启docker服务:
systemctl daemon-reload
systemctl restart docker
查看修改后状态:
docker info | grep Cgroup

2.3.9复习docker常用命令

2.4使用kubeadm快速安装

2.5安装yum源 2.5.1新建repo文件

vi /etc/yum.repos.d/kubernates.repo

2.5.2文件内容

[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

2.5.3更新缓存

yum clean all
yum -y makecache

2.5.4验证源是否可用

yum list | grep kubeadm
如果提示要验证yum-key.gpg是否可用,输入y。
查找到kubeadm。显示版本

2.5.5查看k8s版本

yum list kubelet --showduplicates  | sort -r

2.5.6安装k8s-1.17.5

yum install -y kubelet-1.17.5 kubeadm-1.17.5 kubectl-1.17.5

2.6设置kubelet 2.6.1增加配置信息

如果不配置kubelet,可能会导致K8S集群无法启动。为实现docker使用的cgroupdriver与kubelet
使用的cgroup的一致性。
vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"

2.6.2设置开机启动

systemctl  enable kubelet

2.7初始化镜像(选学部分) 如果是第一次安装k8s,手里没有备份好的镜像,可以执行如下操作。也可以使用资料包中的镜像 备份跳过本章节学习内容。 教学目标:本章节重点为下载镜像和备份镜像。 2.7.1查看安装集群需要的镜像

kubeadm config images list

2.7.2编写执行脚本

mkdir -p /data
cd /data
vi images.sh
#!/bin/bash

#下面的镜像应该去除"k8s.gcr.io"的前缀,版本换成kubeadm config images list命令获取

#到的版本
images=(
    kube-apiserver:v1.17.17
	kube-controller-manager:v1.17.17
	kube-scheduler:v1.17.17
	kube-proxy:v1.17.17
	pause:3.1
    etcd:3.4.3-0
	coredns:1.6.5
)
for imageName in ${images[@]} ;
do
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
	docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
	docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
done

2.7.3执行脚本

mkdir -p /data
cd /data
给脚本授权
chmod +x images.sh
执行脚本
./images.sh

2.7.4保存镜像: idea的列编辑模式:alt+鼠标左键

docker save -o k8s.1.17.5.tar \
k8s.gcr.io/kube-apiserver:v1.17.17 \
k8s.gcr.io/kube-controller-manager:v1.17.17 \
k8s.gcr.io/kube-scheduler:v1.17.17 \
k8s.gcr.io/kube-proxy:v1.17.17 \
k8s.gcr.io/pause:3.1 \
k8s.gcr.io/etcd:3.4.3-0 \
k8s.gcr.io/coredns:1.6.5 \

2.8导入镜像 2.81导入master节点镜像tar包

master节点需要全部镜像
docker load -i k8s.1.17.5.tar

2.8.2导入node节点镜像tar包

node节点需要kube-proxy:v1.17.5和pause:3.1,2个镜像
docker load -i k8s.1.17.5.node.tar

2.9初始化集群 配置k8s集群网络 2.9.0calico官网地址

官网下载地址:
https://docs.projectcalico.org/v3.14/manifests/calico.yaml
github地址:
https://github.com/projectcalico/calico
镜像下载:
docker pull calico/cni:v3.14.2
docker pull calico/pod2daemon-flexvol:v3.14.2
docker pull calico/node:v3.14.2
docker pull calico/kube-controllers:v3.14.2
配置hostname:
hostnamectl set-hostname k8s-master01
hostnamectl set-hostname k8s-node01
hostnamectl set-hostname k8s-node02
hostnamectl set-hostname k8s-node03
配置ip地址:

2.2.8hosts配置

vi /etc/hosts
文件内容如下:
cat <<EOF >>/etc/hosts
192.168.61.211 k8s-master01
192.168.61.95 k8s-node01
192.168.61.17 k8s-node02
192.168.61.40 k8s-node03
EOF

2.9.1初始化集群信息:calico网络

kubeadm init --apiserver-advertise-address=192.168.61.211 --kubernetes-version v1.17.5 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.81.0.0/16

报错

[preflight] Running pre-flight checks
	[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
	[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 23.0.4. Latest validated version: 19.03
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master01 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.29.200]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master01 localhost] and IPs [192.168.29.200 127.0.0.1 ::1]

使用

这块加了指定阿里云源 --image-repository=registry.aliyuncs.com/google_containers

kubeadm init --apiserver-advertise-address=192.168.61.211 --kubernetes-version v1.17.5 --image-repository=registry.aliyuncs.com/google_containers --service-cidr=10.1.0.0/16 --pod-network-cidr=10.81.0.0/16 

报错

The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.

解决方法

# #添加以下内容

vim /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

# 重启docker

systemctl restart docker

# 重新初始化

kubeadm reset # 先重置

2.9.2执行配置命令

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

2.9.3node节点加入集群信息

kubeadm join 192.168.61.211:6443 --token s9sbd6.8hcbn8a17c6gu5jt \
    --discovery-token-ca-cert-hash sha256:f69bcfa9a2df0bc8d7c82f8c04b92b05b9204cc5cdda0831ac4a5cfd4d4bafcb 

2.9.4kubectl命令自动补全

echo "source <(kubectl completion bash)" >> ~/.bash_profile
source ~/.bash_profile

2.9.5发送邮件问题

在 bash 中设置当前 shell 的自动补全,要先安装 bash-completion 包。
echo "unset MAILCHECK">> /etc/profile
source /etc/profile
在你的 bash shell 中永久的添加自动补全
[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
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐