kubernetes集群部署

kubernetes集群部署

  • Kubernetes是什么
  • Kubernetes集群架构与组件
  • 快速部署一个Kubernetes集群
    • 生产环境部署K8s的2种方式
    • 服务器硬件配置推荐
    • 使用kubeadm快速部署一个K8s集群
    • 部署的网络组件起什么作用?
    • 查看集群状态
  • Kubernetes将弃用Docker
  • kubernetes快速部署
    • 安装要求
    • 学习目标
    • 准备环境
    • 所有节点安装Docker/kubeadm/kubelet
      • 安装Docker
      • 添加kubernetes阿里云YUM软件源
      • 安装kubeadm,kubelet和kubectl
    • 部署Kubernetes Master
    • 安装Pod网络插件(CNI)
    • 加入Kubernetes Node
    • 测试kubernetes集群

Kubernetes是什么

Kubernetes是Google在2014年开源的一个容器集群管理系统,Kubernetes简称K8s。. Kubernetes用于容器化应用程序的部署,扩展和管理,目标是让部署容器化应用简单高效。
官方网站
官方文档

Kubernetes集群架构与组件

Master组件

  • kube-apiserver
    Kubernetes APl,集群的统一入口,各组件协调者,以RESTfulAPI提供接口服务,所有对象资源的增删改查和监听操作都交给APlServer处理后再提交给Etcd存储。
  • kube-controller-manager
    处理集群中常规后台任务,一个资源对应一个控制器,而ControllerManager就是负责管理这些控制器的。
  • kube-scheduler(调度器)
    根据调度算法为新创建的Pod选择一个Node节点,可以任意部署,可以部署在同一个节点上,也可以部署在不同的节点上。
  • etcd
    分布式键值存储系统。用于保存集群状态数据,比如Pod、Service等对象信息。

Node组件

  • kubelet
    kubelet是Master在Node节点上的Agent,管理本机运行容器的生命周期,比如创建容器、Pod挂载数据卷、下载secret、获取容器和节点状态等工作。kubelet将每个Pod转换成一组容器。
  • kube-proxy
    在Node节点上实现Pod网络代理,维护网络规则和四层负载均衡工作。
  • docker或rocket
    容器引擎,运行容器。
    kubernetes集群部署_第1张图片

快速部署一个Kubernetes集群

  • 生产环境部署K8s的2种方式
  • 服务器硬件配置推荐
  • 使用kubeadm快速部署一个K8s集群
  • 部署的网络组件起什么作用?
  • 查看集群状态
  • Kubernetes将弃用Docker

生产环境部署K8s的2种方式

  • kubeadm
    Kubeadm是一个工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。部署地址: https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
  • 二进制
    从官方下载发行版的二进制包,手动部署每个组件,组成Kubernetes集群。下载地址: https://github.com/kubernetes/kubernetes/releases

服务器硬件配置推荐

kubernetes集群部署_第2张图片

使用kubeadm快速部署一个K8s集群

  1. 安装Docker
  2. 创建一个 Master节点kubeadm init
  3. 将一个Node节点加入到当前集群中
    kubeadm join
  4. 部署容器网络(CNI)
    kubectl apply -f calico.yaml
  5. 部署Web UI (Dashboard)

部署的网络组件起什么作用?

部署网络组件的目的是打通Pod到Pod之间网络、Node与Pod之间网络,从而集群中数据包可以任意传输,形成了一个扁平化网络。
主流网络组件有:Flannel、Calico等
而所谓的CNI ( Container Network Interface,容器网络接口)就是k8s对接这些第三方网络组件的接口。

查看集群状态

查看master组件状态:
kubectl get cs
查看node状态:
kubectl get node
查看Apiserver代理的URL:
kubectl cluster-info
查看集群详细信息:
kubectl cluster-info dump
查看资源信息:
kubectl describe<资源><名称>

Kubernetes将弃用Docker

在Kubernetes平台中,为了解决与容器运行时(例如Docker)集成问题,在早期社区推出了CRI (Container Runtime Interface,容器运行时接口),以支持更多的容器运行时。
当我们使用Docker作为容器运行时之后,架构是这样的,如图所示:

Kubernetes 计划弃用就是kubelet中dockershim。即 Kuberneteskubelet实现中的组件之一,它能够与Docker Engine 进行通信。
kubernetes集群部署_第3张图片
为什么这么做?
Docker内部调用链比较复杂,多层封装和调用,导致性能降低、提升故障率、不易排查
Docker还会在宿主机创建网络规则、存储卷,也带来了安全隐患
如何应对?
在未来的Kubernetes 版本彻底放弃Docker支持之前,引入受支持的容器运行时。除了docker之外,CRl还支持很多容器运行时,例如:

  • containerd:containerd与Docker相兼容,相比Docker轻量很多,目前较为成熟
  • cri-o,podman:都是红帽(RedHat)项目,目前红帽主推podman

kubernetes快速部署

kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。这个工具能通过两条指令完成一个kubernetes集群的部暑︰

# 创建一个 Master 节点
$ kubeadm init

# 将一个 Node 节点加入到当前集群中
$ kubeadm join 

安装要求

在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
至少3台机器,操作系统CentOS7+

  • 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘20GB或更多
  • 集群中所有机器之间网络互通
  • 可以访问外网,需要拉取镜像
  • 禁止swap分区

学习目标

  1. 在所有节点上安装Docker和kubeadm
  2. 部署Kubernetes Master
  3. 部署容器网络插件
  4. 部署Kubernetes Node,将节点加入Kubernetes集群中
  5. 部署Dashboard Web页面,可视化查看Kubernetes资源

准备环境

kubernetes集群部署_第4张图片

角色 IP 系统 需求
master 192.168.47.160 centos 8 2核CPU,2G内存
node1 192.168.47.161 centos 7 4核CPU,3G内存
node2 192.168.47.163 centos 8 4核CPU,3G内存
// 关闭防火墙和selinux
[root@master ~]# systemctl disable --now firewalld
[root@master ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config

[root@node1 ~]# systemctl disable --now firewalld
[root@node1 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config

[root@node2 ~]# systemctl disable --now firewalld
[root@node2 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config

// 关闭swap
注释掉或删掉swap分区(我这里是注释掉的)
[root@master ~]# vim /etc/fstab
#/dev/mapper/cs-swap     none                    swap    defaults        0 0

[root@node1 ~]# vim /etc/fstab 
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@node2 ~]# vim /etc/fstab
#/dev/mapper/cs-swap     none                    swap    defaults        0 0

// 设置主机名
[root@localhost ~]# hostnamectl set-hostname master.example.com
[root@localhost ~]# bash
[root@master ~]# hostname
master.example.com

[root@localhost ~]# hostnamectl set-hostname node1.example.com
[root@localhost ~]# bash
[root@node1 ~]# hostname
node1.example.com

[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# bash
[root@node2 ~]# hostname
node2.example.com

// 在master添加hosts
[root@master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.47.160 master master.example.com
192.168.47.161 node1 node1.example.com
192.168.47.163 node2 node2.example.com

// 将桥接的IPv4流量传递到iptables的链
[root@master ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@master ~]# sysctl --system      # 生效

// 时间同步
[root@master ~]# yum -y install chrony
[root@master ~]# vim /etc/chrony.conf 
  3 pool time1.aliyun.com iburst
[root@master ~]# systemctl enable --now chronyd

[root@node1 ~]# yum -y install chrony
[root@node1 ~]# vim /etc/chrony.conf 
  3 server time1.aliyun.com iburst
  4 #server 0.centos.pool.ntp.org iburst
  5 #server 1.centos.pool.ntp.org iburst
  6 #server 2.centos.pool.ntp.org iburst
  7 #server 3.centos.pool.ntp.org iburst
[root@node1 ~]# systemctl enable --now chronyd

[root@node2 ~]# yum -y install chrony
[root@node2 ~]# vim /etc/chrony.conf 
  3 pool time1.aliyun.com iburst
[root@node2 ~]# systemctl enable --now chronyd

// 免密认证
[root@master ~]# ssh-keygen -t rsa
[root@master ~]# ssh-copy-id master
[root@master ~]# ssh-copy-id node1
[root@master ~]# ssh-copy-id node2

// 测试
[root@master ~]# for i in master node1 node2;do ssh $i 'date';done
20211217日 星期五 23:47:59 CST
20211217日 星期五 23:47:59 CST
20211217日 星期五 23:47:59 CST

// 重启让刚刚的操作生效
[root@master ~]# reboot
[root@node1 ~]# reboot
[root@node2 ~]# reboot

所有节点安装Docker/kubeadm/kubelet

Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。

安装Docker

// 下载docker源
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

[root@node2 ~]# cd /etc/yum.repos.d/
[root@node2 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

// 安装docker
[root@master ~]# yum -y install docker-ce

[root@node1 ~]# yum -y install docker-ce

[root@node2 ~]# yum -y install docker-ce

// 设置docker开机自启
[root@master ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

[root@node1 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

[root@node2 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

// 查看docker版本号
[root@master ~]# docker --version
Docker version 20.10.12, build e91ed57

[root@node1 ~]# docker --version
Docker version 20.10.12, build e91ed57

[root@node2 ~]# docker --version
Docker version 20.10.12, build e91ed57

// 设置docker的加速器
[root@master ~]# cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://2bkybiwf.mirror.aliyuncs.com"],     ## 配置加速器
  "exec-opts": ["native.cgroupdriver=systemd"],       ## 用systemd的方式来管理cgroup
  "log-driver": "json-file",         ## 日志格式用json
  "log-opts": {
    "max-size": "100m"      ## 最大的大小100M,达到100M就滚动
  },
  "storage-driver": "overlay2"      ## 存储驱动用overlay2
}
EOF

[root@node1 ~]# cat > /etc/docker/daemon.json << EOF
> {
>   "registry-mirrors": ["https://2bkybiwf.mirror.aliyuncs.com"],
>   "exec-opts": ["native.cgroupdriver=systemd"],
>   "log-driver": "json-file",
>   "log-opts": {
>     "max-size": "100m"
>   },
>   "storage-driver": "overlay2"
> }
> EOF

[root@node2 ~]# cat > /etc/docker/daemon.json << EOF
> {
>   "registry-mirrors": ["https://2bkybiwf.mirror.aliyuncs.com"],
>   "exec-opts": ["native.cgroupdriver=systemd"],
>   "log-driver": "json-file",
>   "log-opts": {
>     "max-size": "100m"
>   },
>   "storage-driver": "overlay2"
> }
> EOF

添加kubernetes阿里云YUM软件源

[root@master ~]# cat > /etc/yum.repos.d/kubernetes.repo << EOF
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=0
> repo_gpgcheck=0
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@master ~]# dnf clean all
[root@master ~]# dnf makecache

[root@node1 ~]# cat > /etc/yum.repos.d/kubernetes.repo << EOF
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=0
> repo_gpgcheck=0
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@node1 ~]# dnf clean all
[root@node1 ~]# dnf makecache

[root@node2 ~]# cat > /etc/yum.repos.d/kubernetes.repo << EOF
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=0
> repo_gpgcheck=0
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@node2 ~]# dnf clean all
[root@node2 ~]# dnf makecache

安装kubeadm,kubelet和kubectl

由于版本更新频繁,这里指定版本号部署:

[root@master ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@master ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.

[root@node1 ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@node1 ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.

[root@node2 ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@node2 ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.

部署Kubernetes Master

在192.168.122.131(Master)执行。

[root@master ~]# kubeadm init --apiserver-advertise-address=192.168.47.160 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.20.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.20.0
[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 FileExisting-tc]: tc not found in system path
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.12. Latest validated version: 19.03
[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 master.example.com] and IPs [10.96.0.1 192.168.47.160]
[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 master.example.com] and IPs [192.168.47.160 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master.example.com] and IPs [192.168.47.160 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 8.503152 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.20" 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.example.com as control-plane by adding the labels "node-role.kubernetes.io/master=''" and "node-role.kubernetes.io/control-plane='' (deprecated)"
[mark-control-plane] Marking the node master.example.com as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 1f7l8p.opg4o9u8in5rq5yp
[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

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 192.168.47.160:6443 --token 1f7l8p.opg4o9u8in5rq5yp \
    --discovery-token-ca-cert-hash sha256:3b4c00837e5ce13b487e894a230a4d9204f2a8d3885abe2bee8b9eacd51a4990 

[root@master ~]# docker images
REPOSITORY                                                        TAG        IMAGE ID       CREATED         SIZE
registry.aliyuncs.com/google_containers/kube-proxy                v1.20.0    10cc881966cf   12 months ago   118MB
registry.aliyuncs.com/google_containers/kube-apiserver            v1.20.0    ca9843d3b545   12 months ago   122MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.20.0    b9fa1895dcaa   12 months ago   116MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.20.0    3138b6e3d471   12 months ago   46.4MB
registry.aliyuncs.com/google_containers/etcd                      3.4.13-0   0369cf4303ff   15 months ago   253MB
registry.aliyuncs.com/google_containers/coredns                   1.7.0      bfe3a36ebd25   18 months ago   45.2MB
registry.aliyuncs.com/google_containers/pause                     3.2        80d28bedfe5d   22 months ago   683kB

[root@master ~]# cat init 
kubeadm join 192.168.47.160:6443 --token 1f7l8p.opg4o9u8in5rq5yp \
    --discovery-token-ca-cert-hash sha256:3b4c00837e5ce13b487e894a230a4d9204f2a8d3885abe2bee8b9eacd51a4990 

由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。

使用kubectl工具:

// 让环境变量永久生效
[root@master ~]# echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' > /etc/profile.d/k8s.sh
[root@master ~]# source /etc/profile.d/k8s.sh 

// 查看是否有控制节点
[root@master ~]# kubectl get nodes
NAME                 STATUS     ROLES                  AGE   VERSION
master.example.com   NotReady   control-plane,master   13m   v1.20.0

安装Pod网络插件(CNI)

// 下载kube-flannel.yml
[root@master ~]# ls
anaconda-ks.cfg  init  kube-flannel.yml
[root@master ~]# kubectl apply -f 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 created

确保能够访问到quay.io这个registery。

加入Kubernetes Node

在192.168.47.161、192.168.47.163上(Node)执行。

向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:

// 把刚刚保存的文件复制到node1和node2上去执行,拉镜像
[root@master ~]# cat init 
kubeadm join 192.168.47.160:6443 --token 1f7l8p.opg4o9u8in5rq5yp \
    --discovery-token-ca-cert-hash sha256:3b4c00837e5ce13b487e894a230a4d9204f2a8d3885abe2bee8b9eacd51a4990 

[root@node1 ~]# kubeadm join 192.168.47.160:6443 --token 1f7l8p.opg4o9u8in5rq5yp \
>     --discovery-token-ca-cert-hash sha256:3b4c00837e5ce13b487e894a230a4d9204f2a8d3885abe2bee8b9eacd51a4990 
[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 SystemVerification]: this Docker version is not on the list of validated versions: 20.10.12. Latest validated version: 19.03
        [WARNING Hostname]: hostname "node1.example.com" could not be reached
        [WARNING Hostname]: hostname "node1.example.com": lookup node1.example.com on 192.168.47.2:53: no such host
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[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@node2 ~]# kubeadm join 192.168.47.160:6443 --token 1f7l8p.opg4o9u8in5rq5yp \
>     --discovery-token-ca-cert-hash sha256:3b4c00837e5ce13b487e894a230a4d9204f2a8d3885abe2bee8b9eacd51a4990 
[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 FileExisting-tc]: tc not found in system path
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.12. Latest validated version: 19.03
        [WARNING Hostname]: hostname "node2.example.com" could not be reached
        [WARNING Hostname]: hostname "node2.example.com": lookup node2.example.com on 192.168.47.2:53: server misbehaving
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[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.

// 查看,下面node2还是notready,是因为它的后台还有东西在运行,等一会就可以了,等它变成ready
[root@master ~]# kubectl get nodes
NAME                 STATUS     ROLES                  AGE     VERSION
master.example.com   Ready      control-plane,master   25m     v1.20.0
node1.example.com    Ready      <none>                 2m37s   v1.20.0
node2.example.com    NotReady   <none>                 2m18s   v1.20.0

// 三个全都是ready之后,集群就部署好了
[root@master ~]# kubectl get nodes
NAME                 STATUS   ROLES                  AGE     VERSION
master.example.com   Ready    control-plane,master   27m     v1.20.0
node1.example.com    Ready    <none>                 4m14s   v1.20.0
node2.example.com    Ready    <none>                 3m55s   v1.20.0

//在node1和node2上查看镜像
[root@node1 ~]# docker images
REPOSITORY                                           TAG       IMAGE ID       CREATED         SIZE
quay.io/coreos/flannel                               v0.15.1   e6ea68648f0c   4 weeks ago     69.5MB
rancher/mirrored-flannelcni-flannel-cni-plugin       v1.0.0    cd5235cd7dc2   7 weeks ago     9.03MB
registry.aliyuncs.com/google_containers/kube-proxy   v1.20.0   10cc881966cf   12 months ago   118MB
registry.aliyuncs.com/google_containers/pause        3.2       80d28bedfe5d   22 months ago   683kB

[root@node2 ~]# docker images
REPOSITORY                                           TAG       IMAGE ID       CREATED         SIZE
quay.io/coreos/flannel                               v0.15.1   e6ea68648f0c   4 weeks ago     69.5MB
rancher/mirrored-flannelcni-flannel-cni-plugin       v1.0.0    cd5235cd7dc2   7 weeks ago     9.03MB
registry.aliyuncs.com/google_containers/kube-proxy   v1.20.0   10cc881966cf   12 months ago   118MB
registry.aliyuncs.com/google_containers/pause        3.2       80d28bedfe5d   22 months ago   683kB

// 部署好之后,node1和node2上会自动运行一些容器
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE                                                COMMAND                  CREATED         STATUS                     PORTS     NAMES
90be46c739f6   e6ea68648f0c                                         "/opt/bin/flanneld -…"   6 minutes ago   Up 6 minutes                         k8s_kube-flannel_kube-flannel-ds-9kxkg_kube-system_4f152d43-a1bd-4293-b3bf-be89715538fa_0
436d9e9e7d06   quay.io/coreos/flannel                               "cp -f /etc/kube-fla…"   6 minutes ago   Exited (0) 6 minutes ago             k8s_install-cni_kube-flannel-ds-9kxkg_kube-system_4f152d43-a1bd-4293-b3bf-be89715538fa_0
475e701c89ab   rancher/mirrored-flannelcni-flannel-cni-plugin       "cp -f /flannel /opt…"   6 minutes ago   Exited (0) 6 minutes ago             k8s_install-cni-plugin_kube-flannel-ds-9kxkg_kube-system_4f152d43-a1bd-4293-b3bf-be89715538fa_0
e6e5b73a1e48   registry.aliyuncs.com/google_containers/kube-proxy   "/usr/local/bin/kube…"   7 minutes ago   Up 7 minutes                         k8s_kube-proxy_kube-proxy-cxr8n_kube-system_8129a25c-3feb-4fc0-82bb-1927db0f1854_0
2b9e1a8828e6   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 7 minutes ago   Up 7 minutes                         k8s_POD_kube-flannel-ds-9kxkg_kube-system_4f152d43-a1bd-4293-b3bf-be89715538fa_0
a0419097243f   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 7 minutes ago   Up 7 minutes                         k8s_POD_kube-proxy-cxr8n_kube-system_8129a25c-3feb-4fc0-82bb-1927db0f1854_0

[root@node2 ~]# docker ps -a
CONTAINER ID   IMAGE                                                COMMAND                  CREATED         STATUS                     PORTS     NAMES
f2b7159cdf74   e6ea68648f0c                                         "/opt/bin/flanneld -…"   5 minutes ago   Up 5 minutes                         k8s_kube-flannel_kube-flannel-ds-j6mnd_kube-system_50c1a168-d23d-44a9-823f-bcd661f9d971_0
ff6eb0721822   quay.io/coreos/flannel                               "cp -f /etc/kube-fla…"   5 minutes ago   Exited (0) 5 minutes ago             k8s_install-cni_kube-flannel-ds-j6mnd_kube-system_50c1a168-d23d-44a9-823f-bcd661f9d971_0
95cf338b10a2   rancher/mirrored-flannelcni-flannel-cni-plugin       "cp -f /flannel /opt…"   6 minutes ago   Exited (0) 6 minutes ago             k8s_install-cni-plugin_kube-flannel-ds-j6mnd_kube-system_50c1a168-d23d-44a9-823f-bcd661f9d971_0
9f8c52aa64a5   registry.aliyuncs.com/google_containers/kube-proxy   "/usr/local/bin/kube…"   7 minutes ago   Up 7 minutes                         k8s_kube-proxy_kube-proxy-tk9zw_kube-system_761d9ed2-d1ae-42ad-93b5-cb97a503e243_0
6914dcb035f5   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 7 minutes ago   Up 7 minutes                         k8s_POD_kube-flannel-ds-j6mnd_kube-system_50c1a168-d23d-44a9-823f-bcd661f9d971_0
8d0f91297c9e   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 7 minutes ago   Up 7 minutes                         k8s_POD_kube-proxy-tk9zw_kube-system_761d9ed2-d1ae-42ad-93b5-cb97a503e243_0

//查看名称空间
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   33m
kube-node-lease   Active   33m
kube-public       Active   33m
kube-system       Active   33m

// 指定名称空间去看pods的信息
[root@master ~]# kubectl get pods -n kube-system 
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-8gff5                     1/1     Running   0          34m
coredns-7f89b7bc75-d5hs6                     1/1     Running   0          34m
etcd-master.example.com                      1/1     Running   0          34m
kube-apiserver-master.example.com            1/1     Running   0          34m
kube-controller-manager-master.example.com   1/1     Running   0          34m
kube-flannel-ds-9kxkg                        1/1     Running   0          11m
kube-flannel-ds-j6mnd                        1/1     Running   0          11m
kube-flannel-ds-zw62f                        1/1     Running   0          15m
kube-proxy-cxr8n                             1/1     Running   0          11m
kube-proxy-mhvn6                             1/1     Running   0          34m
kube-proxy-tk9zw                             1/1     Running   0          11m
kube-scheduler-master.example.com            1/1     Running   0          34m

// 查看pods在哪运行
[root@master ~]# kubectl get pods -n kube-system -o wide
NAME                                         READY   STATUS    RESTARTS   AGE   IP               NODE                 NOMINATED NODE   READINESS GATES
coredns-7f89b7bc75-8gff5                     1/1     Running   0          38m   10.244.0.2       master.example.com   <none>           <none>
coredns-7f89b7bc75-d5hs6                     1/1     Running   0          38m   10.244.0.3       master.example.com   <none>           <none>
etcd-master.example.com                      1/1     Running   0          38m   192.168.47.160   master.example.com   <none>           <none>
kube-apiserver-master.example.com            1/1     Running   0          38m   192.168.47.160   master.example.com   <none>           <none>
kube-controller-manager-master.example.com   1/1     Running   0          38m   192.168.47.160   master.example.com   <none>           <none>
kube-flannel-ds-9kxkg                        1/1     Running   0          15m   192.168.47.161   node1.example.com    <none>           <none>
kube-flannel-ds-j6mnd                        1/1     Running   0          15m   192.168.47.163   node2.example.com    <none>           <none>
kube-flannel-ds-zw62f                        1/1     Running   0          19m   192.168.47.160   master.example.com   <none>           <none>
kube-proxy-cxr8n                             1/1     Running   0          15m   192.168.47.161   node1.example.com    <none>           <none>
kube-proxy-mhvn6                             1/1     Running   0          38m   192.168.47.160   master.example.com   <none>           <none>
kube-proxy-tk9zw                             1/1     Running   0          15m   192.168.47.163   node2.example.com    <none>           <none>
kube-scheduler-master.example.com            1/1     Running   0          38m   192.168.47.160   master.example.com   <none>           <none>

测试kubernetes集群

在Kubernetes集群中创建一个pod,验证是否正常运行:

[root@master ~]# kubectl create deployment nginx --image nginx
deployment.apps/nginx created
[root@master ~]# kubectl expose deployment nginx --port=80 --type=NodePort      ## 暴露端口号,暴露的是service的端口号,不能用pod的IP地址去访问,也不能用pod的主机名去访问,要访问的话只能用service访问
service/nginx exposed

// 查看service的IP
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        77m
nginx        NodePort    10.111.4.86    <none>        80:30859/TCP   36m

[root@master ~]# kubectl get pods,svc
NAME                         READY   STATUS              RESTARTS   AGE
pod/nginx-6799fc88d8-zrwsl   0/1     ContainerCreating   0          3m8s

NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP        43m
service/nginx        NodePort    10.111.4.86   <none>        80:30859/TCP   2m5s

// ping service的IP(这里不知道为什么ping不通,但是后面可以访问得到)
[root@master ~]# ping 10.111.4.86
PING 10.111.4.86 (10.111.4.86) 56(84) bytes of data.
^Z
[5]+  已停止               ping 10.111.4.86

// 访问service的IP(这里需要等一会访问才能访问到)
[root@master ~]# curl http://10.111.4.86
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

// 查看pod
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-zrwsl   1/1     Running   0          32m

//查看pod IP
[root@master ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE                NOMINATED NODE   READINESS GATES
nginx-6799fc88d8-zrwsl   1/1     Running   0          33m   10.244.1.2   node1.example.com   <none>           <none>

// 测试能否ping通(这个IP是随时会变的)
[root@master ~]# ping 10.244.1.2  
PING 10.244.1.2 (10.244.1.2) 56(84) bytes of data.
64 bytes from 10.244.1.2: icmp_seq=1 ttl=63 time=0.315 ms
64 bytes from 10.244.1.2: icmp_seq=2 ttl=63 time=0.231 ms

// 测试能否访问
[root@master ~]# curl http://10.244.1.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

网页上访问
master IP:service端口号(我这里的端口号是30859)
kubernetes集群部署_第5张图片

你可能感兴趣的:(kubernetes,kubernetes,docker,容器)