kubernetes 安装

环境准备

yum install vim wget ntpdate -y


1.关闭 firewalld,selinux,swap
# A 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl stop iptables
systemctl disable iptables

# B 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
setenforce 0  # 临时

# C 关闭swap
swapoff -a  # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久

2.开启 时间同步, 网桥过滤,ipvs

# D 时间同步
yum install ntpdate -y
ntpdate edu.ntp.org.cn
yum install chrony
# 如果是centos8的话,要用chrony作为时间同步的工具了

# E 网桥过滤 添加如下配置:
vim /etc/sysctl.d/kubernetes.conf 
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
# 重新加载配置
sysctl -p
# 加载网桥过滤模块
modprobe br_netfilter
# 查看网桥过滤模块是否加载成功
lsmod | grep br_netfilter

## F 安装ipvs
# 1.安装ipset和ipvsadm
yum install ipset ipvsadmin -y
ipvsadmin 失败了安装 ipvsadm 看你安装的yum 源了
# 2.添加需要加载的模块写入脚本文件
cat >> /etc/sysconfig/modules/ipvs.modules << EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
# 3.为脚本添加执行权限
 chmod +x /etc/sysconfig/modules/ipvs.modules
# 4.执行脚本文件
/bin/bash /etc/sysconfig/modules/ipvs.modules
# 5.查看对应的模块是否加载成功
lsmod | grep -e -ip_vs -e nf_conntrack_ipv4

# G 本地dns
cat >> /etc/hosts << EOF
192.168.162.133    k8smaster
192.168.162.134    k8snode1
192.168.162.135    k8snode2  
EOF

docker-ce 安装

#1.配置docker-ce 镜像源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

#2. 安装docker-ce
yum install docker-ce -y

#3.配置
#Docker 在默认情况下使用Vgroup Driver为cgroupfs,而Kubernetes推荐使用systemd来替代cgroupfs
cat > /etc/docker/daemon.json << EOF
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "registry-mirrors": ["https://kn0t2bca.mirror.aliyuncs.com"]
}
EOF

#3.启动
systemctl restart docker
systemctl enable docker

# cgroups 是什么
cgroups 的全称是 Linux Control Groups,主要作用是限制、记录和隔离进程组(process groups)使用的物理资源(cpu、memory、IO 等)cgroup 内核功能没有提供任何的系统调用接口,而是对 linux vfs 的一个实现,因此可以用类似文件系统的方式进行操作systemd、lxc、docker 这些封装了 cgroups 的软件也能让你通过它们定义的接口控制 cgroups 的内容

kubernetes 安装

# kubernetes 镜像源配置
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 安装 
yum install kubelet kubeadm kubectl -y

# 开机自启
systemctl enable kubelet

# 查看版本
kubectl version

# 初始化
kubeadm init \
    --apiserver-advertise-address=139.198.161.129 \
    --image-repository registry.aliyuncs.com/google_containers \
    --kubernetes-version v1.22.3 \
    --service-cidr=10.96.0.0/12 \
    --pod-network-cidr=10.244.0.0/16
# 注:
#apiserver-advertise 一定是master 的地址,并且如果是内网的话,要用内网的地址
# --ignore-preflight-errors=all 如果要忽略错误 加这个参数

# 如果初始化失败,那么重置后,重新初始化
kubeadm reset
rm -rf $HOME/.kube/config

部署CNI插件

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get pods -n kube-system

# 注:要等一会时间才好

至此环境完成

image.png

可视化界面:

Kuboard, dashboard, 

多master集群:

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.

This error is likely caused by:
                - The kubelet is not running
                - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)



image.png

CentOS7 环境初始化脚本 k8s-init-centos7.sh

#!/bin/bash
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
yum install vim wget ntpdate ipset ipvsadmin -y

systemctl stop firewalld
systemctl disable firewalld
systemctl stop iptables
systemctl disable iptables

# B 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

# C 关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

# D 时间同步
ntpdate edu.ntp.org.cn

# E 网桥过滤 添加如下配置:
touch /etc/sysctl.d/kubernetes.conf 
cat >> /etc/sysctl.d/kubernetes.conf <> /etc/sysconfig/modules/ipvs.modules << EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod +x /etc/sysconfig/modules/ipvs.modules
/bin/bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep -e -ip_vs -e nf_conntrack_ipv4

# G 安装docker-ce
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum install docker-ce -y
cat > /etc/docker/daemon.json << EOF
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "registry-mirrors": ["https://kn0t2bca.mirror.aliyuncs.com"]
}
EOF
systemctl enable docker
systemctl start docker

# H 安装k8s工具
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install kubelet-1.22.2 kubeadm-1.22.2 kubectl-1.22.2 -y
systemctl enable kubelet
kubectl version


cat >> /etc/hosts << EOF
10.120.109.28    master01
10.120.117.5    master02
10.120.117.5    master03  
EOF

你可能感兴趣的:(kubernetes 安装)