Kubernetes is production grade container orchestration tool which automates container deployment, scaling and management. It is a google product that is used at google internally. Kubernetes newer outgrows because it is flexible. Kubernetes is opensource so there is no restriction, fees etc. Some of companies that use kubernetes is Pearson, Ebay, Wikimedia. Kubernetes have the ability scale applications on the fly.
Install Kubernetes
We start by downloading Google Repository keys for Ubuntu and adding them to key rings.
$ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 663 100 663 0 0 775 0 --:--:-- --:--:-- --:--:-- 776 OK
We add required repository to our sources.list
$ sudo cat <<EOF > /etc/apt/sources.list.d/kubernetes.list > deb http://apt.kubernetes.io/ kubernetes-xenial main > EOF
Update our application list to get kubernetes info.
$ sudo apt-get update Hit:1 http://security.ubuntu.com/ubuntu xenial-security InRelease Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease ...
And install kubernetes packages from google repositories.
$ sudo apt-get install -y docker.io kubelet kubeadm kubectl kubernetes-cni
We install all other systems those will join to the kubernetes cluster. We implement same command described in this kubernetes installation section.
Initialize Kubernetes Cluster
We will initialize the cluster head with the following command.
$ sudo kubeadm init <master/tokens> generated token: "3e313d.c5d75f78f4f9db27" <master/pki> created keys and certificates in "/etc/kubernetes/pki" <util/kubeconfig> created "/etc/kubernetes/kubelet.conf" <util/kubeconfig> created "/etc/kubernetes/admin.conf" <master/apiclient> created API client configuration <master/apiclient> created API client, waiting for the control plane to become ready <master/apiclient> all control plane components are healthy after 46.303552 seconds <master/apiclient> waiting for at least one node to register and become ready <master/apiclient> first node is ready after 3.502990 seconds <master/discovery> created essential addon: kube-discovery, waiting for it to become ready <master/discovery> kube-discovery is ready after 38.002656 seconds <master/addons> created essential addon: kube-proxy <master/addons> created essential addon: kube-dns Kubernetes master initialised successfully! You can now join any number of machines by running the following on each node: kubeadm join --token 3e313d.c5d75f78f4f9db27 192.168.122.13
Our second system named poftut5. We have installed kubernetes as stated before. We will run “kubeadm join –token 3e313d.c5d75f78f4f9db27 192.168.122.137
” which is produced by poftut4.
$ sudo kubeadm join --token 3e313d.c5d75f78f4f9db27 192.168.122.137 <util/tokens> validating provided token <node/discovery> created cluster info discovery client, requesting info from "http://192.168.122.137:9898/cluster-info/v1/?token-id=3 e313d" <node/discovery> cluster info object received, verifying signature using given token <node/discovery> cluster info signature and contents are valid, will use API endpoints [https://192.168.122.137:443] <node/csr> created API client to obtain unique certificate for this node, generating keys and certificate signing request <node/csr> received signed certificate from the API server, generating kubelet configuration <util/kubeconfig> created "/etc/kubernetes/kubelet.conf" Node join complete: * Certificate signing request sent to master and response received. * Kubelet informed of new secure connection details. Run 'kubectl get nodes' on the master to see this machine join.
On poftut4 we will check if poftut5 is joined to the cluster.
$ kubectl get nodes NAME STATUS AGE poftut4 Ready 21m poftut5 Ready 2m
Good is seems everything is OK. We can see our two systems are joined to the kubernetes cluster and their status is Ready.
To make systems in kubernetes jargon pods communication eachother. Install a daemong with the followinf command.
$ kubectl apply -f https://git.io/weave-kube daemonset "weave-net" created
Deploying Test Application
We use a github project as our test application. We get the project with git.
$ git clone https://github.com/microservices-demo/microservices-demo Cloning into 'microservices-demo'... remote: Counting objects: 4885, done. ...
We use application provided deployment manifest like below
$ kubectl apply -f microservices-demo/deploy/kubernetes/manifests deployment "cart-db" created service "cart-db" created deployment "cart" created service "cart" created deployment "catalogue-db" created service "catalogue-db" created ...
Here the containers are created. We can get info by issuing kubectl get pods.
$ kubectl get pods NAME READY STATUS RESTARTS AGE cart-3694116665-8ovlg 1/1 Running 0 1m cart-db-2305146297-6zvky 0/1 ContainerCreating 0 1m catalogue-11453786-yu44e 0/1 ContainerCreating 0 1m catalogue-db-393939662-fm3g6 0/1 ContainerCreating 0 1m front-end-3820830240-zojqb 0/1 ContainerCreating 0 1m orders-3498886496-qbafq 0/1 ContainerCreating 0 1m orders-db-1775353731-0knx5 0/1 ContainerCreating 0 1m payment-3012088042-edphj 0/1 ContainerCreating 0 1m queue-master-936560853-zaiqj 0/1 ContainerCreating 0 1m rabbitmq-1897447621-242ds 0/1 ContainerCreating 0 1m shipping-1232389217-mraq4 0/1 ContainerCreating 0 1m user-3090014237-y7q8m 0/1 ContainerCreating 0 1m user-db-1338754314-nu2sp 0/1 ContainerCreating 0 1m
ContainerCreating means it is progressing and current the application is not running.
After 2 min if we issue the command again we ca see that some of the pods are running and other are creating.
$ kubectl get pods NAME READY STATUS RESTARTS AGE cart-3694116665-8ovlg 1/1 Running 0 3m cart-db-2305146297-6zvky 1/1 Running 0 3m catalogue-11453786-yu44e 1/1 Running 0 3m catalogue-db-393939662-fm3g6 1/1 Running 0 3m front-end-3820830240-zojqb 1/1 Running 0 3m orders-3498886496-qbafq 1/1 Running 0 3m orders-db-1775353731-0knx5 1/1 Running 0 3m payment-3012088042-edphj 0/1 ContainerCreating 0 3m queue-master-936560853-zaiqj 0/1 ContainerCreating 0 3m rabbitmq-1897447621-242ds 0/1 ContainerCreating 0 3m shipping-1232389217-mraq4 0/1 ContainerCreating 0 3m user-3090014237-y7q8m 0/1 ContainerCreating 0 3m user-db-1338754314-nu2sp 0/1 ContainerCreating 0 3m
We can see frontend services status and get information about them by using describe command.
$ kubectl describe svc front-end Name: front-end Namespace: default Labels: name=front-end Selector: name=front-end Type: NodePort IP: 100.70.194.72 Port: <unset> 80/TCP NodePort: <unset> 31425/TCP Endpoints: <none> Session Affinity: None
Delete Pod From Kubernetes Cluster
We have deployed our application successfully. But what if we want to destroy it. It similar to the install procedure just use delete command with kubectl.
$ kubectl delete -f microservices-demo/deploy/kubernetes/manifests deployment "cart-db" deleted service "cart-db" deleted deployment "cart" deleted service "cart" deleted deployment "catalogue-db" deleted service "catalogue-db" deleted deployment "catalogue" deleted service "catalogue" deleted deployment "front-end" deleted service "front-end" deleted networkpolicy "cart-access" deleted networkpolicy "cart-db-access" deleted networkpolicy "catalogue-access" deleted networkpolicy "catalogue-db-access" deleted networkpolicy "front-end-access" deleted networkpolicy "orders-access" deleted networkpolicy "orders-db-access" deleted networkpolicy "payment-access" deleted networkpolicy "prism-access" deleted networkpolicy "rabbitmq-access" deleted networkpolicy "shipping-access" deleted networkpolicy "user-access" deleted networkpolicy "user-db-access" deleted deployment "orders-db" deleted service "orders-db" deleted deployment "orders" deleted service "orders" deleted deployment "payment" deleted service "payment" deleted deployment "queue-master" deleted service "queue-master" deleted deployment "rabbitmq" deleted service "rabbitmq" deleted deployment "shipping" deleted service "shipping" deleted deployment "user-db" deleted service "user-db" deleted deployment "user" deleted service "user" deleted
How to Setup Kubernetes 1.4 on Ubuntu Infographic
