安装docker环境
请参考 https://www.yuque.com/luoqiz/vcxnfs/yzow6q
机器配置
#设置每个机器自己的hostname
hostnamectl set-hostname xxx
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
#关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
虚拟机将桥接的IPV4流量传递到iptables链
#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
配置阿里云镜像源
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=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
安装K8S组件
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
yum install -y kubelet-1.22.3 kubeadm-1.22.3 kubectl-1.22.3 –disableexcludes=kubernetes
sudo systemctl enable --now kubelet && sudo systemctl start kubelet
初始化K8S master节点
# 所有节点指明主节点域名、和主机名
echo "192.168.116.131 cluster-endpoint" >> /etc/hosts
echo "192.168.116.131 master.k8s" >> /etc/hosts
echo "192.168.116.132 node1.k8s" >> /etc/hosts
echo "192.168.116.133 node2.k8s" >> /etc/hosts
echo "主节点IP 主节点hostname" >> /etc/hosts
echo "worker节点IP worker节点hostname" >> /etc/hosts
kubeadm init \
--apiserver-advertise-address 192.168.116.131 \
--control-plane-endpoint=cluster-endpoint \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.3 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=Swap
- —apiserver-advertise-address:指定master节点版本
- —control-plane-endpoint:k8s域名解析主节点域名
- —kubernetes-version:指定kubeadm版本
- —pod-network-cidr:指定pod所属网络
- —service-cidr:指定service网段 (此处因使用
kube-flannel.yml
,特设置10.244.0.0/16
) - —ignore-preflight-errors=Swap/all:忽略 swap/所有 报错
若是出现[kubelet-check] It seems like the kubelet isn't running or healthy.
说明docker 服务未配置cgroupfs
可以参考 dockers 部署安装那一篇文章
K8S 安装成功后的提示信息
······
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
####################### 主节点加入 ###########################
kubeadm join cluster-endpoint:6443 --token 1i2r1u.2w0u5zhhwon390zu \
--discovery-token-ca-cert-hash sha256:b2be8c4fdd92d73b8aa43ad23ebf8ff0528348b2a8f4250637862e33e5a5c4d7 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
####################### worker点加入 ###########################
kubeadm join cluster-endpoint:6443 --token 1i2r1u.2w0u5zhhwon390zu \
--discovery-token-ca-cert-hash sha256:b2be8c4fdd92d73b8aa43ad23ebf8ff0528348b2a8f4250637862e33e5a5c4d7
kubectl 对用户的配置
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl 测试
kubectl get nodes
其它节点加入命令 (root用户执行,24小时内有效)
kubeadm join cluster-endpoint:6443 --token 1i2r1u.2w0u5zhhwon390zu \
--discovery-token-ca-cert-hash sha256:b2be8c4fdd92d73b8aa43ad23ebf8ff0528348b2a8f4250637862e33e5a5c4d7
#若是加入节点的 token 过期,则重新创建
kubeadm token create --print-join-command
部署pod网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://luoqiz-jdk.oss-cn-hongkong.aliyuncs.com/kube-flannel.yml
kubectl delete -f https://luoqiz-jdk.oss-cn-hongkong.aliyuncs.com/kube-flannel.yml
#更新docker源
kubectl apply -f https://luoqiz-jdk.oss-cn-hongkong.aliyuncs.com/kube-flannel-custom.yml
注意:此文件中需要的docker镜像从国外docker服务器下载,速度慢甚至无法下载。需要更新为国内源。具体修改信息访问:
https://blog.csdn.net/zsd498537806/article/details/85157560
一些相关命令
kubectl get nodes #获取所有节点
kubectl get ns #获取所有命名空间
kubectl get pods --all-namespaces #获取所有命名空间的pods
watch kubectl get pod -n kube-system -o wide #监听其它节点的初始化
kubectl get all # 查看系统内的所有资源
kubectl get all -o wide #查看系统分配的资源信息
主节点安装可视化界面插件(在企业中目前没有见到使用这个的)
https://github.com/kubernetes/dashboard/releases
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml