本文主要是线上服务器的Kubernetes安装,初始化节点时,部分镜像需要科学上网才能下载;离线安装请看另一篇文章

离线安装Kubernetes:https://blog.csdn.net/weixin_45456679/article/details/123654914

安装依赖包

yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl systat libseccomp wget vim net-tools git iptables-services

关闭防火墙,为iptables设置规则

systemctl stop firewalld && systemctl disable firewalld && systemctl status firewalld
systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save

关闭SWAP 和 SELINUX

swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

调整内核参数,对于k8s

cat > /etc/sysctl.d/kubernetes.conf << EOF
net.bridge.bridge-nf-call-iptables=1   #开启网桥模式
net.bridge.bridge-nf-call-ip6tables=1   #开启网桥模式
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0        #禁止使用 swap 空间, 只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1    #不检查物理内存是否够用
vm.panic_on_oom=0         #开启OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1   #关闭IPV6协议
net.netfilter.nf_conntrack_max=2310720
EOF

sysctl -p /etc/sysctl.d/kubernetes.conf

调整系统时区

# 设置系统时区为 中国/上海
timedatectl set-timezone Asia/Shanghai

关闭系统不需要服务,postfix是邮件服务

systemctl stop postfix && systemctl disable postfix

设置rsyslogd 和 systemd journald

# 创建持久化保存日志目录
mkdir -p /var/log/journal
# 创建配置文件存放目录
mkdir -p /etc/systemd/journald.conf.d

# 创建配置文件
cat > /etc/systemd/journald.conf.d/99-prophet.conf << EOF
[Journal]
#持久化保存到磁盘
Storage=persistent
#压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
#最大占用空间10G
SystemMaxUse=10G
#单日志文件最大200M
SystemMaxFileSize=200M
#日志保存时间2周
MaxRetentionSec=2week
#不将日志转发到syslog
ForwardToSyslog=no
EOF

# 重启journald
systemctl restart systemd-journald

kube-proxy开启ipvs的前置条件

modprobe br_netfilter

cat > /etc/sysconfig/modules/ipvs.modules << EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

安装 Docker 软件

https://blog.csdn.net/weixin_45456679/article/details/120850613

# 配置daemon
cat > /etc/docker/daemon.json << EOF
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "100m"
    }
}
EOF
# 重启docker
systemctl daemon-reload && systemctl restart docker

安装 Kubeadm (主从配置)

# 配置yum源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 安装 kubeadm 初始化工具,kubectl 命令行管理工具,kubelet
yum -y install kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1
# 设置开机自启
systemctl enable kubelet

初始化主节点

注意:
1.advertiseAddress需要更换为master服务器的ip地址

# 打印默认的初始化文件,打印到kubeadm-init.yaml
kubeadm config print init-defaults > kubeadm-init.yaml
# 修改
cat > kubeadm-init.yaml << EOF
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: xx.xx.xx.xx   # master节点的IP地址
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: master
  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: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.15.1
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16
  serviceSubnet: 10.96.0.0/12
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates:
  SupportIPVSProxyMode: true
mode: ipvs
EOF
# 启动
kubeadm init --config=kubeadm-init.yaml | tee kubeadm-init.log

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

mkdir -p /root/install-k8s/core
mv /root/kubeadm-init.* /root/install-k8s/core

安装 flannel

mkdir -p /root/install-k8s/plugin/flannel
cd /root/install-k8s/plugin/flannel
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f /root/install-k8s/plugin/flannel/kube-flannel.yml
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐