mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #87105 from BenTheElder/agnhost-replica
s/slave/replica in guestbook app
This commit is contained in:
commit
4ea576e4f5
@ -194,13 +194,13 @@ Usage:
|
|||||||
Starts a HTTP server on the given `--http-port` (default: 80), serving various endpoints representing a
|
Starts a HTTP server on the given `--http-port` (default: 80), serving various endpoints representing a
|
||||||
guestbook app. The endpoints and their purpose are:
|
guestbook app. The endpoints and their purpose are:
|
||||||
|
|
||||||
- `/register`: A guestbook slave will subscribe to a master, to its given `--slaveof` endpoint. The master
|
- `/register`: A guestbook replica will subscribe to a master, to its given `--replicaof` endpoint. The master
|
||||||
will then push any updates it receives to its registered slaves through the `--backend-port` (default: 6379).
|
will then push any updates it receives to its registered replicas through the `--backend-port` (default: 6379).
|
||||||
- `/get`: Returns `{"data": value}`, where the `value` is the stored value for the given `key` if non-empty,
|
- `/get`: Returns `{"data": value}`, where the `value` is the stored value for the given `key` if non-empty,
|
||||||
or the entire store.
|
or the entire store.
|
||||||
- `/set`: Will set the given key-value pair in its own store and propagate it to its slaves, if any.
|
- `/set`: Will set the given key-value pair in its own store and propagate it to its replicas, if any.
|
||||||
Will return `{"data": "Updated"}` to the caller on success.
|
Will return `{"data": "Updated"}` to the caller on success.
|
||||||
- `/guestbook`: Will proxy the request to `agnhost-master` if the given `cmd` is `set`, or `agnhost-slave`
|
- `/guestbook`: Will proxy the request to `agnhost-master` if the given `cmd` is `set`, or `agnhost-replica`
|
||||||
if the given `cmd` is `get`.
|
if the given `cmd` is `get`.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
@ -1 +1 @@
|
|||||||
2.17
|
2.18
|
||||||
|
@ -38,10 +38,10 @@ var CmdGuestbook = &cobra.Command{
|
|||||||
Short: "Creates a HTTP server with various endpoints representing a guestbook app",
|
Short: "Creates a HTTP server with various endpoints representing a guestbook app",
|
||||||
Long: `Starts a HTTP server on the given --http-port (default: 80), serving various endpoints representing a guestbook app. The endpoints and their purpose are:
|
Long: `Starts a HTTP server on the given --http-port (default: 80), serving various endpoints representing a guestbook app. The endpoints and their purpose are:
|
||||||
|
|
||||||
- /register: A guestbook slave will subscribe to a master, to its given --slaveof endpoint. The master will then push any updates it receives to its registered slaves through the --backend-port.
|
- /register: A guestbook replica will subscribe to a master, to its given --replicaof endpoint. The master will then push any updates it receives to its registered replicas through the --backend-port.
|
||||||
- /get: Returns '{"data": value}', where the value is the stored value for the given key if non-empty, or the entire store.
|
- /get: Returns '{"data": value}', where the value is the stored value for the given key if non-empty, or the entire store.
|
||||||
- /set: Will set the given key-value pair in its own store and propagate it to its slaves, if any. Will return '{"data": "Updated"}' to the caller on success.
|
- /set: Will set the given key-value pair in its own store and propagate it to its replicas, if any. Will return '{"data": "Updated"}' to the caller on success.
|
||||||
- /guestbook: Will proxy the request to agnhost-master if the given cmd is 'set', or agnhost-slave if the given cmd is 'get'.`,
|
- /guestbook: Will proxy the request to agnhost-master if the given cmd is 'set', or agnhost-replica if the given cmd is 'get'.`,
|
||||||
Args: cobra.MaximumNArgs(0),
|
Args: cobra.MaximumNArgs(0),
|
||||||
Run: main,
|
Run: main,
|
||||||
}
|
}
|
||||||
@ -49,8 +49,8 @@ var CmdGuestbook = &cobra.Command{
|
|||||||
var (
|
var (
|
||||||
httpPort string
|
httpPort string
|
||||||
backendPort string
|
backendPort string
|
||||||
slaveOf string
|
replicaOf string
|
||||||
slaves []string
|
replicas []string
|
||||||
store map[string]interface{}
|
store map[string]interface{}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -62,12 +62,12 @@ const (
|
|||||||
func init() {
|
func init() {
|
||||||
CmdGuestbook.Flags().StringVar(&httpPort, "http-port", "80", "HTTP Listen Port")
|
CmdGuestbook.Flags().StringVar(&httpPort, "http-port", "80", "HTTP Listen Port")
|
||||||
CmdGuestbook.Flags().StringVar(&backendPort, "backend-port", "6379", "Backend's HTTP Listen Port")
|
CmdGuestbook.Flags().StringVar(&backendPort, "backend-port", "6379", "Backend's HTTP Listen Port")
|
||||||
CmdGuestbook.Flags().StringVar(&slaveOf, "slaveof", "", "The host's name to register to")
|
CmdGuestbook.Flags().StringVar(&replicaOf, "replicaof", "", "The host's name to register to")
|
||||||
store = make(map[string]interface{})
|
store = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func main(cmd *cobra.Command, args []string) {
|
func main(cmd *cobra.Command, args []string) {
|
||||||
go registerNode(slaveOf, backendPort)
|
go registerNode(replicaOf, backendPort)
|
||||||
startHTTPServer(httpPort)
|
startHTTPServer(httpPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ func registerNode(registerTo, port string) {
|
|||||||
log.Printf("Registering to master: %s/%s", hostPort, request)
|
log.Printf("Registering to master: %s/%s", hostPort, request)
|
||||||
_, err = net.ResolveTCPAddr("tcp", hostPort)
|
_, err = net.ResolveTCPAddr("tcp", hostPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("unable to resolve %s, --slaveof param and/or --backend-port param are invalid: %v. Retrying in %s.", hostPort, err, sleep)
|
log.Printf("unable to resolve %s, --replicaof param and/or --backend-port param are invalid: %v. Retrying in %s.", hostPort, err, sleep)
|
||||||
time.Sleep(sleep)
|
time.Sleep(sleep)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -129,8 +129,8 @@ func startHTTPServer(port string) {
|
|||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
// registerHandler will register the caller in this server's list of slaves.
|
// registerHandler will register the caller in this server's list of replicas.
|
||||||
// /set requests will be propagated to slaves, if any.
|
// /set requests will be propagated to replicas, if any.
|
||||||
func registerHandler(w http.ResponseWriter, r *http.Request) {
|
func registerHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
values, err := url.Parse(r.URL.RequestURI())
|
values, err := url.Parse(r.URL.RequestURI())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -141,7 +141,7 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
ip := values.Query().Get("host")
|
ip := values.Query().Get("host")
|
||||||
log.Printf("GET /register?host=%s", ip)
|
log.Printf("GET /register?host=%s", ip)
|
||||||
|
|
||||||
// send all the store to the slave as well.
|
// send all the store to the replica as well.
|
||||||
output := make(map[string]interface{})
|
output := make(map[string]interface{})
|
||||||
output["data"] = store
|
output["data"] = store
|
||||||
bytes, err := json.Marshal(output)
|
bytes, err := json.Marshal(output)
|
||||||
@ -150,7 +150,7 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, string(bytes))
|
fmt.Fprint(w, string(bytes))
|
||||||
slaves = append(slaves, ip)
|
replicas = append(replicas, ip)
|
||||||
log.Printf("Node '%s' registered.", ip)
|
log.Printf("Node '%s' registered.", ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ func getHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setHandler will set the given key-value pair in its own store and propagate
|
// setHandler will set the given key-value pair in its own store and propagate
|
||||||
// it to its slaves, if any. Will return '{"message": "Updated"}' to the caller on success.
|
// it to its replicas, if any. Will return '{"message": "Updated"}' to the caller on success.
|
||||||
func setHandler(w http.ResponseWriter, r *http.Request) {
|
func setHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
values, err := url.Parse(r.URL.RequestURI())
|
values, err := url.Parse(r.URL.RequestURI())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -207,11 +207,11 @@ func setHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
store[key] = value
|
store[key] = value
|
||||||
request := fmt.Sprintf("set?key=%s&value=%s", key, value)
|
request := fmt.Sprintf("set?key=%s&value=%s", key, value)
|
||||||
for _, slave := range slaves {
|
for _, replica := range replicas {
|
||||||
hostPort := net.JoinHostPort(slave, backendPort)
|
hostPort := net.JoinHostPort(replica, backendPort)
|
||||||
_, err = dialHTTP(request, hostPort)
|
_, err = dialHTTP(request, hostPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("encountered error while propagating to slave '%s': %v", slave, err), http.StatusExpectationFailed)
|
http.Error(w, fmt.Sprintf("encountered error while propagating to replica '%s': %v", replica, err), http.StatusExpectationFailed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -227,7 +227,7 @@ func setHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// guestbookHandler will proxy the request to agnhost-master if the given cmd is
|
// guestbookHandler will proxy the request to agnhost-master if the given cmd is
|
||||||
// 'set' or agnhost-slave if the given cmd is 'get'.
|
// 'set' or agnhost-replica if the given cmd is 'get'.
|
||||||
func guestbookHandler(w http.ResponseWriter, r *http.Request) {
|
func guestbookHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
values, err := url.Parse(r.URL.RequestURI())
|
values, err := url.Parse(r.URL.RequestURI())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -252,7 +252,7 @@ func guestbookHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
host := "agnhost-master"
|
host := "agnhost-master"
|
||||||
if cmd == "get" {
|
if cmd == "get" {
|
||||||
host = "agnhost-slave"
|
host = "agnhost-replica"
|
||||||
}
|
}
|
||||||
|
|
||||||
hostPort := net.JoinHostPort(host, backendPort)
|
hostPort := net.JoinHostPort(host, backendPort)
|
||||||
|
Loading…
Reference in New Issue
Block a user