K8s服务器搭建
【代码】K8s服务器搭建。
·
K8s服务器搭建
1、系统配置
- 关闭防火墙
systemctl stop firewalld
- 禁用SELINUX
setenforce 0
vi /etc/selinux/config
SELINUX=disabled
- 添加k8s配置文件
vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
- 执行命令使其生效
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
2、安装Docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#查看docker版本
yum list docker-ce.x86_64 --showduplicates |sort -r
#下载18.09.3版本
yum install -y --setopt=obsoletes=0 \
docker-ce-18.09.3
systemctl start docker
systemctl enable docker
3、kubectl配置
- kube-proxy开启ipvs的前置条件
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
#修改权限
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
- 安装kubeadm和kubelet
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum makecache fast
yum install -y kubelet-1.15.9 kubeadm-1.15.9 kubectl-1.15.9
- 关闭swap
#关闭系统的Swap,如果不关闭,默认配置下kubelet将无法启动
swapoff -a
#修改 /etc/fstab 文件,注释掉 SWAP 的自动挂载,使用free -m确认swap已经关闭
#修改/etc/sysctl.d/k8s.conf添加下面一行
vm.swappiness=0
sysctl -p /etc/sysctl.d/k8s.conf
4、使用kubeadm init初始化集群
##pod-network-cidr 不要和绑定ip一个网段
kubeadm init \
--kubernetes-version=v1.25.0 \
--pod-network-cidr=172.17.0.0/16 \
--apiserver-advertise-address=172.24.105.186 \
--image-repository registry.k8s.io \
--token-ttl 0
5、配置常规用户如何使用kubectl访问集群
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
6、创建节点
vim pod.yaml
piVersion: apps/v1
kind: Deployment
metadata:
name: test-file
namespace: default
labels:
appkey: test-file
spec:
replicas: 1
selector:
matchLabels:
appkey: test-file
template:
metadata:
labels:
appkey: test-file
spec:
containers:
- name: test-file
image: centos:latest
command: ["/bin/bash", "-ce", "tail -f /dev/null"]
imagePullPolicy: IfNotPresent
volumeMounts:
- name: test-file-data
mountPath: /mnt
volumes:
- name: test-file-data
hostPath:
path: /mnt
kubectl create -f pod.yaml
7、修改api权限
##修改配置文件:
vim /var/lib/kubelet/config.yaml
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
anonymous:
enabled: true ##将false改为true
webhook:
cacheTTL: 0s
enabled: true
x509:
clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
mode: AlwaysAllow ###修改为所有人皆可访问
##重启kubelet:
systemctl restart kubelet
##参考链接:https://www.jianshu.com/p/4a1af3ee06ba
初始化中遇到的报错及解决方案:
1、初始化报错 container runtime is not running
- 安装docker或者containerd之后,默认在/etc/containerd/config.toml禁用了CRI,需要注释掉disabled_plugins = [“cri”],否则执行kubeadm进行部署时会报错,注释完成后,重启containerd,并重新初始化
# 重启containerd
[root@k8s-master ~]# systemctl restart containerd
# 重置节点
[root@k8s-master ~]# kubeadm reset
#重复上一步,重新初始化
- kubeadm config images pull 拉取镜像失败的问题
首先查看kubeadm config 依赖的images有哪些
#查看kubeadm config所需的镜像
kubeadm config images list
#执行结果如下
k8s.gcr.io/kube-apiserver:v1.15.9
k8s.gcr.io/kube-controller-manager:v1.15.9
k8s.gcr.io/kube-scheduler:v1.15.9
k8s.gcr.io/kube-proxy:v1.15.9
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns/coredns:1.3.1
- 从国内镜像源拉取这些镜像
#从国内镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.9
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.9
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.9
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.9
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10
docker pull coredns/coredns:1.3.1
- 对images重命名
#将拉取下来的images重命名为kubeadm config所需的镜像名字
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.9 k8s.gcr.io/kube-apiserver:v1.15.9
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.9 k8s.gcr.io/kube-controller-manager:v1.15.9
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.9 k8s.gcr.io/kube-scheduler:v1.15.9
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.9 k8s.gcr.io/kube-proxy:v1.15.9
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.1-0 k8s.gcr.io/etcd:3.3.10
docker tag coredns/coredns:1.8.6 k8s.gcr.io/coredns/coredns:1.3.1
2、初始化报错: [WARNING IsDockerSystemdCheck]: detected “cgroupfs” as the Docker cgroup driver. The recommended driver is “systemd”.
vim /etc/docker/daemon.json
#添加如下内容
{
"exec-opts":["native.cgroupdriver=systemd"]
}
#重启docker
systemctl restart docker
3、节点报错:1 node(s) had taints that the pod didn‘t tolerate
#查看Taints
[root@localhost k8s]# kubectl describe nodes localhost |grep Taints
Taints: node-role.kubernetes.io/control-plane:NoSchedule
#清理Taints
[root@localhost k8s]# kubectl taint nodes --all node-role.kubernetes.io/control-plane-
node/localhost untainted
#再次查看Taints 已被删除
[root@localhost k8s]# kubectl describe nodes localhost |grep Taints
Taints: <none>
4、节点报错:“Container runtime network not ready” networkReady=“NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized”
vim /var/lib/kubelet/kubeadm-flags.env
#注释掉--network-plugin=cni
#重启kubelet
systemctl restart kubelet
参考文档链接:
构建参考:https://www.kubernetes.org.cn/5551.html
构建参考:https://www.cnblogs.com/renshengdezheli/p/16686769.html
使用参考:https://www.cnblogs.com/xingzheanan/p/14867340.html
报错参考:https://blog.csdn.net/weixin_46601322/article/details/126722122
拉取镜像失败问题参考:https://blog.csdn.net/qq_40279964/article/details/125430992
更多推荐
已为社区贡献1条内容
所有评论(0)