1. 环境准备
主要是网络打通
操作系统:centos7
环境需要:docker
配置:2核,2G,20G
ip:
k8s-master: 192.168.99.60
k8s-node1: 192.168.99.61
k8s-node2: 192.168.99.62
1.2 安装ifconfig
用于查看ip,可选
yum -y install net-tools
1.2 修改ip
vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO = static
IP修改为指定的ip
# 重启网络服务
systemctl restart network
1.3 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld && iptables -F
1.4 关闭selinux
这个是用来加强安全性的一个组件,但非常容易出错且难以定位,一般上来装完系统就先给禁用了
# 查看状态
getenforce
# 永久关闭
sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/config
1.5 关闭swap分区
这个当内存不足时,linux会自动使用swap,将部分内存数据存放到磁盘中,这个这样会使性能下降,为了性能考虑推荐关掉。Prior to Kubernetes 1.22, nodes did not support the use of swap memory, and a kubelet would by default fail to start if swap was detected on a node. In 1.22 onwards, swap memory support can be enabled on a per-node basis.
关闭swap分区
vi /etc/fstab
注释掉 /dev/****/swap 行
# 检查 下面swap 显示都是0
free -h
1.6 修改host文件,
修改主机名
# master
hostnamectl set-hostname k8s-master
# node1
hostnamectl set-hostname k8s-node1
# node2
hostnamectl set-hostname k8s-node2
修改本地host 文件,联通网络
vi /etc/hosts
192.168.99.60 k8s-master
192.168.99.61 k8s-node1
192.168.99.62 k8s-node2
1.7 修改内核参数
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
1.8 加载ip_vs内核模块
如果kube-proxy 模式为ip_vs则必须加载,本文采用iptables
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
设置下次开机自动加载
cat > /etc/modules-load.d/ip_vs.conf << EOF
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack_ipv4
EOF
重启
1.9 配置yum源
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=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
EOF
# 安装kubelet,kubeadm,kubectl
yum install -y kubelet-1.18.8 kubeadm-1.18.8 kubectl-1.18.8
# 设置开机自启动
systemctl enable kubelet
二. 安装
2.1 master 节点初始化
# 只需要在master 节点执行
kubeadm init \
--kubernetes-version 1.18.8 \
--apiserver-advertise-address=192.168.99.60 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.245.0.0/16 \
--image-repository registry.aliyuncs.com/google_containers
--kubernetes-version v1.18.8 指定版本
--apiserver-advertise-address 为通告给其它组件的IP,一般应为master节点的IP地址
--service-cidr 指定service网络,不能和node网络冲突
--pod-network-cidr 指定pod网络,不能和node网络、service网络冲突
--image-repository registry.aliyuncs.com/google_containers 指定镜像源,由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。
如果k8s版本比较新,可能阿里云没有对应的镜像,就需要自己从其它地方获取镜像了。
--control-plane-endpoint 标志应该被设置成负载均衡器的地址或 DNS 和端口(可选)
安装成功后会弹出下面的加入集群得代码
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.99.60:6443 --token 8oq437.w2n7nzqty40xe7ej --discovery-token-ca-cert-hash sha256:692c92bd7caea1c3ce01c6fa0829b7ad21e283a0f07436960025059b2ce3e8c1
执行它的三个命令:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
2.2 设定kubeletl网络(主节点部署)**
部署容器网络,CNI网络插件(在Master上执行,著名的有flannel、calico、canal和kube-router等,简单易用的实现是为CoreOS提供的flannel项目),这里使用Flannel实现。
下载kube-flannel.yml
:
wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
然后修改配置文件,找到如下位置,修改 Newwork
与执行 kubeadm init
输入的网段一致:
Newwork == pod-network-cidr(前面的)
net-conf.json: |
{
"Network": "10.245.0.0/16",
"Backend"": {
"Type": "vxlan"
}
}
修改配置之后安装组件(如果安装的时候卡在pull镜像的时候,试一试手动用docker将镜像拉取下来):
# 主节点安装flannel
kubectl apply -f kube-flannel.yml
查看flannel pod
状态(必须要为Running
状态,如果kube-flannel
起不来,那么就用kubectl describe pod kube-flannel-ds-f5jn6 -n kube-flannel
命令查看pod
起不来的原因,然后去搜度娘获取解决方案):
[root@k8s-master home]# # 必须所有的容器都是Running
[root@k8s-master home]# kubectl get pod --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-flannel kube-flannel-ds-f5jn6 1/1 Running 0 8m21s
kube-system coredns-6d8c4cb4d-ctqw5 1/1 Running 0 42m
kube-system coredns-6d8c4cb4d-n52fq 1/1 Running 0 42m
kube-system etcd-k8s-master 1/1 Running 0 42m
kube-system kube-apiserver-k8s-master 1/1 Running 0 42m
kube-system kube-controller-manager-k8s-master 1/1 Running 0 42m
kube-system kube-proxy-swpkz 1/1 Running 0 42m
kube-system kube-scheduler-k8s-master 1/1 Running 0 42m
查看通信状态:
[root@k8s-master home]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-6d8c4cb4d-ctqw5 1/1 Running 0 52m
coredns-6d8c4cb4d-n52fq 1/1 Running 0 52m
etcd-k8s-master 1/1 Running 0 53m
kube-apiserver-k8s-master 1/1 Running 0 53m
kube-controller-manager-k8s-master 1/1 Running 0 53m
kube-proxy-swpkz 1/1 Running 0 52m
kube-scheduler-k8s-master 1/1 Running 0 53m
# 获取主节点的状态
kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health":"true","reason":""}
[root@k8s-master home]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 52m v1.23.0
查看节点状态(此时还只有主节点,还没添加子节点):
# 查看集群的节点
kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 53m v1.23.0
2.3 子节点加入集群(在子节点上操作)**
初始化会生成join
命令,需要在子节点执行即可,以下token
作为举例,以实际为主,例如:
执行上面的join 命名
kubeadm join 192.168.99.60:6443 --token 8oq437.w2n7nzqty40xe7ej --discovery-token-ca-cert-hash sha256:692c92bd7caea1c3ce01c6fa0829b7ad21e283a0f07436960025059b2ce3e8c1
默认的 join token
有效期限为24小时,当过期后该 token
就不能用了,这时需要重新创建 token
,创建新的join token
需要在主节点上创建,创建命令如下:
# 在master 节点重新创建token
kubeadm token create --print-join-command
加入之后再在主节点查看集群中节点的状态(必须要都为Ready
状态):
# 查看所有节点
kubectl get nodes
NAME STATUS ROLES AGE
k8s-master Ready control-plane,master 63m
k8s-node1 Ready <none> 3m57s
k8s-node2 Ready <none> 29s
如果所有的节点STATUS
都为Ready
的话,那么到此,所有的子节点加入完成!
顺便测试了下开机重启,会自动加入节点。