Getting Started: Azure Kubernetes Services
Disclaimer: Currently I am employed by Microsoft, but my views and thoughts are still my own. The reason I joined Microsoft was, the work Microsoft have been doing for last couple of years in Open Source Space. Today I am a advocate for Open Source representing Microsoft.
For managing a cluster of containers, there are several different options; Docker Swarm, Rancher, Kubernetes, Open Shift, etc.
Azure provides three different ways to run Kubernetes:
- Infrastructure as a Service (IaaS)
- Running and managing your own Kubernetes
- Azure Kubernetes Service (AKS)
- Azure Red Hat Open Shift (AROS)
NOTE: There are several ways to manage Azure, but here I am going to use Azure-CLI. All the commands are cross-platform.
Prerequisites
Assuming you have:
- Azure Subscription
- Azure CLI installed
- Logged in to Azure CLI
- Created a Resource Group for AKS cluster
Provisioning a Kubernetes cluster
You can create AKS cluster with:
az aks create --name "{AKS-CLUSTER-NAME}" --resource-group AKS-CLUSTER-NAME --node-count "{NODES}" --generate-ssh-keys
Example:
az aks create --name aks-cluster --resource-group aks-resource-group --node-count 3 --generate-ssh-keys
This will create a new AKS cluster with name aks-cluster
in resource group aks-resource-group
with 3
nodes.
NOTE: The AKS cluster will be created in the same Azure region as the resource group.
This will take 10-20 mins.
Configure your client
We can control the Kubernetes cluster with kubectl
Azure CLI can help you with the installation of kubectl
:
sudo az aks install-cli
Now you need to tell kubectl
to use AKS:
az aks get-credentials --name "{AKS-CLUSTER-NAME}" --resource-group "{AKS-CLUSTER-NAME}"
Make sure to use the correct --name
and --resource-group
.
You can verify the connection to AKS cluster with:
kubectl get nodes
This will return a list of nodes in the connected AKS cluster.
Test AKS Cluster
Create deployment manifest
Copy and paste the flowing code to a nginx.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-deployment
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Deploy the manifest with:
kubectl apply -f nginx.yml
Verify pod deployment:
kubectl get pods
You should get something like this:
NAME READY STATUS RESTARTS AGE
nginx-deployment-6574bd76c-ppfzx 1/1 Running 0 20s
Get External IP of the Service
Now we can ask AKS for the external IP to the nginx service
:
kubectl get services
NOTE: It can take sometime to get the external IP, just run the above command again if
kubectl
return<pending>
after a minute or two.
Now you can see the external IP:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-deployment LoadBalancer 10.0.192.33 <EXTERNAL-IP> 80:31257/TCP 53s
When you enter the <EXTERNAL-IP>
in your browser you should be greated with the NGINX Welcome Screen.
Create a namespace
The best practice in Kubernetes is to use separate namespaces.
You create a new namespace in Kubernetes with:
kubectl create namespace mssql