Kubernetes(简称,K8S)是Google开源的容器集群管理系统,在Docker技术的基础上,为容器化的应用提供部署运行、资源调度、服务发现和动态伸缩等一系列完整功能,提高了大规模容器集群管理的便捷性。 其主要功能如下:
# 主节点
hostnamectl set-hostname k8s-master
# 从节点
hostnamectl set-hostname k8s-node1
cat >>/etc/hosts<<EOF
192.168.0.122 k8s-master
192.168.0.123 k8s-node1
EOF
systemctl stop firewalld
systemctl disable firewalld
# 临时关闭
setenforce 0
# 永久关闭,改为 SELINUX=disabled
vi /etc/sysconfig/selinux
# 创建文件
vi /etc/sysctl.d/k8s.conf
# 内容如下
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
sysctl -p /etc/sysctl.d/k8s.conf
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: No such file or directory
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-iptables: No such file or directory
解决办法:
modprobe br_netfilter
ls /proc/sys/net/bridge
sysctl -p /etc/sysctl.d/k8s.conf #这时再次执行sysctl -p命令即可
4.3. kube-proxy 开启ipvs的前置条件;
4.3.1. 覆写ipvs.modules文件;
# 覆写ipvs.modules文件
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
4.3.2. 执行ipvs.modules文件
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs –e nf_conntrack_ipv4
4.4. 所有节点关闭swap;
# 临时关闭
swapoff -a
# 永久关闭,注释掉以下字段/dev/mapper/centos-swap swap swap defaults 0 0
vi /etc/fstab
kubelet、kubeadm、kubectl简介
yum clean all
5.2. 设置yum源
cat <<EOF > /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
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
5.3. 安装kubelet kubeadm kubectl
yum install -y kubelet kubeadm kubectl
5.4. kubelet 设置开机启动(注意:先不启动,现在启动的话会报错)
systemctl enable kubelet
5.5. 查看版本(根据安装的实际情况,版本可能会变化)
kubelet --version
vi /etc/docker/daemon.json
# 加入如下配置
{
"exec-opts":["native.cgroupdriver=systemd"]
}
1.2. 改虚拟机的CPU的个数,至少为2个(不然会报这个错误:[ERROR NumCPU]: the number of available CPUs 1 is less than the required 2);
kubeadm init --kubernetes-version=1.18.2 \
--apiserver-advertise-address=192.168.0.122 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
可能出现的问题:
初始化 Kubernetes 问题(端口占用):
[preflight] Some fatal errors occurred:
[ERROR Port-6443]: Port 6443 is in use
[ERROR Port-10250]: Port 10250 is in use
[ERROR Port-10251]: Port 10251 is in use
[ERROR Port-10252]: Port 10252 is in use
[ERROR FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml]: /etc/kubernetes/manifests/kube-controller-manager.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml]: /etc/kubernetes/manifests/kube-scheduler.yaml already exists
解决办法:
kubeadm reset #重启kubeadm
kubeadm join 192.168.0.122:6443 --token udccd5.61sx3avu56249ggd \
--discovery-token-ca-cert-hash sha256:3a8a90f22fb74f86cda060e1d6a51b2c56ff0424d3858d5e762f898bb6ab51f0
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
mkdir k8s
cd k8s
wget https://docs.projectcalico.org/v3.10/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
sed -i 's/192.168.0.0/10.244.0.0/g' calico.yaml
kubectl apply -f calico.yaml
kubectl get pod --all-namespaces -o wide
kubeadm join 192.168.0.122:6443 --token udccd5.61sx3avu56249ggd \ --discovery-token-ca-cert-hash sha256:3a8a90f22fb74f86cda060e1d6a51b2c56ff0424d3858d5e762f898bb6ab51f0
systemctl start kubelet
kubectl get nodes