After you have created a Kubernetes cluster, for instance, by using Azure Container Service, you probably want to start running some containers on it. In this post, I will describe how to do this, by using VSTS. I’ll explain how to execute commands and queries on Kubernetes, by using the CLI and by using Tasks.
If you use VSTS, you can configure a Build and Release pipeline. The Build pipeline will take your code, compile it, and package it into container images. The Release pipeline will then deploy those containers to your cluster. First, you’ll need to specify a Service Endpoint to be able to talk to the cluster.
Open the the VSTS portal, navigate to ‘Services’.
Click on the button ‘New Service Endpoint’, and select ‘Kubernetes’ as the type.
This will show the following screen:
The connection name should describe the use of the Endpoint. It will appear in a dropdown while creating the Release later.
For the Server URL, use the DNS name of the Load Balancer in front of the Master node(s). (a.k.a. Master FQDN)
Finally, you’ll need to provide Kubeconfig. To get this value, open an SSH connection to the Master FQDN. For example, by using Putty, or SSH. (connect to cluster)
Once connected, run this command and copy the output to the clipboard:
kubectl config view --flatten
This will output an un-redacted definition of the cluster connection configuration. The information comes from a file ‘$HOME/.kube/config’. Please note that this is sensitive information. (this is why it’s partly missing from the screendump above…) It can be used to access and configure the cluster. Not just from VSTS, but from anywhere.
During a release, VSTS will configure the tool kubectl on the Build/Release agent using this information.
Paste the entire file content into the field Kubeconfig. Click ‘OK’ to save the new endpoint.
In your Release definition, you can start using this Endpoint. Use the Kubernetes template, to get a quick start.
In the field Kubernetes Service Connection select the Endpoint you have just created. The Task will be able use the tool kubectl, combined with the configuration, to manage your Kubernetes cluster.
One example is to run the command ‘apply’. This command can be used for many things, one of which is to deploy a container to the cluster.
kubectl apply -f k8s.yml
In this example I’m applying the desired configuration described in a YAML file I created, called ‘k8s.yml’. To do this, configure the task like this:
You can also query information from the cluster. For example, you can query the current state of a deployed service called ‘svc-api-gateway’ by using this command:
kubectl get service svc-api-gateway -o json
The output of this command is configured to be in JSON format. It will look similar to this:
To do this in VSTS, configure the task like this:
The output returned from the command, is copied to the specified variable. This enables you to use it in later Tasks. To use the output in PowerShell, I first needed to copy the content of the variable to a file. This can be done by using a tokenize Task and an empty file that holds a token with the same variable name as you used in Output variable name.