Skip to content

k8s运维(七)安装k8s

接下来,只需要在master服务器上操作

一、初始化k8s

1.初始化命令

$ kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.21.14 --apiserver-advertise-address=10.211.55.20  

# --pod-network-cidr pod网络ip,这里使用的是flannel,因此ip是 10.244.0.0/16
# --kubernetes-version 安装的k8s版本
# --apiserver-advertise-address master服务器的ip

问题1:

[init] Using Kubernetes version: v1.21.14
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
        [ERROR CRI]: container runtime is not running: output: E1214 22:39:08.594780   13921 remote_runtime.go:948] "Status from runtime service failed" err="rpc error: code = Unimplemented desc = unknown service runtime.v1alpha2.RuntimeService"
time="2022-12-14T22:39:08+08:00" level=fatal msg="getting status of runtime: rpc error: code = Unimplemented desc = unknown service runtime.v1alpha2.RuntimeService"
, error: exit status 1

解决1:

$ rm -rf /etc/containerd/config.toml
$ systemctl restart containerd

成功的样子:

16710705215831

1.1 To start using your cluster
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ export KUBECONFIG=/etc/kubernetes/admin.conf
1.2 修改server

16710893762591

1.3 安装网络

会发现,coredns都是pending状态 16710950325908

安装

$ kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/v0.20.2/Documentation/kube-flannel.yml

再看 16711051112686

1.4 加入集群

其他node节点的服务器加入集群里面

$ kubeadm join 10.211.55.20:6443 --token hfyd2f.328gw1n7oyic6kav \
        --discovery-token-ca-cert-hash sha256:f6d1cddcc92b4624a5425e7a87eb5eea16c97d5e1b67af52f720a5b1a5ce3d0c
        
如果找不到,可以获取生成命令

$ kubeadm token create --print-join-command

在master上观察初始化
$ watch kubectl get nodes
最终
Every 2.0s: kubectl get nodes                                                                               Thu Dec 15 17:25:40 2022

NAME          STATUS   ROLES                  AGE     VERSION
kube-master   Ready    control-plane,master   9h      v1.21.14
kube-node-1   Ready    <none>                 4m20s   v1.21.14
kube-node-2   Ready    <none>                 2m7s    v1.21.14

恭喜你,成功了

查看详细输出

$ kubectl get nodes -o wide

NAME          STATUS   ROLES                  AGE     VERSION    INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
kube-master   Ready    control-plane,master   9h      v1.21.14   10.211.55.20   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.21
kube-node-1   Ready    <none>                 5m22s   v1.21.14   10.211.55.21   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.21
kube-node-2   Ready    <none>                 3m9s    v1.21.14   10.211.55.22   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.21

2.卸载k8s

kubeadm reset
rm -rf /etc/cni/net.d 
rm -rf $HOME/.kube/config 
rm -rf /etc/kubernetes/

删除镜像
touch k8s_uninstall.sh
输入
kubeadm reset -f
rm -rf /etc/cni/net.d 
rm -rf $HOME/.kube/config 
rm -rf /etc/kubernetes/
yum remove -y kubelet kubectl kubeadm kubernetes-cni
yum remove -y docker docker-common docker-selinux docker-engine docker-ce-cli docker-client containerd.io
systemctl daemon-reload
rm -rf /var/lib/docker

执行sh k8s_uninstall.sh