标签:kubectl kubernetes harbor -- dashboard kubeadm com docker
一、kubeadm部署
1、环境准备
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
master(2C/4G,cpu核心数要求大于2) 192.168.142.3 docker、kubeadm、kubelet、kubectl、flannel
node01(2C/2G) 192.168.142.4 docker、kubeadm、kubelet、kubectl、flannel
node02(2C/2G) 192.168.142.5 docker、kubeadm、kubelet、kubectl、flannel
Harbor节点(hub.lk.com) 192.168.142.6 docker、docker-compose、harbor-offline-v1.2.2
1、在所有节点上安装Docker和kubeadm
2、部署Kubernetes Master
3、部署容器网络插件
4、部署 Kubernetes Node,将节点加入Kubernetes集群中
5、部署 Dashboard Web 页面,可视化查看Kubernetes资源
6、部署 Harbor 私有仓库,存放镜像资源
#//所有节点,关闭防火墙规则,关闭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.142.3 master01
192.168.142.4 node01
192.168.142.5 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
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104170603006-874888587.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104191448664-1956044937.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104170607114-1934218172.png)
2、所有节点安装docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https:
yum install -y docker-ce docker-ce-cli containerd.io
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors" : [ "https://jd2yg3cx.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 enable --now docker.service
docker info | grep "Cgroup Driver"
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104171219353-1339305651.png)
3、所有节点安装kubeadm,kubelet和kubectl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#定义kubernetes源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https:
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https:
EOF
yum install -y kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1
#开机自启kubelet
systemctl enable kubelet.service
#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104172246383-1227627661.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104172250142-1410537676.png)
二、部署K8S集群
1、查看初始化需要的镜像
1
|
kubeadm config images list
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104173132087-983624764.png)
2、在 master 节点上传 kubeadm-basic.images.tar.gz 压缩包至 /opt 目录
1
2
3
4
|
cd /opt
tar zxvf kubeadm-basic.images.tar.gz
for i in $(ls /opt/kubeadm-basic.images/*.tar); do docker load -i $i; done
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104184412803-1824556196.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104184416240-1527143098.png)
3、复制镜像到两个node节点,并在node节点上加载镜像
1
2
3
|
scp -r kubeadm-basic.images root@node01:/opt
scp -r kubeadm-basic.images root@node02:/opt
for i in $(ls /opt/kubeadm-basic.images/*.tar); do docker load -i $i; done
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104185104592-1992323621.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104185108314-437865407.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211104185111512-1183337563.png)
4、初始化kubeadm(只在master01上部署,让node进入即可)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
方法一:
kubeadm config print init-defaults > /opt/kubeadm-config.yaml
cd /opt/
vim kubeadm-config.yaml
......
11 localAPIEndpoint:
12 advertiseAddress: 192.168.142.3 #指定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
featureGates:
SupportIPVSProxyMode: true
mode: ipvs #把默认的service调度方式改为ipvs模式
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs | tee kubeadm-init.log
#--experimental-upload-certs 参数可以在后续执行加入节点时自动分发证书文件,k8sV1.16版本开始替换为 --upload-certs
#tee kubeadm-init.log 用以输出日志
less kubeadm-init.log
ls /etc/kubernetes/
ls /etc/kubernetes/pki
方法二:
kubeadm init \
--apiserver-advertise-address=0.0.0.0 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.15.1 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
==========================================================
初始化集群需使用kubeadm init命令,可以指定具体参数初始化,也可以指定配置文件初始化。
可选参数:
--apiserver-advertise-address:apiserver的监听地址
--apiserver-bind-port:apiserver的监听端口,默认是6443
--cert-dir:通讯的ssl证书文件,默认/etc/kubernetes/pki
--control-plane-endpoint:控制台平面的共享终端,可以是负载均衡的ip地址或者dns域名,高可用集群时需要添加
--image-repository:拉取镜像的镜像仓库,默认是k8s.gcr.io
--kubernetes-version:指定kubernetes版本
--pod-network-cidr:pod资源的网段,需与pod网络插件的值设置一致。通常,Flannel网络插件的默认为10.244.0.0/16,Calico插件的默认值为192.168.0.0/16;
--service-cidr:service资源的网段
--service-dns-domain:service全域名的后缀,默认是cluster.local
==========================================================
方法二初始化后需要修改 kube-proxy 的 configmap,开启 ipvs
命令:kubectl edit cm kube-proxy -n=kube-system
修改mode: ipvs
提示:
......
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:
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.80.11:6443 --token rc0kfs.a1sfe3gl4dvopck5 \
--discovery-token-ca-cert-hash sha256:864fe553c812df2af262b406b707db68b0fd450dc08b34efb73dd5a4771d37a2
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108135734279-278181611.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108135738921-940467204.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108145734391-2081737886.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108145738089-356957061.png)
5、设定kubectl和加入node节点进集群
1
2
3
4
5
6
7
8
9
|
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
|
kubeadm join 192.168.142.3:6443 --token joal2w.fuk0wt2byvevbpuf \
--discovery-token-ca-cert-hash sha256:7d48eedfc5d78eaa3bec8865dcbcd3e68332e61a8f329816e01bd22fdc832b44
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108150254820-474234496.png)
6、所有节点部署网络插件flannel
1
2
3
4
5
6
7
8
9
10
11
|
方法一:
cd /opt
docker load -i flannel.tar
kubectl apply -f kube-flannel.yml
方法二:
kubectl apply -f https:
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108151440700-1070425867.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108151444050-1711862580.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108151952105-243929867.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108151955352-1486385295.png)
7、在master节点查看节点状态
1
2
|
命令:kubectl get nodes
kubectl get pods -n kube-system
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108152145872-990524086.png)
8、测试pod资源创建
1
2
|
1.kubectl create deployment nginx --image=nginx
2.kubectl get pods -o wide
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108152501515-1733038292.png)
9、暴露端口提供服务
1
2
3
|
1.kubectl expose deployment nginx --port=80 --type=NodePort
2.kubectl get svc
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108152707248-1286423965.png)
10、测试访问
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108152859420-1793533947.png)
11、扩展3个副本
1
2
|
1.kubectl scale deployment nginx --replicas=3
2.kubectl get pods -o wide
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108153237087-531493236.png)
三、安装dashboard
1、所有节点安装dashboard并在主节点加载
1
2
3
4
5
6
7
8
9
10
|
方法一:
cd /opt/
docker load < dashboard.tar
kubectl apply -f kubernetes-dashboard.yaml
方法二:
kubectl apply -f https:
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108161826127-1390140730.png)
2、查看所有容器运行状态
1
|
kubectl get pods,svc -n kube-system -o wide
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108162036194-1995038242.png)
3、浏览器访问
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108185131804-402261283.png)
4、创建service account并绑定默认cluster-admin管理员集群角色
1
2
3
|
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108185715588-1854636979.png)
5、获取令牌密钥
1
|
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}' )
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108185852543-1060725218.png)
6、复制token令牌直接登录网站
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108190942642-1681094378.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108191104176-555317668.png)
四、安装Harbor私有仓库
1、修改主机名,并所有主机添加映射
1
2
3
|
hostnamectl set -hostname harbor
echo '192.168.142.6 hu.jc.com' >> /etc/hosts
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108192432321-452822214.png)
2、安装docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https:
yum install -y docker-ce docker-ce-cli containerd.io
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors" : [ "https://jd2yg3cx.mirror.aliyuncs.com" ],
"exec-opts" : [ "native.cgroupdriver=systemd" ],
"log-driver" : "json-file" ,
"log-opts" : {
"max-size" : "100m"
},
"insecure-registries" : [ "https://hub.jc.com" ]
}
EOF
systemctl start docker
systemctl enable docker
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108192654615-1124769179.png)
3、所有node节点都修改docker配置文件,加上私有仓库配置
1
2
3
|
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors" : [ "https://jd2yg3cx.mirror.aliyuncs.com" ], "exec-opts" : [ "native.cgroupdriver=systemd" ], "log-driver" : "json-file" , "log-opts" : { "max-size" : "100m" }, "insecure-registries" : [ "https://hub.jc.com" ] } EOF systemctl daemon-reloadsystemctl restart docker
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108193115493-1104417585.png)
五、安装Harbor
1、上传harbor-offline-installer-v1.2.2.tgz 和 docker-compose文件到/opt目录
1
2
3
4
5
6
7
8
9
10
11
12
|
1.cd /opt
2.移动两个包进/opt目录
3.cp docker-compose /usr/local/bin/
4.chmod +x /usr/local/bin/docker-compose
5.tar zxvf harbor-offline-installer-v1.2.2.tgz
6.cd harbor/
7.vim harbor.cfg
hostname = hub.jc.com #第5行
ui_url_protocol = https #第9行
ssl_cert = /data/cert/server.crt #第24行
ssl_cert_key = /data/cert/server.key #第25行
harbor_admin_password = Harbor12345 #第59行
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108193808385-255961858.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108194223296-838875380.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108194227028-1145789940.png)
2、生成证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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
输入组织名:KGC
输入机构名:KGC
输入域名:hub.kgc.com
输入管理员邮箱:[email protected]
其它全部直接回车
#备份私钥
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
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108194640428-926080072.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108195035764-1757289028.png)
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108195039313-344551377.png)
3、浏览器访问
1
2
3
|
https:
用户名:admin
密码:Harbor12345
|
![](/i/l/?n=20&i=blog/2392266/202111/2392266-20211108195352679-1132954493.png)
六、harbor仓库测试
1、在一个node节点上登录harbor
1
|
docker login -u admin -p Harbor12345 https:
|
2、上传镜像
1
2
|
docker tag nginx:latest hub.jc.com/library/nginx:jcbb
docker push hub.jc.com/library/nginx:jcbb
|
3、在master节点上删除之前创建的nginx资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
1.kubectl delete deployment nginx
2.kubectl run nginx-deployment --image=hub.gxd.com/library/nginx:gxd111 --port=80 --replicas=3
3.kubectl expose deployment nginx-deployment --port=30000 --target-port=80 #做端口映射,集群端口与pod端口
4.kubectl get svc,pods
5.yum install ipvsadm -y
ipvsadm -Ln
6.curl 10.100.118.35:30000 #集群ip+集群端口
7.kubectl edit svc nginx-deployment
8.kubectl get svc
9.浏览器访问:
192.168.80.11:30189
192.168.80.12:30189
192.168.80.13:30189
|
七、内核参数优化方案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0 #禁止使用 swap 空间,只有当系统内存不足(OOM)时才允许使用它
vm.overcommit_memory=1 #不检查物理内存是否够用
vm.panic_on_oom=0 #开启 OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963 #指定最大文件句柄数
fs.nr_open=52706963 #仅4.4以上版本支持
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
|
标签:kubectl,
kubernetes,
harbor,
--,
dashboard,
kubeadm,
com,
docker
From: https://www.cnblogs.com/y0226/p/17099536.html