使用Centos7.6以上的 操作系统
集群服务器设定如下:
主机IP | 主机名 |
---|---|
10.150.201.17 | k8s-master01 |
10.150.201.2 | k8s-node01 |
10.150.201.224 | k8s-node02 |
10.150.201.231 | k8s-node03 |
打通外网访问条件
cat /etc/centos-release
本例使用使用centos7.9 操作系统
1.下载安装wget
yum install -y wget
2.备份默认的yum
mv /etc/yum.repos.d /etc/yum.repos.d.backup
3.设置新的yum目录
mkdir -p /etc/yum.repos.d
4.下载阿里yum配置到该目录中,选择对应版本
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
5.下载epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel7.repo
修改yum.conf,注释exclude=kernel和exclude=centos-release,否则无法升级Centos系统的版本
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
#exclude=kernel*
#exclude=centos-release*
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-lt
grep initrd16 /boot/grub2/grub.cfg
grub2-set-default 0
#重启系统
reboot
uname -r
uname -a
lscpu
free
free -h
fdisk -l
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
setenforce 0
vi /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
net.ipv4.ip_forward=1
net.ipv4.ip_forward_use_pmtu = 0
生效命令
sysctl --system
查看效果
sysctl -a|grep "ip_forward"
安装IPVS
yum -y install ipset ipvsdm
编译ipvs.modules文件
vi /etc/sysconfig/modules/ipvs.modules
文件内容如下
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
赋予权限并执行
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash
/etc/sysconfig/modules/ipvs.modules &&lsmod | grep -e ip_vs -e
nf_conntrack
重启电脑,检查是否生效
reboot
lsmod | grep ip_vs_rr
注意:当内核版本高于4.9时,nf_conntrack_ipv4 要改为nf_conntrack
安装软件
yum -y install ntpdate
向阿里云服务器同步时间
ntpdate time1.aliyun.com
查看时间
date -R || date
安装bash-completion
yum -y install bash-completion bash-completion-extras
使用bash-completion
source /etc/profile.d/bash_completion.sh
临时关闭:
swapoff -a
永久关闭:
vi /etc/fstab
将文件中的/dev/mapper/centos-swap这行代码注释掉
#/dev/mapper/centos-swap swap swap defaults 0 0
确认swap已经关闭:若swap行都显示 0 则表示关闭成功
free -m
vi /etc/hosts
10.150.201.17 k8s-master01
10.150.201.2 k8s-node01
10.150.201.224 k8s-node02
10.150.201.231 k8s-node03
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo
vi /etc/yum.repos.d/docker-ce.repo
# 按ecs进行非编辑模式
:%s/https:\/\/download.docker.com/https:\/\/mirrors.aliyun.com\/docker-ce/g
yum makecache fast
yum list docker-ce --showduplicates | sort -r
安装指定版本:
yum -y install docker-ce-18.09.8
可以通过docker version命令查看
docker-client版本:当前最新版本
docker-server版本为:18.09.8
systemctl start docker
systemctl status docker
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://trqcg3pj.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com"
],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl restart docker
查看修改后状态:
docker info | grep Cgroup
systemctl enable docker
vi /etc/yum.repos.d/kubernates.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
yum clean all
yum -y makecache
yum list | grep kubeadm
如果提示要验证yum-key.gpg是否可用,输入y。
查找到kubeadm。显示版本
yum list kubelet --showduplicates | sort -r
yum install -y kubelet-1.17.5 kubeadm-1.17.5 kubectl-1.17.5
如果不配置kubelet,可能会导致K8S集群无法启动。为实现docker使用的cgroupdriver与kubelet
使用的cgroup的一致性。
vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
systemctl enable kubelet
在可以科学上网的机器上拉取所需要的镜像,然后导出 k8s.1.17.5.tar
docker save -o k8s.1.17.5.tar k8s.gcr.io/kube-proxy:v1.17.5 k8s.gcr.io/kube-apiserver:v1.17.5 k8s.gcr.io/kube-controller-manager:v1.17.5 k8s.gcr.io/kube-scheduler:v1.17.5 k8s.gcr.io/coredns:1.6.5 k8s.gcr.io/etcd:3.4.3-0 k8s.gcr.io/pause:3.1
docker save -o calico.3.14.2.tar calico/cni:v3.14.2 calico/pod2daemon-flexvol:v3.14.2 calico/node:v3.14.2 calico/kube-controllers:v3.14.2
接着将 k8s.1.17.5.tar 导入到集群所有的服务器
docker load -i k8s.1.17.5.tar
docker load -i calico.3.14.2.tar
配置hostname:
hostnamectl set-hostname k8s-master01
其他子节点按host名称依依对应 如10.150.201.2:
hostnamectl set-hostname k8s-node01
kubeadm init --apiserver-advertise-address=10.150.201.17 --kubernetes-version v1.17.5 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.81.0.0/16
执行上述命令成功后注意保留 最后成功后的信息!这条信息为node节点加入master 集群信息的命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
依据master节点执行初始化成功的信息 !(不能照抄)
kubeadm join 10.150.201.17:6443 --token 15bh7o.065xrrnlhs66fy6n \
--discovery-token-ca-cert-hash sha256:04c7c4597fbebccde91f24f1065937c9994a6f0b9119a29ddc7800b846a1c720
get nodes -o wide
此时可以看到节点信息,但都处于unready状态
wget https://docs.projectcalico.org/v3.14/manifests/calico.yaml
kubectl apply -f ./calico.yaml
#最后查看节点状态
get nodes -o wide
#官方网站
https://github.com/kubernetes/dashboard
#下载配置文件
wget https://github.com/kubernetes/dashboard/blob/v2.0.3/aio/deploy/recommended.yaml
179行左右
containers:
- name: kubernetes-dashboard
image: kubernetesui/dashboard:v2.0.0-rc7
imagePullPolicy: IfNotPresent
262行左右。新增下载策略
containers:
- name: dashboard-metrics-scraper
image: kubernetesui/metrics-scraper:v1.0.4
imagePullPolicy: IfNotPresent
默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部访问。找到Services配置。在配置文件上边。增加type:NodePort和 nodePort:30100端口
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec: ports:
- port: 443
targetPort: 8443
nodePort: 30100
type: NodePort
selector:
k8s-app: kubernetes-dashboard
在配置文件最下边增加用户及给用户授予集群管理员角色
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
name: dashboard-admin
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: dashboard-admin-cluster-role
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
kubectl apply -f ./recommended.yaml
kubectl get pods -n kubernetes-dashboard -o wide
kubectl get svc -n kubernetes-dashboard
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes- dashboard get secret | grep dashboard-admin | awk '{print $1}')
或者
1.根据命名空间找到我们创建的用户
kubectl get sa -n kubernetes-dashboard
2.查看我们创建用户的详细信息。找到token属性对应的secret值
kubectl describe sa dashboard-admin -n kubernetes-dashboard
kubectl describe secrets dashboard-admin-token-9pl4b -n kubernetes-dashboard
3.或者是根据命名空间查找secrets。获得dashboard-admin用户的secret。
kubectl get secrets -n kubernetes-dashboard kubectl describe secrets dashboard-admin-token-9pl4b -n kubernetes-dashboard
注意:是https方式访问
https://10.150.201.17:30100/