ksync介绍---作为K8S的容器与本地文件夹同步工具

一、什么是ksync

ksync官方网站

1、采用 syncthing 实现同步功能
2、本地 跟 容器内部文件夹创建联系,场景更多是频繁/多次修改内容的

官方介绍:

  • ksync加快了为Kubernetes开发应用程序的开发速度。它可以透明地从本地签出更新集群上运行的容器。这使开发人员能够使用他们最喜欢的IDE,例如Atom或Sublime Text,从集群内部而不是集群外部进行工作。当您可以在几秒钟内看到结果时,没有理由等待几分钟来测试代码更改。

请添加图片描述

二、安装

以下在K8S的master节点进行测试

2.1 在线安装
curl https://ksync.github.io/gimme-that/gimme.sh | bash

ksync init --upgrade

执行上面两个命令,会下载syncthing以及检查当前环境,同时在k8s环境部署ksync,执行成功即安装成功。

2.2 离线安装

下载ksync安装包:https://github.com/ksync/ksync/releases
下载syncthing安装包:https://github.com/syncthing/syncthing

安装

mv ksync_linux_amd64 ksync
mv ksync /usr/local/bin 

因为是离线原因,执行 ksync init --upgrade 会报错,但是会创建 ~/.ksync

tar xvf syncthing-linux-amd64-v1.19.2.tar.gz
把解压的文件放置到 ~/.ksync/bin 中,确保有 ~/.ksync/bin/syncthing

ksync init 的过程,会在 k8s 中安装 ksync,注意集群中镜像是否存在。

2.3 执行
ksync watch &
  • 执行完成上面命令之后,另开一个终端

测试pods

kubectl apply -f https://ksync.github.io/ksync/example/app/app.yaml

验证容器是否部署成功

[root@master-01 ~]# kubectl get po --selector=app=app
NAME                   READY   STATUS    RESTARTS   AGE
app-69df5fc456-2kr59   1/1     Running   0          23h

把app中的code文件夹,挂载到本地

mkdir -p $(pwd)/ksync
ksync create --selector=app=app -n default $(pwd)/ksync /code
  • 观察 ksync watch & 打印的内容
[root@master-01 ~]# ksync watch &
[1] 24938
[root@master-01 ~]# INFO[0000] new pod detected                              pod=app-69df5fc456-2kr59 spec=modern-hookworm
INFO[0000] listening                                     bind=127.0.0.1 port=40322
INFO[0002] syncthing listening                           port=8384 syncthing=localhost
INFO[0007] finished unary call with code OK              grpc.code=OK grpc.method=GetSpecList grpc.service=proto.ksync.Ksync grpc.start_time="2022-04-21T09:22:33+08:00" grpc.time_ns=188543 peer.address="127.0.0.1:49228" span.kind=server system=grpc

其他操作

[root@mos-dev-master-01 ~]# ksync create -l app=mos-ncc-ecmd -n mos /u01/ccz-test/ksync/emergency-command /emergency-command
[root@mos-dev-master-01 ~]# ksync create -l app=mos-tech-gis-web -n mos /u01/ccz-test/ksync/mos-tech-gis-web /static
2.4 注意,偶尔切换环境,ksync get 所有都是waiting 或者 starting

每一个监听,只能在当前watch查看,如果改变当前监听地址,则无法继续监听?

解决方法,ksync delete 后,再 ksync create 则可以解决问题。

2.5 查看ksync的所有功能
[root@mos-dev-master-01 ~]# ksync
Inspect and sync files from remote containers.

Usage:
  ksync [command]

Available Commands:
  clean       Remove installed pieces
  create      Create a new spec
  delete      Delete an existing spec
  doctor      Troubleshoot and verify your setup is correct.
  get         Get all specs.
  help        Help about any command
  init        Prepare ksync.
  reload      Reload a remote spec.
  update      Update ksync to the latest version.
  version     View the versions of both the local binary and remote service.
  watch       Watch configured specs and start syncing files when required.

Flags:
      --context string     name of the kubeconfig context to use
  -h, --help               help for ksync
      --log-level string   log level to use. (default "info")
  -n, --namespace string   namespace to use (default "default")
  -o, --output string      output format to use (e.g. "json") (default "pretty")
      --port int           port on watch listens on locally (default 40322)

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

执行所有操作之前,都需要执行 watch

常用操作:
获取所有挂载:

ksync get
ksync delete xxx

三、出现异常

xxxx
context deadline exceeded

可以使用debug日志的方式来查找错误

ksync doctor --log-level=debug

例如我在安装过程中,出现缺少socat

手动安装即可:

yum install -y socat
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值