# Bootstrapping the Kubernetes Control Plane In this lab you will bootstrap the Kubernetes control plane across three EC2 instances and configure it for high availability. You will also create an external load balancer that exposes the Kubernetes API Servers to remote clients. The following components will be installed on each node: Kubernetes API Server, Scheduler, and Controller Manager. ## Prerequisites The commands in this lab must be run on each master instance: `master-0`, `master-1`, and `master-2`. Login to each master instance using ssh. Example: ``` $ aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-xxxxxxxxxxxxxxxxx \ --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0],InstanceId,Placement.AvailabilityZone,PrivateIpAddress,PublicIpAddress,State.Name]' \ --output text | sort | grep master master-0 i-xxxxxxxxxxxxxxxxx ap-northeast-1c 10.240.0.10 xx.xxx.xxx.xxx running ... $ ssh -i ~/.ssh/your_ssh_key ubuntu@xx.xxx.xxx.xxx ``` ### Running commands in parallel with tmux [tmux](https://github.com/tmux/tmux/wiki) can be used to run commands on multiple EC2 instances at the same time. See the [Running commands in parallel with tmux](01-prerequisites.md#running-commands-in-parallel-with-tmux) section in the Prerequisites lab. ## Provision the Kubernetes Control Plane Create the Kubernetes configuration directory: ``` master-x $ sudo mkdir -p /etc/kubernetes/config ``` ### Download and Install the Kubernetes Controller Binaries Download the official Kubernetes release binaries - `kube-apiserver`, `kube-controller-manager`, `kube-scheduler`, and `kubectl`: ``` master-x $ wget -q --show-progress --https-only --timestamping \ "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kube-apiserver" \ "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kube-controller-manager" \ "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kube-scheduler" \ "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kubectl" ``` Install the Kubernetes binaries: ``` master-x $ chmod +x kube-apiserver kube-controller-manager kube-scheduler kubectl master-x $ sudo mv kube-apiserver kube-controller-manager kube-scheduler kubectl /usr/local/bin/ ``` ### Configure the Kubernetes API Server ``` master-x $ sudo mkdir -p /var/lib/kubernetes/ master-x $ sudo mv ca.pem ca-key.pem kubernetes-key.pem kubernetes.pem \ service-account-key.pem service-account.pem \ encryption-config.yaml /var/lib/kubernetes/ ``` The instance internal IP address will be used to advertise the API Server to members of the cluster. Retrieve the internal IP address for the current EC2 instance. ``` master-x $ INTERNAL_IP=$(curl 169.254.169.254/latest/meta-data/local-ipv4) ``` Create the `kube-apiserver.service` systemd unit file: ``` master-x $ cat < Allow up to 10 seconds for the Kubernetes API Server to fully initialize. Verify controller services are running. ``` master-x $ for svc in kube-apiserver kube-controller-manager kube-scheduler; \ do sudo systemctl status --no-pager $svc | grep -B 3 Active; \ done ● kube-apiserver.service - Kubernetes API Server Loaded: loaded (/etc/systemd/system/kube-apiserver.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-01-21 11:05:50 UTC; 3h 39min ago ● kube-controller-manager.service - Kubernetes Controller Manager Loaded: loaded (/etc/systemd/system/kube-controller-manager.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-01-21 11:05:50 UTC; 3h 39min ago ● kube-scheduler.service - Kubernetes Scheduler Loaded: loaded (/etc/systemd/system/kube-scheduler.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-01-21 11:05:50 UTC; 3h 39min ago ``` ### Enable HTTP Health Checks AWS [Network Load Balancer (NLB)](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html) will be used to distribute traffic across the three API servers and allow each API server to terminate TLS connections and validate client certificates. We use HTTP NLB health checks instead of HTTPS endpoint exposed by the API server. For health check purpose, the nginx webserver can be used to proxy HTTP health checks. In this section nginx will be installed and configured to accept HTTP health checks on port `80` and proxy the connections to the API server on `https://127.0.0.1:6443/healthz`. > The `/healthz` API server endpoint does not require authentication by default. Install a basic web server to handle HTTP health checks: ``` master-x $ sudo apt-get update master-x $ sudo apt-get install -y nginx ``` Configure nginx config file to proxy HTTP health check. ``` master-x $ cat > kubernetes.default.svc.cluster.local < Remember to run the above commands on each master node: `master-0`, `master-1`, and `master-2`. ## RBAC for Kubelet Authorization In this section you will configure RBAC permissions to allow the Kubernetes API Server to access the Kubelet API on each worker node (`master --> worker`). Access to the Kubelet API is required for retrieving metrics, logs, and executing commands in pods. > This tutorial sets the Kubelet `--authorization-mode` flag to `Webhook`. Webhook mode uses the [SubjectAccessReview](https://kubernetes.io/docs/admin/authorization/#checking-api-access) API to determine authorization. The commands in this section will effect the entire cluster and only need to be run once from **one of the** master nodes. ``` $ aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-xxxxxxxxxxxxxxxxx \ --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0],InstanceId,Placement.AvailabilityZone,PrivateIpAddress,PublicIpAddress,State.Name]' \ --output text | sort | grep master master-0 i-xxxxxxxxxxxxxxxxx ap-northeast-1c 10.240.0.10 xx.xxx.xxx.xxx running ... $ ssh -i ~/.ssh/your_ssh_key ubuntu@xx.xxx.xxx.xxx ``` Create the `system:kube-apiserver-to-kubelet` [ClusterRole](https://kubernetes.io/docs/admin/authorization/rbac/#role-and-clusterrole) with permissions to access the Kubelet API and perform most common tasks associated with managing pods. > NOTE: you should turn off tmux multiple sync sessions when executing following command on one master node as `ClusterRole` is a cluster-wide resource. ``` master-0 $ hostname master-0 master-0 $ cat <