Kubernetes 1.18環境構築実戦(ipvs編)



kubeadmを使用してKubernetes 1.18単一ノードクラスタを構築する実戦(Oracle Linux 8.1ベース)
 
詳細手順:
 
1、IPアドレスを192.168.56.118に変更し、hostsを変更します.
 
sudo su -

 
vi /etc/hosts

 
enp 0 s 8のipとhostnameを追加し、例えば
 
192.168.56.118 k8s118-master

 
hostnamectl set-hostname k8s118-master

 
2,install-k 8 s.shを作成する
 
export PUBLIC_IP=YOUR_PUBLIC_IP # or DNS_NAME of YOUR_PUBLIC_IP

 
vi install-public-k8s.sh

 
#!/bin/bash

echo "Start"

dnf config-manager --set-enabled ol8_u1_baseos_base
dnf config-manager --set-enabled ol8_addons
dnf remove podman -y

export IP_ADDR=$(ip addr show enp0s8 | grep -Po 'inet \K[\d.]+')
echo $IP_ADDR

sudo su - << FOE

# Stop firewall and selinux
sudo systemctl disable --now firewalld
sudo /usr/sbin/setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config

# Ignore Swap Error while installing kubernetes cluster with Swap
cat< /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS=--fail-swap-on=false
EOF

# Install neccessary system tools
sudo yum install -y dnf-utils

# Open ipvs
cat </etc/sysconfig/modules/ipvs.modules
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF

sudo chmod 755 /etc/sysconfig/modules/ipvs.modules
sudo bash /etc/sysconfig/modules/ipvs.modules
sudo lsmod | grep -e ip_vs -e nf_conntrack_ipv4
sudo dnf install ipset ipvsadm -y

# Config iptables
echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf
cat< /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sudo modprobe br_netfilter
sudo sysctl --system

# Add Docker Repo
sudo dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Install Docker-CE
sudo dnf makecache timer
sudo dnf -y install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
sudo dnf -y install docker-ce
#sudo dnf -y install --nobest docker-ce
# Enable Docker
sudo systemctl enable --now docker
# Config Docker
if [ ! -d "/etc/docker" ]; then
  mkdir /etc/docker
fi

cat< /etc/docker/daemon.json
{
   "exec-opts": ["native.cgroupdriver=systemd"],
   "log-driver": "json-file",
   "log-opts": {
     "max-size": "100m"
   },
   "storage-driver": "overlay2",
   "storage-opts": [
     "overlay2.override_kernel_check=true"
   ],
   "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
EOF

sudo systemctl daemon-reload
sudo systemctl restart docker

# Add Kubernetes Repo
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

sudo dnf install -y kubeadm kubectl kubelet
sudo systemctl enable kubelet

cat < kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: $IP_ADDR
  bindPort: 6443
---
apiServer:
  timeoutForControlPlane: 4m0s
  certSANs:
  - ${PUBLIC_IP}
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
    serverCertSANs:
    - $PUBLIC_IP
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.18.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.1.0.0/16
  podSubnet: 10.244.0.0/16
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs
EOF

# Create Kubernetes Cluster
#kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=$IP_ADDR --kubernetes-version stable-1.18 --ignore-preflight-errors=Swap --image-repository registry.aliyuncs.com/google_containers --config kubeadm-config.yaml
kubeadm init --config kubeadm-config.yaml --ignore-preflight-errors=Swap

FOE

sleep 10s

# Add User to docker group
sudo usermod -a -G docker $(id -nu)

# Create .kube folder
if [ -f $HOME/.kube/config ]; then
  rm -rf $HOME/.kube/config
fi

if [ ! -d $HOME/.kube ]; then
  mkdir $HOME/.kube
fi

# Copy Kubernetes config file
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# Apply network plugin
result=1
while [ $result -ne 0 ]
do
#https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
        curl https://docs.projectcalico.org/v3.10/manifests/calico.yaml -O
        sed -i -e "s?192.168.0.0/16?10.244.0.0/16?g" calico.yaml
        kubectl apply -f calico.yaml
        result=$?
        sleep 20s
done

# Taint master node
kubectl taint nodes --all node-role.kubernetes.io/master-

echo "Complete"

 
3,install-public-k 8 s.shを実行
 
chmod +x install-public-k8s.sh
./install-public-k8s.sh

 
4,nodesを表示する
 
kubectl get nodes

 
結果
 
NAME            STATUS   ROLES    AGE    VERSION
k8s118-master   Ready    master   6m8s   v1.18.0

 
5、使用したpodsを表示
 
kubectl get pods -A

 
結果
 
NAMESPACE     NAME                                      READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-dc4469c7f-gqmlg   1/1     Running   0          6m16s
kube-system   calico-node-gqsnq                         1/1     Running   0          6m16s
kube-system   coredns-7ff77c879f-d2zfv                  1/1     Running   0          6m16s
kube-system   coredns-7ff77c879f-fksml                  1/1     Running   0          6m16s
kube-system   etcd-k8s118-master                        1/1     Running   0          6m29s
kube-system   kube-apiserver-k8s118-master              1/1     Running   0          6m29s
kube-system   kube-controller-manager-k8s118-master     1/1     Running   0          6m29s
kube-system   kube-proxy-zt6mf                          1/1     Running   0          6m16s
kube-system   kube-scheduler-k8s118-master              1/1     Running   0          6m29s

 
6,nginx deploymentを発行し,検証を行う.
 
パブリッシュ
 
kubectl create deployment nginx --image=nginx

 
deploymentsの表示
 
kubectl get deployments

 
deploymentsの結果の表示
 
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           17s

 
podsの表示
 
kubectl get pods -o wide

 
podsの結果の表示
 
NAME                    READY   STATUS    RESTARTS   AGE   IP             NODE            NOMINATED NODE   READINESS GATES
nginx-f89759699-49zrf   1/1     Running   0          34s   10.244.76.68   k8s118-master              

 
curlでnginxアプリケーションにアクセスする(IPはpodsの結果を表示するIP値)
 
curl 10.244.0.4

 
curlの結果
 



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.


 
7,操作の便宜上、~/.bashrcに以下の内容を加える
 
source 

 
そして実行
 
source ~/.bashrc

 
これでkubectlコマンドの代わりにkを使用できます.
 
おめでとうございます.kubernetes 1.18.0の正常なインストールが完了しました.