交互平台:
Interactive Tutorial - Creating a Cluster | Kubernetes 使用以下命令启动k8s

minikube start

使用kubectl get nodes 和 kubectl cluster-info查看节点和集群信息

k8s怎么进入gitlab k8s怎么进入节点执行命令_docker

基本操作:

部署应用:

kubectl create deployment kubernetes-bootcamp \
     --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080

暴露端口:

kubectl expose deployment.apps/kubernetes-bootcamp \
     --type="NodePort" --port 8080

查看映射的端口并访问:

k8s怎么进入gitlab k8s怎么进入节点执行命令_k8s怎么进入gitlab_02


设置副本数:

kubectl scale deployment.apps/kubernetes-bootcamp --replicas=3

版本升级:

kubectl set image deployment.apps/kubernetes-bootcamp \
kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

查看Pod:

k8s怎么进入gitlab k8s怎么进入节点执行命令_k8s怎么进入gitlab_03

版本回滚:

kubectl rollout undo deployments/kubernetes-bootcamp

访问服务的方法:

使用vi将下面的配置信息粘贴过去即可

配置bootcamp1.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bootcamp1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: bootcamp
  template:
    metadata:
      labels:
        app: bootcamp
    spec:
      containers:
      - name: bootcamp1
        image: docker.io/jocatalin/kubernetes-bootcamp:v1
        ports:
        - containerPort: 8080

配置 bootcamp2.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bootcamp2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: bootcamp
  template:
    metadata:
      labels:
        app: bootcamp
    spec:
      containers:
      - name: bootcamp2
        image: docker.io/jocatalin/kubernetes-bootcamp:v2
        ports:
        - containerPort: 8080

配置svc.yml

apiVersion: v1
kind: Service
metadata:
  name: svc
spec:
  type: NodePort
  selector:
    app: bootcamp
  ports:
  - name: bootcamp
    protocol: TCP
    port: 80
    targetPort: 8080
    nodePort: 30000

启动pod和service:

kubectl create -f bootcamp1.yml
kubectl create -f bootcamp2.yml
kubectl create -f svc.yml

如果要删除对应的pod或者service,只需要kubectl delete 命令即可,例如:

kubectl delete svc/svc
kubectl delete -f svc.yml

获取pod的IP地址:

kubectl get pods -l app=bootcamp -o yaml | grep podIP

k8s怎么进入gitlab k8s怎么进入节点执行命令_docker_04


查看svc:

k8s怎么进入gitlab k8s怎么进入节点执行命令_Pod_05


通过podIP访问服务:

k8s怎么进入gitlab k8s怎么进入节点执行命令_容器_06


通过clusterIP访问服务:

k8s怎么进入gitlab k8s怎么进入节点执行命令_k8s怎么进入gitlab_07


通过NodePort访问服务:

k8s怎么进入gitlab k8s怎么进入节点执行命令_k8s怎么进入gitlab_08


如果安装kube-dns组件的话,可以通过域名的形式直接访问服务。

数据持久化(PVC):

PV静态供给
使用vi将下面的配置信息粘贴过去即可

pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /home/disk
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube

pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage

运行命令:

kubectl apply -f pv.yml
kubectl apply -f pvc.yml

使用命令查看pv和pvc:

k8s怎么进入gitlab k8s怎么进入节点执行命令_docker_09


配置pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:   
  containers:
    - name: mypod1
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      volumeMounts:
        - mountPath: "/mydata"
          name: mydata
  volumes:
    - name: mydata
      persistentVolumeClaim: 
        claimName: pvc

运行命令:

mkidr /home/disk
kubectl apply -f pod.yml

查看Pod状态:

k8s怎么进入gitlab k8s怎么进入节点执行命令_k8s怎么进入gitlab_10


在挂载目录下写入文件:

kubectl exec mypod1 --  touch  /mydata/test

查看文件:

k8s怎么进入gitlab k8s怎么进入节点执行命令_k8s怎么进入gitlab_11

PV动态供给

查看StorageClass:

kubectl get storageclass.storage.k8s.io

minikube有一个默认的StorageClass为standard

k8s怎么进入gitlab k8s怎么进入节点执行命令_Pod_12


配置pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
  - ReadWriteMany
  storageClassName: standard

查看pv,pvc

k8s怎么进入gitlab k8s怎么进入节点执行命令_docker_13


配置并启动mypod1,mypod2,同上,其中将mypod2配置文件的metadata的name改为mypod2,再执行下次命令:

kubectl exec mypod1 --  touch  /mydata/test
kubectl exec mypod2 -- ls /mydata

结果如图所示,这样就实现了mypod1和mypod2的数据共享

k8s怎么进入gitlab k8s怎么进入节点执行命令_Pod_14

Secret & ConfigMap

创建secret:

kubectl create secret generic mysecret --from-literal=password=123456

查看secret:

kubectl describe secret mysecret

k8s怎么进入gitlab k8s怎么进入节点执行命令_docker_15


创建configmap:

kubectl create configmap myconfmap --from-literal=user=admin

查看configmap:

kubectl describe configmap myconfmap

k8s怎么进入gitlab k8s怎么进入节点执行命令_Pod_16


将Secret和ConfigMap应用到容器中:

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:   
  containers:
    - name: mypod1
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      volumeMounts:
        - mountPath: "/mysecret"
          name: mysecret
        - mountPath: "/myconf"
          name: myconf
  volumes:
    - name: mysecret
      secret:
        secretName: mysecret
        items:
        - key: password
          path: pass
    - name: myconf
      configMap:
        name: myconfmap
        items:
        - key: user
          path: user

从容器中查看Secret和ConfigMap信息:

kubectl exec mypod1 -- cat /mysecret/pass
kubectl exec mypod1 -- cat /myconf/user

k8s怎么进入gitlab k8s怎么进入节点执行命令_docker_17

健康检查:

Liveness探测:

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:
  restartPolicy: OnFailure   
  containers:
    - name: mypod1
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      args:
      - /bin/sh
      - -c
      - touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 100
      livenessProbe:
        exec:
          command:
          - cat
          - /tmp/healthy
        initialDelaySeconds: 5
        periodSeconds: 3

使用kubectl describe pod/mypod1查看Liveness事件

k8s怎么进入gitlab k8s怎么进入节点执行命令_docker_18

Readiness探测:

apiVersion: v1
kind: Pod
metadata:
  name: mypod2
spec:
  restartPolicy: OnFailure   
  containers:
    - name: mypod2
      image: docker.io/jocatalin/kubernetes-bootcamp:v1
      args:
      - /bin/sh
      - -c
      - touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 100
      readinessProbe:
        exec:
          command:
          - cat
          - /tmp/healthy
        initialDelaySeconds: 5
        periodSeconds: 3

使用kubectl describe pod/mypod2查看Readiness事件

k8s怎么进入gitlab k8s怎么进入节点执行命令_kubernetes_19