hostnamectl set-hostname master hostnamectl set-hostname node01 hostnamectl set-hostname node02
[root@master k8s]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.150.128 master 192.168.150.129 node1 192.168.150.130 node2
yum -y install conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git
systemctl stop firewalld && systemctl disable firewalld && yum -y install iptables-services && \\ systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
swapoff -a && sed -i '/swap/d' /etc/fstab && setenforce 0 && sed -i 's/enforcing/SELINUX=disabled/g' /etc/selinux/config cat > /etc/sysctl.d/kubernetes.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 net.ipv4.tcp_tw_recycle=0
#禁止使用swap空间,只有当系统OOM时才容许使用它 vm.swappiness = 0 #不检查物理内存是否够用 vm.overcommit_memory=1 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 net.netfilter.nf_conntrack_max=2310720 #4.0内核能够设置这个参数 EOF sysctl -p /etc/sysctl.d/kubernetes.conf
timedatectl set-timezone Asia/Shanghai #将当前的UTC时间写入硬件时钟 timedatectl set-local-rtc 0
systemctl restart rsyslog systemctl restart crond
systemctl stop postfix && systemctl disable postfix
mkdir /var/log/journal mkdir /etc/systemd/jorunald.conf.d cat > /etc/systemd/jorunald.conf.d/99-prophet.conf << EOF [Journal] Storage=persistent Compress=yes SyncIntervalSec=5m RateLimitInterval=30s RateLimitBurst=1000 #最大占用空间 SystemMaxUse=10G #单日志文件最大大小 SystemMaxFileSize=200M #日志保存时间2周 MaxRetentionSec=2week #不将日志转发到syslog ForwardToSyslog=no EOF systemctl restart systemd-journald
#CentOS7.x系统自带的3.10.x内核存在一些Bug,致使运行的Docker、Kubernetes不稳定 rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm # 安装完成后检查/boot/grub2/grub.cfg 中对应内核menuentry中是否包含initrd16配置,若是没有,再安装一次 yum --enablerepo=elrepo-kernel install -y kernel-lt # 设置开机重新内核启动 grub2-set-default "CentOS Linux (4.4.182-1.el7.elrepo.x86\_64) 7 (Core)"
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
yum -y install yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum -y update && yum -y install docker-ce ## 建立docker配置目录 mkdir /etc/docker #配置daemon cat > /etc/docker/daemon.json << EOF { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"] } EOF mkdir -p /etc/systemd/system/docker.service.d # 启动docker systemctl daemon-reload && systemctl start docker && systemctl enable docker
# 导入阿里云kubeadm镜像仓库 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=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 -y install kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1 # 设置开机自启动 systemctl enable kubelet.service
由于在国内访问不了google的docker仓库,可是咱们能够在阿里云上找到须要的镜像,下载下来,而后从新打上标签便可,可使用下面的脚本下载所需镜像node
镜像下载地址 https://cr.console.aliyun.com/images/cn-hangzhou/google_containers/kube-apiserver-amd64/detail
linux
下面是镜像下载脚本:git
#!/bin/bash image_aliyun=(kube-apiserver-amd64:v1.17.1 kube-controller-manager-amd64:v1.17.1 kube-scheduler-amd64:v1.17.1 kube-proxy-amd64:v1.17.1 pause-amd64:3.1 etcd-amd64:3.4.3-0 coredns:1.6.5) for image in ${image_aliyun[@]} do docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$image docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$image k8s.gcr.io/${image/-amd64/} docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$image done
生成默认配置yaml配置文件,生成以后须要修改一些配置 kubeadm config print init-defaults > kubeadm-config.yaml # 修改advertiseAddress为master节点IP # 修改kubernetesVersion: v1.17.1 # 在serviceSubnet: 10.96.0.0/12上面添加一行 podSubnet: "10.244.0.0/16" 缘由是flanne默认网段就是这个网段 # 在配置文件最后添加下面的字段,将默认的调度方式改成ipvs --- apiVersion: kubeproxy.config.k8s.io/v1alpha1 kind: KubeProxyConfiguration featureGates: SupportIPVSProxyMode: true mode: ipvs # 安装以及自动颁发证书 kubeadm init --config=kubeadm-config.yaml | tee kubeadm-init.log
# cat kubeadm-init.log ... [addons] Applied essential addon: kube-proxy Your Kubernetes control-plane has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.175.133:6443 --token 6bu2e9.heokh2kwn5h2eph1 --discovery-token-ca-cert-hash sha256:5382721c5a684d10d2d43a6a825af039c044c4dda46c4d49dd64d2f41ffa6c97 ...
mkdir -p $HOME/.kube # 该目录用于保存kubectl 和kubeapi的链接缓存以及认证文件 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get nodes kubectl get cs kubectl get pod -n kube-system
mkdir k8sInstall mv kubeadm-config.yaml kubeadm-init.log k8sInstall
kubeadm join MasterIP:6443 --token wyy67p.9wmda1iw4o8ds0c5 --discovery-token-ca-cert-hash sha256:3de3e4401de1cdf3b4c778ad1ac3920d9f7b15ca34b4c5ebe44d92e60d1290e0 #token和cert hash须要修改成你的配置,能够在kubeadm-init.log 中找到