Kubernetes on Azure in 30 Minutes

This guide is meant to streamline you to your first Kubernetes (k8s) cluster on Microsoft Azure, with the essentials to monitor and scale your container service. In the interest of time/reading, this will NOT be an indepth tutorial on Kubernetes.

Prerequisites

We will be using cli tools and package managers to provision azure resources and to manage our clusters.

Install Azure CLI

Azure CLI is used to deploy and manage resources within your azure subscription

Install on Mac with Homebrew

1
2
brew update
brew install azure-cli

Install on Windows

Download and run Azure CLI Installer (MSI)

For more informaion on installing Azure CLI

Install Kubernetes CLI (kubectl)

Kubectl is used to deploy and manage your kubernetes cluster

Install on Mac with Homebrew

1
brew install kubectl

Install on Windows

  1. Download latest release from here
  2. Add binary to PATH
    • Open Control Panel
    • Open Advanced System Properties
    • Click Environment Variables
    • Edit Path under System variables
    • Add Path to kubectl executable

For more information on installing Kubernetes CLI (kubectl)

Setting up Azure Container Service

Create a Resource Group and Create the Azure Container Service within that group. This will create a cluster named MyK8sCluster with 1 linux master and 3 linux agent nodes.

1
2
az group create --name AzureKubernetesTutorial --location westus
az acs create --orchestrator-type kubernetes --resource-group AzureKubernetesTutorial --name MyK8sCluster --generate-ssh-keys

Connect to your Cluster

Kubernetes CLI needs to be configured to access your cluster from your machine. This command configures your .kube/config.

1
2
3
4
5
6
7
8
9
az acs kubernetes get-credentials --resource-group=myResourceGroup --name=myK8sCluster
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-agent-631d3265-0 Ready agent 6d v1.7.7
k8s-agent-631d3265-1 Ready agent 6d v1.7.7
k8s-agent-631d3265-2 Ready agent 6d v1.7.7
k8s-master-631d3265-0 Ready master 6d v1.7.7

Monitoring your Cluster

Although k8s will try to keep up your clusters health, proactive monitoring is good for seeing CPU, memory, and disk metrics as well as how your pod is behaving within your cluster.

Ensure Heapster is installed

Heapster is a pod in your cluster that is responsible for aggregating monitoring data across all nodes and pods within your cluster. It is necessary to have Heapster for viewing data on Grafana.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
kubectl get pods --namespace=kube-system
NAME READY STATUS RESTARTS AGE
heapster-342135353-zq0fv 2/2 Running 0 4d
kube-addon-manager-k8s-master-631d3265-0 1/1 Running 0 8d
kube-apiserver-k8s-master-631d3265-0 1/1 Running 0 8d
kube-controller-manager-k8s-master-631d3265-0 1/1 Running 0 8d
kube-dns-v20-3003781527-13rws 3/3 Running 0 8d
kube-dns-v20-3003781527-n6rj6 3/3 Running 0 8d
kube-proxy-3fwdq 1/1 Running 0 8d
kube-proxy-7698s 1/1 Running 0 8d
kube-proxy-d421d 1/1 Running 0 8d
kube-proxy-fvp2m 1/1 Running 0 8d
kube-scheduler-k8s-master-631d3265-0 1/1 Running 0 8d
kubernetes-dashboard-924040265-tflfr 1/1 Running 0 8d
tiller-deploy-4061877079-d758t 1/1 Running 0 2h

Basic Node Metrics with Kubectl

The fastest way to see metrics about your node is to use kubectl.

1
2
3
4
5
6
7
kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master-631d3265-0 120m 6% 1928Mi 28%
k8s-agent-631d3265-2 44m 2% 1222Mi 17%
k8s-agent-631d3265-0 31m 1% 1310Mi 19%
k8s-agent-631d3265-1 36m 1% 1367Mi 19%

Installing Grafana and Influx DB

  1. Download InfluxDB deployment manifest by using curl or by going to the url and copy and pasting the source

    1
    curl -o ./influxdb.yaml -k https://raw.githubusercontent.com/kubernetes/heapster/release-1.5/deploy/kube-config/influxdb/influxdb.yaml
  2. Download Grafana deployment manifest by using curl or bu going to the url and copy and pasting the source

    1
    curl -o ./grafana.yaml -k https://raw.githubusercontent.com/kubernetes/heapster/release-1.5/deploy/kube-config/influxdb/grafana.yaml
  3. Edit both influxdb.yaml and grafana.yaml and remove

    1
    2
    3
    4
    5
    6
    7
    #influx.yaml
    kubernetes.io/cluster-service: 'true'
    kubernetes.io/name: monitoring-influxdb
    #grafana.yaml
    kubernetes.io/cluster-service: 'true'
    kubernetes.io/name: monitoring-grafana
  4. Run both influxedb and grafana manifest files. You should see both influxdb and grafana as Running pods in your kube-system namespace. Notice the newly created grafana and influxdb pods.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    kubectl apply -f influxdb.yaml
    kubectl apply -f grafana.yaml
    kubectl get pods --namespace kube-system
    NAME READY STATUS RESTARTS AGE
    heapster-342135353-zq0fv 2/2 Running 0 4d
    kube-addon-manager-k8s-master-631d3265-0 1/1 Running 0 8d
    kube-apiserver-k8s-master-631d3265-0 1/1 Running 0 8d
    kube-controller-manager-k8s-master-631d3265-0 1/1 Running 0 8d
    kube-dns-v20-3003781527-13rws 3/3 Running 0 8d
    kube-dns-v20-3003781527-n6rj6 3/3 Running 0 8d
    kube-proxy-3fwdq 1/1 Running 0 8d
    kube-proxy-7698s 1/1 Running 0 8d
    kube-proxy-d421d 1/1 Running 0 8d
    kube-proxy-fvp2m 1/1 Running 0 8d
    kube-scheduler-k8s-master-631d3265-0 1/1 Running 0 8d
    kubernetes-dashboard-924040265-tflfr 1/1 Running 0 8d
    monitoring-grafana-1219411114-bmgd8 1/1 Running 0 52s
    monitoring-influxdb-3087546307-wlxgn 1/1 Running 0 1m
    tiller-deploy-4061877079-d758t 1/1 Running 0 2h
  5. Configure Heapster to use InfluxDB as the data store for your monitoring metrics. Edit your heapster deployment by going under spec > containers > command property and changing the contents from:

    1
    2
    3
    - command:
    - /heapster
    - --source=kubernetes.summary_api:""

    to

    1
    2
    3
    4
    - command:
    - /heapster
    - --source=kubernetes.summary_api:""
    - --sink=influxdb:http://monitoring-influxdb.kube-system.svc:8086

    Save and exit from your editor. This will trigger a change in the deployment. Heapster will restart using InfluxDB as the data store.

  6. To verify your Heapster deployment change

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    getHeapster() { kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' --namespace=kube-system | grep -i heapster; }
    kubectl logs $(getHeapster) --namespace=kube-system -c heapster
    I1130 01:11:32.014809 1 heapster.go:72] /heapster --source=kubernetes.summary_api:"" --sink=influxdb:http://monitoring-influxdb.kube-system.svc:8086
    I1130 01:11:32.014884 1 heapster.go:73] Heapster version v1.4.2
    I1130 01:11:32.015079 1 configs.go:61] Using Kubernetes client with master "https://10.0.0.1:443" and version v1
    I1130 01:11:32.015091 1 configs.go:62] Using kubelet port 10255
    I1130 01:11:32.023608 1 influxdb.go:278] created influxdb sink with options: host:monitoring-influxdb.kube-system.svc:8086 user:root db:k8s
    I1130 01:11:32.023678 1 heapster.go:196] Starting with InfluxDB Sink
    I1130 01:11:32.023689 1 heapster.go:196] Starting with Metric Sink
    I1130 01:11:32.120803 1 heapster.go:106] Starting heapster on port 8082
    I1130 01:12:05.123163 1 influxdb.go:241] Created database "k8s" on influxDB server at "monitoring-influxdb.kube-system.svc:8086"

Viewing Grafana Dashboards

  1. Run kubectl proxy
  2. To view your cluster metrics go to: http://localhost:8001/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy/dashboard/db/cluster
  3. To view your pod metrics go to: http://localhost:8001/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy/dashboard/db/pod
grafana.png

Deploying your First Pod

Now that you have set up monitoring on your cluster, it is time to deploy your first pod. For this example, we will be deploying a simple node app that has already been uploaded to dockerhub. Your clusters state is defined through manifest files. K8s will continue checking if the current state is the same as what was defined in the manifest files.

  1. Download the example deployment manifest by using curl or going to the url and copy and pasting the source

    1
    curl -o ./hello-kubernetes.yaml -k https://raw.githubusercontent.com/wesyao/hello-kubernetes-app/master/deployment/hello-kubernetes.yaml
  2. This manifest file describes to k8s what you want deployed and how the state of the cluster should look like. In this manifest file, we will be deploying the hello-kubernetes-app from dockerhub, requiring that there will be 3 replicas of this pod, and specifying what ports we want to traffic to flow through. We also define a service that exposes an external IP for us to hit once deployed. For more information regarding how to write your own manifest files, see the k8s documentation.

    1
    2
    3
    4
    kubecutl apply -f hello-kubernetes.yaml
    deployment "deployment-hello-kubernetes" configured
    service "service-hello-kubernetes" configured
  3. Verify your deployment was successful. Notice the service-hello-kubernetes is allocating an External IP so that we can access our pods from outside the cluster.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    kubecutl get pods,svc
    NAME READY STATUS RESTARTS AGE
    po/deployment-hello-kubernetes-2906022042-0fxdz 1/1 Running 0 7s
    po/deployment-hello-kubernetes-2906022042-29l3j 1/1 Running 0 7s
    po/deployment-hello-kubernetes-2906022042-kb519 1/1 Running 0 7s
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    svc/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 9d
    svc/service-hello-kubernetes LoadBalancer 10.0.138.10 <pending> 80:31934/TCP 7s
  4. Once your service has finished allocating an External IP, you can access this from your favorite browser. Once you enter the External IP in your browser, you should see Hello Kubernetes!.

    1
    2
    3
    4
    5
    kubectl get svc
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 9d
    service-hello-kubernetes LoadBalancer 10.0.138.10 104.42.238.180 80:31934/TCP 5m

Patting Yourself on the Back

Congratulations! Sometimes it’s hard to remember to stop and smell the roses, but you’ve now stood up your first kubernetes cluster, added monitoring on your nodes, and deployed your first application on your cluster! This is no small feat. There is a vast amount of knowledge and details that weren’t covered here, but there is tons of information on the k8s documentation to give you a deeper dive on the concepts we covered.


Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2018 MAMBA MENTALITY All Rights Reserved.

UV : | PV :