Kubernetes初探2

还是初探,因为我又试了试kind,丝滑!
官网Learning environment中,Install Tools说了四种工具:

kubectl:命令行工具,与集群交互
kind :本地运行Kubernetes 的工具
minikube :也是本地运行Kubernetes的工具
kubeadm :创建和管理集群的工具,可用于生产环境,也是官方推荐的

官方推荐了三种生产环境,部署集群的工具:
Bootstrapping clusters with kubeadm
Installing Kubernetes with kops
Installing Kubernetes with Kubespray

minikube的名字欺骗了我,我以为它会是比较好用的一个,所以第一个就用了它,付出了血泪的教训,但是··你是没用kind,我脑海里只有两个字,丝滑!

PS:我才知道,minikube 其实是在本地docker中运行了一个镜像,然后在这个镜像中运行的Kubernetes集群,这个镜像就是kicbase
如果你exec 进去kicbase用 docker ps 查看,会发现里面还有docker,在docker镜像里面安装docker,这个骚操作,我还没见过。厉害了

# 安装kind
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.15.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

# 启动------超快有木有,没有任何错误
kind create cluster
# 删除
kind delete cluster

# 启动多个cluster
kind create cluster --name kind-2
# cluster 列表
kind get clusters

# 切换 kubectl context
kubectl cluster-info --context kind-kind
kubectl cluster-info --context kind-kind-2

# 查询当前cluster 中的镜像
# docker exec -it my-node-name crictl images
docker exec -it kind-control-plane crictl images

# crictl 是什么?
# crictl 是 CRI 兼容的容器运行时命令行接口,可以使用它来检查和调试 k8s 节点上的容器运行时和应用程序。
# 参考 https://blog.csdn.net/ll837448792/article/details/125756479

然后就可以kubectl create 了
对比一下minikube,minikube 是start/stop,但是kind是create/delete,那如果想停止cluster怎么办?

记录一下遇到的名词 :
cgroups:其名称源自控制组群(control groups)的缩写,是内核的一个特性,用于限制、记录和隔离一组进程的资源使用(CPU、内存、磁盘 I/O、网络等)

事实上,容器技术实现资源层面上的限制和隔离,就依赖于 Linux 内核所提供的 cgroup 和 namespace 技术。

  • cgroup 的主要作用:管理资源的分配、限制;
  • namespace 的主要作用:封装抽象,限制,隔离,使命名空间内的进程看起来拥有他们自己的全局资源;

systemd与cgroupfs 都是cgroup manager
cgroupfs是文件驱动修改,内核功能没有提供任何的系统调用接口,而是对 linux vfs 的一个实现,因此可以用类似文件系统的方式进行操作。
systemd封装了 cgroups 的软件也能让你通过它们定义的接口控制 cgroups 的内容,因此是通过接口调用驱动修改。
多数linux发行版的cgroup的驱动为systemd

minikube、docker 默认用 cgroupfs
Kubernetes 默认用systemd

例如:

# 启动minikube
minikube start --container-runtime=docker --no-kubernetes
# 进入容器
minikube ssh
sudo cat /var/lib/kubelet/config.yaml
docker@minikube:~$ sudo cat /var/lib/kubelet/config.yaml
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
  anonymous:
    enabled: false
  webhook:
    cacheTTL: 0s
    enabled: true
  x509:
    clientCAFile: /var/lib/minikube/certs/ca.crt
authorization:
  mode: Webhook
  webhook:
    cacheAuthorizedTTL: 0s
    cacheUnauthorizedTTL: 0s
cgroupDriver: cgroupfs

2022年9月18日 周末更新:
kind 创建多节点的集群,创建multi-node.yaml

# three node (two wokers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker

# 创建集群
kind create cluster --name multi-node --config=multi-node.yaml
# 查询集群节点数量
[kube@node01 kind]$ kubectl get nodes
NAME                       STATUS   ROLES           AGE   VERSION
multi-node-control-plane   Ready    control-plane   13m   v1.25.0
multi-node-worker          Ready    <none>          12m   v1.25.0
multi-node-worker2         Ready    <none>          12m   v1.25.0

# 同样,docker中也会有三个容器
[kube@node01 kind]$ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                       NAMES
995d531520bd   kindest/node:v1.25.0   "/usr/local/bin/entr…"   15 minutes ago   Up 15 minutes                               multi-node-worker2
69e9af250f5f   kindest/node:v1.25.0   "/usr/local/bin/entr…"   15 minutes ago   Up 15 minutes                               multi-node-worker
8a055d13180d   kindest/node:v1.25.0   "/usr/local/bin/entr…"   15 minutes ago   Up 15 minutes   127.0.0.1:38465->6443/tcp   multi-node-control-plane

下面安装一个nginx试试:
先创建集群,新建config-with-port-mapping.yaml,带端口映射:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30080
    hostPort: 30070
    
- role: worker
- role: worker
# 把原来的cluster 删了
kind delete cluster -n multi-node
# 重新创建集群
kind create cluster --config=config-with-port-mapping.yaml

然后创建Deployment和NodePort Service,新建nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web-nginx
  name: web-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-nginx
  template:
    metadata:
      labels:
        app: web-nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web-nginx
spec:
  selector:
    app: web-nginx
  type: NodePort
  ports:
    - port: 80
      nodePort: 30080

# 部署 nginx
kubectl create -f nginx.yaml
# 查看 replca 分布
[kube@node01 kind]$ kubectl get pods -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP           NODE           NOMINATED NODE   READINESS GATES
web-nginx-7dc47b4fd9-9xn69   1/1     Running   0          39s   10.244.1.2   kind-worker    <none>           <none>
web-nginx-7dc47b4fd9-vr42z   1/1     Running   0          39s   10.244.2.2   kind-worker2   <none>           <none>
web-nginx-7dc47b4fd9-zbmgb   1/1     Running   0          39s   10.244.2.3   kind-worker2   <none>           <none>

# 进入worker2节点,会发现运行了两个nginx
[kube@node01 kind]$ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                                                 NAMES
c38fc8e540ec   kindest/node:v1.25.0   "/usr/local/bin/entr…"   6 minutes ago   Up 6 minutes   127.0.0.1:45433->6443/tcp, 0.0.0.0:30070->30080/tcp   kind-control-plane
ee17975900ad   kindest/node:v1.25.0   "/usr/local/bin/entr…"   6 minutes ago   Up 6 minutes                                                         kind-worker2
b6e49011323a   kindest/node:v1.25.0   "/usr/local/bin/entr…"   6 minutes ago   Up 6 minutes                                                         kind-worker
[kube@node01 kind]$ docker exec -it ee17975900ad bash
root@kind-worker2:/# crictl ps
CONTAINER           IMAGE               CREATED             STATE               NAME                ATTEMPT             POD ID              POD
82495642a6856       2d389e545974d       2 minutes ago       Running             nginx               0                   9ba1ebfeec94d       web-nginx-7dc47b4fd9-zbmgb
d2999040fed9b       2d389e545974d       2 minutes ago       Running             nginx               0                   39c5f9c75b4a6       web-nginx-7dc47b4fd9-vr42z
8e50e2d5fc58d       c12a8a85ef17f       5 minutes ago       Running             kube-proxy          0                   e1ce6958a20d3       kube-proxy-98b2n
81dcc2c72e77c       d921cee849482       5 minutes ago       Running             kindnet-cni         0                   cdb1bfe4a832e       kindnet-7txqz
# 访问nginx,端口是 从主机的30070,到docker内的30080,到集群的80
[kube@node01 kind]$ curl http://localhost:30070
<!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>

原来 kind 的意思是 Kubernetes In Docker,我还纳闷 kind·····

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值