背景
一、配置华为云yum源
1.1 配置华为云yum源。
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.huaweicloud.com/kubernetes/yum/repos/kubernetes-el7-aarch64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.huaweicloud.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.huaweicloud.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
1.2 刷新yum缓存。
yum makecache
二、安装配置K8S
主节点和计算节点都需要安装配置K8s。
2.1 安装Docker
2.1.1 安装yum-utils软件包并设置稳定的存储库。
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
2.1.2 安装特定版本Docker
要安装特定版本的Docker Engine,可以先通过命名查看存储库,列出可用版本,然后选择并安装:
yum list docker-ce --showduplicates | sort -r
yum list docker-ce-cli --showduplicates | sort -r
然后根据版本号安装(本文选择),例如:
yum install docker-ce-18.09.9-3.el7 docker-ce-cli-18.09.9-3.el7 containerd.io
也可以安装最新版,例如直接使用方式,本文选择的不是最新版本,安装的是指定版本18.09
yum install docker-ce docker-ce-cli containerd.io
2.1.3 安装完成启动
systemctl start docker
2.1.4 通过docker version查看版本
2.1.5 设置docker开机启动
systemctl enable docker
2.2 安装基础组件
2.2.1 启用NET.BRIDGE.BRIDGE-NF-CALL-IPTABLES内核选项。
sysctl -w net.bridge.bridge-nf-call-iptables=1
2.2.2 禁用交换分区。
swapoff -a
cp -p /etc/fstab /etc/fstab.bak$(date '+%Y%m%d%H%M%S')
sed -i "s/\/dev\/mapper\/centos-swap/\#\/dev\/mapper\/centos-swap/g" /etc/fstab
2.2.3 安装K8s组件。
yum install -y kubelet kubeadm kubectl kubernetes-cni
2.2.4 查看安装情况。
rpm -qa | grep kubelet
rpm -qa | grep kubeadm
rpm -qa | grep kubectl
rpm -qa | grep kubernetes-cni
例如看到如下内容代表安装成功
[root@master aarch64]# rpm -qa | grep kubelet
kubelet-1.14.2-0.aarch64
[root@master aarch64]# rpm -qa | grep kubeadm
kubeadm-1.14.2-0.aarch64
[root@master aarch64]# rpm -qa | grep kubectl
kubectl-1.14.2-0.aarch64
[root@master aarch64]# rpm -qa | grep kubernetes-cn
kubernetes-cni-0.7.5-0.aarch64
2.2.5 设置iptables。
echo "net.bridge.bridge-nf-call-iptables=1" > /etc/sysctl.d/k8s.conf
2.2.6 开机启动kubelet服务。
systemctl enable kubelet
2.3 通过Docker下载其它组件
2.3.1 查看下载节点初始化所需镜像。
kubeadm config images list
[root@master aarch64]# kubeadm config images list
I1208 09:21:05.327540 2544 version.go:96] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://storage.googleapis.com/kubernetes-release/release/stable-1.txt: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
I1208 09:21:05.327591 2544 version.go:97] falling back to the local client version: v1.14.2
k8s.gcr.io/kube-apiserver:v1.14.2
k8s.gcr.io/kube-controller-manager:v1.14.2
k8s.gcr.io/kube-scheduler:v1.14.2
k8s.gcr.io/kube-proxy:v1.14.2
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
2.3.2 从DockerHub上下载镜像(速度有点慢)
命令如下所示
docker pull docker.io/mirrorgooglecontainers/kube-apiserver-arm64:v1.14.2
docker pull docker.io/mirrorgooglecontainers/kube-controller-manager-arm64:v1.14.2
docker pull docker.io/mirrorgooglecontainers/kube-scheduler-arm64:v1.14.2
docker pull docker.io/mirrorgooglecontainers/kube-proxy-arm64:v1.14.2
docker pull docker.io/mirrorgooglecontainers/pause-arm64:3.1
docker pull docker.io/mirrorgooglecontainers/etcd-arm64:3.3.10
docker pull docker.io/coredns/coredns:1.3.1
下载完成后可以查看本地镜像
[root@master aarch64]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mirrorgooglecontainers/kube-proxy-arm64 v1.14.2 5227f1c3c8a7 19 months ago 82.7MB
mirrorgooglecontainers/kube-apiserver-arm64 v1.14.2 091f78b1b081 19 months ago 207MB
mirrorgooglecontainers/kube-controller-manager-arm64 v1.14.2 e0fa3d206e49 19 months ago 155MB
mirrorgooglecontainers/kube-scheduler-arm64 v1.14.2 485238b2c4a5 19 months ago 82.2MB
coredns/coredns 1.3.1 7e8edeee9a1e 23 months ago 37.4MB
mirrorgooglecontainers/etcd-arm64 3.3.10 ad99d3ead043 2 years ago 300MB
mirrorgooglecontainers/pause-arm64 3.1 6cf7c80fe444 2 years ago 525kB
2.3.3 修改已下载的镜像标签。
docker tag mirrorgooglecontainers/kube-apiserver-arm64:v1.14.2 k8s.gcr.io/kube-apiserver:v1.14.2
docker tag mirrorgooglecontainers/kube-controller-manager-arm64:v1.14.2 k8s.gcr.io/kube-controller-manager:v1.14.2
docker tag mirrorgooglecontainers/kube-scheduler-arm64:v1.14.2 k8s.gcr.io/kube-scheduler:v1.14.2
docker tag mirrorgooglecontainers/kube-proxy-arm64:v1.14.2 k8s.gcr.io/kube-proxy:v1.14.2
docker tag mirrorgooglecontainers/pause-arm64:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd-arm64:3.3.10 k8s.gcr.io/etcd:3.3.10
docker tag coredns/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
2.3.4 查看K8s的Docker镜像组件
docker images | grep k8s
[root@master aarch64]# docker images | grep k8s
k8s.gcr.io/kube-apiserver v1.14.2 091f78b1b081 19 months ago 207MB
k8s.gcr.io/kube-controller-manager v1.14.2 e0fa3d206e49 19 months ago 155MB
k8s.gcr.io/kube-scheduler v1.14.2 485238b2c4a5 19 months ago 82.2MB
k8s.gcr.io/kube-proxy v1.14.2 5227f1c3c8a7 19 months ago 82.7MB
k8s.gcr.io/coredns 1.3.1 7e8edeee9a1e 23 months ago 37.4MB
k8s.gcr.io/etcd 3.3.10 ad99d3ead043 2 years ago 300MB
k8s.gcr.io/pause 3.1 6cf7c80fe444 2 years ago 525kB
2.3.5 删除旧镜像。
命令如下所示
docker rmi mirrorgooglecontainers/kube-apiserver-arm64:v1.14.2
docker rmi mirrorgooglecontainers/kube-controller-manager-arm64:v1.14.2
docker rmi mirrorgooglecontainers/kube-scheduler-arm64:v1.14.2
docker rmi mirrorgooglecontainers/kube-proxy-arm64:v1.14.2
docker rmi mirrorgooglecontainers/pause-arm64:3.1
docker rmi mirrorgooglecontainers/etcd-arm64:3.3.10
docker rmi coredns/coredns:1.3.1
删除后再次查看本地镜像
[root@master aarch64]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-scheduler v1.14.2 485238b2c4a5 19 months ago 82.2MB
k8s.gcr.io/kube-proxy v1.14.2 5227f1c3c8a7 19 months ago 82.7MB
k8s.gcr.io/kube-apiserver v1.14.2 091f78b1b081 19 months ago 207MB
k8s.gcr.io/kube-controller-manager v1.14.2 e0fa3d206e49 19 months ago 155MB
k8s.gcr.io/coredns 1.3.1 7e8edeee9a1e 23 months ago 37.4MB
k8s.gcr.io/etcd 3.3.10 ad99d3ead043 2 years ago 300MB
k8s.gcr.io/pause 3.1 6cf7c80fe444 2 years ago 525kB
三、部署K8S集群
3.1 配置hosts文件
主节点和计算节点都需要配置hosts文件。
执行vi /etc/hosts命令,修改hosts文件,添加集群所有节点的ip及hostname信息。
192.168.0.113 master master
192.168.0.226 node1 node1
每台节点的/etc/hosts文件都要修改。
3.2 配置主节点(master)
3.2.1 在主节点上执行集群初始化命令
kubeadm init --kubernetes-version=1.14.2 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
当看到如下信息时代表初始化成功
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.0.113:6443 --token all7y6.hxs2yemaucku9dct \
--discovery-token-ca-cert-hash sha256:7e1b11d2715fd87d8aeec9a1924c87b68d36da35392ccee57b0581993dfeb821
黄框信息表示主节点配置集群需要执行的命令,红框信息表示计算节点加入集群的token命令,请保存该段命令。
3.2.2 配置集群
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
3.2.3 在主节点查看集群节点信息。
kubectl get node
[root@master aarch64]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady master 97s v1.14.2
3.2.4 保存主节点生成的加入集群信息。
该信息在成功初始化主节点后生成并打印,集群信息如下所示:
kubeadm join 192.168.0.113:6443 --token all7y6.hxs2yemaucku9dct \
--discovery-token-ca-cert-hash sha256:7e1b11d2715fd87d8aeec9a1924c87b68d36da35392ccee57b0581993dfeb821
3.3 配置计算点(node1)
3.3.1 计算节点加入集群
计算节点执行
kubeadm join 192.168.0.113:6443 --token all7y6.hxs2yemaucku9dct \
--discovery-token-ca-cert-hash sha256:7e1b11d2715fd87d8aeec9a1924c87b68d36da35392ccee57b0581993dfeb821
3.3.2 等待1分钟后,在主节点(master)执行kubectl get nodes
[root@master opt]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 3h7m v1.14.2
node1 NotReady <none> 83s v1.14.2
3.3.3 在主节点和计算节点上查看kubelet服务状态
systemctl status kubelet
主节点
子节点
3.4 添加Flannel网络插件
3.4.1 在主节点上执行如下命令安装Flannel网络插件。
当前版本1.14版本执行(如果无法下载,可以查看网盘附件进行本地化安装,下载地址:https://pan.baidu.com/s/1L1epcWiEI7v2C4bHo2nx3Q 提取码:1fsh)
kubectl apply -f https://gitee.com/mirrors/flannel/blob/master/Documentation/kube-flannel-old.yaml
注意:对于 Kubernetes 版本 v1.17+
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
3.4.2 安装完成后,执行kubectl get pod -n kube-system,看到kube-flannel正常运行即可
[root@master opt]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-sgzqj 1/1 Running 0 38m
coredns-fb8b8dccf-xpx5x 1/1 Running 0 38m
etcd-master 1/1 Running 0 37m
kube-apiserver-master 1/1 Running 0 37m
kube-controller-manager-master 1/1 Running 0 37m
kube-flannel-ds-arm64-wxc6f 1/1 Running 0 98s
kube-proxy-tzb4r 1/1 Running 0 38m
kube-scheduler-master 1/1 Running 0 37m
这样我们K8S集群就搭建完成了。
四、(选配)安装Kubernetes Dashboard插件
主节点上操作即可
4.1 用openssl生成自签证书(有第三方证书可跳过)
生成证书请求的key
openssl genrsa -out dashboard.key 2048
生成证书请求
openssl req -new -out dashboard.csr -key dashboard.key -subj '/CN=<your_ip>'
<your_ip>换成自己的ip或域名。
生成自签证书
openssl x509 -days 3650 -req -in dashboard.csr -signkey dashboard.key -out dashboard.crt
这里指定了过期时间3650天,默认365天
4.2 部署Dashboard插件
4.2.1 创建部署kubernetes-dashboard的yaml文件
(文件下载地址:链接:https://pan.baidu.com/s/1L1epcWiEI7v2C4bHo2nx3Q 提取码:1fsh)
4.2.2 部署kubernetes-dashboard
kubectl apply -f /opt/kubernetes-dashboard.yaml
4.2.3 部署完成后还是不能访问,因为yaml文件中注释掉了kubernetes-dashboard-certs,相关的pod没跑起来,所以此时应创建certs
kubectl create secret generic kubernetes-dashboard-certs --from-file=/opt/dashboard.key --from-file=/opt/dashboard.crt -n kubernetes-dashboard
4.2.4 如果失败检查方式
一般经过以上步骤就可以访问dashboard,可以跳过这一步了,但如果此时仍不能访问,pod不是处于"running"状态,可以删除kubenetes-dashboard相关的pod,让kubelet自动生成一个新的可运行的pod
查看kubernetes-dashboard的pod名
kubectl get pods -n kubernetes-dashboard
删除该pod:
kubectl delete pod -n kubernetes-dashboard <pod名>
正确结果
[root@master /]# kubectl get pod -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-57c8667f5c-92fls 1/1 Running 0 5h9m
kubernetes-dashboard-69d48fd659-bw7zb 1/1 Running 0 5h9m
4.3 创建访问用户,用于访问dashboard的Service Account
kubectl create serviceaccount admin-user -n kubernetes-dashboard
4.4 为用户绑定角色,创建ClusterRoleBinding
kubectl create clusterrolebinding admin-user --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:admin-user
4.5 获取登录密钥
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
复制生成的token值即可
[root@master opt]# kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
Name: admin-user-token-64tjt
Namespace: kubernetes-dashboard
Labels: <none>
Annotations: kubernetes.io/service-account.name: admin-user
kubernetes.io/service-account.uid: c882f015-39ca-11eb-9396-fa163ec97a54
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1025 bytes
namespace: 20 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLTY0dGp0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJjODgyZjAxNS0zOWNhLTExZWItOTM5Ni1mYTE2M2VjOTdhNTQiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.nK8gspJ8DAMzrOesHC-CGvjaYKAtahUCB9L1eQTbaeCXv-8ZT-8HVBzzCTk20XnAG30yPwOD80x_MBCksSgaw7QkImNlZVcNM3JXmaNvxFTObeg9aY_Lgdbs7r4hfAMKZd2wi1G3BaNbfAWnWxlecbOXTMYBL5PI6h5oJSuR6Fgm0IEdRT6diI_uaoan-Mqmvm_VKvj6tMl1xTqczTgnp0Hv5kwJvT0lOYx13nhjix-hfo0B2UaSGDnLcyFLrzAIOfKuAlFQMwcJndgs7hz_x7CM-UpfOvCg4oVrWkHAlspOfxRVflHXjfexUGnjGq2Rywph5h4godbTPwhLwJ4tcg
4.6 登录验证
访问我们master地址,通过浏览器访问https://IP:31234,点击继续访问
输入token值
即可看到kubernetes-dashboard UI界面
如果无法访问,可以通过以下方式查看pod状态
[root@master opt]# kubectl get secret -n kubernetes-dashboard
NAME TYPE DATA AGE
admin-user-token-zqdnf kubernetes.io/service-account-token 3 3h4m
default-token-v7v95 kubernetes.io/service-account-token 3 3h9m
kubernetes-dashboard-certs Opaque 2 3h8m
kubernetes-dashboard-csrf Opaque 1 3h9m
kubernetes-dashboard-key-holder Opaque 2 3h9m
kubernetes-dashboard-token-47xg5 kubernetes.io/service-account-token 3 3h9m
UI查看节点信息
五、卸载K8S组件
注意:
下列步骤仅仅指导用户如何手动卸载K8s组件,请勿在正常的部署过程中执行。
清空K8s集群设置。
在卸载K8s组件前,先执行kubeadm reset命令,清空K8s集群设置。
卸载管理组件。
yum erase -y kubelet kubectl kubeadm kubernetes-cni
删除基础组件镜像。
基础组件通过Docker镜像部署,因此只需要强制删除对应镜像即可卸载。
docker rmi -f k8s.gcr.io/kube-apiserver:v1.15.1
docker rmi -f k8s.gcr.io/kube-controller-manager:v1.15.1
docker rmi -f k8s.gcr.io/kube-scheduler:v1.15.1
docker rmi -f k8s.gcr.io/kube-proxy:v1.15.1
docker rmi -f k8s.gcr.io/pause:3.1
docker rmi -f k8s.gcr.io/etcd:3.3.10
docker rmi -f k8s.gcr.io/coredns:1.3.1
(转发请注明出处:http://www.cnblogs.com/zhangyongli2011/ 如发现有错,请留言,谢谢)