惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
N
Netflix TechBlog - Medium
量子位
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
H
Help Net Security
D
Docker
D
DataBreaches.Net
T
Tailwind CSS Blog
阮一峰的网络日志
阮一峰的网络日志
B
Blog
博客园 - 聂微东
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
F
Full Disclosure
GbyAI
GbyAI
F
Fortinet All Blogs
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗
小众软件
小众软件
V
Visual Studio Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
J
Java Code Geeks
雷峰网
雷峰网
P
Privacy & Cybersecurity Law Blog
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog
Webroot Blog
Webroot Blog
美团技术团队
N
News | PayPal Newsroom
G
Google Developers Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园_首页
V
Vulnerabilities – Threatpost

f2h2h1's blog

使用yii3实现一个微框架 claw养殖技术 计算机网络基础知识 定时任务 ACME的使用经验 magento2加上varnish缓存 开发Magento2的模块 在magento2中使用persisted-query socket编程 一些开发笔记 一段CSDN文章主要内容的油猴脚本 电子邮件的不完整总结 git的笔记 在Windows下配置PHP服务器 终端,控制台和外壳 PHP各种运行方式的不完整总结 把网页导出成PDF 和颜色相关的笔记 HTTP认证方式的不完整总结 SEO的经验 密码学入门简明指南 文件的上传和下载 用纯CSS3实现的滑动按钮 在VSCode里调试PHP Linux的GUI 关于字符编码的一些坑 nc的使用和原理 在Windows下安装Magento2 对JS原型链的理解 使用docker-compose部署magento2 浏览器和服务器通讯方式的不完整总结 观察网站性能 一些关于Linux的笔记 telnet的不完整总结 在Windows下安装pear MySQL的时间类型和时间相关的函数 Windows下通过PEB读取进程的环境变量 关于 在VSCode里使用Xdebug远程调试PHP 在Windows下搭建git服务 关于环境变量的不完整总结 使用shell实现的kv数据库 如何完成以xx管理系统为选题的毕业设计 数字号码资源 各种标记语言 使用PowerShell实现的http服务器 DNSSEC简介 nginx+ffmpeg+websocket实现的直播例子 使用Tesseract识别字符验证码 使用docker部署nuxt FirstData后台的设置 paypal,firtdata,支付宝的不完整接入指南 微信支付的不完整接入指南 用docker-compose部署lnmp环境 mongodb分片 练习
kind相关经验
2022-07-30 · via f2h2h1's blog

这篇文章最后更新的时间在六个月之前,文章所叙述的内容可能已经失效,请谨慎参考!

kind 是 k8s in docker 的缩写

先安装 kind 再安装 kubctl , kubectl 的版本要根据 kind 来决定

和 minikube mricrok8s 相比, kind 节点运行在 docker 里,对宿主机影响比较小可以放心的折腾。 kind 也是 k8s 官方文档里推荐的学习环境之一。

大概的运行环境

  • debian 11
  • kind 0.12
  • k8s 1.23
  • docker 20.10.13

安装 kind

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.12.0/kind-linux-amd64
chmod +x ./kind
mv ./kind /usr/local/kind

参考 https://kind.sigs.k8s.io/docs/user/quick-start/

kind 常用命令

  • 查看帮助

    kind help
    
  • 创建集群

    kind create cluster # 默认的集群名是 kind
    kind create cluster --name kind-2
    
  • 查看当前的集群

    kind get clusters
    
  • 删除集群

    kind delete cluster
    kind delete cluster --name kind-2
    

安装 kubectl

生成 kubeconfig 文件

kind get kubeconfig --name="kind" > kubeconfig

使用 docker 容器

  1. 拉取 kubectl 的镜像

    docker pull bitnami/kubectl:1.23
    
  2. 尝试运行

    docker run --rm -it \
        --network=host \
        -v `pwd`/kubeconfig:/.kube/kubeconfig \
        bitnami/kubectl:1.23 --kubeconfig=/.kube/kubeconfig \
        get nodes
    
  3. 设置别名

    1. 修改 ~/.bashrc ,要注意修改 kubeconfig 的路径
      alias kubectl="docker run --rm -it --network=host -v /root/kubeconfig:/.kube/kubeconfig bitnami/kubectl:1.23 --kubeconfig=/.kube/kubeconfig"
      
    2. 运行 source ~/.bashrc
  4. 之后就可以直接在命令行里使用 kubectl 命令了,但因为使用了容器,如果要上传或下载文件,还是需要用原始的 docker 命令

使用原生程序

curl -LO https://dl.k8s.io/release/v1.23.1/bin/linux/amd64/kubectl && \
chmod +x ./kubectl && \
mv ./kubectl /bin/kubectl

使用原生程序,需要把 kubeconfig 复制到 ~/.kube/kubeconfig

新建集群

删除原本的默认集群,原本的默认集群只有一个 master 节点

kind delete cluster

新建一个集群,集群里有一个 master 节点和一个 worker 节点

  1. 新建一个文件 kind-config.yaml

    apiVersion: kind.sigs.k8s.io/v1alpha3
    kind: Cluster
    nodes:
        - role: control-plane
        - role: worker
    
  2. 运行新建集群的命令

    kind create cluster --name kind2 --config=kind-config.yaml
    
  3. 查看集群信息

    kubectl cluster-info --context kind-kind-2
    

kubectl 常用命令

  • 查看集群的信息

    kubectl cluster-info --context kind-kind # kind-集群名
    kubectl cluster-info --context kind-kind-2
    
  • 查看命令空间

    kubectl get namespaces
    
  • 查看命令空间下的 pods

    kubectl -n <namespaces> get pods
    
  • 查看 pod 信息

    kubectl -n <namespaces> describe pod <pod-name>
    
    • 可以通过这个命令获取 pod 里的容器信息,例如 容器名
  • 输出所有 pod 的信息

    kubectl get pods --all-namespaces -o json > pod.json
    
    • 这个命令会输出非常多的信息,最好重定向到文件里
  • 使用 jsonpath

    输出所有 pod
    kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{end}'
    输出所有 命令空间 pod 容器 和镜像
    kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{range .spec.containers[*]}{"\t"}{.name}{"\t"}{.image}{"\n"}{end}{end}'
    
    • jsonpath 十分有用。可以筛选数据和按格式输出(虽然作用有限)
    • kubectl 的 jsonpath 和一般的 jsonpath 有一点的差别,例如不支持正则表达式
  • 复制文件进 pod 的容器里

    kubectl -n <namespaces> cp <local-path> <pod-name>:<container-path>
    如果 pod 里有多个容器,就需要指定容器名
    kubectl -n <namespaces> -c <container-name> cp <local-path> <pod-name>:<container-path>
    
  • 复制 pod 的文件到本地

    kubectl -n <namespaces> cp <pod-name>:<container-path> <local-path>
    如果 pod 里有多个容器,就需要指定容器名
    kubectl -n <namespaces> -c <container-name> cp <pod-name>:<container-path> <local-path>
    
  • 在 pod 的容器里执行命令

    kubectl -n <namespaces> exec <pod-name> -- <command>
    如果 pod 里有多个容器,就需要指定容器名
    kubectl -n <namespaces> -c <container-name> exec <pod-name> -- <command>
    
  • 进入 pod 的容器里

    kubectl -n <namespaces> exec -it <pod-name> -- bash
    如果 pod 里有多个容器,就需要指定容器名
    kubectl -n <namespaces> -c <container-name> exec -it <pod-name> -- bash
    
    • 其实就是在容器里执行 bash 命令,有些容器可能会没有 bash ,可以试试 sh 或用绝对路径 /bin/bash /bin/sh 这类
    • -it 是这两个参数的合并 --interactive --tty
    • --interactive 交互式的,保持 stdin 开启,即使未连接
    • --tty 分配一个伪终端设备
    • 其实就是 --interactive 负责输入, --tty 负责输出,这两个参数的含义,应该和 docker 里一样的

安装 kubernetes-dashboard

  1. 下载并运行配置文件

    kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.0/aio/deploy/recommended.yaml
    
  2. 启动代理

    kubectl proxy
    
  3. 访问

    http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
    
  4. 如果需要远程访问(假设k8s是部署在远程服务器上,且需要在本地访问)

    • 用 ssh 的转发
      ssh -o ServerAliveInterval=60 -L localhost:8001:localhost:8001 -NT username@hostname
      
  • 默认情况下 kubernetes-dashboard 只能在本地访问
  • kubectl proxy 命令常用参数
    • h 查看帮助
    • accept-hosts 表示哪些客户端访问,默认只允许 localhost 和 127.0.0.1
    • address 表示本机绑定的ip地址,默认是 127.0.0.1
    • port 表示代理的接口,如果值为 0 的话,则随机一个端口,可以通过一开始的输出判断绑定了哪个端口
  • 如果使用 token 的方式登录则需要新建一个用户

参考 https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/

新建一个用户

主要是为了能登入 dashboard

  1. 新建 dashboard-adminuser.yaml

    apiVersion: v1
    kind: ServiceAccount
    metadata:
        name: admin-user
        namespace: kubernetes-dashboard
    
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
        name: admin-user
    roleRef:
        apiGroup: rbac.authorization.k8s.io
        kind: ClusterRole
        name: cluster-admin
    subjects:
    - kind: ServiceAccount
        name: admin-user
        namespace: kubernetes-dashboard
    
  2. 上面那个 yaml 文件,是新建一个 ServiceAccount ,并给这个用户绑定了一个角色

  3. 运行配置文件

    kubectl apply -f dashboard-adminuser.yaml
    
  4. 获得 token

    kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
    

参考 https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md