前提条件
以下はKubernetes 1.24のインストール手順を記録したものです。主にCalicoをネットワークプラグイン、Kong Ingressをルーティング転送、Nginxを四層プロキシとして使用します。
サーバー環境
- 2台のTencent Cloudサーバー
- 2コア2GB構成
デプロイメント手順
- hostsファイルの修正
vi /etc/hosts
[マスターIP] master
[ノード1IP] node1
- ホスト名の変更
hostnamectl set-hostname master
hostnamectl set-hostname node1
- ファイアウォールポートの開放
calico 179
k8s API 6443
ノードからマスターへのPodアクセスポート 10250
2379 / 4789
- swap領域の無効化
# スワップ領域の状態確認
sudo free -m
# 一時的に無効化
sudo swapoff -a
# 永続的に無効化: /etc/fstab内のswap行をコメントアウト
sudo sed -i 's/.*swap.*/#&/' /etc/fstab
- SELinuxの無効化
# 一時的に無効化
setenforce 0
# 永続的に無効化
sudo sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
# またはファイルを直接編集: /etc/selinux/config
SELINUX=disabled
- ファイアウォールの停止
systemctl stop firewalld
systemctl disable firewalld
- ブリッジトラフィックのiptables検査を許可
# カーネルIPv4転送を有効にするにはbr_netfilterモジュールが必要なので、ロードします
modprobe br_netfilter
# 設定ファイルを作成
vi /etc/sysctl.d/k8s.conf
# ファイルに以下の内容を追加:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
# 設定を適用
sysctl -p /etc/sysctl.d/k8s.conf
- Dockerのインストール
Alibaba Cloudリポジトリを使用:
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Dockerのインストール:
yum -y install docker-ce docker-ce-cli containerd.io
加速器の設定とDockerドライバーの変更:
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ovj8fg0z.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
サービスの再起動と自動起動設定:
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
- Containerdのインストール
# Containerd 1.5.5を使用します
# ダウンロード
wget https://github.com/containerd/containerd/releases/download/v1.5.5/cri-containerd-cni-1.5.5-linux-amd64.tar.gz
# 圧縮ファイルをシステムディレクトリに展開
tar -C / -xzf cri-containerd-cni-1.5.5-linux-amd64.tar.gz
# PATH環境変数に/usr/local/binと/usr/local/sbinを追加
export PATH=$PATH:/usr/local/bin:/usr/local/sbin
# 立即反映
source ~/.bashrc
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
vi /etc/containerd/config.toml
# 以下の設定を変更
SystemdCgroup = true
sandbox_image = "registry.aliyuncs.com/k8sxio/pause:3.5"
# 設定の再読み込み
systemctl daemon-reload
systemctl enable containerd --now
# 動作確認
ctr version
- Yumリポジトリの指定
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
インターネットアクセスができない場合は、Alibaba Cloudリポジトリを使用:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
- kubeadm、kubelet、kubectlのインストール
# --disableexcludesでkubernetes以外のリポジトリを除外
yum makecache fast
yum install -y kubeadm-1.24.2 kubelet-1.24.2 kubectl-1.24.2 --disableexcludes=kubernetes
kubeadm version
# マスターノードのkubeletを自動起動に設定
systemctl enable --now kubelet
- Kubernetesクラスタの初期化
kubeadm init \
--apiserver-advertise-address=[マスターIP] \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.24.2 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=all \
| tee kubeadm-init.log
- 内部ネットワークの接続確認とノード参加
# マスターノードでkubeadm initが成功した後に生成されるjoinコマンドを他のノードで実行
kubeadm join [マスターIP]:6443 --token [トークン] --discovery-token-ca-cert-hash [ハッシュ値]
- 一般的なエラー
The connection to the server localhost:8080 was refused - did you specify the right host or port?
解決策:
# ノードからマスターのadmin.confをコピー
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
echo "source <(kubectl completion bash)" >> ~/.bash_profile
Calicoのインストール
wget -k https://docs.projectcalico.org/manifests/calico.yaml
kubectl apply -f calico.yaml
kubectl get po -n kube-system
インストールの確認。ネットワークモードの問題やクラウドプロバイダーのポート開放不足により、ノードが準備状態にならない場合があります。
解決策
デプロイ後、kube-proxyのConfigMap内のmodeをipvsに変更します。クラウド環境を使用する場合は179ポートを開放してください。
- name: IP_AUTODETECTION_METHOD
value: "interface=eth0"
Kong Ingressのインストール
wget https://raw.githubusercontent.com/Kong/kubernetes-ingress-controller/master/deploy/single/all-in-one-dbless.yaml
kubectl apply -f all-in-one-dbless.yaml
kubectl get po -n kong
インストールの確認
NginxプロキシをKong Ingressに転送する設定
主にNginxで四層転送を設定し、Kong Ingressで七層ルーティングを行います。
mkdir /home/work
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz && cd pcre-8.35
./configure --prefix=/home/work/pcre
make && make install
pcre-config --version
yum install nginx -y
nginx -V # 現在のnginxモジュール確認
systemctl start nginx.service # 起動
systemctl stop nginx.service # 停止
systemctl restart nginx.service # 再起動
systemctl reload nginx.service # 設定再読み込み
systemctl enable nginx.service # 自動起動設定
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz && cd nginx-1.20.1
mv /usr/sbin/nginx /usr/sbin/nginx.bak
cp -r /etc/nginx /etc/nginx.bak
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake pcre-devel nano wget gcc gcc-c++ ncurses-devel perl redhat-rpm-config.noarch
./configure --with-stream --prefix=/home/work/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/home/work/pcre-8.35
make && make install
/home/work/nginx/sbin/nginx
/home/work/nginx/sbin/nginx -s reload
stream {
server {
listen 80;
proxy_connect_timeout 60s;
proxy_timeout 1800s;
proxy_next_upstream on;
proxy_next_upstream_tries 3;
proxy_next_upstream_timeout 10s;
proxy_pass tcp_80;
}
upstream tcp_80 {
hash $remote_addr consistent;
server node1:30080 max_fails=3 fail_timeout=15s;
# check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
}
server {
listen 443;
proxy_connect_timeout 60s;
proxy_timeout 1800s;
proxy_next_upstream on;
proxy_next_upstream_tries 3;
proxy_next_upstream_timeout 10s;
proxy_pass tcp_443;
}
upstream tcp_443{
hash $remote_addr consistent;
server node1:30443 max_fails=3 fail_timeout=15s;
# check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
}
}
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
include stream.conf;
}
最終テスト
1. YAMLファイルの作成とリソースの定義
vim nginx-test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: preview
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx-container
image: nginx:1.17.1
ports:
- containerPort: 80
kind: Service
metadata:
name: nginx-service
namespace: preview
spec:
selector:
app: nginx-pod
type: ClusterIP
ports:
- port: 80
targetPort: 80
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-nginx
namespace: preview
annotations:
konghq.com/strip-path: "true"
kubernetes.io/ingress.class: kong
spec:
rules:
- host: "example.com"
http:
paths:
- pathType: Prefix
path: /api
backend:
service:
name: nginx-service
port:
number: 80
2. リソースの作成と確認
kubectl apply -f nginx-test.yaml
kubectl get po -n preview
kubectl get svc -n preview
kubectl get ing -n preview
curl example.com/api