For some cases, a private and easily accessible Visual Studio instance may be a good idea. Almost all cloud providers offer a managed Kubernetes service. Today I am going to use DigitalOcean service, but the process should be very similar for other cloud providers. On this occasion, we are going to create a cluster with a single node. You can provision a Kubernetes cluster on the DigitalOcean website or you can use the corresponding Terraform provider. Once the cluster is created, locate the configuration file and download it.
1) Creating the Kubernetes cluster with Terraform
As mentioned before, DigitalOcean has a provider for Terraform. Following the documentation and to keep things simple, I created a single .tf file to call terraform. Some notes about that file:
- The “do_token” for the “token” argument is the DigitalOcean token. This can be obtained on the website under the API tab. When creating it, be sure to assign it Write permissions.
- We are always selecting the latest available version of Kubernetes to create the cluster.
- The rest of the values are pretty straightforward and well-documented in the provider. For the size of the nodes, the smaller ‘droplet’ you can use is 1 vcpu and 2gb of RAM (s-1vcpu-2gb).
- The output is going to have the kubeconfig required to connect the cluster.
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
variable "do_token" {}
provider "digitalocean" {
token = var.do_token
}
data "digitalocean_kubernetes_versions" "version" {}
resource "digitalocean_kubernetes_cluster" "cluster" {
name = "k8s-vs-app"
region = "sfo3"
version = data.digitalocean_kubernetes_versions.version.latest_version
node_pool {
name = "worker-pool"
size = "s-2vcpu-4gb"
auto_scale = false
node_count = 1
}
}
output "kubeconfig" {
value = resource.digitalocean_kubernetes_cluster.cluster.kube_config.0.raw_config
sensitive = true
}
output "cluster-url" {
value = resource.digitalocean_kubernetes_cluster.cluster.endpoint
}
To apply that .tf file, you can use the following commands from the CLI:
# to initialize the provider
terraform init
# to check if everything is right
terraform plan -var="do_token=your_token"
# to apply the changes
terraform apply -auto-approve -var="do_token=your_token"
2) Connecting to your k8s cluster
To connect to the target cluster, we are going to use kubectl, so be sure to have it installed on your system. The first step will be to set the path of the kubeconfig file, which you should be able to download from the DigitalOcean website. If you created the cluster with Terraform file included in the last step, your kubeconfig contents are part of the Terraform output and you can see them by running the following command (your terminal should be in the same path where you created your .tf file):
terraform output -raw kubeconfig
To create your kubeconfig file, simply pipe the terraform output to a new .yaml file:
terraform output -raw kubeconfig >> k8s-config.yaml
Now, just set the KUBECONFIG enviromental variable to point to k8s-config.yaml. If you downloaded the kubeconfig file from the website, be sure to include the absolute path of your configuration file:
export KUBECONFIG=$(pwd)/k8s-config.yaml
Keep in mind, you can also invoke kubectl with the –kubeconfig parameter.
3) The Visual Studio Container Image
After searching on Docker Hub, I found this docker image “kasmweb/vs-code” from a ‘Verified Publisher’ that seems to be stable and open-source. After reviewing the documentation, I was able to gather all the required information to deploy it with k8s.
4) Creating and applying a deployment file
For the deployment we are going to use the vs-code:1.13.1-rolling image. Port 6901 will be exposed in the configuration and the VNC_PW variable will allow us to set our password to log in to the Visual Studio instance:
apiVersion: apps/v1
kind: Deployment
metadata:
name: vs-web-deployment
labels:
app: vs-web-app
spec:
replicas: 1
selector:
matchLabels:
app: vs-web-app
template:
metadata:
labels:
app: vs-web-app
spec:
containers:
- name: vs-web-app
image: kasmweb/vs-code:1.13.1-rolling
ports:
- containerPort: 6901
env:
- name: VNC_PW
value: "password"
Apply the deployment with the following command:
kubectl apply -f vs-deployment.yaml
5) Creating and applying a service file
We are going to create a service for Visual Studio, including a DigitalCloud k8s load balancer. This will allow us to obtain an public IP address to access Visual Studio. As expected, we are referencing the same containers indicated in the deployment and the corresponding ports:
apiVersion: v1
kind: Service
metadata:
name: vs-web-service
labels:
app: vs-web-app
spec:
type: LoadBalancer
selector:
app: vs-web-app
ports:
- name: https
protocol: TCP
port: 6901
targetPort: 6901
Apply the service with the following command:
kubectl apply -f vs-service.yaml
6) Accessing The Visual Studio Instance
Once your deployment and services files are applied, you can run kubectl get pods to see the current status of the containers and kubectl get services to check the external IP to access Visual Studio. While the load balancer is being created you will see ‘Pending’ in the External-IP property of the service. When you see the external IP, go to your browser and paste it:
https:\\your_external_ip:6901
Be sure to include the https:\\. The first thing you should see is a prompt asking for the user and password:
The user will be kasm_user and the password the one you included in the deployment file. Default: password. The next screen will be Visual Studio: