1、环境(centos7.6)
192.168.186.136 k8s-master
192.168.186.137 k8s-node1
2、初始化系统环境安装k8smaster和node节点
1、关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
2、关闭selinux和关闭swap
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久关闭selinux
setenforce 0 # 临时关闭selinux
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久关闭swap
swapoff -a #临时关闭swap
3、修改主机名,并写入三台服务器的host中
hostnamectl set-hostname K8S-master
hostnamectl set-hostname K8S-node01
cat >> /etc/hosts << EOF
192.168.186.136 K8S-master
192.168.186.137 K8S-node01
4、将桥接的IPV4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 配置生效
5、同步时间
yum install -y ntpdate
ntpdate time.windows.com
#如果时区不对执行下面命令,然后在同步
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
6、配置阿里源,下载相应的软件包
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 install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
#kubectl-1.18.0命令行管理工具,kubeadm-1.18.0是引导K8S集群,kubelet-1.18.0管理容器
systemctl enable kubelet
7、Kubernetes 默认 CRI(容器运行时)为 Docker,因此先安装 Docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
8、添加阿里云 YUM 软件源
cat > /etc/docker/daemon.json << EOF
{ "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"] }
EOF
9、部署kubernetes Master节点(master节点上执行)
kubeadm init
--apiserver-advertise-address=192.168.186.136 #指定master监听的地址
--image-repository registry.aliyuncs.com/google_containers #指定下载源
--kubernetes-version v1.18.0 #指定kubernetes版本
--service-cidr=10.96.0.0/12 #设置集群内部的网络
--pod-network-cidr=10.244.0.0/16 #设置pod的网络
10、加入master的命令
mkdir -p HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
11、节点加入k8s集群(node节点上执行)
kubeadm join 192.168.186.136:6443 --token esce21.q6hetwm8si29qxwn --discovery-token-ca-cert-hash sha256:00603a05805807501d7181c3d60b478788408cfe6cedefedb1f97569708be9c5
# 这里的ip写master地址,这条命令会在master节点init的时候会打出来,直接复制就好,--token有过期时间,可以建立永久的,自行百度
12、查看加入集群的主机
kubectl get nodes
3、安装pod网络插件
wget http://120.78.77.38/file/kube-flannel.yaml
kubectl apply -f kube-flannel.yaml
docker pull lizhenliang/flannel:v0.11.0-amd64
kubectl get pods -n kube-system
4、master节点安装管理页面dashboard
wget http://120.78.77.38/file/kubernetes-dashboard.yaml
kubectl apply -f kubernetes-dashboard.yaml
kubectl get pods -n kubernetes-dashboard
#下面的位置需要修改
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 30001 #这行是需要增加的
5、访问web界面
https://192.168.106.104:30001 #需要用火狐浏览器打开访问,其他浏览器不行
#创建token,进入UI界面需要
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}’)
# UI界面Nodes可以看到添加的节点
node加入master失败试试:
systemctl stop firewalld;systemctl disable firewalld;setenforce 0