kubernetes笔记(七)

news/2024/10/8 20:09:40/

一、service管理

1.clusterIP

1)创建服务

# 资源对象模板
[root@master ~]# kubectl create service clusterip mysvc --tcp=80:80 --dry-run=client -o yaml
[root@master ~]# vim mysvc.yaml
---
kind: Service
apiVersion: v1
metadata:name: mysvc
spec:type: ClusterIPselector:app: webports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl apply -f mysvc.yaml [root@master ~]# kubectl get service

2)解析域名

# 安装工具软件包
[root@master ~]# dnf install -y bind-utils# 查看 DNS 服务地址
[root@master ~]# kubectl -n kube-system get service kube-dns
可以获取CLUSTER-IP的值# 域名解析测试
[root@master ~]# host mysvc.default.svc.cluster.local <CLUSTER-IP字段的值>

3)创建后端应用

[root@master ~]# vim myweb.yaml 
---
kind: Pod
apiVersion: v1
metadata:name: web1labels:app: web   # 服务靠标签寻找后端
spec:containers:- name: apacheimage: myos:httpd[root@master ~]# kubectl apply -f myweb.yaml[root@master ~]# curl http://<host命令解析域名后获取的ip地址>

4)负载均衡

[root@master ~]# sed 's,web1,web2,' myweb.yaml |kubectl apply -f -[root@master ~]# sed 's,web1,web3,' myweb.yaml |kubectl apply -f -[root@master ~]# curl -s http://<host命令解析域名后获取的ip地址>/info.php |grep php_host
php_host:       web1
[root@master ~]# curl -s http://<host命令解析域名后获取的ip地址>/info.php |grep php_host
php_host:       web2
[root@master ~]# curl -s http://<host命令解析域名后获取的ip地址>/info.php |grep php_host
php_host:       web3

5)固定IP服务

[root@master ~]# vim mysvc.yaml 
---
kind: Service
apiVersion: v1
metadata:name: mysvc
spec:type: ClusterIPclusterIP: 10.245.1.80    # 可以设置 ClusterIPselector:app: webports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl delete service mysvc[root@master ~]# kubectl apply -f mysvc.yaml [root@master ~]# kubectl get service

6)端口别名

[root@master ~]# kubectl delete pod --all
pod "web1" deleted
pod "web2" deleted
pod "web3" deleted
[root@master ~]# vim mysvc.yaml 
---
kind: Service
apiVersion: v1
metadata:name: mysvc
spec:type: ClusterIPclusterIP: 10.245.1.80selector:app: webports:- protocol: TCPport: 80targetPort: myhttp    # 使用别名查找后端服务端口[root@master ~]# kubectl apply -f mysvc.yaml [root@master ~]# vim myweb.yaml 
---
kind: Pod
apiVersion: v1
metadata:name: web1labels:app: web
spec:containers:- name: apacheimage: myos:httpdports:               # 配置端口规范- name: myhttp       # 端口别名protocol: TCP      # 协议containerPort: 80  # 端口号[root@master ~]# kubectl apply -f myweb.yaml[root@master ~]# curl http://10.245.1.80

2.nodePort

kind ->Service

spec->type: NodePort

使用kubectl create service nodeport --help查看帮助

1)对外发布服务

[root@master ~]# cp -a mysvc.yaml mysvc1.yaml
[root@master ~]# vim mysvc1.yaml
---
kind: Service
apiVersion: v1
metadata:name: mysvc1
spec:type: NodePort            # 服务类型selector:app: webports:- protocol: TCPport: 80nodePort: 30080         # 映射端口号targetPort: myhttp[root@master ~]# kubectl apply -f mysvc1.yaml 
service/mysvc configured
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)
kubernetes   ClusterIP   10.245.0.1    <none>        443/TCP
mysvc        ClusterIP   10.245.1.80   <none>        80/TCP
mysvc1       NodePort    10.245.1.88   <none>        80:30080/TCP[root@master ~]# curl http://node-0001:30080[root@master ~]# curl http://node-0002:30080[root@master ~]# curl http://node-0003:30080[root@master ~]# curl http://node-0004:30080[root@master ~]# curl http://node-0005:30080

3.Ingress

1)安装控制器

[root@master ~]# cd plugins/ingress
[root@master ingress]# docker load -i ingress.tar.xz
[root@master ingress]# docker images|while read i t _;do[[ "${t}" == "TAG" ]] && continue[[ "${i}" =~ ^"harbor:443/".+ ]] && continuedocker 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[root@master ingress]# kubectl apply -f deploy.yaml
# 通过标签指定在那台机器上发布应用
[root@master ingress]# kubectl label nodes node-0001 ingress-ready="true"[root@master ingress]# kubectl -n ingress-nginx get pods

2)验证后端服务

[[root@master ~]# kubectl get pods,services [root@master ~]# curl http://<CLUSTER-IP字段的ip地址>

3)对外发布服务

[root@master ~]# kubectl get ingressclasses.networking.k8s.io # 资源对象模板
[root@master ~]# kubectl create ingress mying --class=nginx --rule=ns.test.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: nginxrules:- host:ns.test.cnhttp:paths:- path: /pathType: Prefixbackend:service:name: mysvcport:number: 80[root@master ~]# kubectl apply -f mying.yaml [root@master ~]# kubectl get ingress[root@master ~]# curl -H "Host: ns.test.cn" http://<ADDRESS字段的ip地址>

二、web管理插件

1.安装Dashboard

[root@master ~]# cd plugins/dashboard
[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/".+ ]] && continuedocker 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[root@master dashboard]# kubectl apply -f recommended.yaml
[root@master dashboard]# kubectl -n kubernetes-dashboard get pods

2.发布服务

# 查看服务状态
[root@master dashboard]# kubectl -n kubernetes-dashboard get service# 获取服务资源对象文件
[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-dashboardname: kubernetes-dashboardnamespace: kubernetes-dashboard
spec:type: NodePortports:- port: 443nodePort: 30443targetPort: 8443selector:k8s-app: kubernetes-dashboard[root@master dashboard]# kubectl apply -f dashboard-svc.yaml [root@master dashboard]# kubectl -n kubernetes-dashboard get service

三、服务账号与权限

1.创建服务账号

查看yaml对象文件

kubectl -n namespece1 create serviceaccount user1 --dry-run=client -o yaml

验证:kubectl -n namespce1 get serviceaccounts

# 资源对象模板
[root@master ~]# kubectl -n kubernetes-dashboard create serviceaccount kube-admin --dry-run=client -o yaml[root@master ~]# vim admin-user.yaml
---
kind: ServiceAccount
apiVersion: v1
metadata:name: kube-adminnamespace: kubernetes-dashboard[root@master ~]# kubectl apply -f admin-user.yaml [root@master ~]# kubectl -n kubernetes-dashboard get serviceaccounts 

2.获取用户token

[root@master ~]# kubectl -n kubernetes-dashboard create token kube-admin

3.角色与鉴权

资源对象描述作用域
ServiceAccount服务账号,为 Pod 中运行的进程提供了一个身份单一名称空间
Role角色,包含一组代表相关权限的规则单一名称空间
ClusterRole角色,包含一组代表相关权限的规则全集群
RoleBinding将权限赋予用户,Role、ClusterRole 均可使用单一名称空间
ClusterRoleBinding将权限赋予用户,只可以使用 ClusterRole全集群

资源对象权限

createdeletedeletecollectiongetlistpatchupdatewatch
创建删除删除集合获取属性获取列表补丁更新监控

1)普通角色

查看帮助:

kubectl create role --help

kubectl create rolebinding --help

[root@master ~]# kubectl cluster-info dump |grep authorization-mode# 资源对象模板
[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: myrolenamespace: default
rules:
- apiGroups:- ""resources:- podsverbs:- get- list---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: kube-admin-rolenamespace: default
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: myrole
subjects:
- kind: ServiceAccountname: kube-adminnamespace: kubernetes-dashboard[root@master ~]# kubectl apply -f myrole.yaml [root@master ~]# kubectl delete -f myrole.yaml 

2)集群管理员

kubectl create clusterrolebinding --help


Usage:
  kubectl create clusterrolebinding NAME --clusterrole=NAME [--user=username] [--group=groupname]
[--serviceaccount=namespace:serviceaccountname] [--dry-run=server|client|none] [options]

[root@master ~]# kubectl get clusterrole# 资源对象模板
[root@master ~]# kubectl create clusterrolebinding kube-admin-role --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:kube-admin --dry-run=client -o yaml[root@master ~]# vim admin-user.yaml 
---
kind: ServiceAccount
apiVersion: v1
metadata:name: kube-adminnamespace: kubernetes-dashboard---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: kube-admin-role
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin
subjects:
- kind: ServiceAccountname: kube-adminnamespace: kubernetes-dashboard[root@master ~]# kubectl apply -f admin-user.yaml 

http://www.ppmy.cn/news/1536274.html

相关文章

Python进阶--函数进阶

目录 1. 函数多返回值 2. 函数多种传参方式 (1). 位置参数 (2). 关键字参数 (3). 缺省参数 (4). 不定长参数 3. 匿名函数 (1). 函数作为参数传递 (2). lambda匿名函数 1. 函数多返回值 def return_num():return 1# 返回1之后就不会再向下继续执行函数体return 2 resu…

用java编写飞机大战

游戏界面使用JFrame和JPanel构建。背景图通过BG类绘制。英雄机和敌机在界面上显示并移动。子弹从英雄机发射并在屏幕上移动。游戏有四种状态&#xff1a;READY、RUNNING、PAUSE、GAMEOVER。状态通过鼠标点击进行切换&#xff1a;点击开始游戏&#xff08;从READY变为RUNNING&am…

力扣203.移除链表元素

题目链接&#xff1a;203. 移除链表元素 - 力扣&#xff08;LeetCode&#xff09; 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6…

解决方案:机器学习中,出现欠拟合和过拟合,这两种情况分别如何解决

文章目录 一、现象二、解决方案欠拟合&#xff08;Underfitting&#xff09;过拟合&#xff08;Overfitting&#xff09; 一、现象 在工作中&#xff0c;在机器学习中&#xff0c;出现欠拟合和过拟合的时候&#xff0c;需要有对应的解决方法&#xff0c;所以整理一下 二、解决…

c基础面试题

1.static和const的作用 static意为静态的&#xff0c;在C语言中可以修饰变量。如果是全局变量则只能在当前文件范围访问。 如果是函数内的局部变量则延长生命周期到整个程序。这意味着如果函数被多次调用&#xff0c;这个变量不会被重新初始化&#xff0c;而是保留上次调用结…

视频剪辑软件推荐电脑版:这5款剪辑软件不容错过!

在视频剪辑领域&#xff0c;选择合适的软件至关重要。不同的软件各有千秋&#xff0c;有的简单易用&#xff0c;适合新手快速上手&#xff1b;有的功能强大&#xff0c;适合专业团队进行深度编辑。以下是一些电脑版视频剪辑软件的推荐&#xff0c;涵盖了从新手到专业级别的不同…

大数据算法的思维

大数据算法的分类 一、分类算法 1. 决策树算法&#xff1a;通过构建树状结构&#xff0c;对数据进行分类。例如 ID3、C4.5 和 CART 算法&#xff0c;它们根据不同的特征选择标准进行分支划分&#xff0c;最终形成一颗能够对新数据进行分类的决策树。 2. 支持向量机&#xff08…

MySQL多表查询:列子查询

先看我的表数据 dept表 emp表 列子查询&#xff0c;也就是多列作为子查询去寻找一些问题 常用操作符&#xff1a;IN, NOT IN, ANY, SOME, ALL 1.查询 "销售部" 和 "市场部" 的所有员工的信息&#xff08;拆分成以下两个问题&#xff09; a. 查询"销…