K8S系列文章之 使用Kind部署K8S 并发布服务

简单介绍

kind 即 Kubernetes In Docker,顾名思义,就是将 k8s 所需要的所有组件,全部部署在一个docker容器中,是一套开箱即用的 k8s 环境搭建方案。使用 kind 搭建的集群无法在生产中使用,但是如果你只是想在本地简单的玩玩 k8s,不想占用太多的资源,那么使用 kind 是你不错的选择。同样,kind 还可以很方便的帮你本地的 k8s 源代码打成对应的镜像,方便测试。

使用kind

在一台 centos 上简单尝试一下 kind,前提是必须要安装好 docker 和 kubectl。

wget https://github.com/kubernetes-sigs/kind/releases/download/0.2.1/kind-linux-amd64
mv kind-linux-amd64 kind
chmod +x kind
mv kind /usr/local/bin

安装完成之后,我们来看看 kind 有哪些命令。

build 用来从 k8s source 构建一个镜像。

create、delete 创建、删除集群。

export 命令目前只有一个 logs 选项,作用是将内部所有容器的日志拷贝到宿主机的某个目录下。

get 查看当前有哪些集群,哪些节点,以及 kubectl 配置文件的地址

load 可以从宿主机向 k8s 容器内导入镜像。

[root@node-2 ~]# kind
kind creates and manages local Kubernetes clusters using Docker container 'nodes'

Usage:
  kind [command]

Available Commands:
  build       Build one of [base-image, node-image]
  create      Creates one of [cluster]
  delete      Deletes one of [cluster]
  export      exports one of [logs]
  get         Gets one of [clusters, nodes, kubeconfig-path]
  help        Help about any command
  load        Loads images into nodes
  version     prints the kind CLI version

Flags:
  -h, --help              help for kind
      --loglevel string   logrus log level [panic, fatal, error, warning, info, debug] (default "warning")
      --version           version for kind

Use "kind [command] --help" for more information about a command.

下面来以最简单的方式安装一个集群

注意点 需要单独下载一个kubectl 不然无法操作 kubectl 等相关命令

[root@node-2 ~]# kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.13.4)  
 ✓ Preparing nodes   
 ✓ Creating kubeadm config   
 ✓ Starting control-plane  ️ 
Cluster creation complete. You can now use the cluster with:

export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
kubectl cluster-info

[root@node-2 ~]# export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
[root@node-2 ~]# kubectl cluster-info
Kubernetes master is running at https://localhost:39284
KubeDNS is running at https://localhost:39284/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@node-2 ~]# kubectl get node
NAME                 STATUS   ROLES    AGE   VERSION
kind-control-plane   Ready    master   99s   v1.13.4

使用 kind create cluster 安装,是没有指定任何配置文件的安装方式。从安装打印出的输出来看,分为4步:

  1. 查看本地上是否存在一个基础的安装镜像,默认是 kindest/node:v1.13.4,这个镜像里面包含了需要安装的所有东西,包括了 kubectl、kubeadm、kubelet 二进制文件,以及安装对应版本 k8s 所需要的镜像,都以 tar 压缩包的形式放在镜像内的一个路径下
  2. 准备你的 node,这里就是做一些启动容器、解压镜像之类的工作
  3. 生成对应的 kubeadm 的配置,之后通过 kubeadm 安装,安装之后还会做另外的一些操作,比如像我刚才仅安装单节点的集群,会帮你删掉 master 节点上的污点,否则对于没有容忍的 pod 无法部署。
  4. 启动完毕

查看当前集群的运行情况

[root@node-2 ~]# kubectl get po -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-86c58d9df4-6g66f                     1/1     Running   0          21m
coredns-86c58d9df4-pqcc4                     1/1     Running   0          21m
etcd-kind-control-plane                      1/1     Running   0          20m
kube-apiserver-kind-control-plane            1/1     Running   0          20m
kube-controller-manager-kind-control-plane   1/1     Running   0          20m
kube-proxy-cjgnt                             1/1     Running   0          21m
kube-scheduler-kind-control-plane            1/1     Running   0          21m
weave-net-ls2v8                              2/2     Running   1          21m

默认方式启动的节点类型是 control-plane 类型,包含了所有的组件。包括2 * coredns、etcd、api-server、controller-manager、kube-proxy、sheduler,网络插件方面默认使用的是 weave,且目前只支持 weave,不支持其他配置,如果需要可以修改 kind 代码进行定制。

基本上,kind 的所有秘密都在那个基础镜像中。下面是基础容器内部的 /kind 目录,在 bin 目录下安装了 kubelet、kubeadm、kubectl 这些二进制文件,images 下面是镜像的 tar 包,kind 在启动基础镜像后会执行一遍 docker load 操作将这些 tar 包导入。manifests 下面是 weave 的 cni。

root@kind-control-plane:/kind# ls
bin  images  kubeadm.conf  manifests  systemd  version

root@kind-control-plane:/kind# ls bin/
kubeadm  kubectl  kubelet

root@kind-control-plane:/kind# ls images/
4.tar  6.tar  8.tar               kube-controller-manager.tar  kube-scheduler.tar
5.tar  7.tar  kube-apiserver.tar  kube-proxy.tar

root@kind-control-plane:/kind# ls manifests/
default-cni.yaml

root@kind-control-plane:/kind# ls systemd/
10-kubeadm.conf  kubelet.service

创建多节点的集群

默认安装的集群只带上了一个控制节点,下面重新创建一个两节点的集群。配置文件如下,在 node 中可以配置的不是很多,除了 role 另外的可以更改 node 使用的镜像,不过我这边还是使用默认的镜像。

apiVersion: kind.sigs.k8s.io/v1alpha3
kind: Cluster
nodes:
  - role: control-plane
  - role: worker

还是使用命令安装

[root@node-2 ~]# kind create cluster --config=kind-config.yaml 
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.13.4)  
 ✓ Preparing nodes    
 ✓ Creating kubeadm config   
 ✓ Starting control-plane  ️ 
 ✓ Joining worker nodes   
Cluster creation complete. You can now use the cluster with:

export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
kubectl cluster-info

[root@node-2 ~]# kubectl get nodes
NAME                 STATUS   ROLES    AGE     VERSION
kind-control-plane   Ready    master   3m20s   v1.13.4
kind-worker          Ready    <none>   3m8s    v1.13.4

[root@node-2 ~]# kubectl get po -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-86c58d9df4-cnqhc                     1/1     Running   0          5m29s
coredns-86c58d9df4-hn9mv                     1/1     Running   0          5m29s
etcd-kind-control-plane                      1/1     Running   0          4m24s
kube-apiserver-kind-control-plane            1/1     Running   0          4m17s
kube-controller-manager-kind-control-plane   1/1     Running   0          4m21s
kube-proxy-8t4xt                             1/1     Running   0          5m27s
kube-proxy-skd5v                             1/1     Running   0          5m29s
kube-scheduler-kind-control-plane            1/1     Running   0          4m18s
weave-net-nmfq2                              2/2     Running   1          5m27s
weave-net-srdfw                              2/2     Running   0          5m29s

大功告成,一套测试集群很轻松就搭建完成啦。

官网链接

建议安装最新版本的 支持部署最新K8S的版本

下面这个是最新版本能支持部署的K8S版本

我们采用的是 K8S In Docker 模式,外部如何访问容器内的K8S集群服务呢?

实战步骤

1. 重建K8s,暴露80、443端口

在上一节中,我们介绍过用kind创建K8s时,是相当在本地运行了一个容器,而K8s Cluster就运行在这个容器中。

所以,如果我们想从外部访问kind K8s的话,就需要把这个容器的端口(K8s的端口)暴露出来。

目前可以看到只有38325这个端口可以从外部访问

为了下一步测试,我们重新创建一个新的K8s cluster并且把80和443端口暴露出来。

运行下列命令删除现在的k8s cluster

kind delete cluster --name tsk8s

运行下列命令创建新的k8s cluster

cat <<EOF | kind create cluster --name tsk8s --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "ingress-ready=true"
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    protocol: TCP
  - containerPort: 443
    hostPort: 443
    protocol: TCP
  - containerPort: 30000
    hostPort: 30000
    protocol: TCP
EOF

说明:

  • extraPortMappings:把K8s容器(相当于K8s所在的服务器)端口暴露出来,这里暴露了80、443、30000
  • node-labels:只允许Ingress controller运行在有"ingress-ready=true"标签的node上

运行结果

这时可以看到80、443、30000端口已经暴露出来了

2. 部署Deployment、Service

部署Deployment

新建文件my-dep.yaml,添加以下内容

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-dep
spec:
  replicas: 1  # number of replicas of frontEnd application
  selector:
    matchLabels:
      app: httpd-app
  template:
    metadata:
      labels: # Must match 'Service' and 'Deployment' labels
        app: httpd-app
    spec:
      containers:
      - name: httpd
        image: httpd # docker image of frontend application
        ports:
        - containerPort: 80

说明:

  • Deployment的名称为"httpd-dep"
  • 管理的Pods需要带有"app: httpd-app"标签
  • Pod模板中指定运行的镜像为 Docker 公共仓库中的httpd

运行以下命令创建Deployment

kubectl apply -f my-dep.yaml

创建Deployment结果

查看新建的Pod,-o wide可以看到更多的信息

kubectl get pods -o wide

说明:可以看到Pod被分配了一个K8s内部IP,这个IP不能从K8s外部访问到,而且这个IP在Pod重建后会变化

部署Service

新建文件my-svc.yaml,添加以下内容

kind: Service
apiVersion: v1
metadata:
  name: httpd-svc
spec:
  selector:
      app: httpd-app
  ports:
  - port: 80

运行以下命令创建Service

kubectl apply -f my-svc.yaml

创建Service结果

查看Service信息

kubectl get svc/httpd-svc

说明:Service的IP是不会变化的(除非重建Service)

查看Service详细信息

kubectl describe svc/httpd-svc

说明:

可以看到这里Service关联的Endpoint的IP和端口就是上面Pod的IP和端口。每次Pod重建后这里的Endpoint就会刷新为新的IP。

目前这个Service只有内部IP和端口,所以这个Service还只能用在K8s内部暴露Pods。

下面我们修改Service配置,使用K8s外部也可以访问到这个Service

更改Serivce(nodePort)

修改my-svc.yaml,添加以下内容

kind: Service
apiVersion: v1
metadata:
  name: httpd-svc
spec:
  selector:
      app: httpd-app
  type: NodePort #1
  ports:
  - port: 80
    nodePort: 30000 #2

说明:

  • #1 Service type默认为ClusterIP,即只有内部IP。改为NodePort后,Service会把K8s内部的端口映射到集群所在的服务器上
  • #2 指定Service映射到服务器上的端口为30000

再次运行 kubectl apply 命令

运行结果

再次查看Service信息,可以看到端口中多了一个30000

30000这个端口被映射到了K8s集群所在的服务器上(即K8s运行的容器),而我们在创建kind K8s时把容器的30000端口又映射到本地,所以现在我们可以在本地用浏览器访问30000端口。

在本地可以访问到30000端口上的httpd应用

引申


除了用NodePort暴露服务,我们还可以用ingress/ingress controller实现相同的功能。我们将在下一篇文章中测试ingress/ingress controller

相关推荐
林鸿群2 分钟前
Docker运行MSSQL2022
运维·docker·容器
A-刘晨阳16 分钟前
ELFK 8.12.2 部署 -- docker部署方式⚽
linux·运维·elk·docker·容器
繁星日月2 小时前
利用docker搭建漏洞环境,使用SSRF+Redis写入centos以及ubuntu的公钥,实现免密登录
redis·安全·ubuntu·docker·容器·centos·渗透
crossoverJie3 小时前
从 Helm 到 Operator:Kubernetes应用管理的进化
kubernetes·helm·operator
biass3 小时前
k8s 部署RuoYi-Vue-Plus之mysql搭建
vue.js·mysql·kubernetes
Python私教11 小时前
docker部署onlyoffice,开启JWT权限校验Token
运维·docker·容器
lendq13 小时前
k8s-第八节-Helm
云原生·容器·kubernetes
mry614 小时前
Docker 容器网络及其配置说明
docker·容器
lendq16 小时前
k8s-第三节-工作负载
云原生·容器·kubernetes
微风◝16 小时前
三、docker配置阿里云镜像仓库并配置docker代理
阿里云·docker·容器