tjtjtjのメモ

自分のためのメモです

kubernetes 学習 hostnetwork

k8sクラスタクラスタの外にdbサーバがあるとする。クラスタとdbは同一ネットワーク上にある。k8sクラスタからdbを参照したい。

192.168.0.1    db
192.168.0.101  master
192.168.0.102  worker
192.168.0.103  worker

クラスタ外の 192.168.0.1 を参照できないことを確認

hello-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
spec:
  containers:
  - name: hello
    image: dockercloud/hello-world
    ports:
    - containerPort: 80

pod 作成

$ kubectl apply -f hello-pod.yaml
pod/hello-pod created

podから192.168.0.1 上のサービスを参照。タイムアウトする。

$ kubectl exec -it hello-pod sh
/ # wget -T 5 192.168.0.1
Connecting to 192.168.0.1 (192.168.0.1:80)
wget: download timed out

クラスタ外 192.168.0.1 を参照する

hostNetwork:true にする

hello-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
spec:
  hostNetwork: true  # <--- here
  containers:
  - name: hello
    image: dockercloud/hello-world
    ports:
    - containerPort: 80

apply 失敗。いったん削除が必要だった

$ kubectl apply -f pod-with-host-network.yaml
The Pod "hello-pod" is invalid: spec: Forbidden: pod updates may not change fields other than `spec.co ntainers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds` or `spec.tolerations`  (only additions to existing tolerations)
:

pod削除->作成。

$ kubectl delete pod hello-pod
pod "hello-pod" deleted
$ kubectl apply -f hello-pod.yaml
pod/hello-pod created

podからクラスタ外192.168.0.1 上のサービスを参照。参照に成功。

$ kubectl exec -it hello-pod sh
/ # wget -T 5 192.168.0.1 -O -
Connecting to 192.168.0.1 (192.168.0.1:80)
<html>
<head>
        <title>Hello world!</title>
: