Debugging .net core application running in Kubernetes cluster, from visual studio 2017.

Continuing my exploration of containers , i started using Kubernetes(K8) as an orchestrator for container applications. If you want to learn more about K8 you can read here.

On exploration i found similar issue which i faced with docker, there was almost no content available on the approach of how to debug your application running in K8 cluster.

I understand that generally you should use logs and most of the issues related to application should have been identified during development of application. Ideally that is the use of docker , docker image just runs everywhere .

But sometime Murphy may strike , and things can go wrong , to help in those situation this article is written.

Following are the tools used by me for exploration

  1. Kubernetes as a service running on stratoscale.
  2. Docker for windows
  3. Visual studio 2017 version 15.3.4 with  .net core 2.0 sdk  on windows 10 machine.
  4. Kubectl
I am going to use same sample application and docker which i used in this blog.

There are three major steps here -
  1. How pod/ container can be accessed from outside cluster.
  2. How to set up container to allow incoming ssh connection.
  3. How to make Visual Studio connect to container in K8 cluster

 How pod/ container can be accessed from outside cluster.


Service are the kuberenet objects which allow access to K8 pods , more about service can be read here.

i have written a service for my application as below -


 apiVersion: v1  
 kind: Service  
 metadata:  
  name: ankush-dicomlibssh-svc  
  labels:  
   app: ankush-dicomlib  
 spec:  
  type: NodePort  
  ports:  
   - port: 8080  
    nodePort: 30114  
    targetPort: 22  
  selector:  
   app: ankush-dicomlib  

here pod is exposed at cluster wide port 30114 and target port which it will hit on container is 22 nothing but ssh port.

How to set up container to allow incoming ssh connection.

now we connect to container in cluster using keubectl -

 kubectl exec -it podname -- /bin/bash  

once connected to Pod we install ssh and start it -

  apt-get update   
  apt-get install openssh-server   
  mkdir /var/run/sshd   
  chmod 0755 /var/run/sshd   
  /usr/sbin/sshd  

ssh is installed and running under root account but as we do not have password of root we need to assign a public key to ssh authorized keys , keys can be generated using ssh key gen as mentioned here

make sure public key is accessible in pod using volumes or any other approach you seem as ideal.

once key is available go to directory where key is available and run below command -

 cat keyname >> ~/.ssh/authorized_keys  

another step which is needed for visual studio to install vsdbg is to install unzip -

 apt-get install unzip   

once done, exit from shell.

How to make Visual Studio connect to container in K8 cluster

And lastly lets get to visual studio, go to  Tools -> Options -> Crossplatform -> Connection manager


lets add a connection to K8 cluster as showed in below image-



here in private key file add same private key file which you generated earlier using ssh key gen along with same passphrase.

once done lets attach to process -


then select the process and tadannnnnnnnnn-





Comments

Post a Comment

Popular posts from this blog

Debugging .net core application running in docker container, from visual studio 2017, in a remote linux machine.

Self Contained Deployment of .Net core Console Application on Ubuntu