Kubernetes 1.18 环境搭建实战(ipvs篇)

 

使用 kubeadm 搭建Kubernetes 1.18单节点集群实战(基于Oracle Linux 8.1)

 

详细步骤:

 

1,把IP地址改为192.168.56.118,然后修改hosts

 

sudo su -

 

vi /etc/hosts

 

追加enp0s8的ip和hostname,例如

 

192.168.56.118 k8s118-master

 

hostnamectl set-hostname k8s118-master

 

2,创建install-k8s.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-k8s.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 <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k

 

然后执行

 

source ~/.bashrc

 

这样就可以用k代替kubectl命令了。

 

恭喜,kubernetes 1.18.0正常安装完成。

 

你可能感兴趣的:(Kubernetes,kubernetes1.18,kubernetes,1.18,kubernetes)