营销网站建设价格,网站制作 网络推广,龙岗网站开发公司,网站建设相关职业岗位职责简单介绍
kind 即 Kubernetes In Docker#xff0c;顾名思义#xff0c;就是将 k8s 所需要的所有组件#xff0c;全部部署在一个docker容器中#xff0c;是一套开箱即用的 k8s 环境搭建方案。使用 kind 搭建的集群无法在生产中使用#xff0c;但是如果你只是想在本地简单…简单介绍
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 容器内导入镜像。
[rootnode-2 ~]# kind
kind creates and manages local Kubernetes clusters using Docker container nodesUsage: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 commandload Loads images into nodesversion prints the kind CLI versionFlags:-h, --help help for kind--loglevel string logrus log level [panic, fatal, error, warning, info, debug] (default warning)--version version for kindUse kind [command] --help for more information about a command.
下面来以最简单的方式安装一个集群 注意点 需要单独下载一个kubectl 不然无法操作 kubectl 等相关命令 [rootnode-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 --namekind)
kubectl cluster-info[rootnode-2 ~]# export KUBECONFIG$(kind get kubeconfig-path --namekind)
[rootnode-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/proxyTo further debug and diagnose cluster problems, use kubectl cluster-info dump.
[rootnode-2 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready master 99s v1.13.4 使用 kind create cluster 安装是没有指定任何配置文件的安装方式。从安装打印出的输出来看分为4步
查看本地上是否存在一个基础的安装镜像默认是 kindest/node:v1.13.4这个镜像里面包含了需要安装的所有东西包括了 kubectl、kubeadm、kubelet 二进制文件以及安装对应版本 k8s 所需要的镜像都以 tar 压缩包的形式放在镜像内的一个路径下准备你的 node这里就是做一些启动容器、解压镜像之类的工作生成对应的 kubeadm 的配置之后通过 kubeadm 安装安装之后还会做另外的一些操作比如像我刚才仅安装单节点的集群会帮你删掉 master 节点上的污点否则对于没有容忍的 pod 无法部署。启动完毕 查看当前集群的运行情况
[rootnode-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。
rootkind-control-plane:/kind# ls
bin images kubeadm.conf manifests systemd versionrootkind-control-plane:/kind# ls bin/
kubeadm kubectl kubeletrootkind-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.tarrootkind-control-plane:/kind# ls manifests/
default-cni.yamlrootkind-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
还是使用命令安装
[rootnode-2 ~]# kind create cluster --configkind-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 --namekind)
kubectl cluster-info[rootnode-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[rootnode-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-planekubeadmConfigPatches:- |kind: InitConfigurationnodeRegistration:kubeletExtraArgs:node-labels: ingress-readytrueextraPortMappings:- containerPort: 80hostPort: 80protocol: TCP- containerPort: 443hostPort: 443protocol: TCP- containerPort: 30000hostPort: 30000protocol: TCP
EOF说明
extraPortMappings把K8s容器相当于K8s所在的服务器端口暴露出来这里暴露了80、443、30000node-labels只允许Ingress controller运行在有ingress-readytrue标签的node上
运行结果 这时可以看到80、443、30000端口已经暴露出来了 注意如果是在公司代理环境下我们要在K8s容器中设置代理才可以正常进行下面的测试设置代理请参考上一篇文章《代理环境下在WSL2中用Kind创建Kubernetes集群》”
2. 部署Deployment、Service
部署Deployment
新建文件my-dep.yaml添加以下内容
apiVersion: apps/v1
kind: Deployment
metadata:name: httpd-dep
spec:replicas: 1 # number of replicas of frontEnd applicationselector:matchLabels:app: httpd-apptemplate:metadata:labels: # Must match Service and Deployment labelsapp: httpd-appspec:containers:- name: httpdimage: httpd # docker image of frontend applicationports:- 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-appports:- 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
更改SerivcenodePort
修改my-svc.yaml添加以下内容
kind: Service
apiVersion: v1
metadata:name: httpd-svc
spec:selector:app: httpd-apptype: NodePort #1ports:- port: 80nodePort: 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