目录
一、Namespace 常用操作
Kubernetes 可以使用 Namespaces(命名空间)创建多个虚拟集群。当团队或项目中具有许多用户时,可以考虑使用 Namespace 来区分,如果是少量用户集群,可以不需要考虑使用 Namespace。
Namespace 为名称提供了一个范围。资源的 Names 在 Namespace 中具有唯一性。
1.创建
(1)命令行创建
kubectl create namespace new-namespace
(2)通过文件创建
vi my-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
kubectl create -f ./my-namespace.yaml
2.删除
kubectl delete namespaces my-namespace
注意:
删除一个 namespace 会自动删除所有属于该 namespace 的资源。
default 和 kube-system 命名空间不可删除。
PersistentVolumes 是不属于任何 namespace 的,但 PersistentVolumeClaim 是属于某个特定 namespace 的。
Events 是否属于 namespace 取决于产生 events 的对象。
3.查看
kubectl get namespaces
或
kubectl get ns
Kubernetes 初始有两个 namespace:default、kube-system
4.查看详细信息
kubectl describe namespaces nginx
二、Deployment 常用操作
Deployment 为 Pod 和 Replica Set(升级版的 Replication Controller)提供声明式更新。
只需要在 Deployment 中描述想要的目标状态是什么,Deployment controller 就会将 Pod 和ReplicaSet 的实际状态改变到目标状态。
注意:避免手动管理由 Deployment 创建的 Replica Set。
1.创建
命令行方式:
kubectl create deployment 名称 [参数]
# --image 指定pod的镜像
# --port 指定端口
# --replicas 指定创建pod数量
# --namespace 指定namespace
kubectl create deploy nginx --image=nginx:latest --port=80 --replicas=3 -n shop
配置文件方式:
kubectl create -f my-deployment.yaml
2.删除
kubectl delete -f my-deployment.yaml
或
kubectl delete deployment name
3.查看
kubectl get deployment
4.查看详细信息
kubectl describe deploy nginx -n shop
注意:带上命名空间
三、Pod 常用操作
Pod 是 Kubernetes 创建或部署的最小/最简单的基本单位,一个 Pod 代表集群上正在运行的一个进程。
一个 Pod 封装一个应用容器(也可以有多个容器),存储资源、一个独立的网络 IP 以及管理控制容器运行方式的策略选项。Pod 代表部署的一个单位:Kubernetes 中单个应用的实例,它可能由单个容器或多个容器共享组成的资源。
1.创建
kubectl create -f my-pod.yaml
2.删除
kubectl delete -f my-pod.yaml
或
kubectl delete pod name
3.查看
kubectl get pod
4.查看详细信息
kubectl describe pod nginx -n shop
注意:带上命名空间
5.查看日志
kubectl logs --tail 200 -f <podname> -n <namesapce> | more
示例:
kubectl logs --tail 200 -n shop -f nginx
6.重启方式
(1)方式1
如果文件有变动,使用:
kubectl apply -f deploy.yaml
没变动则使用:
kubectl replace --force -f deploy.yaml
(2)方式2
删除 pod 后重建。
四、Service 常用操作
Kubernetes Service 定义了这样一种抽象:一个 Pod 的逻辑分组,一种可以访问它们的策略 --通常称为微服务。 这一组 Pod 能够被 Service 访问到,通常是通过 Label Selector 实现的。
1.创建
kubectl create -f my-svc.yaml
2.删除
kubectl delete -f my-svc.yaml
或
kubectl delete svc name
3.查看
kubectl get svc
4.查看详细信息
kubectl describe svc nginx -n shop
注意:带上命名空间
五、Label 常用操作
Labels 其实就一对 key/value ,被关联到对象上,标签的使用我们倾向于能够标示对象的特殊特点,并且对用户而言是有意义的,但是标签对内核系统是没有直接意义的。标签可以用来划分特定组的对象,标签可以在创建一个对象的时候直接给与,也可以在后期随时修改,每一个对象可以拥有多个标签,但是,key值必须是唯一的。
1.打标签
kubectl label pod nginx version=1.0
2.更新标签
kubectl label pod nginx version=2.0 --overwrite
3.查看标签
kubectl get pod nginx --show-labels
4.筛选标签
kubectl get pod -l version!=2.0 --show-labels
5.删除标签
kubectl label pod nginx version-
六、Nginx 部署实战
vi deploy.yaml
piVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 10003
volumeMounts:
- name: conf
mountPath: /etc/nginx/nginx.conf
- name: log
mountPath: /var/log/nginx
- name: source
mountPath: /home/source
volumes:
- name: conf
hostPath:
path: /opt/kube/nginx/conf/nginx.conf
- name: log
hostPath:
path: /opt/kube/nginx/logs
type: Directory
- name: source
hostPath:
path: /home/source
type: Directory
---
apiVersion: v1
kind: Service
metadata:
name: ngx-service
labels:
app: nginx
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 10000
targetPort: 10000
name: nginx-service10000
nodePort: 10000
- port: 10001
name: nginx-service10001
targetPort: 10001
nodePort: 10001
- port: 10002
name: nginx-service10002
targetPort: 10002
nodePort: 10002
- port: 10003
name: nginx-service10003
targetPort: 10003
nodePort: 10003
kubectl apply -f deploy.yaml