Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze go applications in Alpine

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Xiaobian today takes you to understand how to analyze the go application in Alpine. The knowledge points in this article are introduced in great detail. Friends who feel helpful can browse the content of the article together with Xiaobian, hoping to help more friends who want to solve this problem find the answer to the question. Let's learn more about "how to analyze the go application in Alpine" together with Xiaobian.

When we first came into contact with Docker, the most common way to get started was to build virtual machines using systems such as Ubuntu or Debian. The disadvantage of this method was that the images generated were too large. The base image of Ubuntu alone was already 187.9MB, and most of the installed software and dependency libraries would not be used.

The main difference between containers and virtual machines is that containers run in an isolated process space on the host system, so it doesn't need a separate kernel and other system files. This is not a difficult question to answer.

Microbase Mirrors

First we'll discard the idea of using Ubuntu or Debian as a base mirror and focus on lighter options.

[Alpine Linux] is a mini Linux distribution that can be used to build a base container with a volume of only 5M, which is based on an embedded system [BusyBox]. So why not just use BusyBox? There are several advantages to using Alpine. First, Alpine has a package management system apk on it, which simplifies container maintenance. Second, Alpine's kernel has been [PaX patch], which protects it from a large number of 0-day vulnerabilities.

As you can see in the list below, Alpine adds only 4.1MB to BusyBox, but adds the convenience accordingly, which I think is well worth the expense.

| mirror| volume|

| :-------- | :--: |

| ubuntu | 187.90 MB |

| nicholasjackson/microservice-basebox | 15.82 MB |

| alpine | 5.24 MB |

| busybox | 1.10 MB |

Compiling Go apps for Alpine Linux

One advantage of using Go to build microservices is that it will be compiled into binary packages, so it doesn't require frameworks or runtime dependencies, which is advantageous because Alpine is a very lightweight distribution, as mentioned earlier, and not all C dependencies are installed, so Go's dynamic library dependencies are likely not either. Fortunately, there is a special way to disable cgo dependencies and compile applications via links. We just need to tell the compiler to rebuild all our application packages like this:

```sh

$ CGO_ENABLED=0 go build -a -installsuffix cgo .

```

Let's go into more detail about the above command:

CGO_ENABLED=0 is a compilation flag that causes the build system to ignore cgo and statically link all dependencies;

-a forces recompilation, even if all packages are compiled from the latest code;

-installsuffix cgo adds a suffix to the newly compiled package directory, which separates the compiled output from the default path.

running applications

So how do you use configuration files in binary packages? If you're using microservices architecture, you're probably using [Consul], and if you're not already using it, you should really study it, it's really powerful. In this article, I will assume that you are familiar with Consul and that you are using this [Consul template] to manage your profiles.

We usually need to use a guardian process to run these services, such as [Supervisor], but this tool requires Python, which goes against our lightweight intent, so we use [Skaware S6].

S6 is a simple and effective tool that uses the configuration file under/etc/s6 to start the s6-svscan process. As you can see from my base image repository, this directory contains a series of shell scripts.

.S6-svscan

This directory contains two scripts,"crash" and "finish," which are invoked when an application managed by s6 terminates due to an error and when the s6 process closes.

Spp

This directory contains two scripts,"run" and "finish." Run is used to start the main service and will be configured with the execution path of the Go application. In finish, you can set the script to execute when the application is closed.

Consul-template

This directory also contains those two scripts, but this time it is used to configure the consul-template application. The configuration files used by consul-template to generate microservices will be stored on the Consul server. Throughout the life cycle of Go microservices, Consul applications need to run to provide configuration services, so we also need to monitor and guard it through s6.

Docker Base Image

For testing purposes, I have used Alpine to build an image with Skaware S6 and Consul Template, and this [new image] is also only 15.82MB. Even if you built a bloated app based on this image, it would probably be around 30MB, but that would still be only 13% of the size of an Ubuntu base image.

Thank you for reading, the above is "how to analyze the go application in Alpine" all the content, learn friends to quickly operate it. I believe that Xiaobian will definitely bring you better quality articles. Thank you for your support of the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report