Question

Clear guide on Outbound network through Floating IP

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.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

njw
DigitalOcean Employee
DigitalOcean Employee badge
November 11, 2021
Accepted Answer

Hey there!

I’m sorry to hear this has been such a bad experience. The reason that it appears this documentation is lacking is that outbound traffic through a floating IP isn’t technically supported on our services. There are work-around guides and suggestions on how to do so, but it isn’t something that was intended for floating IPs. Floating IP’s point to the anchor IP address of your droplet and such do not have their own interface. The floating IP won’t ever appear as another interface as one does not exist for it. Floating IP’s are for inbound traffic to allow DNS records to be retained while having the ability to move that IP between droplets.

There really isn’t a way to setup a floating IP on its own interface or as its own IP address as it is tied to the anchor IP. Your application looks like it requires 2 separate IP’s using different interfaces to work, which unfortunately we don’t support at this time.

I’d suggest adding the idea of additional IP addresses to our ideas page. DigitalOcean Ideas

Hope it helps! Nate

Thanks for clarifying that, I was still searching how to do it, will stop and look for alternative solutions somewhere else. Thanks!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel