Time to time I need to access resources outside of your Kubernetes cluster. It can be some .net framework app that I can`t put in linux container or shared db, like SQL server.
We can easily access resources in kube via kubefwd or ingress\exposed port. However, that doesn't work backwards. Services in k8s cluster can`t just access your machine, they need to know VM`s host IP.
The main point here is finding an ip of the host machine:
In order to run in from powershell we need to escape quotes:
What can it be use it for?
I can setup that ip in PODS connection string but I don’t want to update every pod when my ip changes (a typical problem with hyberV).
It would be much better to reference resources by dns name and let dns service decide how to resolve that resource.
So let’s make a ‘dns-service’ that will redirect us to host.
What we`ll need is:
2)Make a template of a service:
Xip.io provides wildcard dns to ip. Yes its public and runs outside so if you want security- deploy your own wildcard proxy into cluster.
We don’t want to change ip manually, a simple replace should work (or convert it to helm)
(Get-Content $PSScriptRoot'\masterhost.template.yaml').replace('{IP}', $hostIp) | Set-Content $PSScriptRoot'\masterhost.yaml'
All together in one script:
In my connection string I can use masterhost.shared and if ip changes update just masterhost service.
2) Check your firewall and if necessary create an inbound rule for port 1433
3) Verify you can connect to host IP from minikube
We can easily access resources in kube via kubefwd or ingress\exposed port. However, that doesn't work backwards. Services in k8s cluster can`t just access your machine, they need to know VM`s host IP.
The main point here is finding an ip of the host machine:
$ route -n | grep ^0.0.0.0 | awk "{
print \$2 }"
172.17.8.1
172.17.8.1
In order to run in from powershell we need to escape quotes:
minikube ssh 'route -n | grep ^0.0.0.0 | awk \"{
print \$2 }\"'
172.17.8.1What can it be use it for?
I can setup that ip in PODS connection string but I don’t want to update every pod when my ip changes (a typical problem with hyberV).
It would be much better to reference resources by dns name and let dns service decide how to resolve that resource.
So let’s make a ‘dns-service’ that will redirect us to host.
What we`ll need is:
- grab new ip
- update service.yaml
- deploy it
2)Make a template of a service:
# creates fake dns minikube-host to actual host Ip
apiVersion: v1
kind: Service
metadata:
name: masterhost
namespace: shared
spec:
type: ExternalName
# externalName should be dns name, not ip, so as a workaround we use http://xip.io
externalName: {IP}.xip.io
Xip.io provides wildcard dns to ip. Yes its public and runs outside so if you want security- deploy your own wildcard proxy into cluster.
We don’t want to change ip manually, a simple replace should work (or convert it to helm)
(Get-Content $PSScriptRoot'\masterhost.template.yaml').replace('{IP}', $hostIp) | Set-Content $PSScriptRoot'\masterhost.yaml'
3) kubectl apply -f minikube-host.yaml
All together in one script:
$hostIp = minikube ssh 'route -n | grep ^0.0.0.0 | awk \"{ print \$2 }\"'
(Get-Content $PSScriptRoot'\masterhost.template.yaml').replace('{IP}', $hostIp) | Set-Content $PSScriptRoot'\masterhost.yaml'
kubectl apply -f $PSScriptRoot'\masterhost.yaml'
In my connection string I can use masterhost.shared and if ip changes update just masterhost service.
Troubleshooting access to host from pod (SQL Server as an example)
1) Make sure your service is binding to all ips and you can access server just by ip\machine name. For SQL: Run SQLServerManager12.msc (For SQL 2014) and follow steps https://stackoverflow.com/a/11921896/603622 (don’t forget to restart server)2) Check your firewall and if necessary create an inbound rule for port 1433
3) Verify you can connect to host IP from minikube
minikube ssh
$ route -n | grep ^0.0.0.0 | awk \"{ print \$2 }\"
172.17.8.1 1433
$ telnet 172.17.8.1 1433
4) Verify you can connect to host by IP or masterhost.shared from some running pod. SSH to pod$ apt-get install telnet $ telnet 172.17.8.1 1433 $ telnet masterhost.shared 1433
Comments
Post a Comment