3台服务器2核4G (一台master ,2台node)
创建VPC网络(用于K8S-cluster) IP地址范围 (不要选192.168.0.0/16和172.16.0.0/16,这两个预留给了services 和pods)
创建VPC下的交换机(私有网络,子网)
绑定公网IP
为每个服务器分配一个公网IP
打开组内互信
测试:在VPC里任意一台机器测试PING 其他机器
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
#以下是在安装k8s的时候使用
yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6
systemctl enable docker --now
这里额外添加了docker的生产环境核心配置cgroup
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
#各个机器设置自己的域名
hostnamectl set-hostname xxxx
(master,node1,node2)
# 将 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
#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
# 设置yum源
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
#允许 安装kubelet、kubeadm、kubectl
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
# 启动kubelet
sudo systemctl enable --now kubelet
查看 kubelet状态
systemctl status kubelet
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
chmod +x ./images.sh && ./images.sh
#所有机器添加master域名映射,以下需要修改为自己的(注意,是所有机器,包括master和节点)
echo "172.31.1.4 cluster-endpoint" >> /etc/hosts
#主节点初始化(只在master机器上运行)
kubeadm init \
--apiserver-advertise-address=172.31.1.4 \
--control-plane-endpoint=cluster-endpoint \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16
#注意所有网络范围不可以重叠,即VPC网断,service网段与pod网段都要分开
#如果主节点初始化失败,第二次初始化需要重启kubeadm
#重启一下kubeadm:
kubeadm reset
主节点初始化成功
记录主节点信息
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 bm9tji.rh0o1hf5qhkarv0r \
--discovery-token-ca-cert-hash sha256:138302999aee5f40b7febd9154bd5fcdbe6493b5c185b4495ae58b06b1ce5b14 \
--control-plane
#加入工作节点
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join cluster-endpoint:6443 --token bm9tji.rh0o1hf5qhkarv0r \
--discovery-token-ca-cert-hash sha256:138302999aee5f40b7febd9154bd5fcdbe6493b5c185b4495ae58b06b1ce5b14
**常用命令查看k8s pod信息
#查看集群所有节点
kubectl get nodes
#根据配置文件,给集群创建资源
kubectl apply -f xxxx.yaml
#查看集群部署了哪些应用?
docker ps === kubectl get pods -A
# 运行中的应用在docker里面叫容器,在k8s里面叫Pod
kubectl get pods -A
设置.kube/config
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
安装网络组件
curl https://docs.projectcalico.org/manifests/calico.yaml -O
kubectl apply -f calico.yaml
安装网络组件后等待master节点Ready
kubeadm join cluster-endpoint:6443 --token bm9tji.rh0o1hf5qhkarv0r \
--discovery-token-ca-cert-hash sha256:138302999aee5f40b7febd9154bd5fcdbe6493b5c185b4495ae58b06b1ce5b14
这里有个坑,要在安全组放行6443端口
不知道为什么。开了组内互信,也要放行6443端口