1、安装前
禁用swap分区:
sudo swapoff -a
编写配置:
vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
sysctl --system
2、配置k8s yum源
vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
enable=1
yum clean all
yum makecache
3、安装docker
cd /etc/yum.repos.d/
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum clean all
yum makecache
yum install docker-ce
yum源安装不成功的可以参考博客进行docker的离线安装:centos 7.5离线安装Docker
3、安装kubernetes-cni
yum install kubernetes-cni
4、安装kubeadm, kubelet and kubectl
yum install kubelet-1.11.1 kubeadm-1.11.1 kubectl-1.11.1
systemctl enable docker
systemctl enable kubelet.service
systemctl start docker
systemctl start kubelet
5、下载k8s镜像
vim pullimages.sh
#!/bin/bash
images=(kube-proxy-amd64:v1.11.1 kube-scheduler-amd64:v1.11.1 kube-controller-manager-amd64:v1.11.1
kube-apiserver-amd64:v1.11.1 etcd-amd64:3.2.18 coredns:1.1.3 pause:3.1 )
for imageName in ${images[@]} ; do
docker pull anjia0532/google-containers.$imageName
docker tag anjia0532/google-containers.$imageName k8s.gcr.io/$imageName
docker rmi anjia0532/google-containers.$imageName
done
sh pullimages.sh
6、编写kubeadm.yaml
vim kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1alpha1
kind: MasterConfiguration
controllerManagerExtraArgs:
horizontal-pod-autoscaler-use-rest-clients: "true"
horizontal-pod-autoscaler-sync-period: "10s"
node-monitor-grace-period: "10s"
apiServerExtraArgs:
runtime-config: "api/all=true"
kubernetesVersion: "v1.11.1"
kubeadm init --config kubeadm.yaml kubeadm init --ignore-preflight-errors=Swap
部署完成:
7、配置kubectl与apiserver的认证
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Kubernetes 集群默认需要加密方式访问,这几条命令,将刚刚部署生成的 Kubernetes 集群的安全配置文件,保存到当前用户的.kube 目录下,kubectl 默认会使用这个目录下的授权信息访问 Kubernetes 集群。
查看当前唯一一个节点的状态:
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady master 1d v1.11.1
看到Master 节点的状态是 NotReady,在调试 Kubernetes 集群时,最重要的手段就是用 kubectl describe ,可以通过这个命令来查看这个节点(Node)对象的详细信息、状态和事件(Event):
$ kubectl describe node master
...
Conditions:
...
Ready False ... KubeletNotReady runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
通过 kubectl describe,可以看到 NodeNotReady 的原因在于,尚未部署任何网络插件。
另外,还可以通过 kubectl 检查节点上各个系统 Pod 的状态:
[root@localhost k8s]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-9d85f5447-4j7cs 0/1 Pending 0 13h
coredns-9d85f5447-5dq6d 0/1 Pending 0 13h
etcd-localhost.com 1/1 Running 0 13h
kube-apiserver-localhost.com 1/1 Running 0 13h
kube-controller-manager-localhost.com 1/1 Running 0 13h
kube-proxy-fbc87 1/1 Running 0 13h
kube-scheduler-localhost.com 1/1 Running 0 13h
可以看到,CoreDNS、kube-controller-manager 等依赖于网络的 Pod 都处于 Pending 状态,是因为这个 Master 节点的网络尚未就绪。
8、部署网络插件
kubectl apply -f https://git.io/weave-kube-1.6
重新通过kubectl get 检查 Pod 的状态:
[root@localhost k8s]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-78fcdf6894-5q9r5 1/1 Running 1 4m
coredns-78fcdf6894-sgqzs 1/1 Running 1 4m
etcd-localhost.com 1/1 Running 0 12h
kube-apiserver-localhost.com 1/1 Running 0 12h
kube-controller-manager-localhost.com 1/1 Running 0 11h
kube-proxy-dgnz8 1/1 Running 0 12h
kube-scheduler-localhost.com 1/1 Running 0 11h
kubernetes-dashboard-b74859887-rwwsw 1/1 Running 1 18s
weave-net-sg4n4 2/2 Running 0 11h
这个时候可以看到,所有的系统 Pod 都成功启动了。至此,Kubernetes 的 Master 节点就部署完成了。
9、部署可视化插件
在 Kubernetes 社区中,有一个很受欢迎的 Dashboard 项目,它可以给用户提供一个可视化的 Web 界面来查看当前集群的各种信息。可以尝试部署这个插件:
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc3/aio/deploy/recommended.yaml
修改recommended.yaml,可以通过token认证进入:
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
# 添加Service的type为NodePort
type: NodePort
ports:
- port: 443
targetPort: 8443
# 添加映射到虚拟机的端口,k8s只支持30000以上的端口
nodePort: 30001
selector:
k8s-app: kubernetes-dashboard
kubectl apply -f recommended.yaml
开启proxy服务:
nohup kubectl proxy --address='0.0.0.0' --accept-hosts='^*$' --disable-filter=true &
获取token:
kubectl -n kube-system describe $(kubectl -n kube-system get secret -n kube-system -o name | grep namespace) | grep token
访问dashboard:
https://<node-ip>:<node-port>