我们需要做以下工作:
(1)安装VMware,运行CentOs系统,一个做master,一个做node。
(2)安装K8s。
(3)安装docker和部分镜像会需要访问外网,所以你需要做些网络方面的准备工作,大家可以去某宝找一下,或者在网络上找别人下好的也可以。
一、安装VMware
官网地址:https://my.vmware.com/zh/web/vmware/downloads (网上有很多自带破解的下载,大家也可以找一下)
我这里下载的是VM15(安装和新建虚拟机的步骤就跳过了,我也是百度的)
虚拟机配置:2核2G
CentOs下载地址:https://www.centos.org/download/
1.如果是win10系统,需要关闭Hyper-V。
2.为了方便操作,我这里是通过cmd来操作vw的Linux命令,用到了以下两个工具:
安装 chocolate ( https://chocolatey.org/):
管理员方式启动CMD,运行以下命令安装chocolate :
@"%SystemRoot%System32WindowsPowerShellv1.0powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%chocolateybin"
安装完chocolate 后还需要安装 ssh terminals (https://chocolatey.org/packages/terminals),直接CMD运行以下命令就可以:
choco install terminals
顺便说下如何修改Linux主机名称:
hostnamectl set-hostname 新主机名
二.配置Linux
(1)开启网卡(设置完需要重启):
vi /etc/sysconfig/network-script/ifcfg-eth33
ONBOOT=yes
(2)安装net-tool包:
yum install net-tool
(3)使用cmd通过ssh连接Linux:
首先通过ifconfig查看当前Linux的ip,然后在cmd中运行以下命令(管理员模式):
ssh root@IP
(4)关闭所有节点的SELinux
/etc/selinux/config
SELINUX=disabled
(5)关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
(6)关闭swap
vi /etc/fstab
在行首加 #,注释该行
(7)完成第6步后重启,然后设置iptables
vi /etc/sysctl.conf net.bridge.bridge-nf-call-iptables = 1 sudo sysctl -p
三.安装Docker
(1)下载安装
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
systemctl enable docker
systemctl start docker
最后一步下载docker-ce因为是外网,所以会有网络问题,大家八仙过海各显神通哈,从阿里云下载也可以的,总之版本越新越好,我这里是18.09,完成后通过以下命令查看下:
docker -v
(2)配置docker加速器
加速器怎么配置就不说了,这里是我的阿里云加速地址,大家可以自行替换,然后我们还需要指定下k8s的下载地址:
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://bbw0jgk7.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
cat>>/etc/yum.repos.d/kubrenetes.repo<<EOF [kubernetes] name=Kubernetes Repo baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg EOF
(3)安装:
yum install -y kubeadm kubelet kubectl
(4)设置开机启动
systemctl start docker.service
systemctl enable docker.service
systemctl enable kubelet.service
(5)初始化k8s
kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=cri --kubernetes-version=1.13.0
(6)配置下变量
$ mkdir -p $HOME/.kube $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config $ sudo chown $(id -u):$(id -g) $HOME/.kube/config
(7)重启
docker restart $(docker ps -a -q)
到这一步我们已经可以用以下命令查看集群信息:
kubectl get node
这个时候只有一个master节点,而且Status是NotReady状态,这是因为没有安装网络组件的原因,我们需要安装下
(8)始化网络插件 Flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
同样这一步也需要访问外网下载镜像,配置了阿里云加速也没用的,网上还有有种在yml文件里配置的方式可以通过阿里云下载,我也不是很清楚,大家可以搜下。
这一步执行完成后,master的Status状态就会变为Ready,master配置就完成了。
(9)配置Node
Node的配置和master一样,需要下载docker、kubectl等,但是只需要执行到上面的第(4)步就OK了。
(10)通过以下命令将node加入集群
kubeadm join 192.168.3.131:6443 --token s87gto.ruxhofion1qemfte --discovery-token-ca-cert-hash sha256:ef0333ddcc5ade8541544de89333ab81b20ae96b0345f9a55185eb95e3837905
这个命令第(5)步初始化k8s完成后会自己输出出来,需要注意的是里面的token字段,如果大家忘了token是啥,在master上执行以下命令查看:
kubeadm token list
或者我们也可以在master节点新建token:
kubeadm create token
但是需要注意的是默认情况下,通过 kubeadm create token
创建的 token
,过期时间是24小时,这就是为什么过了一天无法再次使用之前记录的 kube join
原生脚本的原因,也可以运行 kubeadm token create --ttl 0
生成一个永不过期的 token
,详情请参考:kubeadm-token。
然后通过kubectl get node命令就可以看到Node的节点信息了。
如果成功将node加入了集群,但是在master上看不到,参考下:https://www.cnblogs.com/justmine/p/8886675.html
最终运行结果:
四.本地安装Kubectl
kubectl可以用来帮助我们本地连接管理集群,这里我们也是通过choco来进行安装,CMD管理员模式运行以下命令:
choco install kubernetes-cli
安装完成后通过以下命令检查是否成功:
kubectl version
这个时候呢还是不成功的,因为用户证书不匹配,我们还需要做以下工作,连接到master运行以下命令查看admin用户证书:
cd /etc/kubernetes/
cat admin.conf
OK,复制下这个配置文件的内容,默认情况下你应该只有一个admin用户的信息,我们需要用这个替换本地的配置文件内容。
在C:UsersAdministrator.kube路径下有一个conig文件,替换掉里面的内容(里面默认只有一个admin用户信息,可以直接替换掉,如果你还有其他的用户上下文信息啥的,那就在后面按格式追加)
这样完成后再次通过kubectl version命令就可以查看到以下内容了:
到这里我们就无需通过连接到master来管理k8s集群了,在本地就可以:
四.安装dashboard
首先我们需要准备一个dashboard.yaml,我们通过NodePort暴露端口的方式部署dashboard,这样就不需要启动代理:
# Copyright 2017 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Configuration to deploy release version of the Dashboard UI compatible with # Kubernetes 1.8. # # Example usage: kubectl create -f <this_file> # ------------------- Dashboard Secret ------------------- # apiVersion: v1 kind: Secret metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard-certs namespace: kube-system type: Opaque --- # ------------------- Dashboard Service Account ------------------- # apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system --- # ------------------- Dashboard Role & Role Binding ------------------- # kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: kubernetes-dashboard-minimal namespace: kube-system rules: # Allow Dashboard to create 'kubernetes-dashboard-key-holder' secret. - apiGroups: [""] resources: ["secrets"] verbs: ["create"] # Allow Dashboard to create 'kubernetes-dashboard-settings' config map. - apiGroups: [""] resources: ["configmaps"] verbs: ["create"] # Allow Dashboard to get, update and delete Dashboard exclusive secrets. - apiGroups: [""] resources: ["secrets"] resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"] verbs: ["get", "update", "delete"] # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map. - apiGroups: [""] resources: ["configmaps"] resourceNames: ["kubernetes-dashboard-settings"] verbs: ["get", "update"] # Allow Dashboard to get metrics from heapster. - apiGroups: [""] resources: ["services"] resourceNames: ["heapster"] verbs: ["proxy"] - apiGroups: [""] resources: ["services/proxy"] resourceNames: ["heapster", "http:heapster:", "https:heapster:"] verbs: ["get"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: kubernetes-dashboard-minimal namespace: kube-system roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: kubernetes-dashboard-minimal subjects: - kind: ServiceAccount name: kubernetes-dashboard namespace: kube-system --- # ------------------- Dashboard Deployment ------------------- # kind: Deployment apiVersion: apps/v1beta2 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s-app: kubernetes-dashboard template: metadata: labels: k8s-app: kubernetes-dashboard spec: containers: - name: kubernetes-dashboard image: registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.1 ports: - containerPort: 8443 protocol: TCP args: - --auto-generate-certificates # Uncomment the following line to manually specify Kubernetes API server Host # If not specified, Dashboard will attempt to auto discover the API server and connect # to it. Uncomment only if the default does not work. # - --apiserver-host=http://my-address:port volumeMounts: - name: kubernetes-dashboard-certs mountPath: /certs # Create on-disk volume to store exec logs - mountPath: /tmp name: tmp-volume livenessProbe: httpGet: scheme: HTTPS path: / port: 8443 initialDelaySeconds: 30 timeoutSeconds: 30 volumes: - name: kubernetes-dashboard-certs secret: secretName: kubernetes-dashboard-certs - name: tmp-volume emptyDir: {} serviceAccountName: kubernetes-dashboard # Comment the following tolerations if Dashboard must not be deployed on master tolerations: - key: node-role.kubernetes.io/master effect: NoSchedule --- # ------------------- 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 selector: k8s-app: kubernetes-dashboard
然后我们还需要一个用户权限的user.yaml,它用来绑定角色权限:
apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: kubernetes-dashboard labels: k8s-app: kubernetes-dashboard roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: default namespace: default
运行这两个yaml文件:
kubectl apply -f dashboard.yaml
kubectl apply -f user.yaml
查看dashborad的端口:
kubectl get svc -n kube-system
我这里是32166端口,然后我们只需要用master机器的IP+这里的32166端口就可以访问了,因为我们用的是假的证书,谷歌浏览器会阻止,所以我们需要使用火狐浏览器,火狐浏览器默认也会阻止,点击高级-添加例外,就可以访问到以下界面:
我们需要使用Token令牌,我们可以通过以下命令查看令牌信息:
kubectl get secret
这里只是给出了令牌名称,具体的令牌需要用以下命令查看:
kubectl describe secret (默认显示第一个) 或者 kubectl describe secret default-token-2qfb8
复制标记出来的token内容,粘贴到令牌那里就可以了:
OK,暂时到此结束,后期有新进展再更新。
- 还没有人评论,欢迎说说您的想法!