Kubenetes安装部署

基本环境准备

主机名 IP 角色
k8s-master-server1 8.8.8.148 master
k8s-node-server2 8.8.8.149 node

设置主机名

hostnamectl set-hostname k8s-master-server1

配置Hosts

cat >> /etc/hosts << EOF
8.8.8.148 k8s-master-server1
8.8.8.149 k8s-node-server2
EOF

配置基础yum源

\rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache fast
yum install telnet vim tree -y

禁用swap

swapoff -a
vim /etc/fstab

禁用Selinux

sed -i s#SELINUX=enforcing#SELINUX=disabled#g /etc/selinux/config
setenforce 0

禁用firewalld及iptables

yum install iptables-services -y
systemctl stop firewalld;systemctl disable firewalld
systemctl disable iptables

配置iptables转发

cat >> /etc/sysctl.d/kubenetes.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

配置时间同步

systemctl start chronyd;systemctl enable chronyd
systemctl stop postfix;systemctl disable postfix

配置IPVS

vim /etc/sysconfig/modules/ipvs.modules

#!/bin/bash

ipvs_mods_dir="/usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs"

for i in $(ls $ipvs_mods_dir | grep -o "^.*"); do
	
	/sbin/modinfo -F filename $i &> /dev/null
	
	if [ $? -eq 0 ]; then
		/sbin/modprobe $i
	fi
	
done


chmod +x /etc/sysconfig/modules/ipvs.modules
/etc/sysconfig/modules/ipvs.modules

配置Docker及Kubenetes源

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cat <<EOF > /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

yum clean all
yum makecache fast
yum update xfsprogs -y

安装kubenetes的Master节点

安装Master节点之前提前做好规划,确定安装的Kubenetes版本,以确定安装受其信任验证过的Docker版本:

查询Kubenetes与Docker版本兼容性方法:

查看Github的该版本的CHANGELOG-xxx.md,在其搜索docker查找相关说明

https://github.com/kubernetes/kubernetes/releases

这里安装:Kubenetes的1.16版本,经查询最新验证支持的为Docker18.09版本

安装指定的Kubenetes版本:

yum list kubelet --showduplicates|sort -r
yum list kubeadm --showduplicates|sort -r
yum list kubectl --showduplicates|sort -r   (node节点不用安装)

yum install kubeadm-1.16.7-0 kubelet-1.16.7-0 kubectl-1.16.7-0
systemctl enable kubelet.service

安装指定版本Docker:

yum list docker-ce --showduplicates|sort -r
yum install docker-ce-18.09.0
vim /usr/lib/systemd/system/docker.service
	ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
systemctl daemon-reload
mkdir /etc/docker/ -p
vim /etc/docker/daemon.json
{
"exec-opts":["native.cgroupdriver=systemd"]
}

systemctl start docker;systemctl enable docker

初始化Master:

## 初始化master,kubeadm reset 重新初始化Master
kubeadm init --kubernetes-version=v1.16.7 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap --image-repository=registry.aliyuncs.com/google_containers   ##将输出的信息保存到一份文件
或:
# kubeadm init --config=kubeproxy.conf

# 获取帮助,导出配置模板然后融合成kubeproxy.conf
>kubeadm config print init-defaults KubeletConfiguration
>kubeadm config print init-defaults KubeletConfiguration|less
>kubeadm config print init-defaults KubeletConfiguration > kubelet.yaml

# cat kubeproxy.conf
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 0.0.0.0
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: k8s1.host.com
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.18.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
clientConnection:
  acceptContentTypes: ""
  burst: 0
  contentType: ""
  kubeconfig: /var/lib/kube-proxy/kubeconfig.conf
  qps: 0
clusterCIDR: ""
configSyncPeriod: 0s
conntrack:
  maxPerCore: null
  min: null
  tcpCloseWaitTimeout: null
  tcpEstablishedTimeout: null
detectLocalMode: ""
enableProfiling: false
healthzBindAddress: ""
hostnameOverride: ""
iptables:
  masqueradeAll: false
  masqueradeBit: null
  minSyncPeriod: 0s
  syncPeriod: 0s
ipvs:
  excludeCIDRs: null
  minSyncPeriod: 0s
  scheduler: ""
  strictARP: false
  syncPeriod: 0s
  tcpFinTimeout: 0s
  tcpTimeout: 0s
  udpTimeout: 0s
kind: KubeProxyConfiguration
metricsBindAddress: ""
mode: "ipvs"
nodePortAddresses: null
oomScoreAdj: null
portRange: ""
showHiddenMetricsForVersion: ""
udpIdleTimeout: 0s
winkernel:
  enableDSR: false
  networkName: ""
  sourceVip: ""


-----------------------------------------------------------------------------------------------------------------------------------------------------
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config


## 安装flannel附件,若系统无法访问quay.io可以替换为 [quay-mirror.qiniu.com]
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl apply -f kube-flannel.yml  ##替换域名后执行

查看Mater的pods信息及支持的namespace

[root@k8s-master-server1 ~]# kubectl get pods -n "kube-system" -o wide
...

[root@k8s-master-server1 ~]# kubectl get node
NAME                 STATUS   ROLES    AGE   VERSION
k8s-master-server1   Ready    master   51m   v1.16.7
k8s-node-server2     Ready    <none>   21m   v1.16.7

[root@k8s-master-server1 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   50m
kube-node-lease   Active   50m
kube-public       Active   50m
kube-system       Active   50m

安装Kubernetes的Node节点

安装docker、kubeadm及kubectl

yum install docker-ce-18.09.9-3.el7 kubeadm-1.16.7-0 kubelet-1.16.7-0
systemctl enable docker.service kubelet.service

vim /usr/lib/systemd/system/docker.service
	ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
systemctl daemon-reload
mkdir /etc/docker -p

vim /etc/docker/daemon.json
{
"exec-opts":["native.cgroupdriver=systemd"]
}

systemctl restart docker

导入Node节点需要的镜像

## 从master节点将下列镜像导出加载到node节点
docker save -o flannel.tar quay-mirror.qiniu.com/coreos/flannel
docker save -o pause.tar registry.aliyuncs.com/google_containers/pause
docker save -o kube-proxy.tar registry.aliyuncs.com/google_containers/kube-proxy

## 在node节点加载下列镜像
docker load -i flannel.tar 
docker load -i pause.tar 
docker load -i kube-proxy.tar 

加入Kubenetes集群

kubeadm join 8.8.8.148:6443 --token lvnfma.y6oa0i6m8e0nm0v8 \
--discovery-token-ca-cert-hash sha256:a64aa5f435ccad0417597ebd588ff3889f68b7bb47505e8b82f1f0ff431416fb

Master节点查看新节点是否加入成功

kubectl get pods -n "kube-system" -o wide