K8s安装、卸载
默认token有效期为24小时,当过期之后,该token就不可用了。错误:给 k8snode1 节点使用 kubernetes join 命令的时候,出现以下错误。由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。错误:我们再执行查看节点的时候, kubectl get nodes 会出现问题。kubeadm是官方社区推出的一个用于快速部署kubernetes集群的
文章目录
资料:
链接:https://pan.baidu.com/s/10Kn1lIMPaVNRP9o9UdORmA
提取码:yyds
理解!
kube-proxy
–>网络代理
kubelet
–>厂长,由他创建集群
api-server
–>统一入口
controller-manage
–>控制管理
etcd
–>文件库
schedluer
–>节点调度器
coredns
–>核心dns
pause
–>Pod的根容器
flannel
-->网络插件
1. 安装要求(一定要满足!!),所有节点
在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
- 一台或多台机器,操作系统 CentOS7.x-86_x64
- 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
- 可以访问外网,需要拉取镜像,如果服务器不能上网,需要提前下载镜像并导入节点
- 禁止swap分区
2. 准备环境,所有节点
角色 | IP |
---|---|
master | 192.168.111.101 |
node | 192.168.111.102 |
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
# 关闭swap
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 根据规划设置主机名
hostnamectl set-hostname <hostname>
hostname 命令查看主机名
# 添加hosts!!!
cat >> /etc/hosts << EOF
192.168.111.101 master
192.168.111.102 node
EOF
# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
# 时间同步
yum install ntpdate -y
ntpdate time.windows.com
3. 所有节点安装Docker/kubeadm/kubelet
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker
。
3.1 安装Docker
sudo yum remove docker*
sudo yum install -y yum-utils
#配置docker的yum地址
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装指定版本
sudo yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6
# 启动&开机启动docker
systemctl enable docker --now
# docker加速配置
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://441nlr92.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
docker --version
# Docker version 20.10.7, build f0df350
3.2 安装kubeadm,kubelet和kubectl
由于版本更新频繁,这里指定版本号
部署:
#配置k8s的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
EOF
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9
#启动kubelet
sudo systemctl enable kubelet --now
systemctl status kubelet
# 查看状态, 主节点kubeadm init初始化之后kubelet 成功启动!
# 从节点 ,kubeadm join加入集群之后kubelet 成功启动!
4.清除之前部署的网络,所有节点
ifconfig
用
删除重生
的方法,首先停用
网络,然后删除
配置
ifconfig cni0 down
ip link delete cni0
#这是pod的ip地址
ifconfig flannel.1 down
ip link delete flannel.1
5. 部署Kubernetes Master
在 192.168.111.101(Master)执行
kubeadm init \
--apiserver-advertise-address=192.168.111.101 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.15 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16
# 要保证各个网段 不一样
# 使用 kubeadm init时,请指定pod-network-cidr。确保 主机/主网络的ip 不在你引用的子网中!!!
# 即如果你的网络运行在192.168..使用10.0.0.0/16
# 如果你的网络是10.0..使用192.168.0.0/16
使用kubectl(master)
工具:
rm -rf $HOME/.kube
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes #查看节点
docker images查看镜像
八个镜像
一个都不能少,下面还需要网络插件!
registry.aliyuncs.com/google_containers/kube-proxy v1.20.15 46e2cd1b2594 8 months ago 99.7MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.20.15 323f6347f5e2 8 months ago 122MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.20.15 d6296d0e06d2 8 months ago 116MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.20.15 9155e4deabb3 8 months ago 47.3MB
quay.io/coreos/flannel v0.15.1 e6ea68648f0c 10 months ago 69.5MB
registry.aliyuncs.com/google_containers/etcd 3.4.13-0 0369cf4303ff 2 years ago 253MB
registry.aliyuncs.com/google_containers/coredns 1.7.0 bfe3a36ebd25 2 years ago 45.2MB
registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 2 years ago 683kB
不行的话,就手动拉取
docker pull
,kubeadm reset
重置后再kubeadm init
链接:https://pan.baidu.com/s/1uu06EGckeXX0REvX_xqBvA
提取码:yyds
网络插件镜像:quay.io/coreos/flannel:v0.15.1
镜像
quay.io/coreos/flannel
版本需要和kube-flannel.yml
中的一致!
docker load -i flannel.tar #即可获取镜像
6. 加入Kubernetes Node
在192.168.111.102/13(Node)执行
向集群
添加新节点
,执行在kubeadm init
输出的kubeadm join
命令:
kubeadm join 192.168.111.101:6443 --token 3xtclw.c29as49tg9mp7259 \
--discovery-token-ca-cert-hash sha256:e026d26b0d3d0f1d0c90ea63eb9c5fa01fdd189b07377463ca981a99644c13bc
加入成功!有
报错
的话看Error信息
,对应处理即可!
默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,操作如下:
kubeadm token create --print-join-command
子节点
必须有这三个镜像
查看节点 kubectl get nodes
必须都是Ready状态!
7. 部署CNI网络插件,Master节点
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#不行换下面的方式
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: psp.flannel.unprivileged
annotations:
seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
privileged: false
volumes:
- configMap
- secret
- emptyDir
- hostPath
allowedHostPaths:
- pathPrefix: "/etc/cni/net.d"
- pathPrefix: "/etc/kube-flannel"
- pathPrefix: "/run/flannel"
readOnlyRootFilesystem: false
# Users and groups
runAsUser:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAny
# Privilege Escalation
allowPrivilegeEscalation: false
defaultAllowPrivilegeEscalation: false
# Capabilities
allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
defaultAddCapabilities: []
requiredDropCapabilities: []
# Host namespaces
hostPID: false
hostIPC: false
hostNetwork: true
hostPorts:
- min: 0
max: 65535
# SELinux
seLinux:
# SELinux is unused in CaaSP
rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
rules:
- apiGroups: ['extensions']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
labels:
tier: node
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "flannel",
"delegate": {
"hairpinMode": true,
"isDefaultGateway": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
hostNetwork: true
priorityClassName: system-node-critical
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.15.1
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.15.1
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN", "NET_RAW"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
kube-flannel.yml
为该yml的文件名
#运行命令,加载该文件
kubectl apply -f kube-flannel.yml
#查看状态
kubectl get pods -n kube-system
8. 测试kubernetes集群
在Kubernetes集群中创建一个pod,验证是否正常运行:
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
访问地址:http://NodeIP:Port
9.错误
curl -sSL http://localhost:10248/healthz connect: connection refused
The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get http://localhost:10248/healthz: dial tcp [::1]:10248: connect: connection refused
:::rm -rf $HOME/.kube
首先需要到 k8smaster1 节点,创建一个文件
# 创建文件夹
mkdir /etc/systemd/system/kubelet.service.d
# 创建文件
vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# 添加如下内容
Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true --fail-swap-on=false"
# 重置
kubeadm reset
然后删除刚刚创建的配置目录
rm -rf $HOME/.kube
在 k8smaster1 重新初始化
kubeadm init --apiserver-advertise-address=92.168.60.151:6443 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
:::
kubectl get nodes 出现问题Unable to connect to the server
Unable to connect to the server: #删除目录后重建
:::解决方法
rm -rf $HOME/.kube
然后再次创建一下即可
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
这个问题主要是因为我们在执行 kubeadm reset 的时候,没有把 $HOME/.kube 给移除掉,再次创建时就会出现问题了
:::
kubeadm join添加 k8snode1 节点到集群[ERROR FileAvailable–etc-kubernetes-pki-ca.crt]
[root@k8smaster1 ~]# kubeadm join 192.168.60.151:6443 --token jkcz0t.3c40t0bqqz5g8wsb --discovery-token-ca-cert-hash sha256:bc494eeab6b7bac64c0861da16084504626e5a95ba7ede7b9c2dc7571ca4c9e5
[ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1 #设为1即可
[ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists #文件存在,删除即可
:::解决方法
echo “1” > /proc/sys/net/ipv4/ip_forward
rm -rf /etc/kubernetes/pki/ca.crt
:::
docker 版本问题
this Docker version is not on the list of validated versions: 20.10.17. Latest validated version: 19
docker 版本问题
1.卸载docker服务
systemctl stop docker
yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
2.重新下载docker服务
# 指定docker版本
yum install docker-ce-18.06.1.ce-3.el7 docker-ce-cli-18.06.1.ce-3.el7 containerd.io docker-compose-plugin
# docker官网指定格式
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin
coredns- 0/1,状态为Running
过一会再查看
kubectl get po -A
# 使用 kubeadm init时,请指定pod-network-cidr。确保 主机/主网络的ip 不在你引用的子网中!!!
# 即如果你的网络运行在192.168..使用10.0.0.0/16
# 如果你的网络是10.0..使用192.168.0.0/16
“cni0” already has an IP address different from 10.244.1.1/24
用
删除重生
的方法,首先停用
网络,然后删除
配置
ifconfig cni0 down
ip link delete cni0
#这是pod的ip地址
ifconfig flannel.1 down
ip link delete flannel.1
kubeadm reset
重置后再kubeadm init
卸载K8s
执行kubeadm reset命令
清空K8s集群
设置卸载管理组件
systemctl stop kubelet.service # 停掉kubelet
kubeadm reset
# 输入y
# 不行就多试几次
卸载管理组件
yum erase -y kubelet kubectl kubeadm kubernetes-cni
docker全部清理
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
docker 重启
systemctl daemon-reload
systemctl restart docker
systemctl stop docker service
更多推荐
所有评论(0)