フラミナル

考え方や調べたことを書き殴ります。IT技術系記事多め

kubeadmで入れたkube-proxyの設定を変更する

Kubernetes

kubeadmでkubernetesをインストールするとkube-proxyはpodとして起動します。

そのため手動でインストールした時と異なり/var/lib/kube-proxy/config.confを使えないため他の方法を行う必要があります。

方法

方法としては現在動いているpodの設定を書き換え、podを削除し再作成する必要があります。

設定変更を行う

kubectl -n kube-system edit cm kube-proxy
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  config.conf: |-
    apiVersion: kubeproxy.config.k8s.io/v1alpha1
    bindAddress: 0.0.0.0
    clientConnection:
      acceptContentTypes: ""
      burst: 0
      contentType: ""
      kubeconfig: /var/lib/kube-proxy/kubeconfig.conf
      qps: 0
    clusterCIDR: 192.168.20.0/24
    configSyncPeriod: 0s
    conntrack:
      maxPerCore: null
      min: null
      tcpCloseWaitTimeout: null
      tcpEstablishedTimeout: null
〜〜〜

このようにkube-proxyの設定をYAML形式で確認することができます。今回はnodeportにアサインするIPアドレスの範囲を制限してみましょう。

対象の設定は公式サイトを確認してください。注意点としては完全に同じ文言ではない点に気をつけてください。(podの設定と通常のプロセスの設定とで微妙に異なっている)

--nodeport-addresses stringSlice A string slice of values which specify the addresses to use for NodePorts. Values may be valid IP blocks (e.g. 1.2.3.0/24, 1.2.3.4/32). The default empty string slice ([]) means to use all local addresses.

--nodeport-addressesの設定を変更すれば良さそうですね。

podの設定はこちらです。

mode: ""
    nodePortAddresses: null
    oomScoreAdj: null

これを以下のように「:wq!で保存」します。配列指定なので気をつけましょう。

mode: ""
    nodePortAddresses: ["192.168.10.0/24"]
    oomScoreAdj: null

設定の反映を行う

設定の反映を行うにはpodを一度削除する必要があります。

kubectl get pods -A |grep proxy
kube-system   kube-proxy-9t8zx                           1/1     Running   0          14m
kube-system   kube-proxy-wv697                           1/1     Running   0          15m

この2つを削除します。名前は毎回変わるので変更してください。

kubectl delete pod --namespace=kube-system kube-proxy-9t8zx
kubectl delete pod --namespace=kube-system kube-proxy-wv697

するとkubernetesが勝手に検知し再起動してくれます。

kubectl get pods -A |grep proxy
kube-system   kube-proxy-g9fkt                           1/1     Running   0          10s
kube-system   kube-proxy-tmr2w                           1/1     Running   0          10s

確認します。

$ kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP        8h
nginx        NodePort    10.104.9.71   <none>        80:30405/TCP   143m

$ curl 192.168.10.1:30405
  
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>