Kubectl
kubectl是k8s集群的命令工具,通过kubectl能够对集群本身进行管理,并能够在集群进行容器化应用的安装部署
kubectl的命令语法
kubectl [command] [type] [name] [flags]
- command:指定要对资源执行的操作,例如:create,get、describe和delete
- type:指定资源类型,资源类型是大小写,例如node,pod
- name:指定资源的名称,大小写敏感
- flags:指定可选参数,例如 -s 或者 - server 参数指定kubenetes api serer的地址和端口
获取更多信息
kubectl --help
字段 | 说明 |
---|---|
apiVersion | API版本 |
kind | 资源类型 |
metadata | 资源元数据 |
spec | 资源规格 |
replicas | 副本数量 |
selector | 标签选择器 |
template | pod模板 |
metadata | pod元数据 |
spec | pod规格 |
containers | 容器配置 |
编写yaml文件
使用kubectl create 命令生成yaml文件,还未创建pod
使用kubectl get命令导出yaml文件,已经存在的pod
Namespace
名称空间用来隔离资源,用来对集群的资源进行划分,默认只隔离资源,不隔离网络
kubectl create ns hello
kubectl delete ns hello
kubectl get ns # 获取名称空间
apiVersion: v1
kind: Namespace
metadata:
name: hello
Pod
运行中的一组容器,pod是k8s中应用的最小单位,允许一个pod中运行多个容器,k8s通过管理pod来操作pod内的所有容器
kubectl run mynginx --image=nginx
kubectl delete pod mynginx
kubectl describe pod mynginx ## 查看描述信息
kubectl get pod // 默认查看default 名称空间的pod
kubectl exec -it podName -- /bin/bash
apiVersion: v1
kind: Pod
metadata:
lables:
run: mynginx
name: mynginx # pod的名字
spec:
containers:
- image: nginx
name: mynginx # 容器的名字,随便写
Deployment
控制pod,是pod拥有多个副本,自愈,扩容等能力
kubectl run mynginx --image=nginx
kubectl create deployment mytomcat --image=tomcat:8.5.68 --replicas=3 # 部署三份
kubectl get deploy // 查看部署列表
kubectl delete deploy mytomcat // 删除部署
kubectl scale # 扩容,缩容
kubectl scale deploy/mytomcat --replicas=5 # 启动五个副本
kubectl edit deploy tomcat //编辑,将replicas 的值修改
故障转移
如果运行的pod意外停止,k8s会重新启动,如果启动失败,会在其他的节点上,再起一个pod
滚动更新
kubectl set deploy/mytomcat image nginx=nginx:1.16.1 --record=true // 设置mytomcat部署下的nginx的image=nginx:1.16.1
kubectl rollout history deployment/mytomcat // 查看部署历史记录
kubectl rollout undo deploy/mytomcat --to-reversion=1 // 回退到第一次版本的记录
Service
service:pod的服务发现与负载均衡,在pod内部,可以直接通过
kubectl get service // 查看service 列表
kubectl expose deploy mytomcat --port=8000 --target-port=80 // 暴露部署mytomcat的80端口到8000端口
kubectl get pod --show-labels // 查看pod的标签
kubectl delete service mytomcat // 删除服务
apiVersion: v1
kind: Service
metadata:
labels:
app: mytomcat
name: mytomcatSvc
spec:
selector:
app: mytomcat
ports:
-port: 8000 # 服务暴露8000端口出去
protocol: TCP
targetPort:80 # 暴露的容器端口
Ingress
Ingress:service的统一网关入口
安装
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.47.0/deploy/static/provider/baremetal/deploy.yaml
// 修改镜像,将deploy.yaml中的image的值修改成
registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/ingress-nginx-controller:v0.46.0
// 安装
kubectl apply -f deploy.yaml
// 查看
kubectl get svc -A // 查看所有命名空间的服务
创建
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-host-bar
spec:
ingressClassName: nginx
rules:
- host: "hello.atguigu.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
serviceName: hello-server
servicePort: 8000
- host: "demo.atguigu.com"
http:
paths:
- pathType: Prefix
path: "/nginx" # 把请求会转给下面的服务,下面的服务一定要能处理这个路径,不能处理就是404
backend:
serviceName: nginx-demo ## java,比如使用路径重写,去掉前缀nginx
servicePort: 8000
存储抽象
原生方式挂在
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-pv-demo
name: nginx-pv-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx-pv-demo
template:
metadata:
labels:
app: nginx-pv-demo
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
nfs:
server: 192.168.0.9
path: /nfs/data/nginx-pv
1、PV & PVC
PV:持久卷,将应用需要持久化的数据保存到指定位置
PVC:持久卷申明,申明需要使用的持久卷规格
2、创建PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv01-10m
spec:
capacity:
storage: 10M
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/01
server: 192.168.0.9
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv02-1gi
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/02
server: 192.168.0.9
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv03-3gi
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/03
server: 192.168.0.9
[root@master data]# kubectl apply -f pv.yaml
persistentvolume/pv01-10m created
persistentvolume/pv02-1gi created
persistentvolume/pv03-3gi created
[root@master data]# kubectl get persistentvolume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv01-10m 10M RWX Retain Available(可用) nfs 14s
pv02-1gi 1Gi RWX Retain Available nfs 14s
pv03-3gi 3Gi RWX Retain Available nfs 14s
3、创建PVC与绑定
创建PVC,申请200M的
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs
查看
[root@master data]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv01-10m 10M RWX Retain Available nfs 3m45s
pv02-1gi 1Gi RWX Retain Bound default/nginx-pvc nfs 3m45s
pv03-3gi 3Gi RWX Retain Available nfs 3m45s
# 可以看到pv02,被绑定到了
[root@master data]# kubectl get pvc ## 查看pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nginx-pvc Bound pv02-1gi 1Gi RWX nfs 66m
4、PVC绑定Pod
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-deploy-pvc
name: nginx-deploy-pvc
spec:
replicas: 2
selector:
matchLabels:
app: nginx-deploy-pvc
template:
metadata:
labels:
app: nginx-deploy-pvc
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: nginx-pvc
5、ConfigMap
1、创建配置
kubectl create cm redis-conf --from-file=redis.conf ## 创建配置,redis.conf的配置保存到redis
2、查看配置
[root@master data]# kubectl create cm redis-conf --from-file=redis.conf
configmap/redis-conf created
[root@master data]# kubectl get cm ## 查看配置
NAME DATA AGE
redis-conf 1 10s
[root@master data]# mv redis.conf re
recommend.yaml redis.conf
[root@master data]# mv redis.conf redis.bak
[root@master data]# kubectl get cm redis-conf -oyaml ### 将配置文件输出成yaml格式
apiVersion: v1
data:
redis.conf: |
appendonly yes
kind: ConfigMap
metadata:
creationTimestamp: "2022-07-28T22:02:12Z"
name: redis-conf
namespace: default
resourceVersion: "129432"
selfLink: /api/v1/namespaces/default/configmaps/redis-conf
uid: c6226340-24f8-4a17-b3dc-c26cdcab2192
6 、ConfigMap与Pod绑定
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
command:
- redis-server
- "/redis-master/redis.conf" #指的是redis容器内部的位置
ports:
- containerPort: 6379
volumeMounts:
- mountPath: /data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: redis-conf
items:
- key: redis.conf
path: redis.conf