Skip to content

Instantly share code, notes, and snippets.

@sh7ning
Last active September 7, 2021 08:21
Show Gist options
  • Save sh7ning/c0a43f17300b60eca8998686aeea5823 to your computer and use it in GitHub Desktop.
Save sh7ning/c0a43f17300b60eca8998686aeea5823 to your computer and use it in GitHub Desktop.
k8s note

K8S笔记

参考

多k8s集群配置切换配置

export KUBECONFIG=~/.kube/config.aws

常见使用

  • 指定namespace
kubectl -n <namespace>
# 获取所有命名空间下的 pod
kubectl get pod -A

# 获取指定命名空间下所有的资源
kubectl -n elk get all
  • 获取 node
kubectl get nodes -o wide 获取更多详情
kubectl get nodes -o json json 格式

-o wide获取更多详情

kubectl get nodes --watch

--watch一直监听变化

  • 获取所有的api资源
kubectl api-resources 

常用资源:

  1. nodes | no
  2. pods | po
  3. services | svc
  4. namespaces | ns
  5. configmaps | cm
  6. deployments| deploy
  • 列举所有 PV 持久卷,按容量排序
kubectl get pv --sort-by=.spec.capacity.storage
  • 获取资源的描述
kubectl explain <资源名>
  • 获取 endpoints,service关联的pod的ip地址和端口
kubectl get endpoints
  • 创建资源

mysql.yaml

apiVersion: v1
kind: Pod
metadata:
    name: mysql
    labels:
        app: mysql
spec:
    containers:
        - name: mysql
          image: mysql:5.7
          ports:
              - containerPort: 3306
          env:
              - name: MYSQL_ROOT_PASSWORD
                value: "123123"
kubectl create -f mysql.yaml
kubectl apply -f mysql.yaml
  • 删除资源
kubectl delete -f mysql.yaml
kubectl delete pods <name>
  • 显示资源详细信息
kubectl describe nodes <node-name>
kubectl -n admin describe pod 容器
  • 查看日志
kubectl logs --tail=20 -f <pod-name> -c <container-name>
  • 本地端口转发
kubectl port-forward pod/mysql 3310:3306
  • 暴露一个资源作为k8s service

如将mysql pod暴露为一个service:

  1. kubectl expose pod mysql --port=3306 --name=mysql
  2. kubectl get svc
  3. kubectl edit svc mysql 更新 type: NodePort
  4. kubectl get node -o wide
  5. kubectl get pod -o wide
  • 执行命令
kubectl exec mysql -c mysql -it /bin/bash
kubectl -n admin exec -it 容器 /bin/bash 

Helm 使用

  • 增加 repo
helm repo list
helm repo add stable     https://kubernetes-charts.storage.googleapis.com          
helm repo add incubator  https://kubernetes-charts-incubator.storage.googleapis.com
helm repo add elastic    https://helm.elastic.co
helm search repo mysql
helm pull --untar stable/mysql
helm install mysql .
helm uninstall mysql 
# 不真正执行,只看运行时的 yaml 
helm install kfk kafka --dry-run

管理软件

  • Kuboard
  • Lens
  • KubeSphere
  • Wayne
  • rancher
  • Kubernetes Dashboard
Curl -X Delete https://latency-es.demo.com/latency-2020.11.21
@sh7ning
Copy link
Author

sh7ning commented May 10, 2021

kubeadm 部署集群

kubeadm init --apiserver-advertise-address=192.168.x.x --pod-network-cidr=10.244.0.0/16

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl get pods --all-namespaces

@sh7ning
Copy link
Author

sh7ning commented Sep 7, 2021

启动
minikube start --driver=hyperkit --container-runtime=containerd --cpus=4 --memory=8g

minikube ip

minikube stop 

minikube delete --all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment