Skip to content

Instantly share code, notes, and snippets.

@KostyaEsmukov
Last active June 5, 2019 22:28
Show Gist options
  • Save KostyaEsmukov/c16fc411a2040df4ff0b7215cf1bfd17 to your computer and use it in GitHub Desktop.
Save KostyaEsmukov/c16fc411a2040df4ff0b7215cf1bfd17 to your computer and use it in GitHub Desktop.
Consul Network Partitioning lab

Consul Network Partitioning lab

$ vagrant up

$ curl http://127.0.0.1:8501/v1/status/peers
["192.168.99.15:8300","192.168.99.12:8300","192.168.99.11:8300","192.168.99.13:8300","192.168.99.14:8300"]

$ curl http://127.0.0.1:8501/v1/status/leader
"192.168.99.12:8300"

$ curl \
    --request PUT \
    --data hi \
    http://127.0.0.1:8501/v1/kv/testkey
true

$ curl http://127.0.0.1:8505/v1/kv/testkey
[{"LockIndex":0,"Key":"testkey","Flags":0,"Value":"aGk=","CreateIndex":63,"ModifyIndex":63}]

$ curl http://127.0.0.1:8503/v1/kv/testkey
[{"LockIndex":0,"Key":"testkey","Flags":0,"Value":"aGk=","CreateIndex":63,"ModifyIndex":63}]

$ vagrant ssh node5 -- sudo iptables -I INPUT -i eth1 -j DROP
$ vagrant ssh node5 -- sudo iptables -I OUTPUT -o eth1 -j DROP

$ time curl -v http://127.0.0.1:8503/v1/kv/testkey
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8503 (#0)
> GET /v1/kv/testkey HTTP/1.1
> Host: 127.0.0.1:8503
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Vary: Accept-Encoding
< X-Consul-Index: 63
< X-Consul-Knownleader: true
< X-Consul-Lastcontact: 0
< Date: Wed, 05 Jun 2019 21:49:43 GMT
< Content-Length: 92
<
* Connection #0 to host 127.0.0.1 left intact
[{"LockIndex":0,"Key":"testkey","Flags":0,"Value":"aGk=","CreateIndex":63,"ModifyIndex":63}]
real	0m0.018s
user	0m0.006s
sys	0m0.005s

$ time curl -v http://127.0.0.1:8505/v1/kv/testkey
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8505 (#0)
> GET /v1/kv/testkey HTTP/1.1
> Host: 127.0.0.1:8505
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
< Vary: Accept-Encoding
< Date: Wed, 05 Jun 2019 21:49:51 GMT
< Content-Length: 17
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host 127.0.0.1 left intact
No cluster leader
real	0m7.237s
user	0m0.006s
sys	0m0.006s

$ time curl -v 'http://127.0.0.1:8505/v1/kv/testkey?stale'
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8505 (#0)
> GET /v1/kv/testkey?stale HTTP/1.1
> Host: 127.0.0.1:8505
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Vary: Accept-Encoding
< X-Consul-Index: 63
< X-Consul-Knownleader: false
< X-Consul-Lastcontact: 316547
< Date: Wed, 05 Jun 2019 22:28:00 GMT
< Content-Length: 92
<
* Connection #0 to host 127.0.0.1 left intact
[{"LockIndex":0,"Key":"testkey","Flags":0,"Value":"aGk=","CreateIndex":63,"ModifyIndex":63}]
real	0m0.022s
user	0m0.007s
sys	0m0.007s
Vagrant.configure("2") do |config|
config.vm.box = "debian/stretch64"
config.vm.provider "virtualbox" do |vb|
vb.memory = "512"
end
config.vm.provision "docker" do |d|
consul_config = %(
{
"server": true,
"encrypt": "gIMnlYvXyPdackSKWqEM4A==",
"retry_join": [
"192.168.99.11",
"192.168.99.12",
"192.168.99.13",
"192.168.99.14",
"192.168.99.15"
],
"addresses": {
"http": "0.0.0.0"
},
"ui": true,
"bootstrap_expect": 5
}
)
d.run "consul",
image: "consul:1.5",
cmd: "agent",
args: "-h `hostname` --net=host -e CONSUL_BIND_INTERFACE=eth1 -e 'CONSUL_LOCAL_CONFIG=#{consul_config}'"
end
config.vm.define "node1" do |m|
m.vm.hostname = "node1"
m.vm.network "private_network", ip: "192.168.99.11"
m.vm.network "forwarded_port", guest: 8500, host: 8501
end
config.vm.define "node2" do |m|
m.vm.hostname = "node2"
m.vm.network "private_network", ip: "192.168.99.12"
m.vm.network "forwarded_port", guest: 8500, host: 8502
end
config.vm.define "node3" do |m|
m.vm.hostname = "node3"
m.vm.network "private_network", ip: "192.168.99.13"
m.vm.network "forwarded_port", guest: 8500, host: 8503
end
config.vm.define "node4" do |m|
m.vm.hostname = "node4"
m.vm.network "private_network", ip: "192.168.99.14"
m.vm.network "forwarded_port", guest: 8500, host: 8504
end
config.vm.define "node5" do |m|
m.vm.hostname = "node5"
m.vm.network "private_network", ip: "192.168.99.15"
m.vm.network "forwarded_port", guest: 8500, host: 8505
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment