关闭防火墙
[root@k8smaster ~]# vim /etc/selinux/config
[root@k8smaster ~]# swapoff -a
[root@k8smaster ~]# vim /etc/fstab
[root@k8smaster ~]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
[root@k8smaster ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
/etc/hosts
文件[root@k8smaster1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.210 k8smaster
192.168.2.211 k8snode1
192.168.2.212 k8snode2
每台机器都要实现相互之间的通道(以master为例)
生成密钥
ssh-keygen
把本地生成的密钥文件和私钥文件拷贝到远程主机
ssh-copy-id k8smaster
ssh-copy-id k8snode1
ssh-copy-id k8snode2
测试通道是否建立成功
ssh k8smaster
ssh k8snode1
ssh k8snode2
临时关闭
swapoff -a
永久关闭
[root@k8smaster1 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Jul 29 17:46:15 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=104d6b80-24cf-4094-8d9a-09a9878f9895 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
#/dev/mapper/centos-swap swap swap defaults 0 0
注释/dev/mapper/centos-swap swap swap defaults 0 0
即可
为什么要关闭swap交换分区?
Swap是交换分区,如果机器内存不够,会使用swap分区,但是swap分区的性能较低,k8s设计的时候为了能提升性能,默认是不允许使用交换分区的。Kubeadm初始化的时候会检测swap是否关闭,如果没关闭,那就初始化失败。如果不想要关闭交换分区,安装k8s的时候可以指定–ignore-preflight-errors=Swap来解决。
modprobe br_netfilter
echo "modprobe br_netfilter" >> /etc/profile
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
? 这是一个Linux命令,它的作用是将文本输入到/etc/sysctl.d/k8s.conf文件中。">“符号表示覆盖文件内容,如果文件不存在则会创建一个新文件。”<<EOF"表示输入的文本以EOF为结束标志
? 该命令在Kubernetes集群中通常用于配置内核参数,以优化集群的性能和稳定性。在这个例子中,EOF后面的文本应该包含一些内核参数的配置信息
加载配置
sysctl -p /etc/sysctl.d/k8s.conf
[root@k8smaster1 ~]# sysctl -p /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@k8smaster1 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
安装其他软件
yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate telnet ipvsadm
vim /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
安装ntpdate命令
yum install ntpdate -y
跟网络时间做同步
ntpdate cn.pool.ntp.org
把时间同步做成计划任务
crontab -e
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
重启crond服务
service crond restart
yum install docker-ce-20.10.6 -y
设置开启启动
systemctl daemon-reload && systemctl restart docker
编辑文件
vim /etc/docker/daemon.json
{
"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
systemctl daemon-reload && systemctl restart docker
修改docker文件驱动为systemd,默认为cgroupfs,kubelet默认使用systemd,两者必须一致才可以。
yum install -y kubelet-1.20.6 kubeadm-1.20.6 kubectl-1.20.6
设置开机启动
systemctl enable kubelet
Kubeadm kubeadm是一个工具,用来初始化k8s集群的
kubelet 安装在集群所有节点上,用于启动Pod的
kubectl通过kubectl可以部署和管理应用,查看各种资源,创建、删除和更新各种组件
把初始化k8s集群需要的离线镜像包上传到机器上,手动解压:
docker load -i k8simage-1-20-6.tar.gz
k8smaster
执行使用kubeadm初始化k8s集群
kubeadm config print init-defaults > kubeadm.yaml
[root@k8smaster1 ~]# ls
anaconda-ks.cfg k8simage-1-20-6.tar.gz kubeadm.yaml
改配置文件
根据我们自己的需求修改配置,比如修改 imageRepository 的值,kube-proxy 的模式为 ipvs,初始化节点的时候需要指定cgroupDriver为systemd
[root@k8smaster1 ~]# cat kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.2.210
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: k8smaster1
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.20.0
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
podSubnet: 10.244.0.0/16
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd
? --image-repository registry.aliyuncs.com/google_containers为保证拉取镜像不到国外站点拉取,手动指定仓库地址为registry.aliyuncs.com/google_containers。kubeadm默认从k8s.gcr.io拉取镜像。
? mode: ipvs 表示kube-proxy代理模式是ipvs,如果不指定ipvs,会默认使用iptables,但是iptables效率低,所以我们生产环境建议开启ipvs,阿里云和华为云托管的K8s,也提供ipvs模式
我们本地有导入到的离线镜像
k8simage-1-20-6.tar.gz
,所以会优先使用本地的镜像。
基于kubeadm.yaml文件初始化k8s
kubeadm init --config=kubeadm.yaml --ignore-preflight-errors=SystemVerification
显示如下,说明安装完成:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
在k8smaster
上查看加入节点的命令:
kubeadm token create --print-join-command
在k8snode1、k8snode2
输入下面命令:
kubeadm join 192.168.2.210:6443 --token ooveyk.k9pttn2esdowdej0 --discovery-token-ca-cert-hash sha256:69470ed1d44af1f049b11cb85ac4638829063a9df8002dc47de3260d6d34dfe7
在k8smaster查看集群节点状况
[root@k8smaster1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8smaster NotReady control-plane,master 11m v1.20.6
k8snode1 NotReady <none> 25s v1.20.6
k8snode2 NotReady <none> 18s v1.20.6
您在 /var/spool/mail/root 中有新邮件
可以看到k8snode1、k8snode2
的ROLES角色为空(),就表示这个节点是工作节点。
? 可以把k8snode1、k8snode2
的ROLES变成work,按照如下方法:
kubectl label node k8snode1 node-role.kubernetes.io/worker=worker
kubectl label node k8snode2 node-role.kubernetes.io/worker=worker
在k8smaster查看集群节点状况
[root@k8smaster1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8smaster NotReady control-plane,master 17m v1.20.6
k8snode1 NotReady worker 6m19s v1.20.6
k8snode2 NotReady worker 6m12s v1.20.6
k8smaster
安装kubernetes网络组件-Calico
下载配置文件地址:https://docs.tigera.io/calico/latest/manifests/calico.yaml
kubectl apply -f calico.yaml
再次查看集群状态。
[root@k8smaster1 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8smaster Ready control-plane,master 20m v1.20.6
k8snode1 Ready worker 9m20s v1.20.6
k8snode2 Ready worker 9m13s v1.20.6
STATUS状态是Ready,说明k8s集群正常运行了
[root@k8smaster ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-6949477b58-nwmr2 1/1 Running 0 70s
calico-node-5jq26 1/1 Running 0 70s
calico-node-lk4zv 1/1 Running 0 70s
calico-node-n4742 1/1 Running 0 70s
coredns-7f89b7bc75-7zdk5 1/1 Running 0 160m
coredns-7f89b7bc75-x8jcr 1/1 Running 0 160m
etcd-k8smaster 1/1 Running 0 160m
kube-apiserver-k8smaster 1/1 Running 0 160m
kube-controller-manager-k8smaster 1/1 Running 1 160m
kube-proxy-pkt7c 1/1 Running 0 159m
kube-proxy-pps8s 1/1 Running 0 2m15s
kube-proxy-qgs8x 1/1 Running 0 160m
kube-scheduler-k8smaster
这是一个使用 kubectl 工具在 Kubernetes 集群的 kube-system 命名空间中获取 Pod 信息的命令。
解析:
kubectl
: Kubernetes 命令行工具,用于与 Kubernetes 集群进行交互。get
: kubectl 的子命令之一,用于从 Kubernetes 集群中获取资源的信息。pod
: 要获取的资源类型,表示 Pod 对象。-n kube-system
: 选项,指定要获取资源的命名空间为 kube-system。Kubernetes 中的命名空间是用于在集群中对资源进行逻辑隔离的一种机制。执行该命令将显示 kube-system 命名空间中所有 Pod 的名称及其相关信息,如状态、IP 地址、运行时间等。