Today I learned: How to make very small containers for golang binaries

The post Today I learned: How to make very small containers for golang binaries appeared first on Mirantis | Pure Play Open Cloud.
TL;DR – Official Go Docker container images tend to be beefy. The standard image on Docker Hub is called golang (docker pull golang), and tossing in a Go program (such as for interactive execution) will bring it up above 800MB. But building images from scratch (for example, using the scratch container) using compiled binaries that don’t need complex, multi-layered OS and language environment support, will keep things slimmer.
I’ve been building microservices applications for demos lately. A lot of the work has been in Node.js, because it’s easy. But this past weekend, I started learning Go (because ‘all the cool kids,’ obviously) and so, there I was, figuring out how to containerize Go programs.
It turns out this is easy, too. But I was surprised to discover how large the resulting container images were. Suppose we have a minimal program, hello.go, like:
package main

import (
“fmt”
“os”
)

func main() {
fmt.Println(os.Args)
}
… which prints the array containing its arguments. Running this on the command line with:
$ go run hello.go hi there
[/tmp/go-build289681080/b001/exe/hello hi there]
… gets you the standard argument array, beginning with the executable path.
Now you can put this into a golang container using the following Dockerfile…
FROM golang

COPY . .

CMD [“go”,”run”,”hello.go”,”hi”,”there”]
… then build it …
$ docker build -f Dockerfile.golang –tag hello:1.1 .
Sending build context to Docker daemon 2.073MB
Step 1/3 : FROM golang
—> 5fbd6463d24b
Step 2/3 : COPY . .
—> f72803dfaac0
Step 3/3 : CMD [“go”,”run”,”hello.go”,”hi”,”there”]
—> Running in 39e7765bec67
Removing intermediate container 39e7765bec67
—> f96c3d2c0861
Successfully built f96c3d2c0861
Successfully tagged hello:1.1
… and run it, and get the expected result:
$ docker run hello:1.1
[/tmp/go-build847833630/b001/exe/hello hi there]
But when you check with docker images, you see that the container is relatively yuge.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 1.1 f96c3d2c0861 3 minutes ago 812MB
‘Kayso, you can make a much, much smaller container by changing your Dockerfile to this:
FROM scratch

COPY ./hello /go/bin/hello

CMD [“/go/bin/hello”,”hi”,”there”]
… and using the so-called ‘scratch’ image, which is basically an empty container without a shell.
But the scratch image also contains no Go language environment, so you need to compile your application into an executable first:
$ go build hello.go
… which gets you a hello binary in your local directory that you can execute like this …
$ ./hello hi there
[./hello hi there]
… and get back the (drum roll) expected result.
Looking back at the Dockerfile (called, in this case Dockerfile.scratch), you can see all it’s doing is copying the binary (hello) into a directory it creates in the container (/go/bin) and then running it from there. So build …
$ docker build -f Dockerfile.scratch –tag hello:1.2 .
Sending build context to Docker daemon 2.073MB
Step 1/3 : FROM scratch
—>
Step 2/3 : COPY ./hello /go/bin/hello
—> Using cache
—> fbc88299067f
Step 3/3 : CMD [“/go/bin/hello”,”hi”,”there”]
—> Running in dd925ee8a3ab
Removing intermediate container dd925ee8a3ab
—> 4a049a401c79
Successfully built 4a049a401c79
Successfully tagged hello:1.2

… and run …
$ docker run hello:1.2
[/go/bin/hello hi there]
… and (by this time, I figure you’re not surprised) … the expected result. But look at the container image:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 1.2 4a049a401c79 33 seconds ago 2.07MB
That’s a pretty substantial size reduction!
Flipping from shell-based execution to no-shell-in-sight execution naturally comes with many potential gotchas. In this case, for example, if you were to build the arguments passed by your CMD out of expressions that required evaluation/expansion by a shell, things wouldn’t work as planned in a no-shell container. You’ll need to construct code carefully to run in such a stripped-back environment.
Now, be advised: I haven’t thought about side-effects, security, stability, gotchas that would no doubt be obvious to a seasoned Go dev, and I’d love to hear about them in the comments. But as we say in the artisanal code-creation atelier: “It works on my machine.” 
Today I Learned (TIL) is an intermittent journal of somewhat half-baked solutions to “speed bump” problems encountered by Mirantis Technical Marketing folks working well beyond our native spheres of expertise for research purposes. None of what we write about here has been checked by grown-ups, or is appropriate for production without further validation. Use at own risk. Comments, cautions, and suggestions for improvement are very welcome! Email jjainschigg@mirantis.com (Twitter: @jjainschigg).
The post Today I learned: How to make very small containers for golang binaries appeared first on Mirantis | Pure Play Open Cloud.
Quelle: Mirantis

Optimize your Azure costs to help meet your financial objectives

Many of our customers are facing difficult decisions about how to meet their funding needs for critical IT projects. We’re in this together to help you meet your financial objectives. Ensuring your Azure workloads are cost optimized can help free up funds to support essential surge areas like remote work.

For the fourth year running, cost optimization is the top cloud initiative according to Flexera’s 2020 State of the Cloud Report

Today, we’ll cover the Azure tools, offers, and guidance that can help you manage and optimize your cloud costs. You’ll learn how to understand and forecast your bill, cost optimize your workload, and control your spending. Then we’ll show you seven things you can do today to optimize your cloud costs and start saving.

Understand and forecast your costs

To manage and optimize your Azure costs, you first need to understand what you’re spending now and forecast what your bill is likely to be in the future for your current and planned projects.

Azure Cost Management + Billing gives you a full set of cloud cost management capabilities. You can use Cost Management + Billing to:

Monitor and analyze your Azure bill.
Set budgets and spending alerts.
Allocate costs to your teams and projects.

As you embark on new priority workloads or revisit existing workloads for cost optimization opportunities, you’ll want to estimate the costs involved. Cost Management + Billing surfaces rich operational and financial insights, including cost forecasts. In addition, the Azure pricing calculator and Total Cost of Ownership (TCO) calculator can help you estimate costs for your next Azure projects.

 

Cost optimize your workloads

Once you have a better understanding of your spending now and in the future, you can focus on cost optimizing your Azure resources and workloads. In this area, Azure Advisor and the Microsoft Azure Well-Architected Framework are our primary optimization tools, while key Azure offers and licensing terms, like the Azure Hybrid Benefit and Azure Reservations can help you realize significant savings.

Azure Advisor helps you cost optimize your Azure resources with personalized best practice recommendations based on your configurations and usage data. For example, Azure Advisor can help you identify unused resources like idle virtual machines (VMs) and find opportunities to right-size resources like SQL databases.

The Azure Well-Architected Framework provides guidance and best practices at a broader architectural level to help you cost optimize your workloads. You can also take the Azure Well-Architected Review to assess your current or planned workloads from a cost perspective to get tailored recommendations on how to improve.

In addition, Azure offers and licensing terms can make a big difference when it comes to cost savings including:

Azure Hybrid Benefit: AWS is five times more expensive than Azure for Windows Server and SQL Server. Save when you migrate your on-premises workloads to Azure.
Azure Reservations: Get a discount of up to 72 percent over pay-as-you-go pricing on Azure services when you prepay for a one- or three-year term with reservation pricing.
Azure Spot Virtual Machines: Utilize unused Azure compute capacity at deep discounts—up to 90 percent compared to pay-as-you-go prices.
Azure Dev/Test Pricing: Take advantage of significant rate discounts for your ongoing development and testing—and avoid paying Microsoft software charges on your VMs.

Control your costs

Finally, you’ll want to put in place organization-wide cost management policies and guardrails, so you can free up your teams to go fast while keeping your costs under control.

For many customers, governing cost is a major concern when it comes to the cloud. Balancing workload requirements like performance demands and reliability needs with cloud costs can be challenging, especially during times of business transformation like we see today. The Microsoft Cloud Adoption Framework for Azure provides organizational cost management guidance as part of a cloud governance strategy. This guidance can help mitigate cloud spending risks through cost management best practices.

Once you’ve set your organizational policies for cost management, you can implement these cost controls and guardrails directly in your Azure environment with Azure Policy to achieve real-time cloud compliance at scale.

7 ways to optimize your Azure costs today

With many tools to use, offers to take advantage of, and best practices to follow, you may wonder how to get started. To help you get going fast, we recommend starting with these seven ways you can optimize your Azure costs today.

Shut down unused resources: Identify idle virtual machines, ExpressRoute circuits, and other resources with Azure Advisor. Get recommendations on which resources to shut down and see how much you would save.
Right-size underused resources: Find underutilized resources with Azure Advisor and get recommendations on how to reduce spending by reconfiguring or consolidating them.
Reserve instances for consistent workloads: Save money when you reserve resources in advance. Plus, enjoy monthly payment options at no extra cost.
Take advantage of the Azure Hybrid Benefit: Realize major savings when you bring your Windows Server and SQL Server on-premises licenses with Software Assurance to Azure.
Get Azure Dev/Test pricing for development environments: Get rate discounts on Azure to support development and testing scenarios.
Set up budgets and allocate costs to teams and projects: Create and manage budgets for the Azure services you use or subscribe to—and monitor your organization’s cloud spending—with Azure Cost Management.
Explore serverless technologies and new architectures: Optimize your workloads with guidance for building apps and solutions on Azure using best practices and proven patterns.

Get started with cost optimization

Cloud cost optimization has never been more critical than it is today. Azure is here to support you with tools, offers, and best practices to help you optimize your cloud costs and meet your technical and business goals during this difficult time.

We’re also kicking off a new blog series to help you optimize your Azure environment. Check the Azure blog soon for the latest cost optimization guidance and best practices or visit our cost optimization on Azure page to learn more.
Quelle: Azure