1、组件说明(K8s架构)
APISERVER:所有服务访问同一入口;
CrontrollerManager:维持副本期望数目;
Scheduler:负责介绍任务,选择合适的节点进行分配任务;
ETCD:键值对数据库,存储k8s集群所有重要信息(持久化);
Kubelet:直接跟容器引擎交互实现容器的生命周期管理;
Kube-Proxy:负责写入规则至IPTABLES, IPVS 实现服务映射访问;
COREDNS: 可以为集群终端的svc创建一个域名IP的对应关系解析;
DASHBOARD: 给k8s集群提供一个B/S 结构访问体系;
INGRESS CONTROLLER:官方只能实现四层代理类,INGRESS可以实现七层代理
FEDETATYUION: 提供一个可以跨集群中心多k8s统一管理功能
PROMETHEUS: 提供一K8s集群的监控能力;
ELK:提供 K8S 集群日志同意分析介入平台;
2、Pod概念
自主式Pod
控制器管理的Pod
3、集群安装
3.1配置静态 IP
# 系统安装 centos
# 虚拟机使用仅主机模式
vi /etc/sysconfig/network-scripts/ifcfg-ens33
1 TYPE="Ethernet"
2 PROXY_METHOD="none"
3 BROWSER_ONLY="no"
4 BOOTPROTO="static"
5 DEFROUTE="yes"
6 IPV4_FAILURE_FATAL="no"
7 IPV6INIT="yes"
8 IPV6_AUTOCONF="yes"
9 IPV6_DEFROUTE="yes"
10 IPV6_FAILURE_FATAL="no"
11 IPV6_ADDR_GEN_MODE="stable-privacy"
12 NAME="ens33"
13 UUID="6769a131-673e-4869-bc7b-2a5e7f5bbbb6"
14 DEVICE="ens33"
15 ONBOOT="yes"
# 修改静态IP
16 IPADDR=192.168.66.10
17 NETMASK=255.255.255.0
18 GATEWAY=192.168.66.1
19 DNS1=192.168.66.1
20 DNS2=114.114.114.114
3.2 设置系统主机名以及Host文件的相互解析
hostnamectl set-hostname k8s-master
# 解析文件
vi /etc/hosts
192.168.66.10 k8s-master
192.168.66.20 k8s-node01
192.168.66.21 k8s-node02
3.3 安装依赖包
yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git
3.4 设置防火墙为iptables并设置空规则
systemctl stop firewalld && systemctl disable firewalld
yum install -y iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
3.5 关闭SELINUX
swapoff -a && sed -i '/ swap/s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
3.6 调整内核
x cat > kubernetes.conf <<EOFnet.bridge.bridge-nf-call-iptables=1 # 开启网桥模式net.bridge.bridge-nf-call-ip6tables=1 # 开启网桥模式net.ipv4.ip_forward=1net.ipv4.tcp_tw_recycle=0vm.swappiness=0 # 禁止使用swap空间,只有当西戎00M时才允许使用它vm.overcommit_memory=1 # 不检查物理内存是否够用vm.panic_on_oom=0 # 开启oomfs.inotify.max_user_instances=8192fs.inotify.max_user_watches=1048576fs.file-max=52706963fs.nr_open=52706963net.ipv6.conf.all.disable_ipv6=1 # 关闭ipv6协议net.netfilter.nf_conntrack_max=2310720EOFcp kubernetes.conf /etc/sysctl.d/kubernetes.confsysctl -p /etc/sysctl.d/kubernetes.conf
cp kubernetes.conf /etc/sysctl.d/kubernetes.conf
sysctl -p /etc/sysctl.d/kubernetes.conf
3.7 调整系统时区
# 设置系统时区为 中国/上海
timedatecel set-timezone Asia/Shanghai
# 将当前的UTC时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond
关闭系统不需要的服务
systemctl stop postfix && systemctl disable postfix
3.8 设置rsyslogd和systemd journald
mkdir /var/log/journal # 持久化保存日志目录
mkdir /etc/systemd/journald.conf.d # 创建配置文件存放目录
# 创建配置文件
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[journal]
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
# 最大占用空间10G
SystemMaxUse=10G
# 但日志文件最大200M
SystemMaxFileSize=200M
# 日志保存时间两周
MaxRetetionSec=2week
# 日志转发到syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald
3.9 升级系统内核为4.44
CentOS_7.x 系统自带的3.10.x内核存在一些Bugs,导致运行docker,kubernetes不稳定
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 是否包含initrd16配置如果没有,再安装一次
yum --enablerepo=elrepo-kernel install -y kernel-lt
# 设置开机从新内核启动
grub2-set-default "CentOS Linux (4.4.182-el7.elrepo.x86_64) 7 (Core)"
3.10 kube-proxy 开启ipvs的前置条件
modprobe br_netfilter
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe – ip_vs
modprobe – ip_vs_rr
modprobe – ip_vs_wrr
modprobe – ip_vs_sh
modprobe – nf_conntrack_ipv4
EOF
# 修改权限
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ipvs -e nf_conntrack_ipv4
3.11 安装Docker
# 安装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
# 更新docker
yum update -y && yum install -y docker-ce
# 创建 /etc/docker 目录
mkdir /etc/docker
# 配置 /etc/docker/daemon.json
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
EOF
# 创建目录存放Docker配置文件目录
mkdir -p /etc/systemd/system/docker.service.d
# 重启docker服务
systemctl daemon-reload && systemctl restart docker && systemctl enable docker
3.12 安装Kubeadm(主从配置)
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum -y install kubeadm-1
.15.1 kubectl-1.15.1 kubelet-1.15.1
systemctl enable kubelet.service
3.13 初始化节点
kubeadm config print init-defaults > kubeadm-config.yaml
localAPIEndpoint:
advertiseAddress: 192.168.66.10
kubernetesVersion: v1.15.1
networking:
podSubnet: "10.244.0.0/16"
serviceSubnet: 10.96.0.0/12
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: kubeProxyConfiguration
featureGates:
SupportIPVSProxyMode: true
mode: ipvs
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs | tee kubeadm-init.log
3.14 加入主节点以及其余工作节点
# 执行安装日志中的加入命令即可
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3.15 部署网络
mkdir install-k8s
mv kubeadm-init.log kubeadm-config.yaml install-k8s/
cd install-k8s
mkdir core
mv * core/
mkdir plugin
# 创建kube-flannel.yml
kubectl create -f kube-flannel.yml
kubeclt apply -f
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[root@k8s-master install-k8s]#
NAME READY STATUS RESTARTS AGE
coredns-5c98db65d4-mnc4m 1/1 Running 0 70m
coredns-5c98db65d4-xrbtb 1/1 Running 0 70m
etcd-k8s-master 1/1 Running 0 69m
kube-apiserver-k8s-master 1/1 Running 0 69m
kube-controller-manager-k8s-master 1/1 Running 0 69m
kube-flannel-ds-amd64-tcsxk 1/1 Running 0 2m57s
kube-proxy-m5fvn 1/1 Running 0 69m
kube-scheduler-k8s-master 1/1 Running 0 69m
[root@k8s-master install-k8s]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 71m v1.15.1
4、构建habor
# 安装docker 同上面安装方式相同
# 构建Docker 信任证书(假证书)
vim /etc/docker/daemon.json
{
"insecure-registries":["https://hub.atguigu.com"]
}
# 上传docker-compose
[root@k8s-habor ~]# mv docker-compose /usr/local/bin/
# 修改docker-compose文件权限
[root@k8s-habor ~]# chmod a+x /usr/local/bin/docker-compose
# 上传harbor压缩包
# 解压文件
[root@k8s-habor ~]# tar -zxvf harbor-offline-installer-v1.2.0.tgz
[root@k8s-habor ~]# ls
harbor harbor-offline-installer-v1.2.0.tgz
[root@k8s-habor ~]# mv harbor /usr/local
[root@k8s-habor ~]# cd /user/local/harbor
# 修改域名
[root@k8s-habor ~]# vi harbor.cfg
# 修改域名
hostname=hub.atguigu.com
# 创建证书生成目录
[root@k8s-habor ~]# mkdir -p /data/cert
[root@k8s-habor ~]# cd !$
# 创建证书/生成私钥
[root@k8s-habor ~]# openssl genrsa -des3 -out server.key 2048
#Generating RSA private key, 2048 bit long modulus
#................................................+++
#....+++
#e is 65537 (0x10001)
#Enter pass phrase for server.key:
#Verifying - Enter pass phrase for server.key:
# 输入密码和确认密码
# 创建一个证书请求csr
[root@k8s-habor ~]# openssl req -new -key server.key -out server.csr
# 备份私钥
[root@k8s-habor ~]# cp server.key server.key.org
# 转换证书
[root@k8s-habor ~]# openssl rsa -in server.key.org -out server.key # 退掉密码
# 根据证书请求去签名
[root@k8s-habor ~]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
# 修改证书权限
[root@k8s-habor ~]# chmod a+x *
# 进入到harbor目录安装harbor
[root@k8s-habor ~]# cd -
# 安装harbor
[root@k8s-habor ~]# ./install
4.1 修改window系统的host文件
C:\Windows\System32\drivers\etc\host
192.168.66.100 hub.atguigu.com
用户名:admin 密码:Harbor12345
[root@k8s-master ~]# docker login https://hub.atguigu.com
Username:admin
Password:Harbor12345
# 拉取镜像后上传到harbor仓库
[root@k8s-master ~]# docker pull wangyanglinux/myapp:v1
# 将镜像上传到harbor仓库
# 修改本地仓库镜像名

[root@k8s-master ~]# docker tag wangyanglinux/myapp:v1 hub.atguigu.com/library/myapp:v1
[root@k8s-master ~]# docker push hub.atguigu.com/library/myapp:v1

# 删除镜像文件通过pod拉取
[root@k8s-master ~]# docker rmi hub.atguigu.com/library/myapp:v1
[root@k8s-master ~]# docker rmi wangyanglinux/myapp:v1
# 测试k8s集群是否可用以及是否与镜像仓库能够链接
[root@k8s-master ~]# kubectl run nginx-deployment --image=hub.atguigu.com/library/myapp:v1 --port=80 --replicas=1
# image 指定仓库
# port 指定暴露端口,可不指定k8s扁平化管理会自动分配端口
# replicas 指定复本个数
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-deployment created
# 查看deployment
[root@k8s-master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 47s
# 查看rs
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-85756b779 1 1 1 2m20s
# 查看pod
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-85756b779-5c89c 1/1 Running 0 3m6s
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-85756b779-5c89c 1/1 Running 0 3m10s 10.244.2.2 k8s-node02 <none> <none>
# 扩容副本
[root@k8s-master ~]# kubectl scale --replicas=3 deploy/nginx-deployment
deployment.extensions/nginx-deployment scaled
[root@k8s-master ~]# kubectl expose --help
Expose a resource as a new Kubernetes service.
Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector for that
resource as the selector for a new service on the specified port. A deployment or replica set will be exposed as a
service only if its selector is convertible to a selector that service supports, i.e. when the selector contains only
the matchLabels component. Note that if no port is specified via --port and the exposed resource has multiple ports, all
will be re-used by the new service. Also if no labels are specified, the new service will re-use the labels from the
resource it exposes.
Possible resources include (case insensitive):
pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)
Examples:
# Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000.
kubectl expose rc nginx --port=80 --target-port=8000
# Create a service for a replication controller identified by type and name specified in "nginx-controller.yaml",
which serves on port 80 and connects to the containers on port 8000.
kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000
# Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
kubectl expose pod valid-pod --port=444 --name=frontend
# Create a second service based on the above service, exposing the container port 8443 as port 443 with the name
"nginx-https"
kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
# Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
kubectl expose rc streamer --port=4100 --protocol=UDP --name=video-stream
# Create a service for a replicated nginx using replica set, which serves on port 80 and connects to the containers on
port 8000.
kubectl expose rs nginx --port=80 --target-port=8000
# Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000.
kubectl expose deployment nginx --port=80 --target-port=8000
Options:
--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
--cluster-ip='': ClusterIP to be assigned to the service. Leave empty to auto-allocate, or set to 'None' to create
a headless service.
--dry-run=false: If true, only print the object that would be sent, without sending it.
--external-ip='': Additional external IP address (not managed by Kubernetes) to accept for the service. If this IP
is routed to a node, the service can be accessed by this IP in addition to its generated service IP.
-f, --filename=[]: Filename, directory, or URL to files identifying the resource to expose a service
--generator='service/v2': 使用 generator 的名称. 这里有 2 个 generators: 'service/v1' 和 'service/v2'.
为一个不同地方是服务端口在 v1 的情况下叫 'default', 如果在 v2 中没有指定名称.
默认的名称是 'service/v2'.
-k, --kustomize='': Process the kustomization directory. This flag can't be used together with -f or -R.
-l, --labels='': Labels to apply to the service created by this call.
--load-balancer-ip='': IP to assign to the LoadBalancer. If empty, an ephemeral IP will be created and used
(cloud-provider specific).
--name='': 名称为最新创建的对象.
-o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--overrides='': An inline JSON override for the generated object. If this is non-empty, it is used to override the
generated object. Requires that the object supply a valid apiVersion field.
--port='': 服务的端口应该被指定. 如果没有指定, 从被创建的资源中复制
--protocol='': 创建 service 的时候伴随着一个网络协议被创建. 默认是 'TCP'.
--record=false: Record current kubectl command in the resource annotation. If set to false, do not record the
command. If set to true, record the command. If not set, default to updating the existing annotation value only if one
already exists.
-R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
related manifests organized within the same directory.
--save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
--selector='': A label selector to use for this service. Only equality-based selector requirements are supported.
If empty (the default) infer the selector from the replication controller or replica set.)
--session-affinity='': If non-empty, set the session affinity for the service to this; legal values: 'None',
'ClientIP'
--target-port='': Name or number for the port on the container that the service should direct traffic to.
Optional.
--template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
--type='': Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is 'ClusterIP'.
Usage:
kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
# 开启80端口为外部访问端口
[root@k8s-master ~]# kubectl expose deployment nginx-deployment --port=30000 --target-port=80
service/nginx-deployment exposed
# 开启外网访问,修改type:NodePort
[root@k8s-master ~]# kubectl edit svc nginx-deployment
type:NodePort
service/nginx-deployment edited

1万+



被折叠的 条评论
为什么被折叠?



