kubernetes-01-集群搭建-03-集群安装

集群安装前要说明一点是:k8s的集群安装需要的一些镜像再国内是访问不到了,我是有外网所以可以访问到,有得人没有外网就可以用阿里提供的镜像仓库下载安装;

1-准备集群镜像:

准备集群所需要的镜像:
其实我上一个文章写了怎么切换镜像源了;
查看集群镜像:
kubeadm config images list

@master:~$ kubeadm config images list
k8s.gcr.io/kube-apiserver:v1.22.2
k8s.gcr.io/kube-controller-manager:v1.22.2
k8s.gcr.io/kube-scheduler:v1.22.2
k8s.gcr.io/kube-proxy:v1.22.2
k8s.gcr.io/pause:3.5
k8s.gcr.io/etcd:3.5.0-0
k8s.gcr.io/coredns/coredns:v1.8.4

2-集群初始化:

只需要在master节点上操作:

1:初始化主节点(只需要在主节点上操作):
1.1:创建集群
kubeadm init \
    --kubernetes-version=v1.22.2 \ 
    --pod-network-cidr=10.244.0.0/16 \
    --service-cidr=10.96.0.0/12 \
    --apiserver-advertise-address=172.30.10.175 \
    --image-repository registry.aliyuncs.com/google_containers 

apiserver-advertise-address:这个是master节点都ip地址
   
1.2:创建一些必须的文件
在集群初始化成功后,就有一些提示成功的信息,和告诉你要执行的一些脚本;
Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

就执行这三个命令就行了:
创建一个文件夹:mkdir -p $HOME/.kube
拷贝admin.conf到这个文件夹下去:sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
设置权限:sudo chown $(id -u):$(id -g) $HOME/.kube/config


这里还有两句话要关注下:
Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 172.30.10.175:6443 --token 4bfrzp.wvaw7vryirmsz0q1 \
	--discovery-token-ca-cert-hash sha256:2d31fdbd12e7983b15972512bb447b96f850d62028e3f2c4e3ea54e01647210a







问题1:我在创建集群的时候报了一个错误:
[init] Using Kubernetes version: v1.22.2
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
	[ERROR NumCPU]: the number of available CPUs 1 is less than the required 2
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
说我的gpu不足,这里有两种解决方案:
方案1:忽略这个告警,在参数后面加--ignore-preflight-errors=all 参数即可;
方案2:因为我是虚拟机,可以升级虚拟机的cpu配置为2核;

这样集群就创建好了;

注意:⚠️

如果在初始化及亲的时候出现报错,执行kubeadm reset 命令执行重制,

解决提示的报错后再执行初始化操作;

3-把工作节点加入到集群:

其实就是上面安装集群后给的那些提示:

分别在两个节点上执行:

kubeadm join 172.30.10.175:6443 --token 4bfrzp.wvaw7vryirmsz0q1 \
	--discovery-token-ca-cert-hash sha256:2d31fdbd12e7983b15972512bb447b96f850d62028e3f2c4e3ea54e01647210a 



kubectl get nodes 
就会看到新家的两个节点已经存在了
root@master:~# kubectl get nodes
NAME     STATUS     ROLES                  AGE   VERSION
master   NotReady   control-plane,master   14m   v1.22.2
node1    NotReady                    5s    v1.22.2
node2    NotReady                    2s    v1.22.2

4-安装网络插件,保证各个节点可以通信:

上面安装好的集群节点状态都是notready,未准备,因此要安装网络,让集群可以通信;

kubernetes支持多种网络插件,比如fiannel,calico,canal等,可以随便用一个就行;

这里用fiannel
只需要在master执行:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml



执行完后,等一会,再次查看集群状态,就会看到是ready状态了;
root@master:~# kubectl get nodes
NAME     STATUS     ROLES                  AGE   VERSION
master   Ready   control-plane,master   27m   v1.22.2
node1    Ready                       13m   v1.22.2
node2    Ready                       12m   v1.22.2

5-kubectl命令自动补全

查看 completion 帮助
kubectl completion -h

6-检测kubernetes集群是否正常(master节点)

检查组件安装是否正常
kubectl get componentstatuses


查看核心组件运行是否正常
kubectl get pods -n kube-system

你可能感兴趣的:(k8s,docker,k8s)