|
| 1 | +--- |
| 2 | +--- |
| 3 | + |
| 4 | +<style> |
| 5 | +li>.highlighter-rouge {position:relative; top:3px;} |
| 6 | +</style> |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +This quickstart shows you how to easily install a secure Kubernetes cluster on machines running Ubuntu 16.04 or CentOS 7. |
| 11 | +The installation uses a tool called `kubeadm` which is part of Kubernetes 1.4. |
| 12 | + |
| 13 | +This process works with local VMs, physical servers and/or cloud servers. |
| 14 | +It is simple enough that you can easily integrate its use into your own automation (Terraform, Chef, Puppet, etc). |
| 15 | + |
| 16 | +**The `kubeadm` tool is currently in alpha but please try it out and give us [feedback](/docs/getting-started-guides/kubeadm/#feedback)!** |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +1. One or more machines running Ubuntu 16.04 or CentOS 7 |
| 21 | +1. 1GB or more of RAM per machine (any less will leave little room for your apps) |
| 22 | +1. Full network connectivity between all machines in the cluster (public or private network is fine) |
| 23 | + |
| 24 | +## Objectives |
| 25 | + |
| 26 | +* Install a secure Kubernetes cluster on your machines |
| 27 | +* Install a pod network on the cluster so that application components (pods) can talk to each other |
| 28 | +* Install a sample microservices application (a socks shop) on the cluster |
| 29 | + |
| 30 | +## Instructions |
| 31 | + |
| 32 | +### (1/4) Installing kubelet and kubeadm on your hosts |
| 33 | + |
| 34 | +You will install the following packages on all the machines: |
| 35 | + |
| 36 | +* `docker`: the container runtime, which Kubernetes depends on. |
| 37 | +* `kubelet`: the most core component of Kubernetes. |
| 38 | + It runs on all of the machines in your cluster and does things like starting pods and containers. |
| 39 | +* `kubectl`: the command to control the cluster once it's running. |
| 40 | + You will only use this on the master. |
| 41 | +* `kubeadm`: the command to bootstrap the cluster. |
| 42 | + |
| 43 | +For each host in turn: |
| 44 | + |
| 45 | +<!-- |
| 46 | + # curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - |
| 47 | + # cat <<EOF > /etc/apt/sources.list.d/kubernetes.list |
| 48 | + deb http://packages.cloud.google.com/apt kubernetes-xenial main |
| 49 | + EOF |
| 50 | + # apt-get update |
| 51 | + # apt-get install -y kubeadm docker.io§ |
| 52 | +--> |
| 53 | + |
| 54 | + |
| 55 | +* SSH into the machine and become `root` if you are not already (for example, run `sudo su -`). |
| 56 | +* If the machine is running Ubuntu 16.04, run: |
| 57 | + |
| 58 | + # apt-get install -y docker.io socat apt-transport-https |
| 59 | + # curl -s -L \ |
| 60 | + https://storage.googleapis.com/kubeadm/kubernetes-xenial-preview-bundle.txz | tar xJv |
| 61 | + # dpkg -i kubernetes-xenial-preview-bundle/*.deb |
| 62 | + |
| 63 | + If the machine is running CentOS 7, run: |
| 64 | + |
| 65 | + # cat <<EOF > /etc/yum.repos.d/k8s.repo |
| 66 | + [kubelet] |
| 67 | + name=kubelet |
| 68 | + baseurl=http://files.rm-rf.ca/rpms/kubelet/ |
| 69 | + enabled=1 |
| 70 | + gpgcheck=0 |
| 71 | + EOF |
| 72 | + # yum install docker kubelet kubeadm kubectl kubernetes-cni |
| 73 | + # systemctl enable docker && systemctl start docker |
| 74 | + # systemctl enable kubelet && systemctl start kubelet |
| 75 | + |
| 76 | +The kubelet is now restarting every few seconds, as it waits in a crashloop for `kubeadm` to tell it what to do. |
| 77 | + |
| 78 | +### (2/4) Initializing your master |
| 79 | + |
| 80 | +The master is the machine where the "control plane" components run, including `etcd` (the cluster database) and the API server (which the `kubectl` CLI communicates with). |
| 81 | +All of these components run in pods started by `kubelet`. |
| 82 | + |
| 83 | +To initialize the master, pick one of the machines you previously installed `kubelet` and `kubeadm` on, and run: |
| 84 | + |
| 85 | + # kubeadm init --use-kubernetes-version v1.4.0-beta.11 |
| 86 | + |
| 87 | +This will download and install the cluster database and "control plane" components. |
| 88 | +This may take several minutes. |
| 89 | + |
| 90 | +The output should look like: |
| 91 | + |
| 92 | + <master/tokens> generated token: "f0c861.753c505740ecde4c" |
| 93 | + <master/pki> created keys and certificates in "/etc/kubernetes/pki" |
| 94 | + <util/kubeconfig> created "/etc/kubernetes/kubelet.conf" |
| 95 | + <util/kubeconfig> created "/etc/kubernetes/admin.conf" |
| 96 | + <master/apiclient> created API client configuration |
| 97 | + <master/apiclient> created API client, waiting for the control plane to become ready |
| 98 | + <master/apiclient> all control plane components are healthy after 61.346626 seconds |
| 99 | + <master/apiclient> waiting for at least one node to register and become ready |
| 100 | + <master/apiclient> first node is ready after 4.506807 seconds |
| 101 | + <master/discovery> created essential addon: kube-discovery |
| 102 | + <master/addons> created essential addon: kube-proxy |
| 103 | + <master/addons> created essential addon: kube-dns |
| 104 | + |
| 105 | + Kubernetes master initialised successfully! |
| 106 | + |
| 107 | + You can connect any number of nodes by running: |
| 108 | + |
| 109 | + kubeadm join --token <token> <master-ip> |
| 110 | + |
| 111 | +Make a record of the `kubeadm join` command that `kubeadm init` outputs. |
| 112 | +You will need this in a moment. |
| 113 | +The key included here is secret, keep it safe — anyone with this key can add authenticated nodes to your cluster. |
| 114 | + |
| 115 | +The key is used for mutual authentication between the master and the joining nodes. |
| 116 | + |
| 117 | +By default, your cluster will not schedule pods on the master for security reasons. |
| 118 | +If you want to be able to schedule pods on the master, for example if you want a single-machine Kubernetes cluster for development, run: |
| 119 | + |
| 120 | + # kubectl taint nodes --all dedicated- |
| 121 | + node "test-01" tainted |
| 122 | + taint key="dedicated" and effect="" not found. |
| 123 | + taint key="dedicated" and effect="" not found. |
| 124 | + |
| 125 | +This will remove the "dedicated" taint from any nodes that have it, including the master node, meaning that the scheduler will then be able to schedule pods everywhere. |
| 126 | + |
| 127 | +### (3/4) Joining your nodes |
| 128 | + |
| 129 | +The nodes are where your workloads (containers and pods, etc) run. |
| 130 | +If you want to add any new machines as nodes to your cluster, for each machine: SSH to that machine, become root (e.g. `sudo su -`) and run the command that was output by `kubeadm init`. |
| 131 | +For example: |
| 132 | + |
| 133 | + # kubeadm join --token <token> <master-ip> |
| 134 | + <util/tokens> validating provided token |
| 135 | + <node/discovery> created cluster info discovery client, requesting info from "http://138.68.156.129:9898/cluster-info/v1/?token-id=0f8588" |
| 136 | + <node/discovery> cluster info object received, verifying signature using given token |
| 137 | + <node/discovery> cluster info signature and contents are valid, will use API endpoints [https://138.68.156.129:443] |
| 138 | + <node/csr> created API client to obtain unique certificate for this node, generating keys and certificate signing request |
| 139 | + <node/csr> received signed certificate from the API server, generating kubelet configuration |
| 140 | + <util/kubeconfig> created "/etc/kubernetes/kubelet.conf" |
| 141 | + |
| 142 | + Node join complete: |
| 143 | + * Certificate signing request sent to master and response |
| 144 | + received. |
| 145 | + * Kubelet informed of new secure connection details. |
| 146 | + |
| 147 | + Run 'kubectl get nodes' on the master to see this machine join. |
| 148 | + |
| 149 | +A few seconds later, you should notice that running `kubectl get nodes` on the master shows a cluster with as many machines as you created. |
| 150 | + |
| 151 | +**YOUR CLUSTER IS NOT READY YET!** |
| 152 | + |
| 153 | +Before you can deploy applications to it, you need to install a pod network. |
| 154 | + |
| 155 | +### (4/4) Installing a pod network |
| 156 | + |
| 157 | +You must install a pod network add-on so that your pods can communicate with each other when they are on different hosts. |
| 158 | +**It is necessary to do this before you try to deploy any applications to your cluster.** |
| 159 | + |
| 160 | +Several projects provide Kubernetes pod networks. |
| 161 | +You can see a complete list of available network add-ons on the [add-ons page](/docs/admin/addons/). |
| 162 | + |
| 163 | +By way of example, you can install [Weave Net](https://github.com/weaveworks/weave-kube) by logging in to the master and running: |
| 164 | + |
| 165 | + # kubectl apply -f https://git.io/weave-kube |
| 166 | + daemonset "weave-net" created |
| 167 | + |
| 168 | +If you prefer [Calico](https://github.com/projectcalico/calico-containers/tree/master/docs/cni/kubernetes/manifests/kubeadm) or [Canal](https://github.com/tigera/canal/tree/master/k8s-install/kubeadm), please refer to their respective installation guides. |
| 169 | +You should only install one pod network per cluster. |
| 170 | + |
| 171 | +Once a pod network has been installed, you can confirm that it is working by checking that the `kube-dns` pod is `Running` in the output of `kubectl get pods --all-namespaces`. |
| 172 | +**This signifies that your cluster is ready.** |
| 173 | + |
| 174 | +### (Optional) Installing a sample application |
| 175 | + |
| 176 | +As an example, install a sample microservices application, a socks shop, to put your cluster through its paces. |
| 177 | +To learn more about the sample microservices app, see the [GitHub README](https://github.com/microservices-demo/microservices-demo). |
| 178 | + |
| 179 | + # git clone https://github.com/microservices-demo/microservices-demo |
| 180 | + # kubectl apply -f microservices-demo/deploy/kubernetes/manifests |
| 181 | + |
| 182 | +You can then find out the port that the [NodePort feature of services](/docs/user-guide/services/) allocated for the front-end service by running: |
| 183 | + |
| 184 | + # kubectl describe svc front-end |
| 185 | + Name: front-end |
| 186 | + Namespace: default |
| 187 | + Labels: name=front-end |
| 188 | + Selector: name=front-end |
| 189 | + Type: NodePort |
| 190 | + IP: 100.66.88.176 |
| 191 | + Port: <unset> 80/TCP |
| 192 | + NodePort: <unset> 31869/TCP |
| 193 | + Endpoints: <none> |
| 194 | + Session Affinity: None |
| 195 | + |
| 196 | +It takes several minutes to download and start all the containers, watch the output of `kubectl get pods` to see when they're all up and running. |
| 197 | + |
| 198 | +Then go to the IP address of your cluster's master node in your browser, and specify the given port. |
| 199 | +So for example, `http://<master_ip>:<port>`. |
| 200 | +In the example above, this was `31869`, but it is a different port for you. |
| 201 | + |
| 202 | +If there is a firewall, make sure it exposes this port to the internet before you try to access it. |
| 203 | + |
| 204 | +### Explore other add-ons |
| 205 | + |
| 206 | +See the [list of add-ons](/docs/admin/addons/) to explore other add-ons, including tools for logging, monitoring, network policy, visualization & control of your Kubernetes cluster. |
| 207 | + |
| 208 | + |
| 209 | +## What's next |
| 210 | + |
| 211 | +* Learn more about [Kubernetes concepts and kubectl in Kubernetes 101](/docs/user-guide/walkthrough/). |
| 212 | +* Install Kubernetes with [a cloud provider configurations](/docs/getting-started-guides/) to add Load Balancer and Persistent Volume support. |
| 213 | + |
| 214 | + |
| 215 | +## Cleanup |
| 216 | + |
| 217 | +* To uninstall the socks shop, run `kubectl delete -f microservices-demo/deploy/kubernetes/manifests` on the master. |
| 218 | + |
| 219 | +* To undo what `kubeadm` did, simply delete the machines you created for this tutorial, or run the script below and then uninstall the packages. |
| 220 | + <details> |
| 221 | + <pre><code>systemctl stop kubelet; |
| 222 | + docker rm -f $(docker ps -q); mount | grep "/var/lib/kubelet/*" | awk '{print $3}' | xargs umount 1>/dev/null 2>/dev/null; |
| 223 | + rm -rf /var/lib/kubelet /etc/kubernetes /var/lib/etcd /etc/cni; |
| 224 | + ip link set cbr0 down; ip link del cbr0; |
| 225 | + ip link set cni0 down; ip link del cni0; |
| 226 | + systemctl start kubelet</code></pre> |
| 227 | + </details> <!-- *syntax-highlighting-hack --> |
| 228 | + |
| 229 | +## Feedback |
| 230 | + |
| 231 | +* Slack Channel: [#sig-cluster-lifecycle](https://kubernetes.slack.com/messages/sig-cluster-lifecycle/) |
| 232 | +* Mailing List: [kubernetes-sig-cluster-lifecycle](https://groups.google.com/forum/#!forum/kubernetes-sig-cluster-lifecycle) |
| 233 | +* [GitHub Issues](https://github.com/kubernetes/kubernetes/issues): please tag `kubeadm` issues with `@kubernetes/sig-cluster-lifecycle` |
| 234 | + |
| 235 | +## Limitations |
| 236 | + |
| 237 | +Please note: `kubeadm` is a work in progress and these limitations will be addressed in due course. |
| 238 | + |
| 239 | +1. The cluster created here doesn't have cloud-provider integrations, so for example won't work with (for example) [Load Balancers](/docs/user-guide/load-balancer/) (LBs) or [Persistent Volumes](/docs/user-guide/persistent-volumes/walkthrough/) (PVs). |
| 240 | + To easily obtain a cluster which works with LBs and PVs Kubernetes, try [the "hello world" GKE tutorial](/docs/hellonode) or [one of the other cloud-specific installation tutorials](/docs/getting-started-guides/). |
| 241 | + |
| 242 | + Workaround: use the [NodePort feature of services](/docs/user-guide/services/#type-nodeport) for exposing applications to the internet. |
| 243 | +1. The cluster created here has a single master, with a single `etcd` database running on it. |
| 244 | + This means that if the master fails, your cluster loses its configuration data and will need to be recreated from scratch. |
| 245 | + Adding HA support (multiple `etcd` servers, multiple API servers, etc) to `kubeadm` is still a work-in-progress. |
| 246 | + |
| 247 | + Workaround: regularly [back up etcd](https://coreos.com/etcd/docs/latest/admin_guide.html). |
| 248 | + The `etcd` data directory configured by `kubeadm` is at `/var/lib/etcd` on the master. |
| 249 | +1. `kubectl logs` is broken with `kubeadm` clusters due to [#22770](https://github.com/kubernetes/kubernetes/issues/22770). |
| 250 | + |
| 251 | + Workaround: use `docker logs` on the nodes where the containers are running as a workaround. |
| 252 | +1. There is not yet an easy way to generate a `kubeconfig` file which can be used to authenticate to the cluster remotely with `kubectl` on, for example, your workstation. |
| 253 | + |
| 254 | + Workaround: copy the kubelet's `kubeconfig` from the master: use `scp root@<master>:/etc/kubernetes/admin.conf .` and then e.g. `kubectl --kubeconfig ./admin.conf get nodes` from your workstation. |
0 commit comments