在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1、kubectl run创建deployment

[root@k8s-master-101 ~]# kubectl run nginx --replicas=3 --labels="app=nginx-example" --image=nginx:1.10 --port=80
deployment.apps/nginx created

2、kubectl get all查看所有信息

[root@k8s-master-101 ~]# kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6d74bfcd4d-6sdbq   1/1     Running   0          36s
pod/nginx-6d74bfcd4d-dkzp5   1/1     Running   0          36s
pod/nginx-6d74bfcd4d-rmgxz   1/1     Running   0          36s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.10.10.1   <none>        443/TCP   19d

NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3         3         3            3           36s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-6d74bfcd4d   3         3         3       36s

3、kubectl describe查看指定pod信息

[root@k8s-master-101 ~]# kubectl describe pod nginx-6d74bfcd4d-6sdbq 

#kubectl get pod -o wide 查看pods具体情况,对应的地址
[root@k8s-master-101 ~]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE
nginx-6d74bfcd4d-6sdbq   1/1     Running   0          7m59s   172.17.50.2   10.0.0.102   <none>
nginx-6d74bfcd4d-dkzp5   1/1     Running   0          7m59s   172.17.71.5   10.0.0.103   <none>
nginx-6d74bfcd4d-rmgxz   1/1     Running   0          7m59s   172.17.71.6   10.0.0.103   <none>

4、创建时传入命令

[root@k8s-master-101 ~]# kubectl run busybox --image=busybox --command -- ping www.baidu.com

5、kubectl get pods --show-labels 查看标签
如果不指定镜像标签的话,默认会创建一个随机的标签,末尾为run=<Controller_Name>

[root@k8s-master-101 ~]# kubectl get pods --show-labels
NAME                       READY   STATUS    RESTARTS   AGE     LABELS
busybox-5fb9cd66d6-flf6r   1/1     Running   0          5m47s   pod-template-hash=5fb9cd66d6,run=busybox
nginx-6d74bfcd4d-6sdbq     1/1     Running   0          15m     app=nginx-example,pod-template-hash=6d74bfcd4d
nginx-6d74bfcd4d-dkzp5     1/1     Running   0          15m     app=nginx-example,pod-template-hash=6d74bfcd4d
nginx-6d74bfcd4d-rmgxz     1/1     Running   0          15m     app=nginx-example,pod-template-hash=6d74bfcd4d

6、创建service,指定端口发布,deployment nginx 指定匹配的deployment的名称为nginx

kubectl expose deployment nginx --port=88 --type=NodePort --target-port=80 --name=nginx-service

7、kubectl get svc 查询service

[root@k8s-master-101 ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.10.10.1     <none>        443/TCP        19d
nginx-service   NodePort    10.10.10.252   <none>        88:44950/TCP   60s

8、内部访问,在node节点上 curl 10.10.10.252:88,访问这个地址会转发到容器的80端口

[root@k8s-node1-102 ~]# curl 10.10.10.252:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>

9、查看转发规则

[root@k8s-node1-102 ~]# iptables-save | grep 88 
-A KUBE-SERVICES -d 10.10.10.252/32 -p tcp -m comment --comment "default/nginx-service: cluster IP" -m tcp --dport 88 -j KUBE-SVC-GKN7Y2BSGW4NJTYL

10、kubectl exec -it xxx bash 分配伪终端进入pod

[root@k8s-node1-102 ~]# kubectl exec -it nginx-6d74bfcd4d-6sdbq bash  
root@nginx-6d74bfcd4d-6sdbq:/# 

11、重新发布一个镜像,更新项目,会把原本的删除然后替换
kubectl set image deployment/nginx nginx=nginx:1.11

[root@k8s-node1-102 ~]# kubectl set image deployment/nginx nginx=nginx:1.11 
deployment.extensions/nginx image updated

#有一个正在创建,把原本的pod删除重建了
[root@k8s-node1-102 ~]# kubectl get pods
NAME                       READY   STATUS              RESTARTS   AGE
busybox-5fb9cd66d6-flf6r   1/1     Running             0          20m
nginx-6d74bfcd4d-6sdbq     1/1     Running             0          30m
nginx-6d74bfcd4d-dkzp5     1/1     Running             0          30m
nginx-744b88cdbf-cqpb7     1/1     Running             0          71s
nginx-744b88cdbf-v8qqf     0/1     ContainerCreating   0          12s

12、编辑资源配置文件
kubectl edit deploy/nginx
将nginx版本修改为1.12,修改后退出就会生效了
在这里插入图片描述

13、查看发布状态,K8S会对发布做版本记录
kubectl rollout status deploy/nginx

[root@k8s-node1-102 ~]# kubectl rollout status deploy/nginx
deployment "nginx" successfully rolled out

14、查看发布历史,之前没有使用–record 所以这里没显示消息只有数字标号
kubectl rollout history deploy/nginx

[root@k8s-node1-102 ~]# kubectl rollout history deploy/nginx 
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>

15、将版本升级到1.13并使用–record

[root@k8s-node1-102 ~]# kubectl set image deployment/nginx nginx=nginx:1.13 --record
deployment.extensions/nginx image updated

16、查看历史记录可以看到有record记录

[root@k8s-node1-102 ~]# kubectl rollout history deploy/nginx
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>
4         kubectl set image deployment/nginx nginx=nginx:1.13 --record=true

17、版本回滚 kubectl rollout undo
kubectl rollout undo deployment/nginx 默认回滚到上一版本 加上–to-revision=3 可以指定回滚的版本

[root@k8s-node1-102 ~]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx

#这里的5是原本的版本3,因为4回滚是3的版本
[root@k8s-node1-102 ~]# kubectl rollout history deploy/nginx 
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
2         <none>
4         kubectl set image deployment/nginx nginx=nginx:1.13 --record=true
5         <none>

18、指定版本回滚

[root@k8s-node1-102 ~]# kubectl rollout undo deployment/nginx --to-revision=4
deployment.extensions/nginx
[root@k8s-node1-102 ~]# kubectl rollout history deploy/nginx                 
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
2         <none>
5         <none>
6         kubectl set image deployment/nginx nginx=nginx:1.13 --record=tru

19、资源扩容或减少,kubectl scale指定资源数量

[root@k8s-node1-102 ~]# kubectl scale deployment/nginx --replicas=5
deployment.extensions/nginx scaled
[root@k8s-node1-102 ~]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
busybox-5fb9cd66d6-flf6r   1/1     Running   0          33m
nginx-58866f58cf-2hlns     1/1     Running   0          92s
nginx-58866f58cf-gnxtz     1/1     Running   0          86s
nginx-58866f58cf-pdhwd     1/1     Running   0          37s
nginx-58866f58cf-pkppn     1/1     Running   0          89s
nginx-58866f58cf-q9cmx     1/1     Running   0          37s

20、资源释放,删除deploy 再删除service,删除deploy后, rs和pod会自动释放

[root@k8s-node1-102 ~]# kubectl delete deploy/nginx
deployment.extensions "nginx" deleted
[root@k8s-node1-102 ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐