kubernetes(k8s) 概念以及常用命令

概念

资源

就是pod service daemonset之类

常用命令

查看k8s版本

kubectl get nodes
kubectl version

在这里插入图片描述

获取名称空间

kubectl get namespaces

或者

kubectl get ns

获取default名称空间的资源

kubectl get all -n default

或者

kubectl get all

创建名称空间

kubectl create  ns chenweirui-namespace

获取名称空间

kubectl get ns

创建deployment

kubectl create deployment chenweirui-deployment-php7-test --image=php:7.0.8-fpm-alpine -n chenweirui-namespace

deployment就是pod的控制器
执行此命令后,将会创建pod,pod可以这样查看

kubectl  get pods -A |grep chen

在这里插入图片描述
还可以这样查看deployment

[root@test-cz-manager ~]# kubectl get deployment -n chenweirui-namespace
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
chenweirui-deployment-php7-test   1/1     1            1           8m51s

查询pod信息,比如ip地址,在哪台node
-o wide 扩展查看

[root@test-cz-manager ~]# kubectl get pods -n chenweirui-namespace
NAME                                               READY   STATUS    RESTARTS   AGE
chenweirui-deployment-php7-test-66df5d59fd-pm8c6   1/1     Running   0          11m
[root@test-cz-manager ~]# kubectl get pods -n chenweirui-namespace -o wide
NAME                                               READY   STATUS    RESTARTS   AGE   IP         NODE                         NOMINATED NODE   READINESS GATES
chenweirui-deployment-php7-test-66df5d59fd-pm8c6   1/1     Running   0          12m   10.0.0.3   cn-shenzhen.172.30.100.221              
[root@test-cz-manager ~]# 

获取deployment的详细描述
名称空间下的所有deployment的详细描述

[root@test-cz-manager ~]# kubectl describe deployment -n chenweirui-namespace
Name:                   chenweirui-deployment-php7-test
Namespace:              chenweirui-namespace
CreationTimestamp:      Sun, 07 Mar 2021 15:02:19 +0800
Labels:                 app=chenweirui-deployment-php7-test
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=chenweirui-deployment-php7-test
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=chenweirui-deployment-php7-test
  Containers:
   php:
    Image:        php:7.0.8-fpm-alpine
    Port:         
    Host Port:    
    Environment:  
    Mounts:       
  Volumes:        
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  
NewReplicaSet:   chenweirui-deployment-php7-test-66df5d59fd (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  26m   deployment-controller  Scaled up replica set chenweirui-deployment-php7-test-66df5d59fd to 1
[root@test-cz-manager ~]# 

如果要获取指定deployment的详细描述,可以这样

kubectl describe deployment chenweirui-deployment-php7-test -n chenweirui-namespace

进入pod资源

kubectl exec -it chenweirui-deployment-php7-test-66df5d59fd-pm8c6 sh -n chenweirui-namespace

查看ip信息

ip add

查看主机名

hostname

查看集群信息

[root@test-cz-manager ~]# kubectl cluster-info 
Kubernetes master is running at https://172.30.100.218:6443
metrics-server is running at https://172.30.100.218:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://172.30.100.218:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

暴露服务

kubectl expose deployment nginx --port=80 --type=NodePort

部署一个本地存在的镜像

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-stark-tools
  name: my-stark-tools
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-stark-tools
  template:
    metadata:
      labels:
        app: my-stark-tools
    spec:
      containers:
      - image: my/stark-tools:3.0
        imagePullPolicy: IfNotPresent # 这个设置 会从本地先找,没有从远处拉取
        command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
        name: stark-tools
        ports:
        - containerPort: 80

说明:如果一个镜像里面没有服务在跑,k8s是会报错的:

Warning BackOff 5s (x4 over 33s) kubelet Back-off restarting failed container

容器没有任务的时候会处于非running状态,要保持running状态大家推荐的方法是在容器起来之后去循环访问一个系统自带的文件/dev/null,默认情况下里面是空的,tail -f就是实时打印日志的命令

一pod多容器案例

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
      - image: my/stark-tools:3.0
        imagePullPolicy: IfNotPresent # 这个设置 会从本地先找,没有从远处拉取
        command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
        name: stark-tools
        ports:
        - containerPort: 89

进入容器

kubectl.exe exec -it nginx-and-stark-tools-9794bf86b-d8n88 -c nginx -- bash

其中,-c 后面为容器名,如果一个pod只有一个容器时,-c可以不指定

部署 service实现网络连接
模板

apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    protocol: TCP
    name: https
  selector:
    run: my-nginx

经过测试没用,只能用epose

容易混淆的概念

1、NodePort和port

前者是将服务暴露给外部用户使用并在node上、后者则是为内部组件相互通信提供服务的,是在service上的端口。

2、targetPort

targetPort是pod上的端口,用来将pod内的container与外部进行通信的端口

3、port、NodePort、targetPort在哪儿?

port在service上,负责处理对内的通信,clusterIP:port

NodePort在node上,负责对外通信,NodeIP:NodePort

targetPort在pod上、负责与kube-proxy代理的port和Nodeport数据进行通信

你可能感兴趣的:(docker,k8s,k8s)