Deploying a demo deployment using federated deployment
We will first create a namespace
that will be deployed to all the federated clusters. All resources deployed in this specific namespace
will be distributed among all federated clusters.
First, you need to create a namespace
on the KubeFed host cluster.
apiVersion: v1
kind: Namespace
metadata:
name: federated-ns
ns.yaml
To create the namespace, execute:
> kubectl apply -f ns.yaml
namespace/federated-ns created
You can label
each cluster with a standard tag to share resources across all clusters. While deploying federated resources, you can specify this label to distribute the resources among clusters.
> kubectl label kubefedclusters -n kube-federation-system host-cluster federation-enabled=true
> kubectl label kubefedclusters -n kube-federation-system federated-cluster-1 federation-enabled=true
> kubectl label kubefedclusters -n kube-federation-system federated-cluster-2 federation-enabled=true
To share a namespace
resource (in this case, the federated-ns
namespace) with other clusters, you need to create a FederatedNamespace
object:
apiVersion: types.kubefed.io/v1beta1
kind: FederatedNamespace
metadata:
name: federated-ns
namespace: federated-ns
spec:
placement:
clusterSelector:
matchLabels:
federation-enabled: "true"
federated-ns.yaml
> kubectl apply -f federated-ns.yaml
To get the federated namespace
from each cluster, execute:
> for context in $(kubectl config get-contexts --no-headers=true -o name)
do
echo "Getting namespaces in context $context"
kubectl get ns federated-ns --context=$context
done
Getting namespaces in context federated-cluster-1
NAME STATUS AGE
federated-ns Active 23s
Getting namespaces in context federated-cluster-2
NAME STATUS AGE
federated-ns Active 23s
Getting namespaces in context host-cluster
NAME STATUS AGE
federated-ns Active 12m
Now that the namespace
has been federated, you can begin federating deployments
. To test, start with a sample app deployment and a load balancer service:
apiVersion: types.kubefed.io/v1beta1
kind: FederatedDeployment
metadata:
name: sample-app
namespace: federated-ns
spec:
placement:
clusterSelector:
matchLabels: {}
template:
spec:
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
spec:
containers:
- image: k8s.gcr.io/echoserver:1.10
name: sample-app
ports:
- containerPort: 8080
name: http
- containerPort: 8443
name: https
federated-deployment.yaml
> kubectl apply -f federated-deployment.yaml
Next, verify federated deployment resource status:
> kubectl get federateddeployment sample-app -n federated-ns -o json | jq .status.clusters
[
{
"name": "federated-cluster-2"
},
{
"name": "host-cluster"
},
{
"name": "federated-cluster-1"
}
]
The deployment is federated to all the clusters. Now, let’s take a look at the pods:
> for context in $(kubectl config get-contexts --no-headers=true -o name)
do
echo "Getting pods in context $context"
kubectl get pods -n federated-ns --context=$context
done
Getting pods in context federated-cluster-1
NAME READY STATUS RESTARTS AGE
sample-app-dc484855d-wl5br 1/1 Running 0 7m49s
Getting pods in context federated-cluster-2
NAME READY STATUS RESTARTS AGE
sample-app-dc484855d-4n6g7 1/1 Running 0 7m50s
Getting pods in context host-cluster
NAME READY STATUS RESTARTS AGE
sample-app-dc484855d-g86sk 1/1 Running 0 7m51s
To deploy a load balancer service for deployment, apply a federated service manifest:
apiVersion: types.kubefed.io/v1beta1
kind: FederatedService
metadata:
name: sample-app-svc-lb
namespace: federated-ns
spec:
placement:
clusterSelector:
matchLabels: {}
template:
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
- name: https
port: 8443
protocol: TCP
targetPort: 8443
selector:
app: sample-app
type: LoadBalancer
federated-svc.yaml
> kubectl apply -f federated-svc.yaml
federatedservice.types.kubefed.io/sample-app-svc-lb created
To verify load balancer service in all the clusters, execute this command:
> for context in $(kubectl config get-contexts --no-headers=true -o name)
do
echo "Getting service in context $context"
kubectl get svc sample-app-svc-lb -n federated-ns --context=$context
done
Getting service in context federated-cluster-1
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sample-app-svc-lb LoadBalancer 10.100.154.92 a8d553a33d1f44da1bdea1feb066fe90-942868406.eu-west-1.elb.amazonaws.com 8080:32027/TCP,8443:31651/TCP 9s
Getting service in context federated-cluster-2
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sample-app-svc-lb LoadBalancer 10.100.174.82 aea26b82f526e49b6b70697733f4b613-1748865926.eu-central-1.elb.amazonaws.com 8080:31325/TCP,8443:31678/TCP 11s
Getting service in context host-cluster
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sample-app-svc-lb LoadBalancer 10.100.115.143 a78df2cd114fd470dac51f322c2edd5c-1497053958.us-east-2.elb.amazonaws.com 8080:30211/TCP,8443:30215/TCP 11s
You have now successfully federated a namespace, deployment
, and service
across three Kubernetes clusters.
Cleanup
To delete all the resources created above, execute:
> kubectl delete -f federated-svc.yaml
> kubectl delete -f federated-deployment.yaml
> kubectl delete -f federated-ns.yaml
> kubectl delete -f ns.yaml
> eksctl delete cluster -f federated-cluster-2.yaml
> eksctl delete cluster -f federated-cluster-1.yaml
> eksctl delete cluster -f host-cluster.yaml