#设置镜像加速
cat /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"], #此处在k8s1.2版本以上部署时建议与该内容一致。
"registry-mirrors": ["https://zzypv4yy.mirror.aliyuncs.com"]
}
#--dry-run输出pod创建的yaml文件
[root@k8s-master-01 ~]# kubectl run www --image=nginx --dry-run -o yaml
W0211 12:25:12.671160 43802 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: www
name: www
spec:
containers:
- image: nginx
name: www
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@k8s-master-01 ~]# kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://192.168.71.133:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
namespace: app01
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
#查看api对象与版本
[root@k8s-master-01 ~]# kubectl api-resources
#查看kube-sytem ns中的pod运行信息
[root@k8s-master-01 ~]# kubectl get pods -o wide -n kube-system
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-6d8c4cb4d-5x5gq 1/1 Running 8 (103m ago) 2d18h 10.244.0.31 k8s-master-01 <none> <none>
coredns-6d8c4cb4d-m2gjp 1/1 Running 8 (103m ago) 2d18h 10.244.0.30 k8s-master-01 <none> <none>
etcd-k8s-master-01 1/1 Running 10 (103m ago) 2d18h 192.168.71.133 k8s-master-01 <none> <none>
kube-apiserver-k8s-master-01 1/1 Running 10 (103m ago) 2d18h 192.168.71.133 k8s-master-01 <none> <none>
kube-controller-manager-k8s-master-01 1/1 Running 10 (103m ago) 2d18h 192.168.71.133 k8s-master-01 <none> <none>
kube-flannel-ds-8tzbw 1/1 Running 7 (101m ago) 2d18h 192.168.71.134 k8s-node-01 <none> <none>
kube-flannel-ds-d6fpt 1/1 Running 0 13h 192.168.71.135 k8s-node-02 <none> <none>
kube-flannel-ds-p46hm 1/1 Running 12 (102m ago) 2d18h 192.168.71.133 k8s-master-01 <none> <none>
kube-proxy-kfjhw 1/1 Running 0 13h 192.168.71.135 k8s-node-02 <none> <none>
kube-proxy-p5gws 1/1 Running 5 (101m ago) 2d18h 192.168.71.134 k8s-node-01 <none> <none>
kube-proxy-tmc9w 1/1 Running 8 (103m ago) 2d18h 192.168.71.133 k8s-master-01 <none> <none>
kube-scheduler-k8s-master-01 1/1 Running 10 (103m ago) 2d18h 192.168.71.133 k8s-master-01 <none> <none>
#查看pod内容选项
[root@k8s-master-01 ~]# kubectl explain pod.spec
#查看pod标签
[root@k8s-master-01 ~]# kubectl get pods --show-labels
#查看pod信息
[root@k8s-master-01 ~]# kubectl describe pod www
#查看node信息
[root@k8s-master-01 ~]# kubectl describe node k8s-node-01
#修改pod信息
[root@k8s-master-01 ~]# kubectl edit pod www
#查看event
[root@k8s-master-01 ~]# kubectl get event
#删除pod
[root@k8s-master-01 ~]# kubectl delete pod www --grace-period=0 --force
#在pod中执行命令
[root@k8s-master-01 ~]# kubectl exec -it www -- ls /
bin docker-entrypoint.d home media proc sbin tmp
boot docker-entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
#从宿主机拷贝文件至pod相应目录
[root@k8s-master-01 ~]# kubectl cp /etc/hosts www:/tmp
[root@k8s-master-01 ~]# kubectl exec -it www -- ls /tmp
hosts
#从pod拷贝文件至宿主机
[root@k8s-master-01 ~]# kubectl cp www:/etc/hosts hosts.bak
[root@k8s-master-01 ~]# cat hosts.bak
# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.244.1.15 www
#进入指定pod中的container
[root@k8s-master-01 ~]# kubectl exec -it www -c www -- ls /tmp
hosts
#标签
[root@k8s-master-01 ~]# kubectl label nodes --all app=trust
[root@k8s-master-01 ~]# kubectl label nodes --all app-
[root@k8s-master-01 ~]# kubectl get pods -l app=www
#Taints
[root@k8s-master-01 ~]# kubectl taint node k8s-node-01 xx=yy:NoSchedule
[root@k8s-master-01 ~]# kubectl describe node k8s-node-01 | grep Taints
Taints: xx=yy:NoSchedule
##测试Taint
[root@k8s-master-01 ~]# kubectl run pod www --image=nginx --dry-run -o yaml
W0211 17:04:11.389901 16593 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod
name: pod
spec:
containers:
- args:
- www
image: nginx
name: pod
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
##以上内容复制到www02.yaml并创建pod,给仅有的node节点打上了Taint,导致无法调度
[root@k8s-master-01 ~]# kubectl apply -f www02.yaml
pod/pod created
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod 0/1 Pending 0 3s
##修改www02.yaml
[root@k8s-master-01 ~]# cat www02.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod
name: www02
spec:
containers:
- image: nginx
name: nginx-01
tolerations:
- effect: NoSchedule
key: xx
value: yy
operator: Equal
dnsPolicy: ClusterFirst
restartPolicy: Always
[root@k8s-master-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
www02 1/1 Running 0 42s
##去除Taint
[root@k8s-master-01 ~]# kubectl taint node k8s-node-01 xx:NoSchedule-
node/k8s-node-01 untainted