1.更改linux环境,关闭selinux等
swapoff -a //关闭swap
setenforce 0 //临时关闭selinux
每台机器的以下内容不能相同
sudo cat /sys/class/dmi/id/product_uuid //每台机器的uuid不能相同
ifconfig -a //ip不能相同
开放一下端口
协议 | 方向 | 端口范围 | 作用 | 使用者 |
TCP | 入站 | 6443 | Kubernetes API 服务器 | 所有组件 |
2379-2380 | etcd 服务器客户端 API | kube-apiserver, etcd | ||
10250 | Kubelet API | kubelet 自身、控制平面组件 | ||
10251 | kube-scheduler | kube-scheduler 自身 | ||
10252 | kube-controller-manager | kube-controller-manager 自身 | ||
8080 | kubelet | kubelet自身 |
30000-32767 | NodePort 服务† |
端口号一定要安排明白!!!!否者会出现类似dial tcp 10.96.0.1:443: connect: no route to host错误,如果测试环境一直弄不好,可以关闭防火墙。ps:及其不建议。
允许 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
2.下载安装
首页可以来到github上查询自己想要的版本 官网地址;
https://github.com/kubernetes/kubernetes/releases这里楼主选择
添加镜像
cat <<EOF | sudo tee /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
repo_gpgcheck=0
EOF
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes 下载镜像
sudo systemctl enable --now kubelet
//查看是否安装成功
kubeadm version
kubectl version --client
kubelet --version
出现以下显示证明安装成功,且能看到安装版本
安装插件
yum -y install socat conntrack-tools
3.修改docker组为systemd
提前安装好docker 可以参考:
sudo mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker
4.k8s拉取需要镜像
可以通过kubeadm config images pull命令下载镜像,但是很大概率连接失效,所以使用阿里镜像下载 然后更改镜像名称
如果使用v1.21.0版本,语句可以跟楼主相同,否则请根据以上命令需要的自行拉取
使用镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
docker pull coredns/coredns:1.8.0
但是k8s需要k8s.gcr.io前缀的镜像,这里更改一下名称
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.1 k8s.gcr.io/kube-apiserver:v1.21.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.1 k8s.gcr.io/kube-proxy:v1.21.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.1 k8s.gcr.io/kube-controller-manager:v1.21.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.1 k8s.gcr.io/kube-scheduler:v1.21.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1 k8s.gcr.io/pause:3.4.1
docker tag coredns/coredns:1.8.0 k8s.gcr.io/coredns/coredns:v1.8.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0
5.安装启动
修改主机映射
vi /etc/hosts
初始化
kubeadm init --apiserver-advertise-address=192.168.44.15 --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.21.1 |tee kubeadmin-init.log
出现以下字样初始化成功
要使非 root 用户可以运行 kubectl,请运行以下命令, 它们也是
kubeadm init
输出的一部分:
1. mkdir -p $HOME/.kube
2. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
3. sudo chown $(id -u):$(id -g) $HOME/.kube/config
或者,如果你是
root
用户,则可以运行:
export KUBECONFIG=/etc/kubernetes/admin.conf
这步不能省略,否则会出现Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "kubernetes")
kubeadm reset执行后可以重新初始化
6.安装flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
如果出现Connecting to raw.githubusercontent.com refused,可以执行vi /etc/hosts 在后面添加199.232.96.133 raw.githubusercontent.com
验证master是否成功
kubectl get nodes
如果出现ready则代表安装完成 master节点已经注册到了k8s 如果出现NotReady 可以执行以下语句判断服务器状态
kubectl get nodes -o yaml
以下绿色部分没有问题 红色部分异常message:docker: network plugin is not ready: cni config uninitialized'
如果出现以上问题 查看日志
journalctl -f -u kubelet.service
如果出现以下日志
"Error validating CNI config list" configList="{\n \"name\": \"cbr0\",\n \"cniVersion\": \"0.3.1\",\n \"plugins\": [\n {\n \"type\": \"flannel\",\n \"delegate\": {\n \"hairpinMode\": true,\n \"isDefaultGateway\": true\n }\n },\n {\n \"type\": \"portmap\",\n \"capabilities\": {\n \"portMappings\": true\n }\n }\n ]\n}\n" err="[failed to find plugin \"flannel\" in path [/opt/cni/bin] failed to find plugin \"portmap\" in path [/opt/cni/bin]]"
执行
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
yum clean all
yum install kubernetes-cni -y
如果出现其他异常 请参考:
https://blog.csdn.net/qq_34857250/article/details/825625147.卸载
1. kubeadm reset -f
2. modprobe -r ipip
3. lsmod
4. rm -rf ~/.kube/
5. rm -rf /etc/kubernetes/
6. rm -rf /etc/systemd/system/kubelet.service.d
7. rm -rf /etc/systemd/system/kubelet.service
8. rm -rf /usr/bin/kube*
9. rm -rf /etc/cni
10. rm -rf /opt/cni
11. rm -rf /var/lib/etcd
12. rm -rf /var/etcd