kubernetes简介

kubernetes和swarm一样是一个容器编排的工具。

kubernetes架构
在这里插入图片描述

和docker swarm集群管理一样kubernetes集群也是由Master和Node节点组成,Master节点提供对外访问集群的API接口。

Kubernetes Master
在这里插入图片描述

Master节点主要由4部分组成:

  • API Server: 给外部访问集群提供接口服务。
  • Scheduler: 调度模块,当运行一个容器时,该模块根据算法决定容器运行在哪个节点上。
  • Controller: 负责容器的控制。
  • etcd: 一个分布式的key-value 存储,主要存储整个k8s集群的状态和配置。

Kubernetes Node
在这里插入图片描述

Node节点的核心就是Pod, Pod是容器的最小单位,具有相同namespaces(包括usernamespaces和netnamespaces)的容器组合。

  • Docker: 当Node节点创建容器时,会用到Docker的容器技术。
  • Kubelet: Master节点管理Node节点就是通过kubelet。
  • kube-proxy: k8s和swarm一样都有service概念,当外界想要访问一个service时,通过kube-proxy来进行端口的代理和转发。同时,kube-proxy也可以实现service的负载均衡。
  • Fluentd: 负责日志的采集存储和查询。

当然Node节点还可以安装一些插件:

Optional Add-ones:

  • DNS: 关于DNS服务的插件。
  • UI:一个web可视化的插件。

Master与Node之间工作的细节
在这里插入图片描述

  • 可以发现Master的核心是API server,负责连接起整个k8s集群的工作。
  • kubectl是一个命令行工具,使用该工具我们可以通过命令访问API server接口来访问k8s集群。

Centos 安装minikube

minikube是一个用于学习和实验k8s单节点Master集群管理的工具,可以通过minikube快速搭建一个k8s单节点Master环境。

但是minikube和k8s都是谷歌推出的,在安装实验环境时需要下载很多image和软件需要翻墙。

本文使用的是阿里云的k8s镜像源来部署的实验环境,如果能够翻墙还是建议跟着官方文档进行实验环境的部署。

  • Centos7 yum 源

    • 更改Centos yum 源为阿里源

    • 增加kubernetes仓库:

      [root@vagrant1 vagrant]# cat > /etc/yum.repos.d/kubernetes.repo << END
      [kubernetes]
      name = kubernetes
      baseurl = https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
      gpgchek = 1
      gpgkey = https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
                https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
      enable = 1
      END
      [root@vagrant1 vagrant]# yum -y upgrade
      
  • 安装minikube

    [root@vagrant2 vagrant]# curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.13.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
    

    查看是否安装成功:

    [root@vagrant2 vagrant]# minikube version
    minikube version: v1.13.0
    commit: 23aa1eb200a03ae5883dd9d453d4daf3e0f59668
    
  • 安装kubectl

    以下步骤都建议通过非root用户登录执行:vagrant。

    [vagrant@vagrant1 vagrant]# sudo yum -y install kubectl
    

    配置信息

    $ export MINIKUBE_WANTUPDATENOTIFICATION=false
    
    $ export MINIKUBE_WANTREPORTERRORPROMPT=false
    
    $ export MINIKUBE_HOME=$HOME
    
    $ export CHANGE_MINIKUBE_NONE_USER=true
    
    $ mkdir $HOME/.kube || true
    
    $ touch $HOME/.kube/config
    
    $ export KUBECONFIG=$HOME/.kube/config
    
  • Docker 环境

    保证Centos 上有运行着的Docker 环境。

    配置Docker拉取镜像加速阿里云容器加速服务:阿里云官方文档

  • minikube 安装和配置要求

    最少2个CPU,2G内存。

    # 注意在vagrant用户下执行,因为前边的配置信息是在vagrant用户下执行的,这一步执行时间有点长
    [vagrant@vagrant1 ~]$ minikube start --image-mirror-country cn
    

    --image-mirror-country cn为指定使用国内镜像。

    安装完后可以使用命令kubectl version查看是否安装成功:

    [vagrant@vagrant1 ~]$ kubectl version
    Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.3", GitCommit:"1e11e4a2108024935ecfcb2912226cedeafd99df", GitTreeState:"clean", BuildDate:"2020-10-14T12:50:19Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"linux/amd64"}
    Server Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"e19964183377d0ec2052d1f1fa930c4d7575bd50", GitTreeState:"clean", BuildDate:"2020-08-26T14:23:04Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}
    

使用kubeadm搭建多节点K8S集群

  • 需要三台带有Docker环境的Centos 虚拟机,关于使用vagrant和virtualbox搭建虚拟机可以看我以前写的这篇文章。

  • 注意使用阿里云的kubernetes仓库源:

    [root@vagrant1 vagrant]# cat > /etc/yum.repos.d/kubernetes.repo << END
    [kubernetes]
    name = kubernetes
    baseurl = https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
    gpgchek = 1
    gpgkey = https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
              https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
    enable = 1
    END
    [root@vagrant1 vagrant]# yum -y upgrade
    
    [vagrant@vagrant1 ~]$ sudo yum install -y kubelet kubeadm kubectl
    
  • 关闭防火墙和交换,设置开机启动:

    [vagrant@vagrant1 ~]$ sudo systemctl stop firewalld
    [vagrant@vagrant1 ~]$ sudo systemctl disable firewalld
    [vagrant@vagrant1 ~]$ sudo swapoff -a
    [vagrant@vagrant1 ~]$ sudo systemctl enable kubelet.service
    
  • 检查是否安装成功:

    [vagrant@vagrant2 ~]$ which kubelet
    /usr/bin/kubelet
    [vagrant@vagrant2 ~]$ which kubeadm
    /usr/bin/kubeadm
    [vagrant@vagrant2 ~]$ which kubectl
    /usr/bin/kubectl
    

初始化master 节点

一定要有个好的网络。。。否则你会奔溃

[vagrant@vagrant1 ~]$ sudo kubeadm init --pod-network-cidr 172.100.0.0/16 --apiserver-advertise-address 192.168.43.10 --image-repository registry.aliyuncs.com/google_containers

[init] Using Kubernetes version: v1.19.3
[preflight] Running pre-flight checks
        [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[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'
[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 [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local vagrant1] and IPs [10.96.0.1 192.168.43.10]
[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 [localhost vagrant1] and IPs [192.168.43.10 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost vagrant1] and IPs [192.168.43.10 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
[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
[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"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[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 10.504080 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.19" 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 vagrant1 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node vagrant1 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: t6zcrk.8hds6cp7gezn6pb5
[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.43.10:6443 --token t6zcrk.8hds6cp7gezn6pb5 \
    --discovery-token-ca-cert-hash sha256:96d5b026a832cf92a2c98b507b527bca6f82066c88b4c948eb4ed2bd33c2a3c8

  • 初始化命令说明

    --apiserver-advertise-address 
    

    指明用 Master 的哪个 interface 与 Cluster 的其他节点通信。如果 Master 有多个 interface,建议明确指定,如果不指定,kubeadm 会自动选择有默认网关的 interface。

  • --pod-network-cidr
    

    指定 Pod 网络的范围。

  • --image-repository
    

    Kubenetes默认Registries地址是 k8s.gcr.io,在国内并不能访问 gcr.io,我们可以增加–image-repository参数,默认值是 k8s.gcr.io,将其指定为阿里云镜像地址:registry.aliyuncs.com/google_containers。

  • --kubernetes-version=v1.13.1 
    

    关闭版本探测,因为它的默认值是stable-1,会导致从https://dl.k8s.io/release/stable-1.txt下载最新的版本号,我们可以将其指定为固定版本(最新版:v1.13.1)来跳过网络请求。

  • 初始化过程说明:

    1. [preflight] kubeadm 执行初始化前的检查。
    2. [kubelet-start] 生成kubelet的配置文件”/var/lib/kubelet/config.yaml”
    3. [certificates] 生成相关的各种token和证书
    4. [kubeconfig] 生成 KubeConfig 文件,kubelet 需要这个文件与 Master 通信
    5. [control-plane] 安装 Master 组件,会从指定的 Registry 下载组件的 Docker 镜像。
    6. [bootstraptoken] 生成token记录下来,后边使用kubeadm join往集群中添加节点时会用到
    7. [addons] 安装附加组件 kube-proxy 和 kube-dns。
    8. Kubernetes Master 初始化成功,提示如何配置常规用户使用kubectl访问集群。
    9. 提示如何安装 Pod 网络。
    10. 提示如何注册其他节点到 Cluster

然后在master节点上运行(使用非root用户登录),需要注意的是当登录用户切换后也需要执行下边几条命令,否则会报错Unable to connect to the server: dial tcp 172.17.0.2:8443: connect: no route to host, 这是因为

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

检查pod:

[vagrant@vagrant1 ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-6d56c8448f-7kn5f           0/1     Pending   0          11m
kube-system   coredns-6d56c8448f-lqw9t           0/1     Pending   0          11m
kube-system   etcd-vagrant1                      1/1     Running   0          11m
kube-system   kube-apiserver-vagrant1            1/1     Running   0          11m
kube-system   kube-controller-manager-vagrant1   1/1     Running   0          11m
kube-system   kube-proxy-5hwx8                   1/1     Running   0          11m
kube-system   kube-scheduler-vagrant1            1/1     Running   0          11m

安装网络插件

kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

执行完毕等待几秒在查看所有pod命名空间:

[vagrant@vagrant1 ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-6d56c8448f-7kn5f           1/1     Running   0          23m
kube-system   coredns-6d56c8448f-lqw9t           1/1     Running   0          23m
kube-system   etcd-vagrant1                      1/1     Running   0          23m
kube-system   kube-apiserver-vagrant1            1/1     Running   0          23m
kube-system   kube-controller-manager-vagrant1   1/1     Running   0          23m
kube-system   kube-proxy-5hwx8                   1/1     Running   0          23m
kube-system   kube-scheduler-vagrant1            1/1     Running   0          23m
kube-system   weave-net-bwgwf                    2/2     Running   0          62s

添加worker节点

需要注意使用root用户,添加节点信息为master节点初始化完毕后显示的信息,如果忘记了这个信息可以使用命令kubeadm token create --print-join-command W生成新的token.

[root@vagrant2 vagrant]# kubeadm join 192.168.43.10:6443 --token t6zcrk.8hds6cp7gezn6pb5 \
>     --discovery-token-ca-cert-hash sha256:96d5b026a832cf92a2c98b507b527bca6f82066c88b4c948eb4ed2bd33c2a3c8
[preflight] Running pre-flight checks
        [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[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] 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.

查看节点信息

在master主机上查看节点信息:

[vagrant@vagrant1 ~]$ kubectl get nodes
NAME       STATUS   ROLES    AGE     VERSION
vagrant1   Ready    master   48m     v1.19.3
vagrant2   Ready    <none>   4m13s   v1.19.3
Logo

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

更多推荐