https://www.cnblogs.com/superlinux/p/12582009.html
基础准备[ALL]
# 1. 关闭防火墙功能
systemctl stop firewalld
systemctl disable firewalld
# 2.关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
# 3. 关闭swap
swapoff -a
#或将命令加入开机启动
echo "swapoff -a" >>/etc/profile
# 4. 服务器规划 ip和主机名按照实际情况
cat > /etc/hosts << EOF
192.168.26.134 k8smaster1
192.168.26.135 k8snode1
192.168.26.136 k8snode2
EOF
#5. 主机名配置:
hostnamectl set-hostname k8smaster1
bash
#6. 时间同步配置
yum install -y ntpdate
ntpdate time.windows.com
ntpdate ntp.aliyun.com
#开启转发
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
EOF
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.d/k8s.conf
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.d/k8s.conf
sysctl --system
# 以上可以全部复制粘贴直接运行,但是主机名配置需要重新修改
安装docker[ALL]
#源添加
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
#安装docker-ce
yum install docker-ce -y
# 安装指定版版本
yum -y install docker-ce-18.09.9-3.el7
###############
# 24之后的版本默认支持containerd
containerd config default > /etc/containerd/config.toml
sed -i 's#registry.k8s.io/pause:3.6#registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6#g' /etc/containerd/config.toml
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml
#也可以查看版本安装
#yum list docker-ce --showduplicates | sort -r
#启动docker
systemctl start docker
systemctl enable containerd --now
docker镜像加速&更换镜像位置[ALL]
#第一个是修改默认镜像仓库、
#第二个是更改 docker cgroup的驱动器 改为system
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://docker.m.daocloud.io"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
K8S yum源[ALL]
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=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
安装kubadmin 和kublete[ALL]
yum install -y kubelet-1.17.0 kubeadm-1.17.0 kubectl-1.17.0
systemctl enable kubelet
master部署[master]
kubeadm init \
--apiserver-advertise-address=210.47.47.11 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
##如下为结果
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.26.134:6443 --token h8ppvi.m3slvdd04yfi69l0 \
--discovery-token-ca-cert-hash sha256:578d4ccd2514ba5c463f4bec700a70d40efda104019ce57f6e9683cd9ccd85b1
### 最新
kubeadm init --apiserver-advertise-address=192.168.204.133 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.29.9 --service-cidr=10.1.0.0/16
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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.204.133:6443 --token bzvofy.ybhe06r9z2dic4z1 \
--discovery-token-ca-cert-hash sha256:51a5759b51c2f48d892be6434d03c25e2e054db9f7573e78ba5fb0de84d25542
配置 kubctl [master]
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看集群和node状态并处理问题
[root@k8smaster1 ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
[root@k8smaster1 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8smaster1 NotReady master 5m46s v1.17.0
#kubctl get nodes 一直是noready
看/var/log/messages 或者journalctl -f -u kubelet
Jan 26 21:54:52 k8smaster1 kubelet[43973]: E0126 21:54:52.699858 43973 kubelet.go:2183] Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
Jan 26 21:54:56 k8smaster1 kubelet[43973]: W0126 21:54:56.735289 43973 cni.go:237] Unable to update cni config: no networks found in /etc/cni/net.d
是因为flannel 没装导致,安装flannel 过程比较慢关注flannel的pod状态
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
从机执行[]
kubeadm join 210.47.47.11:6443 --token hl3fx2.dan7m88q0um4z2vi --discovery-token-ca-cert-hash sha256:08d4139e4a13dfbca62dfac7aca88a80ad96bc464e718324bd4eea2566fee7c4
第二次安装2409
补充1 内核升级
补充2 containerd相关配置
补充3 registry 国内相关
#docker修改
/etc/docker/daemon.json 添加
{
"registry-mirrors": ["https://docker.m.daocloud.io"],
"exec-opts": ["native.cgroupdriver=systemd"]
"ipv6": false,
"fixed-cidr-v6": ""
}
# containerd 修改 ,k8s默认走这的registry 不走docker 的
sed -i 's#registry.k8s.io/pause:3.6#registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6#g' /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".registry]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://https://docker.m.daocloud.io"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]
endpoint = ["https://registry.aliyuncs.com/k8sxio"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]
endpoint = ["xxx"]
daocloud
国内其他registry
修改containerd registry
补充4 system管理cgroup
##kubelet
# 查看当前kubelet使用哪种方式驱动 cgroup
kubectl get cm kubelet-config -n kube-system -oyaml |grep -i cgroup
#修改 cgroupDriver: systemd
kubectl edit cm kubelet-config -n kube-system
## containerd
sed -i 's#systemdcgroup = false#systemdcgroup = true#g' /etc/containerd/config.toml
systemctl restartd containerd
##############
#containerd 重启之后 docker 也需要重启因为之前的监听文件的协议变了,与下面问题有关
docker 报错如下
Failed to get event" error="rpc error: code = Unavailable desc = error reading from server: EOF" module=libcontainerd namespace=moby
#### 这里有个问题 目前我的电脑是默认的
如果 containerd 改为 systemdcgroup = true
ep 17 02:24:45 tam3 kubelet: E0917 14:24:45.592732 12366 run.go:74] "command failed" err="failed to run Kubelet: validate service connection: validate CRI v1 runtime API for endpoint \"unix:///var/run/containerd/containerd.sock\": rpc error: code = Unimplemented desc = unknown service runtime.v1.RuntimeService"
Sep 17 02:24:45 tam3 systemd: kubelet.service: main process exited, code=exited, status=1/FAILURE
###报错 感觉是 system 管理 containerd 的 cgroup runtime.v1 可能会变成v2 就不行了只能改回去 之后再研究
## docker
#查看
docker info | grep Cgroup
-- Cgroup Driver: cgroupfs
#修改
/etc/docker/daemon.json 添加
{
"registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
systemctl restart docker
快照创建机器后操作
1、OS相关
# 修改ip及主机名
sed 's#192.168.204.131#192.168.204.130#g' /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network
hostname set-hostname tam0
#修改集群主机hosts
echo "192.168.204.130 tam0" >> /etc/hosts
echo "192.168.204.131 tam1" >> /etc/hosts
echo "192.168.204.134 tam4" >> /etc/hosts
scp /etc/hosts tam0:/etc/hosts
scp /etc/hosts tam4:/etc/hosts
#免密(客户端执行,生成公钥匙,分发到目的服务器)
ssh-keygen -t rsa
ssh-copy-id 192.168.204.130
ssh-copy-id 192.168.204.134
ssh-copy-id 192.168.204.131
# 分发命令
编辑 all.sh
for i in 0 1 4 ; do ssh tam$i -C "hostname && $* " ;done
#修改时区
timedatectl set-timezone Asia/Shanghai
#修改dns
echo "nameserver 223.5.5.5" > /etc/resolv.conf
echo "nameserver 223.6.6.6" >> /etc/resolv.conf
sleep 10
ping baidu.com -c2
# 关闭防火墙
systemctl disable firewalld --now
systemctl status firewalld
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
# 关闭swap
swapoff -a
#或将命令加入开机启动
echo "swapoff -a" >>/etc/profile
#时间同步配置
yum install -y ntpdate
ntpdate ntp.aliyun.com
#开启转发
echo -e "net.bridge.bridge-nf-call-iptables = 1 \nnet.ipv4.ip_forward = 1" > /etc/sysctl.d/k8s.conf
sysctl --system
#升级内核 见补充1
init 6重启后
uname -r 检查
2、容器基础相关
https://www.cnblogs.com/caixiaozi/p/18154003
########docker
curl https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
#安装docker-ce
yum install docker-ce -y
#也可以查看版本安装
yum list docker-ce --showduplicates | sort -r
#第一个是修改默认镜像仓库、
#第二个是更改 docker cgroup的驱动器 改为system
mkdir -p /etc/docker
echo -e '{\n "registry-mirrors": ["https://docker.m.daocloud.io"],\n "exec-opts": ["native.cgroupdriver=systemd"]\n }' >>/etc/docker/daemon.json
sudo systemctl daemon-reload
sudo systemctl restart docker
###############24之后的版本默认支持containerd
containerd config default > /etc/containerd/config.toml
sed -i 's#registry.k8s.io/pause:3.6#registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6#g' /etc/containerd/config.toml
sed -i 's#systemd_cgroup = false#systemd_cgroup = true#g' /etc/containerd/config.toml
grep systemd_cgroup /etc/containerd/config.toml
systemctl enable containerd --now
systemctl enable docker --now
#启动docker 默认会自动启动containerd
3、K8s
###yum安装 kubelet kubeadm kubectl
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.29/rpm/repodata/repomd.xml.key
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet --now
### kubeadmin初始化k8s amster
kubeadm init --apiserver-advertise-address=192.168.204.130 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.29.9 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
### 初始化结果
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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.204.130:6443 --token n2gv0a.74xu0uj39do2zghu \
--discovery-token-ca-cert-hash sha256:054b10e8d92c9919c38a208a4bba62f5f78e0a2c23e7acca91ef3a3944ad5bfe
### cni
wget https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml
kubectl create -f calico.yaml
watch kubectl get pods -A