はじめに
Oracle Cloud Infurastructure には、Kubernetes のマネージドサービス Oracle Container Engine for Kubernetes (OKE) が提供されています。数クリックで Kubernetes の環境が立ち上がってきて、簡単にコンテナワークロードを動作出来るのが特徴の一つです。
OKE を利用するときに、会社のネットワークによっては、直接インターネットに出られない環境の時があります。社内に Proxy サーバーが存在していて、インターネットに出るときは必ず Proxy サーバーを経由する必要がある環境です。Kubernetes を操作するために kubectl コマンドがありますが、Proxy 環境下で動かすことは出来るのでしょうか?自分の環境では正常に動作しましたので、その時の手順を備忘録として載せます。
ポイントは2点あります
- Proxy サーバーとして、Squid を準備。Kubernetes API Server の 6443 Port の Proxy を許可
- kubectl の実行元は、Squid を環境変数に指定
特に1点目のポイントですが、自分で Proxy サーバーを準備したので、6443 Port を許可することができましたが、社内のProxyサーバーは6443の通信が許可されていないこともあると思います。情シス担当にお願いして 6443 Port を許可するか、どこかの踏み台サーバー経由で kubectl コマンドを実行すると良いでしょう。
それでは手順を紹介していきます。
Proxy Server (Squid) の作成
CentOS 7 を適当に準備して、Squid をインストールします
sudo yum -y install squid
一応、バージョンや依存関係も載せておきます
=================================================================================================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================================================================================================
Installing:
squid x86_64 7:3.5.20-15.el7_8.1 updates 3.1 M
Installing for dependencies:
libecap x86_64 1.0.0-1.el7 base 21 k
libtool-ltdl x86_64 2.4.2-22.el7_3 base 49 k
perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 base 32 k
perl-Compress-Raw-Zlib x86_64 1:2.061-4.el7 base 57 k
perl-DBI x86_64 1.627-4.el7 base 802 k
perl-Data-Dumper x86_64 2.145-3.el7 base 47 k
perl-Digest noarch 1.17-245.el7 base 23 k
perl-Digest-MD5 x86_64 2.52-3.el7 base 30 k
perl-IO-Compress noarch 2.061-2.el7 base 260 k
perl-Net-Daemon noarch 0.48-5.el7 base 51 k
perl-PlRPC noarch 0.2020-14.el7 base 36 k
squid-migration-script x86_64 7:3.5.20-15.el7_8.1 updates 50 k
Transaction Summary
=================================================================================================================================================================================================================
Firewalld停止をします。Squid は Oracle Cloud 上に構成しており、Cloud 側で Firewall をかけています。
sudo systemctl stop firewalld
sudo systemctl disable firewalld
Squidの設定を変更します
vim /etc/squid/squid.conf
変更点のみを抜粋して記載します。ポイントは、acl SSL_ports port 443 6443
の部分です。6443 Port (Kubernetes API Server) の通信を許可しています。
# kube api server の port 定義を追加
acl SSL_ports port 443 6443
# Private Subnet の定義
acl my_localnet src 10.0.100.0/24
# Private Subnet の許可
http_access allow my_localnet
# キャッシュしないよう設定
no_cache deny all
# ログのフォーマットを見やすく整形
logformat combined %tl %>a %Ss/%03>Hs %<st %rm %ru %[un %Sh/%<a %mt
access_log /var/log/squid/access.log combined
# プロキシサーバー経由での接続を隠蔽
forwarded_for off
request_header_access Referer deny all
request_header_access X-FORWARDED-FOR deny all
request_header_access Via deny all
request_header_access Cache-Control deny all
Squid サーバーの設定全体も載せておきます。
[root@squid01 ~]# cat /etc/squid/squid.conf
#
# Recommended minimum configuration:
#
# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443 6443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
# Defide for Private Subent
acl my_localnet src 10.0.100.0/24
#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports
# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports
# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager
# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
# http_access deny to_localhost
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost
# Allow for Private Subnet
http_access allow my_localnet
# And finally deny all other access to this proxy
http_access deny all
# Squid normally listens to port 3128
http_port 3128
# Uncomment and adjust the following to add a disk cache directory.
# cache_dir ufs /var/spool/squid 100 16 256
# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid
#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320
# キャッシュしないよう設定
no_cache deny all
# ログのフォーマットを見やすく整形
logformat combined %tl %>a %Ss/%03>Hs %<st %rm %ru %[un %Sh/%<a %mt
access_log /var/log/squid/access.log combined
# プロキシサーバー経由での接続を隠蔽
forwarded_for off
request_header_access Referer deny all
request_header_access X-FORWARDED-FOR deny all
request_header_access Via deny all
request_header_access Cache-Control deny all
[root@squid01 ~]#
Squid を起動します。
systemctl start squid
systemctl enable squid
systemctl status squid
Proxy 配下で kubectl を実行する
環境変数に作成した Proxy サーバーを指定します。このコマンドは一時的な設定なので、恒久的に使いたいときは、bashrc などに追加すると良いでしょう。
export http_proxy=http://10.0.0.2:3128
export https_proxy=http://10.0.0.2:3128
Proxy 設定の動作確認もかねて、kubectl をダウンロードします。正常にダウンロードできていれば、Proxy が動作していることがわかります。
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
実行権限を付与して、PATH配下に移動します。これでインストール完了です。
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
それでは、Proxy 環境下で kubectl コマンドが動作するか確認します。いくつかコマンドを打ってみましょう。
Node の詳細
[opc@kubectl01 ~]$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
10.0.10.2 Ready node 11h v1.17.9 10.0.10.2 <none> Oracle Linux Server 7.8 4.14.35-1902.306.2.el7uek.x86_64 docker://19.3.11
Deployment の作成
[opc@kubectl01 ~]$ kubectl apply -f https://raw.githubusercontent.com/Sugi275/oke-handson/master/deployment-cowweb.yaml
deployment.apps/cowweb created
Pod の動作状況
[opc@kubectl01 ~]$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cowweb-7cb44c5768-7gxv5 1/1 Running 0 41s 10.244.0.5 10.0.10.2 <none> <none>
cowweb-7cb44c5768-xtnn9 1/1 Running 0 41s 10.244.0.4 10.0.10.2 <none> <none>
exec で Pod 内で Shell を起動
[opc@kubectl01 ~]$ kubectl exec -it cowweb-7cb44c5768-7gxv5 -- /bin/sh
~ $
各種コマンド
~ $ hostname
cowweb-7cb44c5768-7gxv5
~ $
~ $ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
3: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 8950 qdisc noqueue state UP
link/ether 12:9e:7b:65:37:72 brd ff:ff:ff:ff:ff:ff
inet 10.244.0.5/25 brd 10.244.0.127 scope global eth0
valid_lft forever preferred_lft forever
~ $
はい、これで Proxy 環境下で、kubectl が動作することが確認できました。
参考URL