kubernetes-the-hard-way/docs/08-bootstrapping-kubernetes...

379 lines
12 KiB
Markdown
Raw Normal View History

2017-08-29 00:19:25 +03:00
# Bootstrapping the Kubernetes Control Plane
In this lab you will bootstrap the Kubernetes control plane across three compute 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
2021-02-05 09:40:21 +03:00
The commands in this lab must be run on each controller instance: `controller-0`, `controller-1`, and `controller-2`. Login to each controller instance using our `oci-ssh` shell function. Example:
2017-08-29 00:19:25 +03:00
```
2021-02-05 09:40:21 +03:00
oci-ssh controller-0
2017-08-29 00:19:25 +03:00
```
### Running commands in parallel with tmux
[tmux](https://github.com/tmux/tmux/wiki) can be used to run commands on multiple compute 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.
2021-02-05 09:40:21 +03:00
**Note**: please import the shell functions defined [here](02-client-tools.md#shell-functions) in each tmux window/pane, as we will make use of them.
2017-08-29 00:19:25 +03:00
## Provision the Kubernetes Control Plane
Create the Kubernetes configuration directory:
```
sudo mkdir -p /etc/kubernetes/config
```
2017-08-29 00:19:25 +03:00
### Download and Install the Kubernetes Controller Binaries
Download the official Kubernetes release binaries:
```
wget -q --show-progress --https-only --timestamping \
2020-07-18 10:24:55 +03:00
"https://storage.googleapis.com/kubernetes-release/release/v1.18.6/bin/linux/amd64/kube-apiserver" \
"https://storage.googleapis.com/kubernetes-release/release/v1.18.6/bin/linux/amd64/kube-controller-manager" \
"https://storage.googleapis.com/kubernetes-release/release/v1.18.6/bin/linux/amd64/kube-scheduler" \
"https://storage.googleapis.com/kubernetes-release/release/v1.18.6/bin/linux/amd64/kubectl"
2017-08-29 00:19:25 +03:00
```
Install the Kubernetes binaries:
```
{
chmod +x kube-apiserver kube-controller-manager kube-scheduler kubectl
sudo mv kube-apiserver kube-controller-manager kube-scheduler kubectl /usr/local/bin/
}
2017-08-29 00:19:25 +03:00
```
### Configure the Kubernetes API Server
```
{
sudo mkdir -p /var/lib/kubernetes/
2017-08-29 00:19:25 +03:00
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/
}
2017-08-29 00:19:25 +03:00
```
2018-01-04 17:27:37 +03:00
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 compute instance:
2017-08-29 00:19:25 +03:00
```
2021-02-05 09:40:21 +03:00
INTERNAL_IP=$(curl -H "Authorization: Bearer Oracle" -L http://169.254.169.254/opc/v2/vnics | jq -r .[0].privateIp)
2017-08-29 00:19:25 +03:00
```
Create the `kube-apiserver.service` systemd unit file:
```
cat <<EOF | sudo tee /etc/systemd/system/kube-apiserver.service
2017-08-29 00:19:25 +03:00
[Unit]
Description=Kubernetes API Server
2017-12-18 18:07:54 +03:00
Documentation=https://github.com/kubernetes/kubernetes
2017-08-29 00:19:25 +03:00
[Service]
ExecStart=/usr/local/bin/kube-apiserver \\
--advertise-address=${INTERNAL_IP} \\
--allow-privileged=true \\
--apiserver-count=3 \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/var/log/audit.log \\
--authorization-mode=Node,RBAC \\
--bind-address=0.0.0.0 \\
--client-ca-file=/var/lib/kubernetes/ca.pem \\
2019-09-14 21:41:56 +03:00
--enable-admission-plugins=NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota \\
2017-08-29 00:19:25 +03:00
--etcd-cafile=/var/lib/kubernetes/ca.pem \\
--etcd-certfile=/var/lib/kubernetes/kubernetes.pem \\
--etcd-keyfile=/var/lib/kubernetes/kubernetes-key.pem \\
--etcd-servers=https://10.240.0.10:2379,https://10.240.0.11:2379,https://10.240.0.12:2379 \\
--event-ttl=1h \\
2019-09-14 21:41:56 +03:00
--encryption-provider-config=/var/lib/kubernetes/encryption-config.yaml \\
2017-08-29 00:19:25 +03:00
--kubelet-certificate-authority=/var/lib/kubernetes/ca.pem \\
--kubelet-client-certificate=/var/lib/kubernetes/kubernetes.pem \\
--kubelet-client-key=/var/lib/kubernetes/kubernetes-key.pem \\
--kubelet-https=true \\
2020-07-18 10:24:55 +03:00
--runtime-config='api/all=true' \\
--service-account-key-file=/var/lib/kubernetes/service-account.pem \\
2017-08-29 00:19:25 +03:00
--service-cluster-ip-range=10.32.0.0/24 \\
--service-node-port-range=30000-32767 \\
--tls-cert-file=/var/lib/kubernetes/kubernetes.pem \\
--tls-private-key-file=/var/lib/kubernetes/kubernetes-key.pem \\
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Configure the Kubernetes Controller Manager
Move the `kube-controller-manager` kubeconfig into place:
```
sudo mv kube-controller-manager.kubeconfig /var/lib/kubernetes/
```
2017-08-29 00:19:25 +03:00
Create the `kube-controller-manager.service` systemd unit file:
```
cat <<EOF | sudo tee /etc/systemd/system/kube-controller-manager.service
2017-08-29 00:19:25 +03:00
[Unit]
Description=Kubernetes Controller Manager
2017-12-18 18:07:54 +03:00
Documentation=https://github.com/kubernetes/kubernetes
2017-08-29 00:19:25 +03:00
[Service]
ExecStart=/usr/local/bin/kube-controller-manager \\
2020-07-18 10:24:55 +03:00
--bind-address=0.0.0.0 \\
2017-08-29 00:19:25 +03:00
--cluster-cidr=10.200.0.0/16 \\
--cluster-name=kubernetes \\
--cluster-signing-cert-file=/var/lib/kubernetes/ca.pem \\
--cluster-signing-key-file=/var/lib/kubernetes/ca-key.pem \\
--kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig \\
2017-08-29 00:19:25 +03:00
--leader-elect=true \\
--root-ca-file=/var/lib/kubernetes/ca.pem \\
--service-account-private-key-file=/var/lib/kubernetes/service-account-key.pem \\
--service-cluster-ip-range=10.32.0.0/24 \\
--use-service-account-credentials=true \\
2017-08-29 00:19:25 +03:00
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Configure the Kubernetes Scheduler
Move the `kube-scheduler` kubeconfig into place:
```
sudo mv kube-scheduler.kubeconfig /var/lib/kubernetes/
```
Create the `kube-scheduler.yaml` configuration file:
```
cat <<EOF | sudo tee /etc/kubernetes/config/kube-scheduler.yaml
2019-09-14 21:41:56 +03:00
apiVersion: kubescheduler.config.k8s.io/v1alpha1
kind: KubeSchedulerConfiguration
clientConnection:
kubeconfig: "/var/lib/kubernetes/kube-scheduler.kubeconfig"
leaderElection:
leaderElect: true
EOF
```
2017-08-29 00:19:25 +03:00
Create the `kube-scheduler.service` systemd unit file:
```
cat <<EOF | sudo tee /etc/systemd/system/kube-scheduler.service
2017-08-29 00:19:25 +03:00
[Unit]
Description=Kubernetes Scheduler
2017-12-18 18:07:54 +03:00
Documentation=https://github.com/kubernetes/kubernetes
2017-08-29 00:19:25 +03:00
[Service]
ExecStart=/usr/local/bin/kube-scheduler \\
--config=/etc/kubernetes/config/kube-scheduler.yaml \\
2017-08-29 00:19:25 +03:00
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Start the Controller Services
```
{
sudo systemctl daemon-reload
sudo systemctl enable kube-apiserver kube-controller-manager kube-scheduler
sudo systemctl start kube-apiserver kube-controller-manager kube-scheduler
}
2017-08-29 00:19:25 +03:00
```
> Allow up to 10 seconds for the Kubernetes API Server to fully initialize.
### Enable HTTP Health Checks
2021-02-05 09:40:21 +03:00
Our [OCI Load Balancer](https://docs.oracle.com/en-us/iaas/Content/Balance/Concepts/balanceoverview.htm) will be used to distribute traffic across the three API servers and allow each API server to terminate TLS connections and validate client certificates. OCI Load Balancers only supports HTTP health checks which means the HTTPS endpoint exposed by the API server cannot be used. As a workaround 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 `8888` 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:
2017-08-29 00:19:25 +03:00
```
2019-09-14 21:41:56 +03:00
sudo apt-get update
sudo apt-get install -y nginx
2017-08-29 00:19:25 +03:00
```
```
cat > kubernetes.default.svc.cluster.local <<EOF
server {
2021-02-05 09:40:21 +03:00
listen 8888;
location /healthz {
proxy_pass https://127.0.0.1:6443/healthz;
proxy_ssl_trusted_certificate /var/lib/kubernetes/ca.pem;
}
}
EOF
2017-08-29 00:19:25 +03:00
```
```
{
sudo mv kubernetes.default.svc.cluster.local \
/etc/nginx/sites-available/kubernetes.default.svc.cluster.local
sudo ln -s /etc/nginx/sites-available/kubernetes.default.svc.cluster.local /etc/nginx/sites-enabled/
}
2017-08-29 00:19:25 +03:00
```
```
sudo systemctl restart nginx
```
```
sudo systemctl enable nginx
```
2017-08-29 00:19:25 +03:00
### Verification
```
kubectl get componentstatuses --kubeconfig admin.kubeconfig
2017-08-29 00:19:25 +03:00
```
```
2020-07-18 10:24:55 +03:00
NAME STATUS MESSAGE ERROR
2017-09-04 00:18:03 +03:00
scheduler Healthy ok
2020-07-18 10:24:55 +03:00
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
etcd-1 Healthy {"health":"true"}
etcd-2 Healthy {"health":"true"}
2017-08-29 00:19:25 +03:00
```
Test the nginx HTTP health check proxy:
```
2021-02-05 09:40:21 +03:00
curl -H "Host: kubernetes.default.svc.cluster.local" -i http://127.0.0.1:8888/healthz
```
```
HTTP/1.1 200 OK
2020-07-18 10:24:55 +03:00
Server: nginx/1.18.0 (Ubuntu)
Date: Sat, 18 Jul 2020 06:20:48 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 2
Connection: keep-alive
2020-07-18 10:24:55 +03:00
Cache-Control: no-cache, private
2019-09-14 21:41:56 +03:00
X-Content-Type-Options: nosniff
ok
```
2017-08-29 00:19:25 +03:00
> Remember to run the above commands on each controller node: `controller-0`, `controller-1`, and `controller-2`.
2017-10-02 06:37:09 +03:00
## 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. 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.
2019-09-14 21:41:56 +03:00
The commands in this section will effect the entire cluster and only need to be run once from one of the controller nodes.
2017-10-02 06:37:09 +03:00
```
2021-02-05 09:40:21 +03:00
oci-ssh controller-0
2017-10-02 06:37:09 +03:00
```
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:
```
cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
2017-10-02 06:37:09 +03:00
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:kube-apiserver-to-kubelet
rules:
- apiGroups:
- ""
resources:
- nodes/proxy
- nodes/stats
- nodes/log
- nodes/spec
- nodes/metrics
verbs:
- "*"
EOF
```
The Kubernetes API Server authenticates to the Kubelet as the `kubernetes` user using the client certificate as defined by the `--kubelet-client-certificate` flag.
Bind the `system:kube-apiserver-to-kubelet` ClusterRole to the `kubernetes` user:
```
cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
2017-10-02 06:37:09 +03:00
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: system:kube-apiserver
namespace: ""
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:kube-apiserver-to-kubelet
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: kubernetes
EOF
```
2017-08-29 00:19:25 +03:00
## The Kubernetes Frontend Load Balancer
2021-02-05 09:40:21 +03:00
Now we'll verify external connectivity to our cluster via the external Load Balancer
2017-08-29 00:19:25 +03:00
### Verification
2019-09-14 21:41:56 +03:00
> The compute instances created in this tutorial will not have permission to complete this section. **Run the following commands from the same machine used to create the compute instances**.
2021-02-05 09:40:21 +03:00
Retrieve the `kubernetes-the-hard-way` Load Balancer public IP address:
2017-08-29 00:19:25 +03:00
```
2021-02-05 09:40:21 +03:00
KUBERNETES_PUBLIC_ADDRESS=$(oci lb load-balancer list --all | jq '.data[] | select(."display-name"=="kubernetes-the-hard-way")' | jq -r '."ip-addresses"[0]."ip-address"')
2017-08-29 00:19:25 +03:00
```
Make a HTTP request for the Kubernetes version info:
```
2017-10-02 06:37:09 +03:00
curl --cacert ca.pem https://${KUBERNETES_PUBLIC_ADDRESS}:6443/version
2017-08-29 00:19:25 +03:00
```
> output
```
{
"major": "1",
2020-07-18 10:24:55 +03:00
"minor": "18",
"gitVersion": "v1.18.6",
"gitCommit": "dff82dc0de47299ab66c83c626e08b245ab19037",
2017-08-29 00:19:25 +03:00
"gitTreeState": "clean",
2020-07-18 10:24:55 +03:00
"buildDate": "2020-07-15T16:51:04Z",
"goVersion": "go1.13.9",
2017-08-29 00:19:25 +03:00
"compiler": "gc",
"platform": "linux/amd64"
}
```
Next: [Bootstrapping the Kubernetes Worker Nodes](09-bootstrapping-kubernetes-workers.md)