Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Hi, I spent half a day trying to figure out how to use a floating IP for outbound traffic, I began having nightmares with such a bad time I am having, there is no single guide with clear documentation on how to use this. All these trickeries with “route” or “ip route”, first don’t seem correct to me, If I have 2 IPs I have to have the ability to send traffic from either IP, why should I set the default for all traffic to the floating IP. Second, the official guide does not explain how to revert the changes if we use a floating IP. Third, the official guide does not work for me …
This is more or less the experiment I am running, I can’t make it run not even a single cycle … It crashes because the IPs are the same, just after the application crashes I do curl checkip.amazonaws.com and it reflects the new IP … It is very obscure and the documentation is extremely poor, it does not explain a single thing that can help.
func GetHttp(urlStr string) ([]byte, error) {
res, err := http.Get(urlStr)
if err != nil {
return nil, err
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return content, err
}
func ExecuteSystemCommand(command string, pipeErr bool) error {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/k", command)
} else if runtime.GOOS == "linux" {
cmd = exec.Command("bash", "-c", command)
} else {
panic("not supported platform")
}
cmd.Stdout = os.Stdout
if pipeErr {
cmd.Stderr = os.Stderr
}
err := cmd.Run()
return err
}
func GetMyIp() (string, error) {
res, err := GetHttp("https://checkip.amazonaws.com")
if err == nil && len(res) > 0 {
return strings.TrimSpace(string(res)), nil
}
return "", err
}
func experiment() {
currentIp, err := GetMyIp()
if err != nil {
panic(err)
}
godoClient := godo.NewFromToken("<MY_TOKEN>")
response, err := http.Get("http://169.254.169.254/metadata/v1/id")
if err != nil {
panic(err)
}
respBody, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
response.Body.Close()
dropletId, err := strconv.Atoi(string(respBody))
if err != nil {
panic(err)
}
response, err = http.Get("http://169.254.169.254/metadata/v1/region")
if err != nil {
panic(err)
}
respBody, err = ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
response.Body.Close()
region := string(respBody)
message := fmt.Sprintf("Initial IP: %s, Droplet ID: %d, Region: %s\n", currentIp, dropletId, region)
log.Print(message)
for {
fp, res, err := godoClient.FloatingIPs.Create(context.Background(), &godo.FloatingIPCreateRequest{
Region: region,
// DropletID: dropletId,
})
if err != nil {
panic(err)
}
if res.StatusCode != 202 {
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
}
message = fmt.Sprintf("Status code not 202 - %s %v\n", resBody, err)
NotifySlack(message)
defaultLog.Error(message)
panic(message)
}
_ = res.Body.Close()
fmt.Printf("We have IP: %s\n", fp.IP)
action, res, err := godoClient.FloatingIPActions.Assign(context.Background(), fp.IP, dropletId)
if err != nil {
panic(err)
}
fmt.Printf("Action %#v\n", action)
response, err = http.Get("http://169.254.169.254/metadata/v1/interfaces/public/0/anchor_ipv4/gateway")
if err != nil {
panic(err)
}
respBody, err = ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
_ = response.Body.Close()
anchor := string(respBody)
// command := "ip route del 0/0"
command := fmt.Sprintf("route del default gw %s", anchor)
log.Print(command + "\n")
err = ExecuteSystemCommand(command, true)
if err != nil {
fmt.Printf("Error on deleting route %v\n", err)
}
log.Print(fmt.Sprintf("Anchor: %s\n", anchor))
command = fmt.Sprintf("route add default gw %s", anchor)
// command := fmt.Sprintf("ip route add default via %s dev eth0", anchor)
err = ExecuteSystemCommand(command, true)
if err != nil {
panic(err)
}
message = fmt.Sprintf("Got Floating IP %s\n", fp.IP)
log.Print(message)
time.Sleep(time.Second * 20)
currentIp2, _ := libNet.GetMyIp()
if currentIp2 == currentIp {
message = fmt.Sprintf("Same IP %s\n", currentIp)
log.Print(message)
panic(message)
} else {
message = fmt.Sprintf("Switched from %s to %s (%s)\n", currentIp, currentIp2, anchor)
log.Print(message)
currentIp = currentIp2
}
time.Sleep(time.Second * 30)
res, err = godoClient.FloatingIPs.Delete(context.Background(), fp.IP)
if err != nil {
panic(err)
}
fmt.Printf("Done cycle\n")
}
}
Please help, I need a way to set the floating IP to a network interface, so I can bind to ip from any programming language, I don’t want to depend on route or “ip route”, I want an easy way to have the floating IP on my droplet and available from a network interface.
ebeecroft
Noor Bakkal
384d3f11289345379905a243f7b817
IsaacMvd
Ervan Agus
Teckno
dc1bce8262314f84b368813178d9e4
gerard
gerard
Stan Flint