Zar Ashouri
Zar Ashouri

Reputation: 184

Containers within kubernetes deployment do not use hostNetwork

I've got familiar with kubernetes very recently. I want to deploy a project into my minikube cluster. Here is the yaml file:

deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: project
  name: project
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      run: project
  template:
    metadata:
      labels:
        run: project
    spec:
      hostNetwork: true
      containers:
      - name: container1
        image: image1
        imagePullPolicy: Never
      - name: container2
        image: image2
        imagePullPolicy: Never

I created the deployment successfully, and both containers in a pod are up and running but these containers can not connect to local resources like databases(e.g. redis)

I get logs via this command and the resut is as below.

kubectl logs -f project-5f5c6df6bc-q82s5 container1

Error 111 connecting to 127.0.0.1:6379. Connection refused.

Upvotes: 1

Views: 512

Answers (1)

Arnaud Develay
Arnaud Develay

Reputation: 3970

I think you can use a Service of type ExternalName with the special hostname provided by Minikube: https://minikube.sigs.k8s.io/docs/handbook/host-access/#hostminikubeinternal This exposes an external Service that can be consumed from Kubernetes.

kind: Service
apiVersion: v1
metadata:
  name: redis
spec:
  type: ExternalName
  externalName: host.minikube.internal

In the code that runs in the cluster simply use the host redis so that Kubernetes can retrieve the mapping to the desired host.

Upvotes: 2

Related Questions