kubectl管理kubernetes集群
[root@master ~]# kubectl get nodes 查看集群节点
NAME STATUS AGE
node1 Ready 25m
node2 Ready 19m
[root@master ~]# kubectl version 查看版本
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
[root@master ~]# kubectl run nginx --image=docker.io/nginx --replicas=1 --port=9000
deployment "nginx" created
[root@master ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 1 1 1 0 15s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-2187705812-8r0h4 1/1 Running 0 1h
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-2187705812-8r0h4 1/1 Running 0 1h 10.255.4.2 node1
想要删除一个容器的时候:
[root@master ~]# kubectl delete pod nginx-2187705812-8r0h4
pod "nginx-2187705812-8r0h4" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-2187705812-6dn2r 0/1 ContainerCreating 0 4s
删除了之后还依然有,这个是因为创建deployment的时候参数--replicas=1起作用了。想要删除的话直接删除deployment就可以了。
[root@master ~]# kubectl delete deployment nginx
deployment "nginx" deleted
yaml语法:
[root@master ~]# kubectl create -f mysql-deployment.yaml
deployment "mysql" created
[root@master ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
mysql 1 1 1 1 9s
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-2261771434-r8td1 1/1 Running 0 16s 10.255.4.2 node1
在node1上查看mysql docker实例
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e33797549b8e docker.io/mysql/mysql-server "/entrypoint.sh my..." 4 minutes ago Up 4 minutes (healthy) k8s_mysql.31ec27ee_mysql-2261771434-r8td1_default_351da1d4-f082-11e8-bbf2-000c297d60e3_089418b7
kubectl其他参数:
logs 取得pod中容器的log信息
exec 在容器中执行一条命令
cp 从容器拷出或者想容器中拷入文件
attach attach到一个运行的容器上
logs
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-2261771434-r8td1 1/1 Running 0 18m
[root@master ~]# kubectl logs mysql-2261771434-r8td1
[Entrypoint] MySQL Docker Image 5.7.20-1.1.2
[Entrypoint] Initializing database
[Entrypoint] Database initialized
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
[Entrypoint] ignoring /docker-entrypoint-initdb.d/*
[Entrypoint] Server shut down
[Entrypoint] MySQL init process done. Ready for start up.
[Entrypoint] Starting MySQL 5.7.20-1.1.2
exec:
[root@master ~]# kubectl exec mysql-2261771434-r8td1 ls
[root@master ~]# kubectl exec -it mysql-2261771434-r8td1 bash
bash-4.2#
cp:
[root@master ~]# kubectl cp mysql-2261771434-r8td1:/tmp/hosts /etc/hosts
error: unexpected EOF
[root@master ~]# kubectl cp --help
Examples:
# !!!Important Note!!!
# Requires that the 'tar' binary is present in your container 使用kubectl cp 你的容器实例中必须有tar命令,如果没有的话就会失败
# image. If 'tar' is not present, 'kubectl cp' will fail.
[root@master ~]# kubectl exec -it mysql-2261771434-r8td1 bash
bash-4.2# yum install tar net-tools -y
bash-4.2# echo 'this is test' > /tmp/test.txt
再次测试:
拷贝出来
[root@master ~]# kubectl cp mysql-2261771434-r8td1:/tmp/test.txt /opt/test.txt
[root@master ~]# more /opt/test.txt
this is test
拷贝回去:
[root@master ~]# echo "this is out" >> /opt/test.txt
[root@master ~]# kubectl cp /opt/test.txt mysql-2261771434-r8td1:/tmp/test.txt
[root@master ~]# kubectl exec -it mysql-2261771434-r8td1 bash
bash-4.2# cat /tmp/test.txt
this is test
this is out
kubectl attach:
用户取得pod中容器的实时信息,可以持续不断实时的取出信息。类似于tail -f