Kubernets
简介
kubeadm-单节点-ubuntu
kubeadm-单节点-centos
资源管理
Namespace
Pod
Pod控制器
Pod生命周期
Pod调度
Label
Service
数据存储
安全认证
DashBoard
本文档使用 MrDoc 发布
-
+
home page
kubeadm-单节点-ubuntu
# 架构 | 集群角色 | 主机名 | IP | 系统架构 | | --- | --- | --- | --- | | master主控 | master | 192.168.1.100 | ubuntu-arm | | node节点1 | node1 | 192.168.1.101 | ubuntu-arm | # 环境初始化 三台机器分别添加解析 ```asp cat << EOF >> /etc/hosts 192.168.1.100 master 192.168.1.101 node1 EOF ``` 三台机器关闭防火墙 ```asp systemctl status ufw systemctl stop ufw systemctl disable ufw ``` 三台机器开启时间同步 ```asp mv /etc/localtime{,.back} ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime date ``` 三台机器关闭swap分区 ```asp vim /etc/fstab ``` 三台设备添加网桥过滤和地址转发功能 ```asp cat <<EOF> /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF ``` 三台机器重新加载配置 ```asp sysctl --system ``` 三台机器加载网桥过滤模块 ```asp modprobe br_netfilter ``` 三台机器查看网桥过滤模块是否加载成功 ```asp lsmod | grep br_netfilter ``` # 安装docker 三台机器添加docker源 ```asp apt update ``` ```asp apt -y install docker.io ``` 三台机器配置Docker修改cgroups为systemd ```asp mkdir -p /etc/docker ``` ```asp cat <<EOF > /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"] } EOF ``` ```asp systemctl daemon-reload ``` 三台机器启动docker ```asp systemctl enable --now docker ``` 三台机器查看docker状态 ```asp systemctl status docker ``` # 集群组件安装 安装依赖环境 ```asp apt-get -y install ca-certificates curl software-properties-common apt-transport-https ``` ```asp curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add - ``` ```asp tee /etc/apt/sources.list.d/kubernetes.list <<EOF deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF ``` ```asp apt update ``` 三台机器安装kubeadm、kubelet、kubectl ```asp apt -y install kubectl=1.21.0-00 kubelet=1.21.0-00 kubeadm=1.21.0-00 ``` 禁止版本更新 ```asp apt-mark hold kubelet kubeadm kubectl ``` # 集群镜像拉取 推荐镜像版本 ```asp kubeadm config images list --kubernetes-version=v1.21.0 ``` 添加K8s镜像源 ```asp docker pull k8s.gcr.io/kube-apiserver:v1.21.0 docker pull k8s.gcr.io/kube-controller-manager:v1.21.0 docker pull k8s.gcr.io/kube-scheduler:v1.21.0 docker pull k8s.gcr.io/kube-proxy:v1.21.0 docker pull k8s.gcr.io/pause:3.4.1 docker pull k8s.gcr.io/etcd:3.4.13-0 docker pull k8s.gcr.io/coredns/coredns:v1.8.0 ``` 查看镜像 ```asp docker images ``` 修改kubeadm启动kubelet时使用的ip,查看kubelet的环境变量文件: ```asp systemctl cat kubelet.service | grep EnvironmentFile ``` kubeadm官方说第一个文件是自动生成的,不建议修改。因此我们修改第二个(不存在则创建) ```asp vim /etc/default/kubelet ``` ```asp KUBELET_EXTRA_ARGS=--node-ip=<指定公网IP> ``` # Master初始化 初始化集群配置文件 ```asp cat <<EOF> kubeadm-init.yaml apiVersion: kubeadm.k8s.io/v1beta2 kind: InitConfiguration bootstrapTokens: - groups: - system:bootstrappers:kubeadm:default-node-token token: abcdef.0123456789abcdef ttl: 24h0m0s usages: - signing - authentication localAPIEndpoint: advertiseAddress: "0.0.0.0" #公网 IP bindPort: 6443 --- apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration kubernetesVersion: v1.21.0 #确保使用与你的kubeadm版本兼容的Kubernetes版本 controlPlaneEndpoint: "0.0.0.0:6443" #你的公网IP和端口 certificatesDir: /etc/kubernetes/pki clusterName: kubernetes networking: dnsDomain: cluster.local podSubnet: "10.244.0.0/16" serviceSubnet: 10.96.0.0/12 etcd: local: dataDir: /var/lib/etcd extraArgs: listen-client-urls: https://0.0.0.0:2379 listen-peer-urls: https://0.0.0.0:2380 apiServer: timeoutForControlPlane: 4m0s controllerManager: {} dns: {} scheduler: {} EOF ``` 在master节点执行,初始化 ```asp kubeadm init --config=kubeadm-init.yaml ``` 在master节点执行,设置Kubernetes配置文件 ```asp mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config ``` # Node 初始化 master节点查看kubeadm加入集群的token信息 ```asp kubeadm token create --print-join-command --ttl 0 ``` node节点设置公网IP(vpc网络特殊配置) ```asp ip link add name ens6 type dummy ip addr add 公网IP/24 dev ens6 ip link set dev ens6 up ``` 在node1和node2执行(token信息是集群初始化之后生成,根据自己集群的输出信息在node节点执行) ```asp kubeadm join 192.168.1.100:6443 --token fgbmpk.p2629mxjy1yd0la5 --discovery-token-ca-cert-hash sha256:31ac28d970a3d6cca982f309e6a07391cf9ea0498de902803717ade45d45e9e2 ``` 查看集群信息(notready状态是网络不通) ```asp kubectl get nodes ``` ```asp NAME STATUS ROLES AGE VERSION master NotReady master 10m v1.17.4 node1 NotReady <none> 6m11s v1.17.4 node2 NotReady <none> 3m2s v1.17.4 ``` # 集群网络安装 > 在Master节点执行 K8s支持多种网络插件如flannel、calico、canal等(版本对应版本) ## flannel 安装flannel.yml ```asp kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml ``` 查看状态 ```asp watch kubectl get pods -n kube-system ``` ```asp NAME READY STATUS RESTARTS AGE coredns-558bd4d5db-d8cll 1/1 Running 0 12m coredns-558bd4d5db-dqtw8 1/1 Running 0 12m etcd-jp-master 1/1 Running 0 12m kube-apiserver-jp-master 1/1 Running 0 12m kube-controller-manager-jp-master 1/1 Running 0 12m kube-proxy-5pcm8 1/1 Running 0 12m kube-proxy-zsmc5 1/1 Running 0 12m kube-scheduler-jp-master 1/1 Running 0 12m ``` ```asp kubectl delete -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml ``` ## calico 安装calico.yaml ```asp kubectl apply -f https://projectcalico.docs.tigera.io/archive/v3.21/manifests/calico.yaml ``` 查看安装进度 ```asp watch kubectl get pods -n kube-system ``` ```asp NAME READY STATUS RESTARTS AGE calico-kube-controllers-65f8bc95db-6cm49 1/1 Running 1 3m calico-node-72rtg 1/1 Running 1 3m calico-node-t9rh4 1/1 Running 1 3m calico-node-xc8f9 1/1 Running 1 3m coredns-5dbbf58dbf-czp64 1/1 Running 1 3m coredns-5dbbf58dbf-z85f9 1/1 Running 1 3m etcd-master 1/1 Running 1 3m kube-apiserver-master 1/1 Running 1 3m kube-controller-manager-master 1/1 Running 1 3m kube-proxy-92f5q 1/1 Running 1 3m kube-proxy-j9g64 1/1 Running 1 3m kube-proxy-nbpvh 1/1 Running 1 3m kube-scheduler-master 1/1 Running 1 3m ``` 删除calico.yaml ```asp kubectl delete -f https://projectcalico.docs.tigera.io/archive/v3.21/manifests/calico.yaml ``` # 查看集群状态 ```asp kubectl get nodes ``` ```asp NAME STATUS ROLES AGE VERSION jp-master Ready control-plane,master 14m v1.21.0 jp-node1 Ready <none> 13m v1.21.0 ``` # 自定义暴露端口 添加配置 ```asp vim /etc/kubernetes/manifests/kube-apiserver.yaml ``` ```asp ... - --service-cluster-ip-range=10.96.0.0/12 - --service-node-port-range=1-65535 #新增 - --tls-cert-file=/etc/kubernetes/pki/apiserver.crt ... ``` 重启api ```asp systemctl daemon-reload systemctl restart kubelet ``` # 集群监控安装 > 在Master节点执行 下载metrics-server(v0.3.6) ```asp wget https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz ``` 解压进入目录 ```asp tar -zxvf v0.3.6.tar.gz && cd metrics-server-0.3.6/deploy/1.8+/ ``` 修改配置文件 ```asp vim metrics-server-deployment.yaml ``` ```asp spec: #新增1行 hostNetwork: true serviceAccountName: metrics-server volumes: # mount in tmp so we can safely use from-scratch images and/or read-only containers - name: tmp-dir emptyDir: {} containers: - name: metrics-server #修改为国内镜像源 image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6 imagePullPolicy: Always #新增3行 args: - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP ``` 部署启动 ```asp kubectl create -f ./ ``` 查看服务状态 ```asp kubectl get pod -n kube-system ``` ```asp NAME READY STATUS RESTARTS AGE ... metrics-server-5f55b696bd-xndm8 1/1 Running 0 60s ``` 查看node资源监控 ```asp kubectl top node ``` ```asp NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% master 314m 7% 954Mi 25% node1 200m 5% 482Mi 13% node2 241m 6% 492Mi 13% ``` # 集群测试 创建pod容器nginx服务 ```asp kubectl create deployment nginx --image=nginx ``` 创建svc暴露nginx对外端口 ```asp kubectl expose deployment nginx --port=80 --type=NodePort ``` 查看pod和svc ```asp kubectl get deployment,svc nginx ``` ```asp NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx 1/1 1 1 100s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/nginx NodePort 10.105.117.217 <none> 80:30584/TCP 98s ``` curl验证nginx服务是否正常(集群100、101、102均能访问nginx) ```asp curl 192.168.1.100:30735 ``` ![](/media/202406/2024-06-16_151124_8900090.9470536339870345.png) 删除svc ```asp kubectl delete svc nginx ``` 删除pod ```asp kubectl delete deployment nginx ``` # 节点授权 node节点授权kubectl命令管理集群,将kube文件发送到node1节点 ```asp scp -r /root/.kube/ node1:/root/ ``` node1节点执行管理命令生效 ```asp kubectl get node ``` # 集群重置 重置集群 ```asp kubeadm reset ``` 删除配置文件 ```asp rm -rf /root/.kube/ rm -rf /etc/kubernetes/ rm -rf /var/lib/kubelet/ rm -rf /var/lib/dockershim rm -rf /var/run/kubernetes rm -rf /var/lib/cni rm -rf /var/lib/etcd rm -rf /etc/cni/net.d ``` 清理防火墙 ```asp iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X ``` # 网络错误 >错误日志: Warning Unhealthy 24s (x196 over 29m) kubelet (combined from similar events): Readiness probe failed: 2024-03-22 02:39:47.813 [INFO][7095] confd/health.go 180: Number of node(s) with BGP peering established = 0 calico/node is not ready: BIRD is not ready: BGP not established with 10.51.10.4,10.51.10.5 修改calico.yaml ```asp vim calico.yaml ``` ```asp 在650行新添加配置: ---------------------------------------------------------------------- # Cluster type to identify the deployment type - name: CLUSTER_TYPE value: "k8s,bgp" #声明集群网卡所在网卡名(查看自己实际的) - name: IP_AUTODETECTION_METHOD value: "interface=enp0s5" # Auto-detect the BGP IP address. - name: IP ---------------------------------------------------------------------- ``` 重建网络 ```asp kubectl apply -f calico.yaml ```
done
Sept. 25, 2024, 5:51 p.m.
转发文档
Collection documents
Last
Next
手机扫码
Copy link
手机扫一扫转发分享
Copy link
Markdown文件
share
link
type
password
Update password