K8s-概念-基本组件

问题:k8s基本组件有哪些?有什么用?怎么用?

解答:常用组件配置

Replication Controller

deployment(deploy)

nodeSelector

设置pod固定节点

kubectl edit deploy [deployment_name]
#添加nodeSelector
      dnsPolicy: ClusterFirst
      nodeSelector:
        kubernetes.io/hostname: host1.xxx.com
      restartPolicy: Always

republicate set(rs)

deployment掌控republicate set(rs),rs用于回滚机制

pod

ImagePullPolicy

启动pod时,获取镜像的策略

  1. Always:镜像存在本地与否,都会去远端registry pull下来,适用于开发更新镜像(一般建议用docker save再docker load加快速度);
  2. Never:不管镜像是否存在都不会进行拉取
  3. IfNotPresent:只有镜像不存在本地时,才会进行镜像拉取,一般应用于部署环境。

service(svc)

job官网

job.yaml,启动perl镜像输出pi的值

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
[root@host1 yf]# kubectl create -f job.yaml
job "pi" created
[root@host1 yf]# kubectl get pods
NAME                              READY     STATUS              RESTARTS   AGE
busybox-g1g7x                     0/1       ImagePullBackOff    0          19h      
pi-m3hnk                          1/1       Running             0          11s
#必须在job的pod被删掉前获取log,否则拿不了log
[root@host1 yf]# pods=$(kubectl get pods --selector=job-name=pi --output=jsonpath={.items..metadata.name})
[root@host1 yf]# kubectl logs $pods
3.141592653589793238462643383279502884197169399375105820974944592307...
[root@host1 yf]# kubectl get job
NAME            DESIRED   SUCCESSFUL   AGE
busybox         3         0            19h
pi              1         1            2m

pod被删掉后再次查看

[root@host1 yf]# pods=$(kubectl get pods --selector=job-name=pi --output=jsonpath={.items..metadata.name})
[root@host1 yf]# kubectl logs $pods
error: expected 'logs POD_NAME [CONTAINER_NAME]'.
POD_NAME is a required argument for the logs command
See 'kubectl logs -h' for help and examples.

设置固定次数的job

apiVersion: batch/v1
kind: Job
metadata:
  name: busybox
spec:
  completions: 3
  template:
    metadata:
      name: busybox
    spec:
      containers:
      - name: busybox
        image: busybox
        command: ["echo", "hello"]
      restartPolicy: Never

ConfigMap

解析
用于存储环境变量和密码信息
命令


#从文件中创建
##文件含有目录
ls docs/user-guide/configmap/kubectl/
game.properties
ui.properties

kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl

#从命令行创建(k8s官网)
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
kubectl create configmap env-config --from-literal=log_level=INFO
#查看
kubectl describe configmaps game-config
kubectl get configmaps game-config -o yaml

参考

1.https://www.kubernetes.org.cn
2.Kubernetes |Pod 深入理解与实践
3.PODS、REPLICA SETS、DEPLOYMENT 三者關係圖

Logo

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

更多推荐