赞
踩
往期回顾:
云端技术驾驭DAY01——云计算底层技术奥秘、云服务器磁盘技术、虚拟化管理、公有云概述
云端技术驾驭DAY02——华为云管理、云主机管理、跳板机配置、制作私有镜像模板
云端技术驾驭DAY03——云主机网站部署、web集群部署、Elasticsearch安装
云端技术驾驭DAY04——Logstash安装部署及插件模块
云端技术驾驭DAY06——容器技术概述、镜像与容器管理、定制简单镜像、容器内安装部署服务
云端技术驾驭DAY07——Dockerfile详解、容器镜像制作、私有仓库
云端技术驾驭DAY08——部署容器服务、Compose微服务管理、harbor仓库部署及管理
云端技术驾驭DAY09——k8s集群安装部署、calico插件部署、计算节点配置管理
云端技术驾驭DAY10——kubectl命令详解、Pod创建过程、Pod的生命周期、定制Pod、资源对象文件
云端技术驾驭DAY11——资源对象文件、Pod自定义命令、多容器Pod、资源监控工具
云端技术驾驭DAY12——Pod调度策略、Pod标签管理、Pod资源配额与限额、全局资源配额与限额策略
云端技术驾驭DAY13——Pod污点、容忍策略、Pod优先级与抢占、容器安全
云端技术驾驭DAY14——存储卷管理、临时卷、持久卷、PV/PVC管理
<服务名称>.<名称空间>.svc.cluster.local
[root@master ~]# vim mysvc.yaml --- apiVersion: v1 kind: Service # 资源对象类型 metadata: name: mysvc # 资源对象名称 spec: ports: - port: 80 # 监听的端口 protocol: TCP # 协议 targetPort: 80 # 后端服务端口 selector: # 选择算符 app: web # Pod标签 type: ClusterIP # 服务类型 [root@master ~]# kubectl apply -f mysvc.yaml service/mysvc created [root@master ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 7d19h mysvc ClusterIP 10.245.74.82 <none> 80/TCP 7s
[root@master ~]# dnf -y install bind-utils
[root@master ~]# kubectl -n kube-system get service kube-dns # 查看 DNS 服务地址
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
kube-dns ClusterIP 10.245.0.10 <none> 53/UDP,53/TCP,9153/TCP
[root@master ~]# host mysvc.default.svc.cluster.local 10.245.0.10 # 域名解析测试
Using domain server:
Name: 10.245.0.10
Address: 10.245.0.10#53
Aliases:
mysvc.default.svc.cluster.local has address 10.245.74.82
[root@master ~]# vim myweb.yaml # 创建后端应用 --- kind: Pod apiVersion: v1 metadata: name: web1 labels: app: web # 服务靠标签寻找后端 spec: containers: - name: apache image: myos:httpd [root@master ~]# kubectl apply -f myweb.yaml pod/web1 created [root@master ~]# curl 10.245.74.82 Welcome to The Apache.
[root@master ~]# sed 's,web1,web2,' myweb.yaml | kubectl apply -f -
pod/web2 created
[root@master ~]# sed 's,web1,web3,' myweb.yaml | kubectl apply -f -
pod/web3 created
[root@master ~]# curl 10.245.74.82/info.php | grep php_host
php_host: web3
[root@master ~]# curl 10.245.74.82/info.php | grep php_host
php_host: web2
[root@master ~]# curl 10.245.74.82/info.php | grep php_host
php_host: web1
kubectl cluster-info dump | grep -i service-cluster-ip-range
查看IP范围[root@master ~]# vim mysvc.yaml --- apiVersion: v1 kind: Service metadata: name: mysvc spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: web type: ClusterIP clusterIP: 10.245.1.80 [root@master ~]# kubectl delete service mysvc service "mysvc" deleted [root@master ~]# kubectl apply -f mysvc.yaml service/mysvc created [root@master ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 7d20h mysvc ClusterIP 10.245.1.80 <none> 80/TCP 7s
[root@master ~]# vim mysvc.yaml
---
apiVersion: v1
kind: Service
metadata:
name: mysvc
spec:
ports:
- port: 80
protocol: TCP
targetPort: myhttp # 使用别名查找后端服务端口
selector:
app: web
type: ClusterIP
clusterIP: 10.245.1.80
[root@master ~]# vim myweb.yaml --- kind: Pod apiVersion: v1 metadata: name: web1 labels: app: web spec: containers: - name: apache image: myos:httpd ports: # 配置端口规范 - name: myhttp # 声明端口别名 protocol: TCP # 协议 containerPort: 80 # 端口号
[root@master ~]# kubectl delete service mysvc
service "mysvc" deleted
[root@master ~]# kubectl apply -f mysvc.yaml
service/mysvc created
[root@master ~]# kubectl apply -f myweb.yaml
pod/web1 created
[root@master ~]# curl 10.245.1.80 # 通过别名和指定IP访问成功
Welcome to The Apache.
[root@master ~]# vim mysvc1.yaml --- kind: Service apiVersion: v1 metadata: name: mysvc1 spec: type: NodePort # 指定服务类型 selector: app: web ports: - protocol: TCP port: 80 nodePort: 30080 # 可选,不指定则使用随机端口 targetPort: 80 [root@master ~]# kubectl apply -f mysvc1.yaml service/mysvc1 created [root@master ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 7d21h mysvc1 NodePort 10.245.238.47 <none> 80:30080/TCP 6m29s
root@master ~]# curl node-0001:30080
Welcome to The Apache.
[root@master ~]# curl node-0002:30080
Welcome to The Apache.
[root@master ~]# curl node-0003:30080
Welcome to The Apache.
[root@master ~]# curl node-0004:30080
Welcome to The Apache.
[root@master ~]# curl node-0005:30080
Welcome to The Apache.
[root@master ingress]# docker load -i ingress.tar.xz # 导入Ingress镜像
[root@master ingress]# docker images|while read i t _;do # 上传镜像
> [[ "${t}" == "TAG" ]] && continue
> [[ "${i}" =~ ^"harbor:443/".+ ]] && continue
> docker tag ${i}:${t} harbor:443/plugins/${i##*/}:${t}
> docker push harbor:443/plugins/${i##*/}:${t}
> docker rmi ${i}:${t} harbor:443/plugins/${i##*/}:${t}
> done
[root@master ingress]# sed -ri 's,^(\s*image: )(.*/)?(.+)@.*,\1harbor:443/plugins/\3,' deploy.yaml # 修改以下三行
458: image: harbor:443/plugins/controller:v1.5.1
565: image: harbor:443/plugins/kube-webhook-certgen:v20220916-gd32f8c343
614: image: harbor:443/plugins/kube-webhook-certgen:v20220916-gd32f8c343
[root@master ingress]# kubectl apply -f deploy.yaml
[root@master ingress]# kubectl label nodes node-0001 ingress-ready="true" # 通过标签指定在那台机器上发布应用
node/node-0001 labeled
[root@master ~]# kubectl -n ingress-nginx get pods
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-bnk2n 0/1 Completed 0 2m12s
ingress-nginx-admission-patch-jsbxh 0/1 Completed 0 2m12s
ingress-nginx-controller-bbbb5f675-m4tck 1/1 Running 0 2m12s
[root@master ~]# kubectl get pods,service
NAME READY STATUS RESTARTS AGE
pod/web1 1/1 Running 1 (31m ago) 3h52m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 8d
service/mysvc ClusterIP 10.245.1.80 <none> 80/TCP 16m
service/mysvc1 NodePort 10.245.209.208 <none> 80:30080/TCP 3h1m
[root@master ~]# curl 10.245.1.80
Welcome to The Apache.
kubectl create ingress mying --class=nginx --rule=nsd.tedu.cn/*=mysvc:80 --dry-run=client -o yaml
[root@master ~]# vim mying.yaml --- kind: Ingress apiVersion: networking.k8s.io/v1 metadata: name: mying spec: ingressClassName: nginx # 使用的类名称 rules: # ingress 规则定义 - host: mying.cn # 域名定义,可以不写 http: # 协议 paths: # 访问的路径定义 - path: / # 访问的url路径 pathType: Prefix # 路径的类型[Exact Prefix] backend: # 后端服务 service: # 服务声明 name: mysvc # 服务名称 port: # 端口号声明 number: 80 # 访问服务的端口号
[root@master ~]# kubectl apply -f mying.yaml
ingress.networking.k8s.io/mying created
[root@master ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
mying nginx mying.cn 80 12s
[root@master ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
mying nginx mying.cn 192.168.1.51 80 14m
[root@master ~]# curl -H "Host: mying.cn" http://192.168.1.51
Welcome to The Apache.
[root@master dashboard]# docker load -i dashboard.tar.xz [root@master dashboard]# docker images|while read i t _;do > [[ "${t}" == "TAG" ]] && continue > [[ "${i}" =~ ^"harbor:443/".+ ]] && continue > docker tag ${i}:${t} harbor:443/plugins/${i##*/}:${t} > docker push harbor:443/plugins/${i##*/}:${t} > docker rmi ${i}:${t} harbor:443/plugins/${i##*/}:${t} > done [root@master dashboard]# sed -ri 's,^(\s*image: )(.*/)?(.+),\1harbor:443/plugins/\3,' recommended.yaml 193: image: harbor:443/plugins/dashboard:v2.7.0 278: image: harbor:443/plugins/metrics-scraper:v1.0.8 [root@master dashboard]# kubectl apply -f recommended.yaml [root@master dashboard]# kubectl -n kubernetes-dashboard get pods NAME READY STATUS RESTARTS AGE dashboard-metrics-scraper-66f6f56b59-trznx 1/1 Running 0 28s kubernetes-dashboard-65ff57f4cf-7dn87 1/1 Running 0 28s
[root@master dashboard]# sed -n '30,45p' recommended.yaml > dashboard-svc.yaml [root@master dashboard]# vim dashboard-svc.yaml --- kind: Service apiVersion: v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboard spec: type: NodePort # 添加类型,以NodePort形式发布 ports: - port: 443 nodePort: 30443 # 指定端口 targetPort: 8443 selector: k8s-app: kubernetes-dashboard [root@master dashboard]# kubectl apply -f dashboard-svc.yaml service/kubernetes-dashboard configured [root@master dashboard]# kubectl -n kubernetes-dashboard get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE dashboard-metrics-scraper ClusterIP 10.245.155.177 <none> 8000/TCP 8m14s kubernetes-dashboard NodePort 10.245.210.63 <none> 443:30443/TCP 8m14s
https://公网IP:30443
,出现如下界面即成功[root@master dashboard]# vim admin-user.yaml
---
kind: ServiceAccount
apiVersion: v1
metadata:
name: kube-admin
namespace: kubernetes-dashboard
[root@master dashboard]# kubectl apply -f admin-user.yaml
serviceaccount/kube-admin created
[root@master dashboard]# kubectl -n kubernetes-dashboard get sa
NAME SECRETS AGE
default 0 30m
kube-admin 0 11s
kubernetes-dashboard 0 30m
[root@master dashboard]# kubectl -n kubernetes-dashboard create token kube-admin
eyJhbGciOiJSUzI1NiIsImtpZCI6Im... ... # 这一串字符就是登陆用的token
[root@master dashboard]# kubectl cluster-info dump | grep authorization-mode
"--authorization-mode=Node,RBAC",
资源对象 | 描述 | 作用域 |
---|---|---|
ServiceAccount | 服务账号,为 Pod 中运行的进程提供了一个身份 | 单一名称空间 |
Role | 角色,包含一组代表相关权限的规则 | 单一名称空间 |
ClusterRole | 角色,包含一组代表相关权限的规则 | 全集群 |
RoleBinding | 将权限赋予用户,Role、ClusterRole 均可使用 | 单一名称空间 |
ClusterRoleBinding | 将权限赋予用户,只可以使用 ClusterRole | 全集群 |
create | delete | deletecollection | get | list | patch | update | watch |
---|---|---|---|---|---|---|---|
创建 | 删除 | 删除集合 | 获取属性 | 获取列表 | 补丁 | 更新 | 监控 |
# 资源对象模板 [root@master ~]# kubectl -n default create role myrole --resource=pods --verb=get,list --dry-run=client -o yaml [root@master ~]# kubectl -n default create rolebinding kube-admin-role --role=myrole --serviceaccount=kubernetes-dashboard:kube-admin --dry-run=client -o yaml [root@master ~]# vim myrole.yaml --- kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: myrole # 角色名称 namespace: default rules: # 规则 - apiGroups: # 资源对象所属组信息 - "" # 分组信息 resources: # 要设置权限的资源对象 - pods # 授权资源对象名称 verbs: # 权限设置 - get # 权限 - list # 权限
... ... # 接着上面的写 --- kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: kube-admin-role # 授权策略名称 namespace: default roleRef: # 关联权限 apiGroup: rbac.authorization.k8s.io # 角色对象组 kind: Role # 角色对象 name: myrole # 角色名称 subjects: # 授权信息 - kind: ServiceAccount # 帐号资源对象 name: kube-admin # 帐号名称 namespace: kubernetes-dashboard # 帐号所在的名称空间 [root@master dashboard]# kubectl apply -f myrole.yaml role.rbac.authorization.k8s.io/myrole created rolebinding.rbac.authorization.k8s.io/kube-admin-role created
... ... --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: kube-admin-role namespace: default roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: kube-admin namespace: kubernetes-dashboard [root@master dashboard]# kubectl apply -f myrole.yaml role.rbac.authorization.k8s.io/myrole created clusterrolebinding.rbac.authorization.k8s.io/kube-admin-role created
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。