Kubernetes k8s学习命令

Kubernetes_k8s视频教程_带你从基础入门到精通k8s集群视频链接

1. 安装 kubeadm,kubelet 和 kubectl

yum install kubelet-1.19.4 kubeadm-1.19.4 kubectl-1.19.4 -y

2. 查看系统的pods

kubectl get pods -n kube-system
[root@k8smaster ~]# kubectl get pods -n kube-system
NAME                                READY   STATUS    RESTARTS   AGE
coredns-6d56c8448f-ftw5w            1/1     Running   2          8d
coredns-6d56c8448f-q9bm4            1/1     Running   2          8d
etcd-k8smaster                      1/1     Running   3          8d
kube-apiserver-k8smaster            1/1     Running   3          8d
kube-controller-manager-k8smaster   1/1     Running   3          8d
kube-flannel-ds-dmq5n               1/1     Running   2          8d
kube-flannel-ds-mwpk2               1/1     Running   2          8d
kube-proxy-428jk                    1/1     Running   3          8d
kube-proxy-cm629                    1/1     Running   3          8d
kube-scheduler-k8smaster            1/1     Running   3          8d

3. 在Kubernetes集群中部署一个Nginx,并暴露端口

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc

4. 查看所有的deployments

kubectl get deployment
[root@k8smaster ~]# kubectl get deployment
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
nginx    1/1     1            1           7d17h
tomcat   1/1     1            1           7d19h

5. 查看非系统外的所有pods

kubectl get pods
[root@k8smaster ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-fkc62    1/1     Running   1          7d17h
tomcat-7d987c7694-g2zmd   1/1     Running   1          7d19h

6. 查看所有的services

查看所有的services。查看所有的services就可以看到所有端口的暴露情况。

kubectl get service
[root@k8smaster ~]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          8d
tomcat       NodePort    10.96.208.117   <none>        8080:31317/TCP   7d19h

7. 删除一个deployment

deployment删除完以后,对应的pods都会跟着删除。

kubectl delete deployment nginx
[root@k8smaster ~]# kubectl delete deployment nginx
deployment.apps "nginx" deleted

8. 空运行一遍部署并生成yaml文件

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > depoly.yaml

9. 通过ymal文件来部署

kubectl apply -f depoly.yaml

10. 常见概念关系

Master控制Node --> service --> deployment(控制器) --> pod --> docker

11. 一个完整的deploy.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: springboot-k8s
  name: springboot-k8s
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springboot-k8s
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: springboot-k8s
    spec:
      containers:
      - image: 38-springboot-k8s-1.0.0-jar
        name: 38-springboot-k8s-1-0-0-jar-4b7hj
        imagePullPolicy: Never
        resources: {}
status: {}

imagePullPolicy: Never代表不去远程仓库拉取

12. 部署k8s控制台

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml

查看命名空间

kubectl get pod -n kubernetes-dashboard

13. 三种端口说明

ports:  
 - name: http
   port: 80
   targetPort: 80
   nodePort: 30008
   protocol: TCP
nodePort

nodePort

外部机器(在windows浏览器)可以访问的端口;
比如一个Web应用需要被其他用户访问,那么需要配置type=NodePort,而且配置nodePort=30001,那么其他机器就可以通过浏览器访问scheme://node:30001访问到该服务;

targetPort

容器的端口,与制作容器时暴露的端口一致(Dockerfile中EXPOSE),例如docker.io官方的nginx暴露的是80端口;

port

Kubernetes集群中的各个服务之间访问的端口,虽然mysql容器暴露了3306端口,但外部机器不能访问到mysql服务,因为他没有配置NodePort类型,该3306端口是集群内其他容器需要通过3306端口访问该服务;

14. 查看Ingress转发规则

kubectl get ingress
[root@k8smaster file]# kubectl get ingress
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
NAME                CLASS    HOSTS           ADDRESS         PORTS   AGE
k8s-ingress         <none>   www.abc.com     192.168.2.160   80      15h
k8s-ingress-cloud   <none>   www.cloud.com   192.168.2.160   80      3m19s

15. 查看pods的log

kubectl logs -f spring-cloud-alibaba-provider-5db86d4cc7-p29qb

16. pods的缩放

kubectl scale -n default deployment nginx --replicas=1

16. 生成暴露端口的yaml文件

kubectl expose deploy nginx --port=80 --dry-run=client -o yaml > nginx_service.yaml

生成的暴露端口service文件:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
status:
  loadBalancer: {}

应用配置文件:

kubectl apply -f nginx_service.yaml

你可能感兴趣的:(kubernetes,学习,docker)