kubernetes简单使用教程(一)

2023-01-04 14:28:38 来源:51CTO博客


(资料图)

一、命名空间

作用:用来隔离资源添加删除命名空间
[root@k8s-master01 ~]# kubectl create ns hellonamespace/hello created[root@k8s-master01 ~]# kubectl get nsNAME                   STATUS   AGEdefault                Active   7d4hhello                  Active   2s[root@k8s-master01 ~]# kubectl delete ns hellonamespace "hello" deleted[root@k8s-master01 ~]# kubectl get nsNAME                   STATUS   AGEdefault                Active   7d4h
使用yaml文件创建命名空间
[root@k8s-master01 ~]# cat hello.yaml apiVersion: v1kind: Namespacemetadata:  name: hello[root@k8s-master01 ~]# kubectl apply -f hello.yaml namespace/hello created[root@k8s-master01 ~]# kubectl get nsNAME                   STATUS   AGEdefault                Active   7d4hhello                  Active   3s

二、Pod

定义:运行中的一组容器,Pod是kubernetes中应用的最小单位说明:一个Pod中可能包含多个容器通过命令行创建Pod
[root@k8s-master01 ~]# kubectl run mynginx --image=nginxpod/mynginx created[root@k8s-master01 ~]# kubectl get pods NAME                      READY   STATUS              RESTARTS   AGEmy-dep-5b7868d854-m6974   1/1     Running             0          3d21hmy-dep-5b7868d854-qndjz   1/1     Running             0          3d21hmy-dep-5b7868d854-tnthn   1/1     Running             0          3d21hmynginx                   0/1     ContainerCreating   0          8s
查看Pod的详细信息
[root@k8s-master01 ~]# kubectl describe pods mynginxName:         mynginxNamespace:    defaultPriority:     0Node:         k8s-node01/172.16.0.14Start Time:   Tue, 03 Jan 2023 18:01:08 +0800Labels:       run=mynginxAnnotations:  cni.projectcalico.org/containerID: 71ecc806be2c4d191bfd18ebc890cd90cf441f46bd9cf4b6c4a2a5d36bd385af              cni.projectcalico.org/podIP: 192.168.85.208/32              cni.projectcalico.org/podIPs: 192.168.85.208/32Status:       RunningIP:           192.168.85.208IPs:  IP:  192.168.85.208Containers:  mynginx:    Container ID:   docker://f9689a2a90672e01136084b09ef8264d055a98e9f709b670c042a79ed79db698    Image:          nginx    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31    Port:               Host Port:          State:          Running      Started:      Tue, 03 Jan 2023 18:01:24 +0800    Ready:          True    Restart Count:  0    Environment:        Mounts:      /var/run/secrets/kubernetes.io/serviceaccount from default-token-gnzps (ro)Conditions:  Type              Status  Initialized       True   Ready             True   ContainersReady   True   PodScheduled      True Volumes:  default-token-gnzps:    Type:        Secret (a volume populated by a Secret)    SecretName:  default-token-gnzps    Optional:    falseQoS Class:       BestEffortNode-Selectors:  Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300sEvents:  Type    Reason     Age   From               Message  ----    ------     ----  ----               -------  Normal  Scheduled  117s  default-scheduler  Successfully assigned default/mynginx to k8s-node01  Normal  Pulling    117s  kubelet            Pulling image "nginx"  Normal  Pulled     102s  kubelet            Successfully pulled image "nginx" in 15.412530753s  Normal  Created    102s  kubelet            Created container mynginx  Normal  Started    102s  kubelet            Started container mynginx[root@k8s-master01 ~]# 
删除Pod
[root@k8s-master01 ~]# kubectl delete pods mynginxpod "mynginx" deleted[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-5b7868d854-m6974   1/1     Running   0          3d21hmy-dep-5b7868d854-qndjz   1/1     Running   0          3d21hmy-dep-5b7868d854-tnthn   1/1     Running   0          3d21h
查看Pod的运行日志
[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-5b7868d854-m6974   1/1     Running   0          3d22hmy-dep-5b7868d854-qndjz   1/1     Running   0          3d22hmy-dep-5b7868d854-tnthn   1/1     Running   0          3d22hmynginx                   1/1     Running   0          23s[root@k8s-master01 ~]# kubectl logs mynginx/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d//docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh/docker-entrypoint.sh: Configuration complete; ready for start up2023/01/03 10:55:40 [notice] 1#1: using the "epoll" event method2023/01/03 10:55:40 [notice] 1#1: nginx/1.21.52023/01/03 10:55:40 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2023/01/03 10:55:40 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_642023/01/03 10:55:40 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762023/01/03 10:55:40 [notice] 1#1: start worker processes2023/01/03 10:55:40 [notice] 1#1: start worker process 332023/01/03 10:55:40 [notice] 1#1: start worker process 342023/01/03 10:55:40 [notice] 1#1: start worker process 352023/01/03 10:55:40 [notice] 1#1: start worker process 36[root@k8s-master01 ~]# 
查看Pode的相信信息
[root@k8s-master01 ~]# kubectl get pods -owideNAME                      READY   STATUS    RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATESmy-dep-5b7868d854-m6974   1/1     Running   0          3d22h   192.168.58.209   k8s-node02              my-dep-5b7868d854-qndjz   1/1     Running   0          3d22h   192.168.58.211   k8s-node02              my-dep-5b7868d854-tnthn   1/1     Running   0          3d22h   192.168.58.210   k8s-node02              mynginx                   1/1     Running   0          81s     192.168.85.209   k8s-node01              [root@k8s-master01 ~]# 
使用Pod的ip+pod里面运行容器的端口测试Pod

注意:集群中的任意一个机器以及任意的应用都能通过Pod分配的ip来访问这个Pod

[root@k8s-master01 ~]# curl 192.168.85.209:80Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

[root@k8s-master01 ~]#
使用yaml文件创建Pod
[root@k8s-master01 ~]# cat mynginx.yaml apiVersion: v1kind: Podmetadata:  labels:    run: mynginx  name: mynginxspec:  containers:  - image: nginx    name: mynginx[root@k8s-master01 ~]# kubectl apply -f mynginx.yaml pod/mynginx created[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-5b7868d854-m6974   1/1     Running   0          3d22hmy-dep-5b7868d854-qndjz   1/1     Running   0          3d22hmy-dep-5b7868d854-tnthn   1/1     Running   0          3d22hmynginx                   1/1     Running   0          25s
使用yaml文件同时创建包含两个容器的Pod
[root@k8s-master01 ~]# cat multicontainer-pod.yaml apiVersion: v1kind: Podmetadata:  labels:    run: myapp  name: myappspec:  containers:  - image: nginx    name: nginx  - image: tomcat:8.5.68    name: tomcat[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-5b7868d854-m6974   1/1     Running   0          3d22hmy-dep-5b7868d854-qndjz   1/1     Running   0          3d22hmy-dep-5b7868d854-tnthn   1/1     Running   0          3d22hmyapp                     2/2     Running   0          91s[root@k8s-master01 ~]# kubectl get pods myapp -oyaml |grep image      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"run":"myapp"},"name":"myapp","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"nginx"},{"image":"tomcat:8.5.68","name":"tomcat"}]}}            f:image: {}            f:imagePullPolicy: {}            f:image: {}            f:imagePullPolicy: {}  - image: nginx    imagePullPolicy: Always  - image: tomcat:8.5.68    imagePullPolicy: IfNotPresent    image: nginx:latest    imageID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31    image: tomcat:8.5.68    imageID: docker-pullable://tomcat@sha256:a266dd222864de2fe72e0464e6d91c406a687c861bb72a07218e6d7c89fe1d3e[root@k8s-master01 ~]# 

三、Deployment

功能:控制Pod,使Pod拥有多副本,自愈,扩缩容等能力特点:删除Pod后,会自动重启生成一个Pod创建多副本
[root@k8s-master01 ~]# kubectl create deployment my-dep --image=nginx --replicas=3deployment.apps/my-dep created[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-5b7868d854-9ghf9   1/1     Running   0          41smy-dep-5b7868d854-l6zqs   1/1     Running   0          41smy-dep-5b7868d854-pflfk   1/1     Running   0          41smyapp                     2/2     Running   0          7m17s[root@k8s-master01 ~]# 
使用yaml文件创建Deploymnet
[root@k8s-master01 ~]# cat my-dep.yaml apiVersion: apps/v1kind: Deploymentmetadata:  labels:    app: my-dep  name: my-depspec:  replicas: 3  selector:    matchLabels:      app: my-dep  template:    metadata:      labels:        app: my-dep    spec:      containers:      - image: nginx        name: nginx[root@k8s-master01 ~]# kubectl apply -f my-dep.yaml deployment.apps/my-dep created[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-5b7868d854-7jb7r   1/1     Running   0          64smy-dep-5b7868d854-rpcxn   1/1     Running   0          64smy-dep-5b7868d854-vxf9h   1/1     Running   0          64smyapp                     2/2     Running   0          17m[root@k8s-master01 ~]# 
扩缩容
[root@k8s-master01 ~]# kubectl scale --replicas=5 deployment/my-depdeployment.apps/my-dep scaled[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-5b7868d854-7jb7r   1/1     Running   0          4m13smy-dep-5b7868d854-cdxtd   1/1     Running   0          65smy-dep-5b7868d854-rpcxn   1/1     Running   0          4m13smy-dep-5b7868d854-vxf9h   1/1     Running   0          4m13smy-dep-5b7868d854-zqfmq   1/1     Running   0          65smyapp                     2/2     Running   0          21m
滚动更新
[root@k8s-master01 ~]# kubectl set image deployment/my-dep nginx=nginx:1.16.1 --recorddeployment.apps/my-dep image updated[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS              RESTARTS   AGEmy-dep-5b7868d854-7jb7r   1/1     Running             0          5m54smy-dep-5b7868d854-rpcxn   1/1     Running             0          5m54smy-dep-5b7868d854-vxf9h   1/1     Running             0          5m54smy-dep-5b7868d854-zqfmq   1/1     Running             0          2m46smy-dep-6b48cbf4f9-qdwz6   0/1     ContainerCreating   0          4smy-dep-6b48cbf4f9-x2xgm   0/1     ContainerCreating   0          4smy-dep-6b48cbf4f9-zvvhk   0/1     ContainerCreating   0          4smyapp                     2/2     Running             0          22m[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS              RESTARTS   AGEmy-dep-5b7868d854-7jb7r   1/1     Running             0          6m2smy-dep-5b7868d854-rpcxn   1/1     Running             0          6m2smy-dep-5b7868d854-vxf9h   1/1     Running             0          6m2smy-dep-5b7868d854-zqfmq   1/1     Running             0          2m54smy-dep-6b48cbf4f9-qdwz6   0/1     ContainerCreating   0          12smy-dep-6b48cbf4f9-x2xgm   0/1     ContainerCreating   0          12smy-dep-6b48cbf4f9-zvvhk   0/1     ContainerCreating   0          12smyapp                     2/2     Running             0          22m[root@k8s-master01 ~]# kubectl get podsNAME                      READY   STATUS    RESTARTS   AGEmy-dep-6b48cbf4f9-4k8zw   1/1     Running   0          36smy-dep-6b48cbf4f9-fk6cw   1/1     Running   0          16smy-dep-6b48cbf4f9-qdwz6   1/1     Running   0          53smy-dep-6b48cbf4f9-x2xgm   1/1     Running   0          53smy-dep-6b48cbf4f9-zvvhk   1/1     Running   0          53smyapp                     2/2     Running   0          23m[root@k8s-master01 ~]# kubectl rollout status deployment/my-depdeployment "my-dep" successfully rolled out
版本回退
[root@k8s-master01 ~]# kubectl rollout history deployment/my-depdeployment.apps/my-dep REVISION  CHANGE-CAUSE1         2         kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record=true[root@k8s-master01 ~]# kubectl rollout history deployment/my-dep --revision=2deployment.apps/my-dep with revision #2Pod Template:  Labels:       app=my-dep        pod-template-hash=6b48cbf4f9  Annotations:  kubernetes.io/change-cause: kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record=true  Containers:   nginx:    Image:      nginx:1.16.1    Port:           Host Port:      Environment:            Mounts:       Volumes:      [root@k8s-master01 ~]# kubectl rollout undo deployment/my-depdeployment.apps/my-dep rolled back[root@k8s-master01 ~]# kubectl rollout history deployment/my-depdeployment.apps/my-dep REVISION  CHANGE-CAUSE2         kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record=true3         [root@k8s-master01 ~]# kubectl rollout undo deployment/my-dep --to-revision=2deployment.apps/my-dep rolled back[root@k8s-master01 ~]# 

四、Service

功能:将一组Pod公开为网络服务的抽象方法暴露Deploy
[root@k8s-master01 ~]# kubectl expose deployment my-dep --port=8000 --target-port=80service/my-dep exposed[root@k8s-master01 ~]# kubectl get svcNAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGEkubernetes   ClusterIP   10.96.0.1               443/TCP    7d6hmy-dep       ClusterIP   10.96.231.188           8000/TCP   5s[root@k8s-master01 ~]# kubectl get pods -l app=my-depNAME                      READY   STATUS    RESTARTS   AGEmy-dep-6b48cbf4f9-45k75   1/1     Running   0          3m50smy-dep-6b48cbf4f9-bkf9j   1/1     Running   0          3m33smy-dep-6b48cbf4f9-c88m6   1/1     Running   0          3m50smy-dep-6b48cbf4f9-k7lpl   1/1     Running   0          3m50smy-dep-6b48cbf4f9-qwxpf   1/1     Running   0          3m33s[root@k8s-master01 ~]# 
使用yaml文件创建Service
[root@k8s-master01 ~]# cat my-dep-service.yaml apiVersion: v1kind: Servicemetadata:  labels:    app: my-dep  name: my-depspec:  selector:    app: my-dep  ports:  - port: 8000    protocol: TCP    targetPort: 80[root@k8s-master01 ~]# kubectl apply -f my-dep-service.yaml service/my-dep created[root@k8s-master01 ~]# kubectl get svc NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGEkubernetes   ClusterIP   10.96.0.1               443/TCP    7d6hmy-dep       ClusterIP   10.96.124.231           8000/TCP   35s[root@k8s-master01 ~]# 
ClusterIP
kubectl expose deployment my-dep --port=8000 --target-port=80 --type=ClusterIP# 等同于没有 --type=ClusterIP
NodePort
[root@k8s-master01 ~]# kubectl expose deployment my-dep --port=8000 --target-port=80 --type=NodePortservice/my-dep exposed[root@k8s-master01 ~]# kubectl get svc NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGEkubernetes   ClusterIP   10.96.0.1            443/TCP          7d6hmy-dep       NodePort    10.96.88.7           8000:30232/TCP   3s
使用yaml文件创建NodePort
[root@k8s-master01 ~]# cat my-dep-NodePort.yaml apiVersion: v1kind: Servicemetadata:  labels:    app: my-dep  name: my-depspec:  ports:  - port: 8000    protocol: TCP    targetPort: 80  selector:    app: my-dep  type: NodePort  [root@k8s-master01 ~]# kubectl apply -f my-dep-NodePort.yaml service/my-dep created[root@k8s-master01 ~]# kubectl get svcNAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGEkubernetes   ClusterIP   10.96.0.1               443/TCP          7d6hmy-dep       NodePort    10.96.208.124           8000:30441/TCP   5s

标签: 文件创建 命名空间 网络服务

上一篇:天天新资讯:第二十章《Java Swing》第4节:事件处理与监听器
下一篇:天天播报:在HbuilderX中实现微信小程序下蓝牙连接打印机完整实战案例