kubeadm部署k8s+docker+Flannel+dashboard+Harbor仓库

Posted 爱show的小卤蛋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kubeadm部署k8s+docker+Flannel+dashboard+Harbor仓库相关的知识,希望对你有一定的参考价值。

《Kubeadm方式搭建一个完整 K8s 集群》

文章目录

一、前置准备

1. 服务器整体规划:

角色IP
master01192.168.100.10
node01192.168.100.20
node02192.168.100.30

2.操作系统初始化配置

#所有节点,关闭防火墙规则,关闭selinux,关闭swap交换
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
swapoff -a						#交换分区必须要关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab		#永久关闭swap分区,&符号在sed命令中代表上次匹配的结果
#加载 ip_vs 模块
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

#修改主机名
hostnamectl set-hostname master01
hostnamectl set-hostname node01
hostnamectl set-hostname node02

#所有节点修改hosts文件
vim /etc/hosts
192.168.100.10 master01
192.168.100.20 node01
192.168.100.30 node02

#调整内核参数
cat > /etc/sysctl.d/kubernetes.conf << EOF
#开启网桥模式,可将网桥的流量传递给iptables链
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
#关闭ipv6协议
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOF

#生效参数
sysctl --system  

master01

node01

node02

二、部署Docker

#所有节点安装docker,部署dockers需要在线源以及DNS解析服务
yum install -y yum-utils device-mapper-persistent-data lvm2 
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
yum install -y docker-ce docker-ce-cli containerd.io

master01

node01

node02

mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF

  "registry-mirrors": ["https://b2wtz7bh.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": 
    "max-size": "100m"
  

EOF
#使用Systemd管理的Cgroup来进行资源控制与管理,因为相对Cgroupfs而言,Systemd限制CPU、内存等资源更加简单和成熟稳定。
#日志使用json-file格式类型存储,大小为100M,保存在/var/log/containers目录下,方便ELK等日志系统收集和管理日志。

systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service 

docker info | grep "Cgroup Driver"
#会出现这个显示驱动Cgroup Driver: systemd

node01

node02

三、所有节点安装kubeadm,kubelet和kubectl

#定义kubernetes源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#
yum install -y kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1

mater01

node01

node02

#开机自启kubelet
systemctl enable kubelet.service
#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启

master01

node01

node02

四、部署k8s集群

#查看初始化需要的镜像
kubeadm config images list

master01

node01

node02

//在 master 节点上传 kubeadm-basic.images.tar.gz 压缩包至 /opt 目录
cd /opt
tar zxvf kubeadm-basic.images.tar.gz

for i in $(ls /opt/kubeadm-basic.images/*.tar); do docker load -i $i; done
#也可以写个shell脚本加载

master01

#复制镜像和脚本到 node 节点,并在 node 节点上执行脚本 bash /opt/load-images.sh
scp -r kubeadm-basic.images root@node01:/opt
scp -r kubeadm-basic.images root@node02:/opt

node01上查看

node02上查看

在node01,node02上加载

 for i in $(ls /opt/kubeadm-basic.images/*.tar); do docker load -i $i; done

在master01上初始化kubeadm

//初始化kubeadm
方法一:
kubeadm config print init-defaults > /opt/kubeadm-config.yaml

cd /opt/
vim kubeadm-config.yaml
......
11 localAPIEndpoint:
12   advertiseAddress: 192.168.100.10		#指定master节点的IP地址
13   bindPort: 6443
......
34 kubernetesVersion: v1.15.1				#指定kubernetes版本号
35 networking:
36   dnsDomain: cluster.local
37   podSubnet: "10.244.0.0/16"				#指定pod网段,10.244.0.0/16用于匹配flannel默认网段
38   serviceSubnet: 10.96.0.0/16			#指定service网段
39 scheduler: 
--- #末尾再添加以下内容
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs									#把默认的service调度方式改为ipvs模式

master 开启集群,等待node节点加入

kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs | tee kubeadm-init.log
#--experimental-upload-certs 参数可以在后续执行加入节点时自动分发证书文件,k8sV1.16版本开始替换为 --upload-certs
#tee kubeadm-init.log 用以输出日志

#成功后的提示信息

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.100.10:6443 --token abcdef.0123456789abcdef \\
    --discovery-token-ca-cert-hash sha256:0acf211eb8ac8650b6b962fd63ee8965585505006d2905a838eef29b609f1f1f 

由上面的提示信息说明成功

//查看 kubeadm-init 日志
less kubeadm-init.log

//kubernetes配置文件目录
ls /etc/kubernetes/

//存放ca等证书和密码的目录
ls /etc/kubernetes/pki

//设定kubectl
kubectl需经由API server认证及授权后方能执行相应的管理操作,kubeadm 部署的集群为其生成了一个具有管理员权限的认证配置文件 /etc/kubernetes/admin.conf,它可由 kubectl 通过默认的 “$HOME/.kube/config” 的路径进行加载。

mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

1

2 node01和node02 节点上执行 kubeadm join 命令加入群集

kubeadm join 192.168.100.10:6443 --token abcdef.0123456789abcdef     --discovery-token-ca-cert-hash sha256:0acf211eb8ac8650b6b962fd63ee8965585505006d2905a838eef29b609f1f1f 

3

在master和两个node节点部署网络插件flannel

//所有节点部署网络插件flannel
方法一:
//所有节点上传flannel镜像 flannel.tar 到 /opt 目录,master节点上传 kube-flannel.yml 文件
cd /opt
docker load < flannel.tar

//在 master 节点创建 flannel 资源
kubectl apply -f kube-flannel.yml 


方法二:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

法一:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7HZR60DX-1635812856330)(C:\\Users\\ZhuShaZhi\\AppData\\Roaming\\Typora\\typora-user-images
)]

在master节点查看节点状态(需要等几分钟)

kubectl get nodes
kubectl get pods -n kube-system

测试 pod 资源创建

kubectl  create deployment nginx --image=nginx
kubectl get pods -o wide

查看pod状态

暴露端口提供服务并测试访问

[root@master01 opt]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
[root@master01 opt]# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP        46m
nginx        NodePort    10.111.75.6   <none>        80:30326/TCP   8s
[root@master01 opt]# curl http://node01:30326
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html  color-scheme: light dark; 
body  width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; 
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


扩展3个副本

kubectl get pods -o wide

五、dashboard 部署

所有节点安装dashboard

方法一:
//所有节点上传dashboard镜像 dashboard.tar 到 /opt 目录,master节点上传kubernetes-dashboard.yaml文件
cd /opt/
docker load < dashboard.tar

kubectl apply -f kubernetes-dashboard.yaml

查看所有容器运行状态

kubectl get pods,svc -n kube-system -o wide

//使用火狐或者360浏览器访问
https://node02:30001/
https://192.168.100.30:30001/
#注:如果在宿主机上面用https://node02:30001/访问的话,要在hosts文件做映射

创建service account并绑定默认cluster-admin管理员集群角色

kubectl create serviceaccount dashboard-admin -n kube-system

获取令牌密钥

kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/print $1')

eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tNHpjZDciLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiN2UzMzQwNjAtMjc4ZC00MDVhLTkzMzEtODQwMjhlODJmMzk4Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.ES0HGIFLPLmfFRB_38BHQ9mz6YSmNXR2K-0HZn8T8fEptbAIAESiQ2rWTLb_nr5WJ8k4XeqPMTF5apKbte2wDiy8owcVtbPXAe4OjsM3ve5wuF6YK7IDfTidzIVyRcGPNgZWeOtI5TlEkrhEfWqGfP2VSMnbrBkkBKdsEBNP8SogAFXBw7zYCXnh5mxEb1k6L7OdopLjcTl5UmBn-FlrNuwZP5KKs29J0zCoxqLKSbW28TQzn6y0V8EeTTweNesog0P9IBUxkxY0DmhCDJIMSPdPuZBR7BQ63U0JD0CfhJBO_H-pyseY32yJ3v4dckQ0WKI6sUBo2xsaj7s2_c7pPA

复制token令牌直接登录网站

线面出现的ImagePullBackOff的问题是我的Dashboard包版本问题

六、安装Harbor私有仓库

//修改主机名
hostnamectl set-hostname hub.zcq.com

//所有节点加上主机名映射
echo '192.168.100.40 hub.zcq.com' >> /etc/hosts

安装 docker

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
yum install -y docker-ce docker-ce-cli containerd.io

scp /etc/docker/daemon.json root@node01:/etc/docker/
scp /etc/docker/daemon.json root@node02:/etc/docker/
scp /etc/docker/daemon.json root@hub.zcq.com:/etc/docker/

安装 Harbor

上传 harbor-offline-installer-v1.2.2.tgz 和 docker-compose 文件到 /opt 目录

cd /opt
cp docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose

tar zxvf harbor-offline-installer-v1.2.2.tgz
cd harbor/
vim harbor.cfg
5  hostname = hub.zcq.com
9  ui_url_protocol = https
24 ssl_cert = /data/cert/server.crt
25 ssl_cert_key = /data/cert/server.key
59 harbor_admin_password = Harbor12345

生成证书

mkdir -p /data/cert
cd /data/cert
#生成私钥
openssl genrsa -des3 -out server.key 2048
输入两遍密码:123456

#生成证书签名请求文件
openssl req -new -key server.key -out server.csr
输入私钥密码:123456
输入国家名:CN
输入省名:BJ
输入市名:BJ
输入组织名:ZCQ
输入机构名:ZCQ
输入域名:hub.zcq.com
输入管理员邮箱:admin@zcq.com
其它全部直接回车

#备份私钥
cp server.key server.key.org

#清除私钥密码
openssl rsa -in server.key.org -out server.key
输入私钥密码:123456

#签名证书
openssl x509 -req -days 1000 -in server.csr -signkey server.key -out server.crt

chmod +x /data/cert/*

cd /opt/harbor/
./install.sh

浏览器访问:https://hub.zcq.com
用户名:admin
密码:Harbor12345

//在一个node节点上登录harbor
docker login -u admin -p Harbor12345 https://hub.zcq.com

//上传镜像
docker tag nginx:latest hub.zcq.com/library/nginx:v1
docker push hub.zcq.com/library/nginx:v1

//在master节点上删除之前创建的nginx资源
kubectl delete deployment nginx

kubectl run nginx-deployment --image=hub.zcq.com/library/nginx:v1 --port=80 --replicas=3

kubectl expose deployment nginx-deployment --port=30000 --target-port=80
[root@master01 ~]# kubectl get svc,pods
NAME                       TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes         ClusterIP   10.96.0.1      <none>        443/TCP        7h33m
service/nginx              NodePort    10.111.75.6    <none>        80:30326/TCP   6h46m
service/nginx-deployment   ClusterIP   10.105.85.31   <none>        30000/TCP      10s

NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-6879486858-fbbhk   1/1     Running   0          33s
pod/nginx-deployment-6879486858-kf92g   1/1     Running   0          33s
pod/nginx-deployment-6879486858-s27qp   1/1     Running   0          33s

yum install ipvsadm -y
ipvsadm -Ln

 curl 10.105.85.31:30000


kubectl edit svc nginx-deployment
25   type: NodePort						#把调度策略改成NodePort

kubectl get svc
NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)           AGE
kubernetes         ClusterIP   10.96.0.1      <none>        443/TCP           7h42m
nginx              NodePort    10.111.75.6    <none>        80:30326/TCP      6h56m
nginx-deployment   NodePort    10.105.85.31   <none>        30000:30004/TCP   9m54s

浏览器访问:
192.168.100.10:30004
192.168.100.20:30004
192.168.100.30:30004


以上是关于kubeadm部署k8s+docker+Flannel+dashboard+Harbor仓库的主要内容,如果未能解决你的问题,请参考以下文章

kubeadm部署k8s+docker+Flannel+dashboard+Harbor仓库

kubeadm部署k8s+docker+Flannel+dashboard+Harbor仓库

kubeadm部署k8s+docker+Flannel+dashboard+Harbor仓库

kubeadm部署k8s+docker+Flannel+dashboard+Harbor仓库

Kubeadm 部署k8s集群

Kubeadm 部署k8s集群