步骤1 - 步骤4 需要对master节点和node节点同样的操作

1. 安装docker

2种方式安装docker:

  • 官方的安装方式
    这种方式去国外下载docker速度很慢,具体方式参考: 树莓派安装docker
  • apt-get安装
 sudo apt-get -y install docker-ce

安装完成查看docker版本

root@master:~# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:45:36 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:44:07 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

2. 设置cgroup的驱动为systemd

# 创建/etc/docker/daemon.json文件添加以下配置

{
    "exec-opts":["native.cgroupdriver=systemd"]
}

 sudo /etc/init.d/docker restart

3. 关闭swap,k8s不支持虚拟内存

vim /etc/fstab

# 在swap分区这行前加 # 禁用掉,保存退出

# 重启生效
sudo reboot

4. 安装kubelet、kubectl、kubeadm

  • 设置k8s的下载源
sudo curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add 

sudo tee /etc/apt/sources.list.d/kubernetes.list <<-'EOF'
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
EOF

sudo apt-get update
  • 查看可用的版本。不要安装最新的版本,因为安装时需要从k8s官网拉取镜像由于国内网络不通无法从官网拉取,所以一般是从其他地方先把镜像拉取下来比如阿里云的镜像仓库中,而很可能阿里的镜像仓库中没有最新版本的镜像
root@master:~# apt-cache madison kubelet
   kubelet |  1.18.6-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.18.5-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.18.4-01 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.18.4-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.18.3-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.18.2-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.18.1-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.18.0-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.17.9-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.17.8-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   kubelet |  1.17.7-01 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
   ......
  • 安装kubelet、kubectl、kubeadm
sudo apt-get update
sudo apt-get install -y kubelet=1.18.2-00
sudo apt-get install -y kubectl=1.18.2-00
sudo apt-get install -y kubeadm=1.18.2-00

5. kubeadm 安装k8s

  • kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.1.121 --kubernetes-version=v1.18.2 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
    参数:
    –apiserver-advertise-address=192.168.1.121 master节点的ip地址
    –kubernetes-version=v1.18.2 安装的k8s的版本,拉镜像是会拉取该版本的镜像
    –image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers 指定镜像仓库的地址,默认是k8s.gcr.io但是这个地址在国内访问不了所以指定为阿里的镜像仓库地址
root@master:~# kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.1.121 --kubernetes-version=v1.18.2 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
W0719 07:02:50.588554    1713 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.2
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.1.121]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master localhost] and IPs [192.168.1.121 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [192.168.1.121 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
W0719 07:03:35.658275    1713 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[control-plane] Creating static Pod manifest for "kube-scheduler"
W0719 07:03:35.660389    1713 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 17.504855 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: utkbpo.7ecwbnywfqkf8jgi
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

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

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 192.168.1.121:6443 --token utkbpo.7ecwbnywfqkf8jgi \
    --discovery-token-ca-cert-hash sha256:4daac87ca7ae8fd798302a95c4405792763311dc16e01d70c7c28d93d6cc1e7e 
root@master:~#
  • 按照提示设置环境变量后才可以使用kubectl命令
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • 查看node状态,发现node是NotReady
root@master:~# kubectl get node
NAME     STATUS     ROLES    AGE   VERSION
master   NotReady   master   26m   v1.18.2
  • 查看node NotReady的原因 kubectl describe node master
root@master:~# kubectl describe node master
Name:               master
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
...

Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  MemoryPressure   False   Sun, 19 Jul 2020 07:43:58 +0000   Sun, 19 Jul 2020 07:03:47 +0000   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Sun, 19 Jul 2020 07:43:58 +0000   Sun, 19 Jul 2020 07:03:47 +0000   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure      False   Sun, 19 Jul 2020 07:43:58 +0000   Sun, 19 Jul 2020 07:03:47 +0000   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready            False   Sun, 19 Jul 2020 07:43:58 +0000   Sun, 19 Jul 2020 07:03:47 +0000   KubeletNotReady              runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
...

发现network plugin is not ready: cni config uninitialized的报错,原因是网络插件没有安装

  • 安装网络插件
kubectl apply -f https://docs.projectcalico.org/v3.11/manifests/calico.yaml
  • 网络插件安装完在再查看node状态就正常了
root@master:~# kubectl get node
NAME     STATUS   ROLES    AGE   VERSION
master   Ready    master   67m   v1.18.2
  • 等待一段时间,所有pod状态正常
root@master:~# kubectl get pod --all-namespaces 
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-59877c7fb4-s24lf   1/1     Running   0          7m21s
kube-system   calico-node-5jtsk                          1/1     Running   0          7m21s
kube-system   coredns-546565776c-96bld                   1/1     Running   0          8m21s
kube-system   coredns-546565776c-9src9                   1/1     Running   0          8m21s
kube-system   etcd-master                                1/1     Running   0          8m37s
kube-system   kube-apiserver-master                      1/1     Running   0          8m37s
kube-system   kube-controller-manager-master             1/1     Running   0          8m37s
kube-system   kube-proxy-ndmr5                           1/1     Running   0          8m20s
kube-system   kube-scheduler-master                      1/1     Running   0          8m37s

6. 纳管node节点

  • 在node1节点上执行命令
kubeadm join 192.168.1.121:6443 --token fsh2b3.glq2dbxz12rl3ob2 \
>     --discovery-token-ca-cert-hash sha256:4193fcb286ab17233938c563f2b27278689e15a37a59a67ad7b74f2ae41c0d4b
W0719 09:11:09.622139   23384 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.18" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster
  • 查看集群的节点状态
root@master:~# kubectl get node
NAME     STATUS   ROLES    AGE    VERSION
master   Ready    master   111m   v1.18.2
node1    Ready    <none>   97m    v1.18.2

7.添加节点

master初始化成功后注意将kubeadm join xxx保存下来,等下node节点需要使用。如果忘记了,可以在master上通过kubeadm token list得到。默认token 24小时就会过期,后续的机器要加入集群需要使用以下命令重新生成token

kubeadm token create

获取ca证书sha256编码hash值

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

在待纳管节点执行以下命令

kubeadm join <master节点的ip>:6443 --token <重新生成的token值> --discovery-token-ca-cert-hash sha256:<sha256编码值>

8. 删除已纳管的节点

删除节点

kubectl delete node <node name>

清空init配置,需要删除的节点上执行

kubeadm reset

9. 卸载k8s

# 卸载或者重置k8s
kubeadm reset

# reset之后可以再使用kubeadm init命令重新部署k8s

参考:
【Kubernetes详解】(二)从零开始搭建Kubernetes集群

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐