一、修改主机名 #全部选中以后直接按tab 按shift+tab可以缩回
hostnamectl set-hostname xxx
二、修改hosts文件vim /etc/hosts
172.16.88.10 k8s-master01
172.16.88.20 k8s-node01
172.16.88.21 k8s-node02
三、将写好的hosts文件拷贝到其他节点
scp /etc/hosts root@k8s-node01:/etc/hosts
四、安装依赖包
yum -y install conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccamp wget vim net-tools git
五、设置防火墙为 Iptables 并设置空规则
#关闭防火墙&&防火墙自启
systemctl stop firewalld && systemctl disable firewalld
#安装Iptables管理工具&&启动Iptables&&设为Iptables开机自启&&清空Iptables规则&&保存Iptables默认规则
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
六、关闭selinux
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab #生产环境建议关闭虚拟内存
#关闭swap分区,永久关闭虚拟内存。K8s初始化init时,会检测swap分区有没有关闭,如果虚拟内存开启,容器pod就可能会放置在虚拟内存中运行,会大大降低运行效率
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
七、必须调整内核参数,对于k8s
cat > 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
###其中必备参数
###$ cat /proc/sys/net/bridge/bridge-nf-call-ip6tables
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1 //开启网桥模式
net.ipv6.conf.all.disable_ipv6=1//关闭ipv6的协议
#其余为优化参数,可不设置
cp kubernetes.conf /etc/sysctl.d/kubernetes.conf //拷贝,开机能调用
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
十、设置 rsyslogd 和 systemd journald
原因:centos7以后,引导方式改为了systemd,所以会有两个日志系统同时工作只保留一个日志(journald)的方法
mkdir /var/log/journal # 持久化保存日志的目录
mkdir /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
十一、升级内核为4.4版本[###特别提醒,内核可以不升级 Linux k8s-master01 3.10.0-693.el7.x86_64]
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)"
十二、kube-proxy开启ipvs的前提条件
#加载netfilter模块
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软件
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum -y update
yum -y install docker-ce
创建目录
mkdir /etc/docker
#配置daemon
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
EOF
mkdir -p /etc/systemd/system/docker.service.d
#重启服务
systemctl daemon-reload && systemctl restart docker && systemctl enable docker
十四 安装kubeadm(主从配置)
1.导入阿里云yum仓库
cat <<EOF > /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.gpg
EOF
2.安装kubeadm初始化工具,命令行管理工具,Docker交互工具
yum -y install kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1
3.设置kubelet开机自启
systemctl enable kubelet.service
十五 初始化节点
#打印k8s默认配置文件
$ kubeadm config print init-defaults>kubeadm-config.yaml
localAPIEndpoint:
advertiseAddress: 172.16.88.10 #修改主节点的IP
bindPort: 6443
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.15.1 #版本信心
apiVersion: kubeproxy.config.k8s.io/vlalphal #修改成ipvs格式
kind: KubeProxyConfiguration
featureGates:
SupportIPVSProxyMode: true
mode: ipvs
安装tree命令:wget http://mama.indstate.edu/users/ice/tree/src/tree-1.7.0.tgz
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs|tree kubeadm-init.log
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
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.16.88.10:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:484b4f11280a4471d7bad063756eb0130470ce64162685bbbc96dea56b4c66d4
十六 加入主节点以及其余工作节点
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.16.88.10:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:484b4f11280a4471d7bad063756eb0130470ce64162685bbbc96dea56b4c66d4
十七 部署网络
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
$ kubectl get pod -n kube-system [名称空间]
NAME READY STATUS RESTARTS AGE
coredns-5c98db65d4-9qtvj 1/1 Running 0 25m
coredns-5c98db65d4-hnkpt 1/1 Running 0 25m
etcd-k8s-master01 1/1 Running 0 25m
kube-apiserver-k8s-master01 1/1 Running 0 25m
kube-controller-manager-k8s-master01 1/1 Running 0 25m
kube-flannel-ds-amd64-2zjh5 1/1 Running 0 100s
kube-proxy-8xcbg 1/1 Running 0 25m
kube-scheduler-k8s-master01 1/1 Running 0 24m
$ kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master01 Ready master 27m v1.15.1
$ kubectl get pod -n kube-system -o wide
$ kubectl get pod -n kube-system -w 监视状态
十八 测试kubernetes集群
在Kubernetes集群中创建一个pod,验证是否正常运行,前提是节点必须存在镜像,并启动才能正常访问
$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
$ kubectl get pod,svc
[root@k8s-master01 ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-554b9c67f9-twn7d 1/1 Running 0 69m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 18h
service/nginx NodePort 10.106.61.158 <none> 80:31786/TCP 69m
访问地址:http://NodeIP:临时端口 =》http://172.16.88.21:31786/
十九 部署 Dashboard
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
默认镜像国内无法访问,修改镜像地址为: lizhenliang/kubernetes-dashboard-amd64:v1.10.1
默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:
containers:
- name: kubernetes-dashboard
image: lizhenliang/kubernetes-dashboard-amd64:v1.10.1 #修改镜像名称
ports:
- containerPort: 8443
protocol: TCP
# ------------------- Dashboard Service ------------------- #
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
type: NodePort #添加节点类型
ports:
- port: 443
targetPort: 8443
nodePort: 30001 #添加节点端口
selector:
k8s-app: kubernetes-dashboard
$ kubectl apply -f kubernetes-dashboard.yaml
$ kubectl get pods -n kube-system 默认放在命名空间
NAME READY STATUS RESTARTS AGE
kubernetes-dashboard-79ddd5-t8xmx 1/1 Running 0 85s
建议火狐浏览器访问地址:https://NodeIP:30001|| https://172.16.88.20:30001
二十 创建账号
创建service account并绑定默认cluster-admin管理员集群角色:
#serviceaccount 虚拟账号,进程和服务访问的账号,面向应用
$ kubectl create serviceaccount dashboard-admin -n kube-system
#集群角色的绑定,创建的账号绑定权限组上
$ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
#令牌 存放机密信息管理机制
$ kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
使用输出的token登录Dashboard。