一、kubectl进行nginx应用测试
这里使用的minikube环境进行测试,可以使用官方版,国内的话可以使用aliyun修改版(https://yq.aliyun.com/articles/221687)
[root@ecs-255b ~]# cat nginx/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
使用如下命令可以创建deployment容器应用
kubectl apply -f nginx/deployment.yaml
注:该yaml文件也可以是远程网络上的一个文件。
此时修改文件中的replicas 数量为4,再次重启apply ,并开另一个窗口,可以查看到pod的变化会逐步由2变为4
[root@ecs-255b ~]# kubectl get deployments nginx-deployment --watch
接下来再对nginx的版本进行升级:
[root@ecs-255b ~]# cat nginx/deployment-update.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1 # Update the version of nginx from 1.14.2 to 1.16.1
ports:
- containerPort: 80
再次使用kubectl apply应用后,可以通过如下命令查看其版本信息,会发现其版本已经由之前的版本升级成新的版本。
[root@ecs-255b ~]# kubectl describe deployments nginx-deployment
二、完整示例
在概念篇里我们有学到k8s有namespace label deployment service pod,这里从namespace开始做一个完整的实验。
1、Namespace在很多情况下用于实现多用户的资源隔离,通过将集群内部的资源对象分配到不同的Namespace中,形成逻辑上的分组,便于不同的分组在共享使用整个集群的资源同时还能被分别管理。
//通过命令创建
[root@ecs-255b ~]# kubectl create namespace test
namespace/test created
//通过yaml文件创建
[root@ecs-255b ~]# cat test.yaml //创建创建文件
apiVersion: v1
kind: Namespace
metadata:
name: test
[root@ecs-255b ~]# kubectl create -f test.yaml
[root@ecs-255b ~]# kubectl get ns //查看namespace
[root@ecs-255b ~]# kubectl replace --force -f test.yaml //重建
namespace test deleted
namespace/test replaced
[root@ecs-255b ~]# kubectl delete namespaces test //删除
namespace test deleted
2、Label是Kubernetes系统中另一个核心概念。一个Label是一个key=value的键值对,其中key与value由用户自己指定。在k8s基本概念篇中我们也总结过,label可以设置ns、Node、Pod、Service、RC等。
//设置label
[root@ecs-255b ~]# kubectl label namespaces test colour=green
namespace/test labeled
[root@ecs-255b ~]# kubectl get namespaces test --show-labels
NAME STATUS AGE LABELS
test Active 9m48s colour=green,kubernetes.io/metadata.name=test
//修改label
[root@ecs-255b ~]# kubectl label namespaces test colour=red --overwrite
namespace/test labeled
[root@ecs-255b ~]# kubectl get namespaces test --show-labels
NAME STATUS AGE LABELS
test Active 10m colour=red,kubernetes.io/metadata.name=test
//删除label
[root@ecs-255b ~]# kubectl label namespaces test colour-
namespace/test labeled
3、Deployment是Kubenetes v1.2引入的新概念,引入的目的是为了更好的解决Pod的编排问题,Deployment内部使用了Replica Set来实现。Deployment的定义与ReplicaSet的定义很类似,除了API声明与Kind类型有所区别。deployment的定义类似如下:
<
pre>
## Deployment apiVersion: apps/v1 kind: Deployment metadata: name: mysql labels: app: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:8.0.19 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD ## 配置Root用户默认密码 value: 123456 resources: limits: cpu: 2000m memory: 512Mi requests: cpu: 2000m memory: 512Mi
应用指令如下:
[root@361way ~]# kubectl create -f mysql.yaml //创建deployment
deployment.apps/mysql-deployment created
[root@361way ~]# kubectl delete deployment mysql-deployment -n elk //删除
deployment.apps mysql-deployment deleted
[root@361way ~]# kubectl replace --force -f mysql.yaml //重建
deployment.apps mysql-deployment deleted
deployment.apps/mysql-deployment replaced
[root@361way ~]# kubectl get deployment -n elk //查看
NAME READY UP-TO-DATE AVAILABLE AGE
elk 1/1 1 1 21h
[root@361way ~]# kubectl get deployments --all-namespaces //查看所有
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
default nginx 1/1 1 1 10d
mysql mysql 1/1 1 1 21h
kube-system coredns 2/2 2 2 11d
4、Service是Kubernetes最核心概念,通过创建Service,可以为一组具有相同功能的容器应用提供一个统一的入口地址,并且将请求负载分发到后端的各个容器应用上。
[root@361way ~]# kubectl create -f mysql-service.yaml //创建
service/mysql created
[root@361way ~]# kubectl replace --force -f mysql-service.yaml //重建
service/mysql replaced
[root@361way ~]# kubectl get svc -n elk //查看
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP 10.1.40.47 80/TCP 21h
mysql NodePort 10.1.7.179 3306:3306/TCP 21h
[root@361way ~]# kubectl delete svc mysql-service -n mysql //删除
service mysql-service deleted
5、Pod是Kubernetes的最重要概念,每一个Pod都有一个特殊的被称为”根容器“的Pause容器。Pause容器对应的镜像属于Kubernetes平台的一部分,除了Pause容器,每个Pod还包含一个或多个紧密相关的用户业务容器。
# kubectl create -f ./pod.json //通过pod.json文件创建一个pod。
# kubectl delete -f ./pod.json //使用 pod.json中指定的资源类型和名称删除pod。
# kubectl delete pod,service test //删除名为test的Pod和Service。
# kubectl delete pods,svc -l name=tank //删除 Label name=tank的pod和Service。
# kubectl delete pod foo --grace-period=0 --force //强制删除dead node上的pod
# kubectl delete pods --all //删除所有pod
//通过设置replicas来删除pod
[root@361way mysql]# kubectl scale --replicas=0 -f mysql-rc.yaml
replicationcontroller/mysql scaled
[root@361way mysql]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-cd7tm 1/1 Terminating 0 57s
nginx-f89759699-545w5 1/1 Running 0 10d
[root@361way mysql]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-545w5 1/1 Running 0 10d
# kubectl replace -f ./pod.json //通过pod.json文件重建pod
# kubectl replace --force -f ./pod.json //通过pod.json文件强制重建
//查看所有pod,svc
[root@361way ~]# kubectl get pod,svc --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default pod/nginx-f89759699-545w5 1/1 Running 0 11d
mysql pod/mysql-ddc4c865b-859ks 1/1 Running 0 22h
kube-system pod/coredns-7ff77c879f-bxgtr 1/1 Running 2 11d
……
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/kubernetes ClusterIP 10.1.0.1 443/TCP 11d
default service/nginx ClusterIP 10.1.40.47 80/TCP 21h
mysql service/mysql NodePort 10.1.7.179 3306:3306/TCP 22h
……