一、概述

1.简介

官方中文文档:https://www.kubernetes.org.cn/docs

Kubernets(k8s)是google在2014年发布的一个开源项目
据说google的数据中心里运行着20多亿个容器,而且google十年前就开始使用容器技术

最初 google开发了一个叫Borg的系统(现在命名为Omega)来调度如此庞大数量的容器和工作负载,在积累了多年的经验后,google决定重写这个容器管理系统,并将其贡献到开源社区,让全世界都能受益 这个项目就是kubernetes

  • Kubernetes是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。
  • Kubernetes一个核心的特点就是能够自主的管理容器来保证云平台中的容器按照用户的期望状态运行着(比如用户想让apache一直运行, 用户不需要关心怎么去做,Kubernetes会自动去监控,然后去重启,新建,总之,让apache一直提供服务),管理员可以加载一个微型服务,让规 划器来找到合适的位置,同时,Kubernetes也系统提升工具以及人性化方面,让用户能够方便的部署自己的应用。

2.Kubernetes的大致组成

一个K8S系统,通常称为一个K8S集群(Cluster)、

这个集群主要包括两个部分:

  • 一个Master节点(主节点)
  • 一群Node节点(计算节点)

 

1.cluster
是计算,存储和网络资源的集和,kubernetes利用这些资源运行各种基于容器的应用

2.Master
是cluster的大脑,它的主要职责是调度,即决定将应用放在哪里运行,Master运行linux操作系统,可以是物理机或者虚拟机,为了实现高可用,可以运行多个Master。

Master节点包括API Server、Scheduler、Controller manager、etcd

API Server是整个系统的对外接口,供客户端和其它组件调用,相当于“营业厅”。

Scheduler负责对集群内部的资源进行调度,相当于“调度室”。

Controller manager负责管理控制器,相当于“大总管”。

3.node
node的职责是运行容器应用,node由master管理,node负责监控并汇报容器的状态,同时根据master的要求管理容器的生命周期,node运行在linux操作系统上,可以是物理机或者是虚拟机

Node节点包括Docker、kubelet、kube-proxy、Fluentd、kube-dns(可选),还有就是Pod

Docker,不用说了,创建容器的。

Kubelet,主要负责监视指派到它所在Node上的Pod,包括创建、修改、监控、删除等。

Kube-proxy,主要负责为Pod对象提供代理。

Fluentd,主要负责日志收集、存储与查询。

Pod是Kubernetes最基本的操作单元。一个Pod代表着集群中运行的一个进程,它内部封装了一个或多个紧密相关的容器。除了Pod之外,K8S还有一个Service的概念,一个Service可以看作一组提供相同服务的Pod的对外访问接口。

4.pod
是kubernetes的最小工作单元,每个pod包含一个或多个容器,pos中的容器会作为一个整体被master调度到一个node上运行

kubernetes引入pod主要基于下面两个目的
1.可管理性
有些容器天生就是需要紧密联系,一起工作,pod提供了比容器更高层次的抽象,将它们封装到一个部署单元,kubernetes以pod为最小单位进行调度,扩展,共享资源,管理生命周期
2.pod中的所有容器使同一个网络,即同样的ip地址和port空间,它们可以直接用localhost通信,同样的,这些容器可以共享存储
controller(控制器) 通过controller来管理pod
 

3.Kubernetes详细组成

  • Kubernetes节点有运行应用容器必备的服务,而这些都是受Master的控制。
  • 每个节点上都要运行Docker。Docker来负责所有具体的映像下载和容器运行。
  • Kubernetes主要由以下几个核心组件组成:

    • etcd保存了整个集群的状态;
    • apiserver提供了资源操作的唯一入口,并提供认证、授权、访问控制、API注册和发现等机制;
    • controller manager负责维护集群的状态,比如故障检测、自动扩展、滚动更新等;
    • scheduler负责资源的调度,按照预定的调度策略将Pod调度到相应的机器上;
    • kubelet负责维护容器的生命周期,同时也负责Volume(CVI)和网络(CNI)的管理;
    • Container runtime负责镜像管理以及Pod和容器的真正运行(CRI);
    • kube-proxy负责为Service提供cluster内部的服务发现和负载均衡;
  • 除了核心组件,还有一些推荐的Add-ons:

    • kube-dns负责为整个集群提供DNS服务
    • Ingress Controller为服务提供外网入口
    • Heapster提供资源监控
    • Dashboard提供GUI
    • Federation提供跨可用区的集群
    • Fluentd-elasticsearch提供集群日志采集、存储与查询

https://kubernetes.io/docs/reference/ #k8s官方文档
https://github.com/kubernetes/kubernetes #github
http://docs.kubernetes.org.cn/31.html  中文文档

二、部署kubernetes cluster

部署三个节点的kubernetes cluster

实验准备:

172.25.28.1 server1 (master)

172.25.28.2 server2(node)

172.25.28.3 server3(node)

master上运行脚本,下载镜像
kubernetes几乎所有的安装组件和docker镜像都放在google自己的网站,国内的话可以使用阿里云

[root@server1 ~]kubeadm config images list #查看所需要的版本
[root@server1 ~]# vim k8s.sh
[root@server1 ~]# chmod +x k8s.sh
下载镜像的脚本

[root@server1 ~]# cat k8s.sh
image_name=(
    kube-proxy:v1.16.3
    kube-apiserver:v1.16.3
    kube-controller-manager:v1.16.3
    kube-scheduler:v1.16.3
    pause:3.1
    etcd:3.3.15-0
    coredns:1.6.2
)
aliyun_registry="registry.cn-hangzhou.aliyuncs.com/google_containers/"

k8s_registry="k8s.gcr.io/"
 
for image in ${image_name[@]};do
docker pull $aliyun_registry$image
docker tag $aliyun_registry$image $k8s_registry$image
docker rmi $aliyun_registry$image
done
[root@server1 ~]# ./k8s.sh
[root@server1 ~]# docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-proxy                v1.16.3             9b65a0f78b09        2 days ago          86.1MB
k8s.gcr.io/kube-apiserver            v1.16.3             df60c7526a3d        2 days ago          217MB
k8s.gcr.io/kube-controller-manager   v1.16.3             bb16442bcd94        2 days ago          163MB
k8s.gcr.io/kube-scheduler            v1.16.3             98fecf43a54f        2 days ago          87.3MB
k8s.gcr.io/etcd                      3.3.15-0            b2756210eeab        2 months ago        247MB
k8s.gcr.io/coredns                   1.6.2               bf261d157914        3 months ago        44.1MB
k8s.gcr.io/pause                     3.1                 da86e6ba6ca1        23 months ago       742kB

下载完成后删除不必要的标签,在脚本里已经写过,所以没必要在执行命令
 

eg:[root@server1 ~]# docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2


1.安装docker并启动(三台虚拟机都做)
2.yum install kubelet kubeadm kubectl -y

    kubelet:运行在cluster所有节点上,负责启动pod和容器
    kubeadm:用于初始化cluster
    kubectl:是kubernetes命令行工具,通过kubectk可以部署和管理应用,查看各种资源,创建,删除和更新各种组件
    配置阿里云网络yum源头(三台虚拟机都做)

[root@server1 ~]# cat /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
       http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

systemctl start docker
systemctl enable docker

4.配置免秘和关闭swap分区
 

master:
ssh-keygen    在server1上生成密码,配置免密
ssh-copy-id server2    将钥匙给server2
ssh server2   
在server1上
ssh-copy-id server3    将钥匙给server3
ssh server3
vim /etc/fstab #三个节点都要做

注释掉/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
或临时关闭:  swapoff -a

5.用kubeadm创建cluster
在master上执行:
# cpu 2个以上 内存:2G
注意两个错误
如果不
[ERROR Swap]: running with swap on is not supported. Please disable swap
1.vim /etc/fstab

# Created by anaconda on Sat May 18 17:02:46 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=c24172a6-4f86-450f-91f7-8542c07f7254 /boot                   xfs     defaults        0 0
#/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

2.    [ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1

报错:提示/proc/sys/net/bridge/bridge-nf-call-iptables里没有设置1

echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables

进行创建:

[root@server1 ~]# kubeadm init --apiserver-advertise-address 172.25.28.1 --pod-network-cidr=10.244.0.0/16

 --apiserver-advertise-address:指明用master的哪个intserface与cluster的其他节点通信(如果master有多个intserface,建议明确指定,如果不指定,kubeadm会自动选择有默认网关的interface)
 --pod-network-cidr= 指定pos网络的范围(lubernetes支持多种网络方案,而且不同的网络方案对--pod-network-cidr 有自己的要求,这里设定为 10.244.0.0/16 是因为我们将使用flannel网络方案,必须设置成这个CIDR)


创建成功的信息如下:

[root@server1 ~]# kubeadm init --apiserver-advertise-address 172.25.28.1 --pod-network-cidr=10.244.0.0/16
W1116 01:02:43.111712   23317 version.go:101] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
W1116 01:02:43.111860   23317 version.go:102] falling back to the local client version: v1.16.3
[init] Using Kubernetes version: v1.16.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/
    [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
[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] Activating the kubelet service
[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 [server1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.25.28.1]
[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 [server1 localhost] and IPs [172.25.28.1 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [server1 localhost] and IPs [172.25.28.1 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"
[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
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 41.003709 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.16" 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 server1 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node server1 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 17qln3.wqjpv9ehpdxbdu8g
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[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
[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 172.25.28.1:6443 --token 17qln3.wqjpv9ehpdxbdu8g \
    --discovery-token-ca-cert-hash sha256:714245e0dab4132da766b0ac9847565cf59ea875a3dcfdfecdb9b4267db20d3a

大致过程如下:
1.kubeadm 执行初始化前的检查
2.生成token和证书
3.生成kubeconfig文件,kubelet需要用这个文件与master通信
5.安装附加组建kube-proxy 和kube-dns
6.kubernetes master初始化成功
7.提示如何配置kubectl
8.提示如何安装pod网络
9.提示如何注册其他节点到cluster

6.配置kubectl
管理kubernetes cluster的命令行工具,前面我们已经在所有节点安装了kubectl mastart初始化完成后需要做一些配置工作,然后kubectl就能使用了
根据提示执行命令 推荐linux的普通用户执行(因为root执行会有一些问题)

[root@server1 ~]# useradd k8s
[root@server1 ~]# passwd k8s
[root@server1 ~]# visudo
k8s ALL=(ALL)       NOPASSWD: ALL #93行左右  添加新用户信息
[root@server1 ~]# su - k8s
[k8s@server1 ~]$ mkdir -p $HOME/.kube
[k8s@server1 ~]$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[k8s@server1 ~]$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
[k8s@server1 ~]$ echo "source <(kubectl completion bash)" >>~/.bashrc

# 为了便捷使用 启用kubectl命令的自动补全功能

7.安装pod网络
要让kubernetes cluster能够工作,必须安装pod网络,否则pod之间无法通信

[k8s@server1 ~]$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created

8.添加node节点

[root@server2 ~]# echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
[root@server2 docker]# kubeadm join 172.25.28.1:6443 --token 17qln3.wqjpv9ehpdxbdu8g --discovery-token-ca-cert-hash sha256:714245e0dab4132da766b0ac9847565cf59ea875a3dcfdfecdb9b4267db20d3a

[root@server3 ~]# kubeadm join 172.25.28.1:6443 --token 17qln3.wqjpv9ehpdxbdu8g --discovery-token-ca-cert-hash sha256:714245e0dab4132da766b0ac9847565cf59ea875a3dcfdfecdb9b4267db20d3a

添加成功显示以下信息;

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初始化的时候可以获得

# Pending、ContainerCreating、ImagePullBackOff都表明Pod没有就绪,Running才是就绪状态。我们可以通过kubectl describe pod <Pod Name>查看Pod的具体情况
# eg:kubectl describe pod coredns-5644d7b6d9-smwsh --namespace=kube-system
# 可以看到在下载image时失败,如果网络质量不好,这种情况是很常见的。我们可以耐心等待,因为Kubernetes会重试 我们也可以自己手动执行docker pull去下载这个镜像

[k8s@server1 ~]$ kubectl get nodes
NAME      STATUS     ROLES    AGE     VERSION
server1   Ready      master   23m     v1.16.3
server2   NotReady   <none>   3m22s   v1.16.3
server3   NotReady   <none>   2m14s   v1.16.3

[k8s@server1 ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                              READY   STATUS              RESTARTS   AGE
kube-system   coredns-5644d7b6d9-dhsrw          1/1     Running             0          23m
kube-system   coredns-5644d7b6d9-z5gjp          1/1     Running             0          23m
kube-system   etcd-server1                      1/1     Running             0          22m
kube-system   kube-apiserver-server1            1/1     Running             0          22m
kube-system   kube-controller-manager-server1   1/1     Running             0          22m
kube-system   kube-flannel-ds-amd64-6qq2k       0/1     Init:0/1            0          2m37s
kube-system   kube-flannel-ds-amd64-ns2dc       0/1     Init:0/1            0          3m45s
kube-system   kube-flannel-ds-amd64-zhhcs       1/1     Running             0          15m
kube-system   kube-proxy-6dqh2                  1/1     Running             0          23m
kube-system   kube-proxy-psjtj                  0/1     ContainerCreating   0          2m37s
kube-system   kube-proxy-zsb74                  0/1     ContainerCreating   0          3m45s
kube-system   kube-scheduler-server1            1/1     Running             0          22m

[k8s@server1 ~]$ kubectl get nodes
NAME      STATUS   ROLES    AGE   VERSION
server1   Ready    master   50m   v1.16.2
server2   Ready    <none>   41m   v1.16.2
server3   Ready    <none>   42m   v1.16.2

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐