有没有办法使用 VS Code 在 kubernetes pod 中编辑代码?

use*_*871 8 kubernetes visual-studio-code

Typically, if I have a remote server, I could access it using ssh, and VS Code gives a beautiful extension for editing and debugging codes for the remote server. But when I create pods in Kuberneters, I can't really ssh into the container and so I cannot edit the code inside the pod or machine. And the kuberneters plugin in VSCode does not really help because the plugin is used to deploy the code. So, I was wondering whether there is a way edit codes inside a pod using VSCode.

P.S. Alternatively if there is a way to ssh into a pod in a kuberneters, that will do too.

Pav*_* K. 15

Kubernetes + 远程开发扩展现在允许:

  • 连接到 k8s pod
  • 打开远程文件夹
  • 远程执行
  • 远程调试
  • 将终端集成到远程

vscode 附加到 k8s pod

一定有:

所需的 vscode 扩展:


Hle*_*lex 9

如果您的要求是“kubectl edit xxx”以使用 VSCode。

解决办法是:

  • 对于Linuxexport EDITOR='code --wait'

  • 对于Windowsset EDITOR=code --wait

  • 对于macOSexport EDITOR='open -a "Visual Studio Code" --wait'

  • 问题不就是远程编辑而不是使用vscode进行本地编辑吗?使用“Kubernetes + 远程开发扩展”的答案更加相关。 (2认同)

Ani*_*oel 5

它实际上很简单,但在扩展中没有很好的记录。

  1. 在 kubernetes 集群资源管理器中,展开要编辑的资源。双击它应该在右侧的编辑器中打开它的 yaml,您也可以右键单击它并选择“加载”。
  2. 对文件进行必要的更改。不要费心保存,因为它会错误地显示“此命令需要一个打开的文件夹”。
  3. Ctrl + Shift + P (Windows),选择命令:Kubernetes:应用
  4. 它会显示差异,提示您确认申请
  5. 魔法!


Rod*_*oza 1

Pod 只是 kubernetes 中的一个部署单元,这意味着您可以调整其中的容器来接收 ssh 连接。

让我们首先获取允许 ssh 连接的 docker 映像。rastasheep/ubuntu-sshd:18.04图像对此非常好。用它创建一个部署。

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: debugger
  name: debugger
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: debugger
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: debugger
    spec:
      containers:
      - name: debugger
        image: rastasheep/ubuntu-sshd:18.04
        imagePullPolicy: "Always"
      hostname: debugger
      restartPolicy: Always

Run Code Online (Sandbox Code Playgroud)

现在让我们创建一个 LoadBalancer 类型的服务,以便我们可以远程访问 pod。

---
apiVersion: v1
kind: Service
metadata:
  namespace: default
  labels:
    app: debugger
  name: debugger
spec:
  type: LoadBalancer
  ports:
  - name: "22"
    port: 22
    targetPort: 22
  selector:
    app: debugger
status:
  loadBalancer: {}
Run Code Online (Sandbox Code Playgroud)

最后通过运行获取外部ip地址kubectl get svc | grep debugger并用它来测试ssh连接ssh root@external_ip_address

注意这个docker镜像的用户/密码分别是root/root

更新 节点端口示例。我对此进行了测试,它可以运行ssh -p30036@ip,但我必须启用防火墙规则才能使其工作。所以,nmap我给你的命令就有了答案。显然,运行 kubernetes 的机器不允许奇怪端口上的入站流量。与他们交谈,以便他们可以为您提供外部 IP 地址或至少节点中的端口。

---
apiVersion: v1
kind: Service
metadata:
  name: debugger
  namespace: default
  labels:
    app: debugger
spec:
  type: NodePort
  ports:
  - name: "ssh"
    port: 22
    nodePort: 30036
  selector:
    app: debugger
status:
  loadBalancer: {}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

3883 次

最近记录:

4 年,2 月 前