K8S集群部署
实验环境:
master | 10.0.1.11 |
---|---|
node1 | 10.0.1.12 |
node2 | 10.0.1.13 |
所有节点均执行:
1、配置hosts表
vim /etc/hosts
10.0.1.11 master
10.0.1.12 node1
10.0.1.13 node2
2、关闭防火墙、selinux和swap交换
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
3、配置内核参数,将桥接的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
4、配置国内yum源
yum install -y wget
mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
yum clean all && yum makecache
5、配置国内Kubernetes源
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
6、配置 docker 源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
7、软件安装
####1、安装docker
yum install -y docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker –version
####2、安装指定版本kubeadm、kubelet、kubectl
yum install -y kubelet-1.15.3 kubeadm-1.15.3 kubectl-1.15.3
systemctl enable kubelet && systemctl start kubelet
部署master 节点:
1、初始化
kubeadm init --kubernetes-version=1.15.3 \
--apiserver-advertise-address=10.0.1.11 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
定义POD的网段为: 10.244.0.0/16, api server地址就是master本机IP地址。
这一步很关键,由于kubeadm 默认从官网k8s.grc.io下载所需镜像,国内无法访问,因此需要通过–image-repository指定阿里云镜像仓库地址,很多新手初次部署都卡在此环节无法进行后续配置。
2、集群初始化成功后返回如下信息:
记录生成的最后部分内容,此内容需要在其它节点加入Kubernetes集群时执行。
kubeadm join 10.0.1.37:6443 --token g66bt6.po8bixrej8ya1y25 --discovery-token-ca-cert-hash sha256:c612c8cc2084ec0f9e4562df49319366e3b4097979e2c9b8814375329a1152a2
3、配置kubectl工具
mkdir -p /root/.kube
cp /etc/kubernetes/admin.conf /root/.kube/config
kubectl get nodes
kubectl get cs
4、部署flannel网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
部署node节点:
1、执行如下命令,使所有node节点加入Kubernetes集群
kubeadm join 10.0.1.37:6443 --token g66bt6.po8bixrej8ya1y25 --discovery-token-ca-cert-hash sha256:c612c8cc2084ec0f9e4562df49319366e3b4097979e2c9b8814375329a1152a2
此命令为集群初始化时(kubeadm init)返回结果中的内容。
验证
在master节点验证:
[root@master yukw]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 135m v1.15.3
node1 Ready <none> 133m v1.15.3
node2 Ready <none> 133m v1.15.3
部署Dashboard:
在master节点部署
1、上传 admin-user-admin.rbac.yaml 和 kubernetes-dashboard.yaml 两个文件到master节点,文件下载地址 git clone https://github.com/luckman666/deploy_Kubernetes-v1.15.0.git 里面的 dashboard 文件夹中
[root@master dashboard]# kubectl create -f admin-user-admin.rbac.yaml
serviceaccount/admin-user created
clusterrolebinding.rbac.authorization.k8s.io/admin-user created
[root@master dashboard]# kubectl create -f kubernetes-dashboard.yaml
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
2、获取对外端口号
[root@master dashboard]# kubectl get service --all-namespaces |grep kubernetes-dashboard
kube-system kubernetes-dashboard NodePort 10.1.85.246 <none> 443:32081/TCP 25s
这里显示对外端口号为 32081
3、通过 https://nodeip:32081 访问
4、获取token值
[root@master dashboard]# kubectl -n kube-system get secret | grep admin-user
admin-user-token-vh8jc kubernetes.io/service-account-token 3 6m3s
[root@master dashboard]# kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
Name: admin-user-token-vh8jc
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: admin-user
kubernetes.io/service-account.uid: fb981598-5422-4e61-9a19-9c1d7c6d7d1d
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1025 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZ.....................................TmU_pmOZ4BFWgBB2BsdIAX4yoj4PDDToMvT8xVPh1P9k6AW6KRhrByFT1llDDxJc1rRSoeS39Ywvm-756d28YbILB1g
5、登陆结果
注:本片博客参考
https://www.kubernetes.org.cn/5462.html
dashboard页面访问用火狐浏览器,谷歌没做兼容,可自行百度解决
好了,这就是部署K8S集群的方法了,如有问题可与博主一起交流讨论!