文章目录
环境准备
主机名 IP地址 服务组件
master(2C/4C.cpu核心数要求大于2) 192.168.16.16 docker、kubeadm、kubectl、flannel
node01(2C/2G) 192.168.16.18 docker、kubeadm、kubectl、flannel
node02(2C/2G) 192.168.16.20 docker、kubeadm、kubectl、flannel
Harbor节点(hub,kgc.com) 192.168.16.22 docker、docker-compose、harbor-offline-v1.2.2
部署步骤
1、在所有节点上安装Docker和kubeadm
2、部署Kubernetes Master
3、部署容器网络插件
4、部署 Kubernetes Node,将节点加入Kubernetees集群中
5、部署 Dashboard Web页面,可视化查看Kubernetes资源
6、部署 Harbor 私有仓库,存放镜像资源
1 操作系统初始化配置
#所有节点,关闭防火墙规则,关闭selinux,关闭swap交换
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
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
sysctl --system #生效参数
#修改主机名
hostnamectl set-hostname master01
hostnamectl set-hostname node01
hostnamectl set-hostname node02
su
#所有节点修改hosts文件
vim /etc/hosts
192.168.16.16 master01
192.168.16.18 node01
192.168.16.20 node02
#调整内核参数
-开启网桥模式,可将网桥的流量传递给iptables链,关闭ipv6协议
cat > /etc/sysctl.d/k8s.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
2 所有节点安装 Docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors":["https://6ijb8ubo.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等日志系统收集和管理日志。
mkdir -p /etc/systemd/system/docker.service.d
#重启docker服务
systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service
docker info | grep "Cgroup Driver"
Cgroup Driver:systemd
3 所有节点安装 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 kubeadm-1.20.11 kubelet-1.20.11 kubectl-1.20.11
systemctl enable kubelet.service #开机自启kubelet
#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启
4 部署 k8s 集群(仅master节点)
#查看初始化需要的镜像
kubeadm config images list
#在master节点上传v1.20.11.zip压缩包至/opt目录
unzip v1.20.11.zip -d /opt/k8s
cd /opt/k8s/v1.20.11
for i in $(ls *.tar); do docker load -i $i; done
#复制镜像和脚本到node节点,并在node节点上执行脚本加载镜像文件
scp -r /opt/k8s root@node01:/opt
scp -r /opt/k8s root@node02:/opt
for i in $(ls *.tar); do docker load -i $i; done
docker images
node01 和 node02 执行脚本加载镜像文件
4.1 初始化 kubeadm
方法一:
kubeadm config print init-defaults > /opt/kubeadm-config.yaml
cd /opt/
vim kubeadm-config.yaml
............
11 localAPIEndpoint:
12 advertiseAddress: 192.168.16.16 #指定master节点的IP地址
13 bindPort: 6443
............
34 kubernetesVersion: v1.20.11 #指定kubernetes版本号
35 networking:
36 dnsDomain: cluster.local
37 serviceSubnet: 10.96.0.0/16 #指定service网段
38 podSubnet: 10.244.0.0/16 #指定pod网段,10.244.0.0/16用于匹配flannel默认网段
39 scheduler: {}
#末尾再添加一下内容
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs #把默认的kube-proxy调度方式改为ipvs模式
kubeadm init --config=kubeadm-config.yaml --upload-certs | tee /opt/kubeadm-init.log
#--experimental-upload-cert 参数可以在后续执行加入节点时自动分发证书文件,K8S V1.16版本开始替换为--upload-certs
#tee kubeadm-init.log用以输出日志
#查看 kubeadm-init 日志
less kubeadm-init.log
#kubernetes配置文件目录
ls /etc/kubernetes/
#存放ca等证书和密码的目录
ls /etc/kubernetes/pki
最后看到下面的内容就代表成功了
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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.16.16:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:e153e269d8851772ad3339df25219ae76d1d86d1eb0f466685d85eed5fa711e3
#node节点加入集群
kubeadm join 192.168.16.16:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:e153e269d8851772ad3339df25219ae76d1d86d1eb0f466685d85eed5fa711e3
#master查看
kubectl get node
方法二:
kubeadm init \
--apiserver-advertise-address=192.168.16.16 \
--images-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.11 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--token-ttl=0
-----------------------------------------------------
初始化集群需使用kubeadm init命令,可以指定具体参数初始化,也可以指定配置文件初始化。
可选参数:
--apiserver-advertise-address: apiserver通告给其他组件的IP地址,一般应该为Master节点的用于集群内部通信的IP地址,0.0.0.0表示节点上所有可用地址
--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
--token-ttl:默认token的有效期为24小时,如果不想过期,可以加上--token-tt1=0这个参数
------------------------------------------------------
cd /etc/kubernetes/pki
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $H0ME/.kube/config
sudo chown $(id-u):$(id -g) $HOME/. kube/config
#方法二初始化后需要修改kube-proxy的configmap,开启ipvs
kubectl edit configmap kube-proxy -n kube-system
mode: ipvs #修改44行
提示:
Your Kubernetes control-plane has initialized successfully!
4.2 健康检查
kubectl get cs
#Master配置
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
- --bind-address=192.168.16.16 #当前节点的Master地址
#- --port=0
httpGet:
host:192.168.16.16
host:192.168.16.16
vim /etc/kubernetes/manifests/kube-scheduler.yaml
- --bind-address=192.168.16.16
#- --port=0
httpGet:
host:192.168.16.16
host:192.168.16.16
systemctl restart kubelet
kubectl get cs
5 部署网络插件
在master节点上操作
docker load -i flannel.tar
scp flannel.tar node01:/opt
scp flannel.tar node02:/opt
#在node01 和 node02 节点上查看并操作
docker load -i flannel.tar
kubectl apply -f kube-flannel.yml
kubectl get nodes
kubectl get pods -A
测试
kubectl create deployment myapp-ky18 --image=soscscs/myapp:v1
kubectl get pods -o wide
curl 10.244.1.2
kubectl expose deployment myapp-ky18 --port=80 --type=NodePort
扩容和缩容
kubectl scale deployment myapp-ky18 --replicas=3 #扩容
kubectl get pods -o wide
kubectl scale deployment myapp-ky18 --replicas=1 #缩容
kubectl get pods -o wide
6 部署 Dashboard
//在 master 节点上操作
#上传dashboard.tar和metrics-scraper.tar文件到/opt/目录下
cd /opt
docker load -i dashboard.tar
docker load -i metrics-scraper.tar
scp dashboard.tar metrics-scraper.tar node01:/opt
scp dashboard.tar metrics-scraper.tar node02:/opt
#在node01 和 node02 节点上查看并操作
docker load -i dashboard.tar
docker load -i metrics-scraper.tar
#上传 recommended.yaml 文件到 /opt/k8s 目录中,部署 CoreDNS
cd /opt/k8s
vim recommended.yaml
#默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
nodePort: 30001 #添加
type: NodePort #添加
selector:
k8s-app: kubernetes-dashboard
kubectl apply -f recommended.yaml
kubectl get pods -A
#创建service account并绑定默认cluster-admin管理员集群角色
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
#使用输出的token登录Dashboard
https://NodeIP:30001
https://192.168.16.18:30001
把 token 复制到 Kubernetes 仪表盘 token 里
7 安装 Harbor 私有仓库
1.修改主机名并所有节点加上主机名映射
#修改主机名
hostnamectl set-hostname hub.roro.com
//所有节点加上主机名映射
echo '192.168.16. hub.roro.com' >> /etc/hosts
安装 docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
cat > /etc/docker/daemon.json <<EOF
{
"insecure-registries": ["https://hub.roro.com"],
"registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
EOF
#重启docker服务
systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service
#所有node节点都修改docker配置文件,加上私有仓库配置
cat > /etc/docker/daemon.json <<EOF
{
"insecure-registries": ["https://hub.roro.com"],
"registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
EOF
systemctl daemon-reload
systemctl restart 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.roro.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
输入两遍密码:121212
#生成证书签名请求文件
openssl req -new -key server.key -out server.csr
输入私钥密码:121212
输入国家名:CN
输入省名:BJ
输入市名:BJ
输入组织名:KGC
输入机构名:KGC
输入域名:hub.roro.com
输入管理员邮箱:admin@roro.com
其它全部直接回车
#备份私钥
cp server.key server.key.org
#清除私钥密码
openssl rsa -in server.key.org -out server.key
输入私钥密码:121212
#签名证书
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.roro.com
添加例外 -> 确认安全例外
用户名:admin
密码:Harbor12345
#在一个node节点上登录harbor
docker login -u admin -p Harbor12345 https://hub.roro.com
systemctl restart docker
docker-compose ps
docker-compose restart
docker pull nginx:1.14
docker images
docker tag nginx:1.14 hub.roro.com/library/nginx:1.14 #上传镜像
docker images
docker push hub.roro.com/library/nginx:1.14
在master节点上删除之前创建的nginx资源
kubectl get pods
kubectl create deployment nginx-ky18 --image=hub.roro.com/library/nginx:1.14 --port=80 --replicas=3
deployment.apps/nginx-ky18 created
kubectl get pods
kubectl expose deployment nginx-ky18 --port=80 --target-port=80
kubectl get svc,pods
curl curl 10.96.192.163
kubectl edit svc nginx-deployment
26 type: NodePort #把调度策略改成NodePort
kubectl get svc
浏览器访问:http://192.168.16.16:31890
yum install ipvsadm -y
ipvsadm -Ln
kubectl logs
内核参数优化方案
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