Hands-On Kubernetes on Azure
上QQ阅读APP看书,第一时间看更新

Using Azure Cloud Shell

Once you have a successful deployment, it is time to play. As promised, we will do it all from the Azure portal with no client installs.

The toughest part of this assignment is finding the small icon near the search bar:

Go ahead. Click on it.

First the portal will ask you to select either PowerShell or Bash as your default shell experience.

Next, the portal will ask you to create a storage account; just confirm and create it.

You might still get this:

Click on the power button; it should restart, and you should see something similar to this:

You can pull the splitter/divider up to see more of the shell:

On the shell, we need to first install kubectl. This is the command-line tool used for many operations when operating and maintaining Kubernetes clusters. Furthermore, kubectl is already installed for you on Azure Cloud Shell.

You need the credentials to access your cluster. For example, on the shell, type the following command:

az aks get-credentials --resource-group handsonaks --name myfirstakscluster

The preceding command will set the correct values in ~/.kube/config so that kubectl can access it.

To verify that you have access, type the following:

kubectl get all

You should see something like this:

There is no need to worry whether all of these are exactly matching or what each item is. For now, our goal is to to be comfortable with kubectl and to ensure that the connection works.
If you are seeing connection refused or other access-related errors, double-check the entries in the following command:
az aks get-credentials --resource-group handsonaks --name myfirstakscluster

You are all connected now. We are going to launch our first application now. 

We are going to use the  vi command-line editor. It is generally confusing to use at first, but you can use the online code editor shown in the next section.

For the online code editor, type the following:

PS Azure:\> code .

For vi , type this:

vi azure-vote.yaml

If you are in vi , type i to get into the insert mode.

On a different browser, open https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough. We will use the code directly there for now.

Go to the section that has the code for azure-vote.yaml and click on the Copy button:

We have included the code from the Azure website for your convenience:

apiVersion: apps/v1
kind: Deployment
metadata:
name: azure-vote-back
spec:
replicas: 1
selector:
matchLabels:
app: azure-vote-back
template:
metadata:
labels:
app: azure-vote-back
spec:
containers:
- name: azure-vote-back
image: redis
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 250m
memory: 256Mi
ports:
- containerPort: 6379
name: redis
---
apiVersion: v1
kind: Service
metadata:
name: azure-vote-back
spec:
ports:
- port: 6379
selector:
app: azure-vote-back
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: azure-vote-front
spec:
replicas: 1
selector:
matchLabels:
app: azure-vote-front
template:
metadata:
labels:
app: azure-vote-front
spec:
containers:
- name: azure-vote-front
image: microsoft/azure-vote-front:v1
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 250m
memory: 256Mi
ports:
- containerPort: 80
env:
- name: REDIS
value: "azure-vote-back"
---
apiVersion: v1
kind: Service
metadata:
name: azure-vote-front
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: azure-vote-front

Back on the Azure online code editor, paste the content of the file.

Then, click on the ... in the right-hand corner to save the file as azure-vote.yaml:

The file should be saved. You can check by typing the following:

cat azure-vote.yaml
Hitting the Tab button expands the file name in Linux. In the preceding scenario, if you hit Tab after typing az it should expand to azure-vote.yaml.

Now, let's launch the application:

kubectl create -f azure-vote.yaml

Now we wait.

You can check the progress by typing the following:

kubectl get pods
Typing kubectl can become tedious. We generally use the alias command to make our life easier. We use alias kc=kubectl. After running the preceding command, we can just use kc get pods.

Hit the Up arrow and press return till you get the status as all pods running. It does take some time to set up everything, which you can check by typing the following:

kubectl get all --all-namespaces

The following screenshot illustrates the output of the preceding command:

It is most likely that it will be stuck on the previous step. If you are impatient, you can also type to see whether the frontend image has been pulled:

kubectl get events | grep -i pulled

The following screenshot illustrates the output of the preceding command:

Once you see Pulled for the frontend, type the following to ensure that everything is running:

In order to access it publicly, we need to wait for one more thing. We could type commands repeatedly or let Kubernetes know once a service is up and running. Now we want to know the public IP of the load balancer so that we can access it.

Type the following command:

kubectl get service azure-vote-front --watch

Wait for the public IP to appear and then press Ctrl + C to exit the watch:

Note the external IP address, and type it on a browser. You should see this:

Click on Cats or Dogs (I would go with Dogs) and watch the count go up.

You have now launched your own cluster and your first Kubernetes application. Note the effort of connecting the frontend and the backend and exposing it to the outside world along with providing storage for the services was all taken care of by Kubernetes.