laitimes

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

author:Linyb Geek Way

preface

A long time ago, I published an article on how SpringCloud's locally developed microservices can call remote k8s microservices. It focuses on how to use kt-connect to get the development environment and K8S environment debugged. The Nocalhost introduced today can not only achieve the above effects, but also become more powerful. Let's get down to business

What is Nocalhost

Nocalhost is an open-source, cloud-native IDE-based application development tool that features:

  • Build, test, and debug applications directly in a Kubernetes cluster
  • Provides easy-to-use IDE plugins (VS Code support and

JetBrains), even when developing and debugging in a Kubernetes cluster, Nocalhost maintains the same development experience as local development

  • Develop with instant file sync: Instantly synchronize your code changes to remote containers without re-image or container restarts.

How Nocalhost works

Nocalhost consists of a single binary CLI and IDE plugin. Ideally, you can use it directly with an IDE you are familiar with. Nocalhost doesn't require server-side components because it communicates directly with your Kubernetes cluster via KubeConfig, just like kubectl.

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Hands-on

Note: This article uses the Java language as an example, and the window environment is demonstrated by IDEA

1. Install the Nocalhost plugin in idea

Set up > plugins via file > > Browse plugin repository... > Search for "Nocalhost" > Install plugin"

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Note: Nocalhost has requirements for the idea version, and the minimum required version of his idea is 2020.3

For other ways to install plugins, check the official website of Nocalhost

https://nocalhost.dev/zh-CN/docs/installation

2. Configure the K8S cluster

Click on the menu bar on the right side of the idea and select the nocalhost tab

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Click on it and see the following interface

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Click the Connect to Cluster button to enter the following interface

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

You can select the file path where kubeconfig is saved by downloading the kubeconfig file to the local and then loading kubeConfig. Or you can copy it directly

kubeconfig content to the text area

If the connection is successful, you will look like this

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost
3. Create an instance application

Taking a Java project as an example, this example uses MVC as a demonstration to write a controller

@RestController

@RequestMapping(value = "develop")

@Api(tags = "开发专用")

@Slf4j

public  class DevelopController {

  

@GetMapping("local")

@ApiOperation(value = "本地调试")

public String testLocalDev(){

return  "localDev";

}

}

  

           
4. Cut the created Java project into a jar (optional)

By execution

mvn clean package -DskipTests

           

After packaging, the corresponding jar will be generated in the target directory, which is shaped as follows

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Note: Whether packaging is required depends on the command parameter configured in config.yaml, if the following example is configured, it can be typed as a jar

# Deployment Name

name: pig-auth

serviceType: deployment

containers:

# Deployment 主容器名称

- name: auth

dev:

# 开发镜像,该镜像包含了 Java Maven 环境

image: registry.cn-hangzhou.aliyuncs.com/zqqq/maven:3.8.6-openjdk-8

# 默认终端为 bash

shell: bash

# Rainbond 提供的 StorageClass Name

storageClass: rainbondvolumerwx

# 配置开发容器资源

resources:

limits:

memory: 4096Mi

cpu: "2"

requests:

memory: 2048Mi

cpu: "1"

persistentVolumeDirs:

# Maven 依赖包缓存路径,配合 storageClass 一起食用

- path: /root/.m2/repository

capacity: 10Gi

command:

# 一键启动命令,安装依赖包和启动 pig-auth 子模块

run:

- mvn

- install

- '&&'

- mvn

- spring-boot:run

- -pl

# 指定子模块启动

- pig-auth

# 一键 Debug 命令,安装依赖包和 Debug pig-auth 子模块

debug:

- mvn

- install

- '&&'

- mvn

- spring-boot:run

- -pl

# 指定子模块启动

- pig-auth

# Java Debug 命令

- -Dspring-boot.run.jvmArguments=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005

debug:

# 远程端口,对应Debug命令中的 address=5005

remoteDebugPort: 5005

# 选择 Java 语言

language: java

# 热加载

hotReload: true

# 文件同步

sync:

type: send

mode: gitIgnore

deleteProtection: true

# 端口转发,转发容器内的3000端口到本地3999

portForward:

- 3999:3000

           

The configuration comes from a sample configuration https://cloud.tencent.com/developer/article/2043129 the blog

5. Publish the application to K8S

It can be operated through the interface, or through kubectl apply -f or helm

Attached: Example deployment.yml file

ApiVersion: apps/v1

kind: Deployment

metadata:

labels:

workload.user.cattle.io/workloadselector: nocalhost-demo

name: nocalhost-demo

namespace: test

spec:

progressDeadlineSeconds: 600

replicas: 1

revisionHistoryLimit: 10

selector:

matchLabels:

workload.user.cattle.io/workloadselector: nocalhost-demo

template:

labels:

workload.user.cattle.io/workloadselector: nocalhost-demo

spec:

affinity: {}

containers:

- env:

- name: SERVER_PORT

value: "80"

image: harbor.images.cn/devops/adoptopenjdk/openjdk8

imagePullPolicy: IfNotPresent

name: nocalhost-demo

ports:

- containerPort: 80

name: 80tcp

protocol: TCP

readinessProbe:

failureThreshold: 5

httpGet:

path: /actuator/health/ping

port: 80

scheme: HTTP

initialDelaySeconds: 10

periodSeconds: 10

successThreshold: 1

timeoutSeconds: 1

resources: {}

terminationMessagePath: /dev/termination-log

terminationMessagePolicy: File

dnsPolicy: ClusterFirst

restartPolicy: Always

schedulerName: default-scheduler

securityContext: {}

terminationGracePeriodSeconds: 30

           

Once published, it can be viewed through the nocalhost panel

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost
6. Use nocalhost for service debugging

a. Select the workload you want to debug

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

b. Right-click the workload and select Dev Config to edit your debug configuration

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

When selected, you will be prompted

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Select Yes to jump to the following page

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

In fact, just jump to this link https://nocalhost.dev/zh-CN/tools, fill in the relevant information, and select the application

Note: However, we use the following more often

Create a .nocalhost folder in the project root directory, and copy the following contents to the config.yaml file

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

example

name: "nocalhost-demo"

serviceType: "deployment"

containers:

-

name: "nocalhost-demo"

dev:

gitUrl: ""

image: "nocalhost-docker.pkg.coding.net/nocalhost/dev-images/java:11"

shell: "bash"

workDir: ""

storageClass: ""

resources:

limits:

memory: "2048Mi"

cpu: "2"

requests:

memory: "512Mi"

cpu: "0.5"

persistentVolumeDirs: []

command:

run:

- "exec"

- "java"

- "-Dlog4j2.formatMsgNoLookups=true"

- "-Djava.security.egd=file:/dev/./urandom"

- "-jar"

- "/home/nocalhost-dev/target/nocalhost-demo.jar"

debug:

- "java"

- "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"

- "-jar"

- "/home/nocalhost-dev/target/nocalhost-demo.jar"

debug:

language: "java"

remoteDebugPort: 5005

hotReload: true

sync:

type: "send"

mode: "pattern"

filePattern:

- "."

ignoreFilePattern:

- ".git"

deleteProtection: true

env:

- name: JAVA_TOOL_OPTIONS

value: -Xms1024M -Xmx1024M

portForward:

# 8082本地端口,80为容器端口

- "8082:80"

sidecarImage: ""

           

Note: These contents can be directly configured through the visual interface provided by NoCalHost https://nocalhost.dev/zh-CN/tools operation configuration, and then copyed

Right-click the workload again and select Start DevMode or Start DevMode (Duplicate)

Nocalhost currently supports two development modes, Repliace DevMode and Duplicate DevMode. where Repliace is the default mode

The difference between the two is:

Repliace DevMode uses direct image replacement to maintain the original call relationship between services, so that the traffic of other services in the cluster hits the service under development. That is, the developed image will directly replace the original image. The following issues can occur with this mode

  1. It is easy to damage the original environment. Problems with the entire environment may occur due to problems with services in development.
  2. It is easy to affect the normal use of the environment by other team members. Other team members may only want to access a normal environment, not an environment under development
  3. Multiple team members cannot develop the same service for the same environment at the same time

Duplicate DevMode does not make any modifications to the original workload, but instead creates a copy of the original workload and develops on that copy. Duplicate DevMode can be used simultaneously on different devices to create copies of workloads for development. The replica will be marked with the ID of the device by Nocalhost to identify which device the copy is developing (the device ID is automatically generated by Nocalhost, transparent to the user. and ensure that the device ID on different devices will not be duplicated). Replicas do not affect each other. This mode may cause problems with the following

Duplicate DevMode allows multiple devices to enter development mode at the same time, if the replica receives online traffic like the original workload, we cannot know which device is being developed on which device is being used by the currently accessed environment, which will lead to unpredictable results of accessing the environment.

More information about these two modes can be found in the following link https://nocalhost.dev/zh-CN/docs/guides/develop-service-dup-en

We started via Duplicate DevMode, observing the console

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Through the console, it can be seen that it has been successfully started

Configure port forwarding (optional)

You can configure the following content through config.yaml (if you have already configured a forwarding port in the configuration file, this step can be skipped)

portForward:

# 8082本地端口,80为容器端口

- "8082:80"

           

You can also right-click the workload and select Port Forward

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

8082 is the local port and 80 is the proxy port

f: Right-click the workload and select Remote run or Remote debug

Under normal circumstances, the Idea console will print the relevant project log information, as follows

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

This log is the log inside the container, we can first put a breakpoint in the project, and then access localhost:8082/develop/local through the browser

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Skip the breakpoint and the browser will jump to the following page

In the cloud native era, the development of the efficiency improvement artifact - Nocalhost

Indicates that debugging has been successful.

For more information about debugging, please refer to the following links https://nocalhost.dev/zh-CN/docs/guides/debug/jetbrains-debug/

summary

In fact, the core of this article comes from the official website, friends who are interested in Nocalhost, you can look at the official website and practice. The information on the official website is still very complete.

Read on