K8S基础知识

常用命令

kubectl

# redis-master是pod名称
kubectl describe pod redis-master
kubectl describe service redis-service
#查看容器日志
kubectl logs redis-master
kubectl logs <pod_name> -c <container_name> # 等价于 docker logs <container_id>
# 查看
kubectl api-versions
kubectl version
kubectl create -f mysl-rc.yaml
kubectl get rc
kubectl get pods
# 查看service
kubectl get svc
# 与create的区别;可使用配置文件的网络地址
kubectl apply -f https://k8s.io/examples/application/guestbook/redis-slave-deployment.yaml
# ?
kubectl create -k ./
kubectl delete -k ./
kubectl get services wordpress
kubectl get service frontend
kubectl get nodes

输出信息

chugangdeMacBook-Pro:basic cg$ kubectl get nodes
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   23d   v1.15.2
kubectl describe node minikube

部分输出信息,其中包含Events。

chugangdeMacBook-Pro:basic cg$ kubectl describe node minikube
Name:               minikube
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=minikube
                    kubernetes.io/os=linux
                    node-role.kubernetes.io/master=
Annotations:        kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Mon, 13 Jan 2020 09:58:16 +0800
Taints:             <none>
Unschedulable:      false
Conditions:
System Info:
Machine ID:                 01d60724debb4041ba73d45cd17b2880
System UUID:                5554DBD6-8259-3749-AB7D-0422392E9BC8
Boot ID:                    33f60c61-2c9f-49a7-a2df-57f31b594a5f
Kernel Version:             4.15.0
OS Image:                   Buildroot 2018.05.3
Operating System:           linux
Architecture:               amd64
Container Runtime Version:  docker://18.9.8
Kubelet Version:            v1.15.2
Kube-Proxy Version:         v1.15.2

service的内外IP

[root@localhost ~]# kubectl get service frontend
NAME       TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
frontend   NodePort   10.111.125.32   <none>        80:31826/TCP   19d

minikube

# create a new cluster 或 重启已经存在的node
minikube start -p <name>
minikube start -p wp
minikube start hello
minikube delete
minikube status hello
# 不使用虚拟机
minikube start --vm-driver=none -p guestbook
minikube start -p wp

输出信息

chugangdeMacBook-Pro:source cg$ minikube start -p wp
😄  [wp] minikube v1.3.1 on Darwin 10.15.1
🔄  Starting existing virtualbox VM for "wp" ...
⌛  Waiting for the host to be provisioned ...
🐳  Preparing Kubernetes v1.15.2 on Docker 18.09.8 ...
🔄  Relaunching Kubernetes using kubeadm ...
^@⌛  Waiting for: apiserver proxy etcd scheduler controller dns
🏄  Done! kubectl is now configured to use "wp"

输出信息

chugangdeMacBook-Pro:wp cg$ minikube service wordpress --url -p wp
http://192.168.99.114:31522

注解

minikube service wordpress –url -p wp 的作用,看权威资料后再确定。

配置文件

rc定义文件

_images/1-1.png

service定义文件

_images/1-2.png

注解

port是外部访问此service时使用的端口吗?

配置范例

注解

抽时间将下面这些配置的注释全部写上

application/guestbook/redis-master-deployment.yaml

#application/guestbook/redis-master-deployment.yaml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: master
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: master
        tier: backend
    spec:
      containers:
      - name: master
        image: k8s.gcr.io/redis:e2e  # or just image: redis
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379

application/guestbook/redis-master-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
    role: master
    # tier是什么
    tier: backend
spec:
  ports:
  # 是外部访问使用的端口吗?
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend

疑问

Kuberntes 开发指南,内容并非源码,而是Kubernets提供的restful API,使用这种知识的场景是什么?创建pod、service等,目前是使用kubectl, 这些API是在代码比如Java中完成同类工作吗?

192.168.99.113