kubeadm一键部署K8S流程

-架构设计:
k8s部署:(master+node1+node2)
master.com:192.168.17.144
node1.com:192.168.17.146
node2.com:192.168.17.148

master上部署:
etcd(数据库)、kube-apiserver、kube-controller-manager、kubectl、kubeade、
kubelet、kube-proxy、flannel(这个是用于服务节点通讯)

node上部署:
kubectl、kubelet、kube-proxy、flannel、docker


三台机器上同时部署:
vim /etc/hosts
192.168.17.144 master.com
192.168.17.146 node1.com
192.168.17.148 node2.com

完成后ping域名测试连通

如果在正实的外网环境上,例如阿里云ECS上要放以下端口
master:TCP:6443 2379 2380 60080 60081
node:UDP端口全部要开放

设置防火墙(三台机器上同时部署):
systemctl stop firewalld NetworkManager
systemctl disable firewalld NetworkManager

sed -i “s/SELINUX=enforcing/SELINUX=disabled/g” /etc/selinux/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
getenforce 0

iptables -F
iptables -X
iptables -Z

iptables -P FORWARD ACCEPT

禁用swap功能(三台机器上同时部署)
swapoff -a
#防止开机挂载swap分区,命令意思就是行首加上注释
sed -i ‘/ swap / s/^(.*)$/#\1/g’ /etc/fstab

yum源配置(三台机器上同时部署)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i ‘aliyuncs/d’ /etc/yum.repo.d/*.repo
yum clean all && yum makecache fast

ntp时间服务器配置(三台机器上同时部署)
yum install chrony -y
systemctl start chronyd
systemctl enable chronyd

改配置文件 加入一行
vim /etc/chrony.conf
server ntp.aliyun.com iburst
hwclock -w

修改linux内核参数,因为用的是iptables转发数据
vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
vm.max_map_count=262144

modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

安装docker环境(三台机器上同时部署)
yum remove docker docker-common docker-selinux docker-engine -y
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum list docker-ce --showduplicates

yum install docker-ce-19.03.15 docker-ce-cli-19.03.15 -y

创建或修改 /etc/docker/daemon.json 文件,修改为如下形式
{
“registry-mirrors” : [“https://ms9g1x6x.mirror.aliyuncs.com”],
“exec-opts”:[“native.cgroupdriver=systemd”]
}

systemctl start docker && systemctlenable docker

安装k8s的初始化工具kubeadm命令(三台机器上同时部署)
curl -o /etc/yum.repos.d/Centos-7.repo http://mirror.aliyun.com/repo/Centos-7
curl -o /etc/yum.repos.d/docker.repo http://mirror.aliyun.com/docker-ce/linux/centos/docker-ce.repo

vim /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.gpd

yum clean all && yum makecache

yum list kubeadm --showduplicates

yum install kubelet-1.19.3 kubeadm-1.19.3 kubectl-1.19.3 ipvsadm

查看kubeadm版本
kubeadm version
开机启动
systemctl enable kubelet
systemctl enable docker

初始化k8s master主节点(只在主节点master上执行)
kubeadm init
–apiserver-advertise-address=192.168.17.144
–image-repository registry-aliyuncs.com/google_containers
–kubernetes-version v1.19.3
–service-cidr=192.168.17.0/24 \ #k8s服务发现网段地址
–pod-network-cidr=192.168.100.0/24 \ #设置pod后的运行网段地址
–service-dns-domain=cluster.local
–ignore-preflight-errors=Swap
–ignore-preflight-errors=NumCPU

根据初始化的最后信息,会有三行,复制后只在master上执行一下:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown ( i d − u ) : (id -u): (idu):(id -g) $HOME/.kube/config

netstat -tunlp 查看端口是否都开了

kubectl get nodes -o wide #查看节点信息

根据初始化的最后信息,会有三行token信息,复制到各node节点上执行一下:
kubeadm join 192.168.17.144:6443 --token … sha256:asfasfasfasfasff4a6f4a6f4asdf4

kubectl get nodes -owide #再查看节点信息

在master上安装flannel网络插件

下载
git clone --depth 1 https://github.com/coreos/flannel.git

改配置文件(网段和前面的设置的pod网段对应上):
vim /root/flannel-master/Documentation/kube-flannel.yml
net-conf.json |
{
“network”:“192.168.100.0/24”,
“Backend”:{“type”:“vxlan”}
}

接着改第二个地方:
使的跨主机容器通讯,指定用哪块网卡指定:
containers:

  • name:kube-flannel
    #image: flannelcni/flannel:v0.19.2 for ppc64le and mips64le (docker…)
    image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.2
    commang:
  • /opt/bin/flanneld
    args:
  • –ip-masq
  • –kube-subnet-mgr
  • –iface=ens33

k8s命令补全:
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo “source <(kubectl completion bash)” >> ~/.bashrc

验证
kubectl get nodes -o wide

创建pod
/root/flannel-master/Documentation/当前目录位置
kubectl create -f ./kube-flannel.yml

/root/flannel-master/Documentation/当前目录位置
建立一个nginx
kubectl run linux0024-pod-1-nginx --image=nginx:1.14.1

你可能感兴趣的:(linux,kubernetes,容器,云原生,linux)