k8s系列---kubectl基礎

kubectl get pods  查看全部podsnode

kubectl  get  services  查看servicespython

kubectl replace --filename=myweb-rc.yaml  根據該yaml更新pod能夠修改副本數爲0,刪除podlinux

docker ps -a   查看全部docker容器git

docker  attach 容器名     進入容器內github

kubectl  describe pod pod名字   查看該pod的詳細信息web

 

 http://blog.itpub.net/28916011/viewspace-2213957/docker

 

k8s
master/node
	master : API Server ,Scheduler,Controller-Manager
	node : kubelet,docker,kube-proxy

Pod:
	自主式Pod
	控制器管理的Pod
		ReplicationController(RC)
		ReplicaSet
		Deployment
		StatefulSet
		DaemonSet
		Job,CronJob

環境
	master,etcd:172.18.0.67
	node1:172.18.0.68
	node2:172.18.0.69
前提;
	1:基於主機名通訊:/etc/hosts
	2:時間同步
	3:關閉firewall和iptables.service
	OS:Centos7.3 1611
		   yum -y install ntp
		   systemctl start ntpd
		   systemctl enable ntpd
		   systemctl stop firewalld
		   systemctl disable firewalld
		   
		   vim /etc/hosts
				172.16.100.64   master
				172.16.100.65   node1
				172.16.100.66   node2
				
		   wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
		   [root@master yum.repos.d]# cat kubernets.repo 
				[kubernetes]
				name=Kubernetes Repo
				baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
				gpgcheck=0
			scp kubernets.repo  docker-ce.repo  node1:/etc/yum.repos.d
			scp kubernets.repo  docker-ce.repo  node2:/etc/yum.repos.d
			yum install docker-ce kubelet kubeadm  kubectl

	
	
	
	
		   vim /usr/lib/systemd/system/docker.service 
				[service]增長
				Environment="HTTPS_PROXY=http://www.ik8s.io:10080"
				Environment="NO_PROXY=127.0.0.0/8,172.16.0.0/16"
		   systemctl daemon-reload
		   systemctl restart docker
		   systemctl status docker
		   docker info
		   確保下面兩個爲1
		   [root@master ~]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables 
			1
		   [root@master ~]# cat /proc/sys/net/bridge/bridge-nf-call-iptables 
			1
	
	
		systemctl status kubelet有出錯信息,須要停掉kubelet,而後設置開機啓動,docker也設成開機啓動,出不出錯都要操做,後面初始化出錯了
		而後初始化
		初始化的時候出錯。由於swap選項
		kubeadm  init --help 查看幫助信息, --kubernetes-version string 指定版本,默認爲當前穩定版
		kubeadm  init --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12
		若是出錯有swap,不支持。須要的操做:
		vim /etc/sysconfig/kubelet
		KUBELET_EXTRA_ARGS="--fail-swap-on=false"
		而後從新初始化,添加忽略參數
	    kubeadm  init --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
		媽逼的代理不成功,總下載失敗,最後搜到能夠這麼幹--image-repository registry.aliyuncs.com/google_containers 配置文件取消掉代理,從新reload,重啓docker
		kubeadm  init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.13.1  --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
		安裝完成結尾會有token和哈希碼,必定要保存下來。否則之後就麻煩了,就是相似下面這串。
		kubeadm join 172.16.100.64:6443 --token yqzclr.ztxgcumdlqzjbpl4 --discovery-token-ca-cert-hash sha256:b5119e9530e8bd01b0317e19f3ca14835ad457f3d33a5de963e7a8fa33f3a541
		
		
		接下來等master安裝完回到master操做
		[root@master ~]#  mkdir -p $HOME/.kube
	    [root@master ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
		
		[root@master ~]# kubectl get cs     查詢組件狀態健康信息
		
		二:部署網絡插件flannel   https://github.com/coreos/flannel
		kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
		部署完成後稍等一分鐘
		kubectl get nodes  能夠發現master狀態Ready了。
		kubectl get pods -n kube-system   會發現flannel的pod
		
		客戶端node操做
		yum -y install docker-ce kubelet kubeadm
		
		master執行 拷貝配置文件到兩臺node上
        scp /usr/lib/systemd/system/docker.service node1:/usr/lib/systemd/system/docker.service
        scp /etc/sysconfig/kubelet node1:/etc/sysconfig/kubelet
		
		客戶端node操做
		yum -y install docker-ce kubelet kubeadm
	    systemctl enable docker kubelet
		systemctl daemon-reload
	    systemctl start docker 
	    docker info
		加入到master,就是以前複製的那一串,而後加上忽略Swap
	    kubeadm join 172.16.100.64:6443 --token yqzclr.ztxgcumdlqzjbpl4 --discovery-token-ca-cert-hash sha256:b5119e9530e8bd01b0317e19f3ca14835ad457f3d33a5de963e7a8fa33f3a541 --ignore-preflight-errors=Swap
		
		以上安裝所有完成
		
		
		
		
安裝配置
	1:etcd cluster 僅master節點
	2:flannel 集羣的全部節點
	3:配置k8s的master 僅master節點
		kubernets-master
		啓動的服務:
			kube-apiserver kube-scheduler kube-controller-manager
	4:配置k8s的各Node節點
		kubernets-node
		
		先設定啓動docker服務
		啓動k8s的服務:
			kube-proxy,kubelte
			
kubeadm
	1:master,node:安裝kubelet kubeadm docker
	2:master:kubeadm init

  

 day2      http://blog.itpub.net/28916011/viewspace-2213957/

k8s相關介紹

資源:對象
	workload:Pod,ReplicaSet,Deployment,StatefulSet,Job,Cronjob...
	服務發現及均衡:Service,Ingress...
	配置與存儲:Volume,CSI,
	    ConfigMap,Secret
	集羣級資源:
		NameSpace,Node,Role,ClusterRole,RoleBind,ClusterRoleBind
	元數據型資源
		HPA,PodTemplate,LimitRange,
	
		
建立資源的配置清單
	apiVersion: group/version
		$ kubectl api-version
	kind:資源類別
	metadata:元數據
		name
		namespace
		labels
		annotations
		每一個資源的引用PATH
			/api/GROUP/VERSION/namespace/NAMESPACE/TYPE/NAME
			
	spec: 指望狀態 disired state
	status:當前狀態  current state,本字段由k8s集羣維護
	
		
	Pod 資源
		spec.containers <[]object>
		
		- name <string>
		  image <string>   //若是是latest標籤,就會一直下載,可是下面若是改爲ifnotpresent 那就不會一直下不會使用默認的aways
		  imagePullPolicy <string>
		    Always,Never,IfNotPresent    //always老是下載,Never不下載,ifnotpresent 若是不存在就下載
		  
		  
		  
標籤操做
	查詢某個pod,node標籤,
	[root@master k8syaml]# kubectl get pods pod-demo --show-labels
	查詢app標籤的pod
	kubectl get pods -l app --show-labels  
	添加標籤爲某個pod
	kubectl label pods pod-demo relase=beata
	修改某個標籤的值
	kubectl label pods pod-demo relase=stable --overwrite
	
	標籤選擇器
		等值關係:=,==,!=
		集合關係:key in (value1,value2);key notin (value1,value2)
		
	查詢relase等於stable,而且app=myapp標籤的pod
	[root@master k8syaml]# kubectl get pods -l relase=stable,app=myapp	
	基於集合的查詢,relase若是等於stable或者beta的pod
	[root@master k8syaml]# kubectl get pods -l "relase in (stable,beta)"
	
	基於節點的
	[root@master k8syaml]# kubectl get nodes  --show-labels
	NAME     STATUS   ROLES    AGE     VERSION   LABELS
	master   Ready    master   2d22h   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
	node1    Ready    <none>   2d20h   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node1
	node2    Ready    <none>   2d20h   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2
	[root@master k8syaml]# kubectl label nodes node1 disktype=ssd
	node/node1 labeled
	[root@master k8syaml]# kubectl get nodes  --show-labels
	NAME     STATUS   ROLES    AGE     VERSION   LABELS
	master   Ready    master   2d22h   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
	node1    Ready    <none>   2d20h   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node1
	node2    Ready    <none>   2d20h   v1.13.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node2


	
	nodeSelector: 指定pod運行在標籤哪一個節點,下面是運行在標籤爲disktype: ssd的nod上
	[root@master k8syaml]# cat demo.yaml 
	apiVersion: v1
	kind: Pod
	metadata:
		name: pod-demo
		namespace: default
		labels:
			app: myapp
			tier: forntend
	spec:
		containers:
		- name: myapp
		  image: ikubernetes/myapp:v1
		- name: busybox
		  image: busybox:latest
		  command: 
		  - "/bin/sh"
		  - "-c"
		  - "sleep 3600"
		nodeSelector:
			disktype: ssd
			
			
	
	nodeName:運行在指定的節點上
	annotations:與label不一樣的地方在於,它不能用於挑選資源對象,僅用於爲對象提供「元數據」
	[root@master k8syaml]# vim demo.yaml 
	apiVersion: v1
	kind: Pod
	metadata:
		name: pod-demo
		namespace: default
		labels:
			app: myapp
			tier: forntend
		annotations:
			lihongxing.com/created-by: cluster admin
	spec:
		containers:
		- name: myapp
		  image: ikubernetes/myapp:v1
		- name: busybox
		  image: busybox:latest
		  command:
		  - "/bin/sh"
		  - "-c"
		  - "sleep 3600"

		nodeSelector:
			disktype: ssd
			
	以上會添加lihongxing.com/created-by: cluster admin的annotations資源註解,用describe能夠查看到
	[root@master k8syaml]# kubectl describe pods pod-demo

		  
Pod生命週期:http://blog.itpub.net/28916011/viewspace-2213957/
	狀態:Pending/Running/Failed/Succeeded/Unknown
	Pod生命週期的重要行爲:
		初始化容器
		容器探測:
			liveness 存活性探針
			readiness  就緒性探針
	restartpolicy 重啓策略:
		Always,OnFailure,Never,Default to Always
	探針類型三種:
		ExecAction,TcpSocketAction,HttpGetAction
		
	[root@master k8syaml]# vim liveness.yaml
	apiVersion: v1
	kind: Pod
	metadata:
		name: liveness-exec.pod
		namespace: default
	spec:
		containers:
		- name: liveness-exec-container
		  image: busybox:latest
		  imagePullPolicy: IfNotPresent
		  command: ["/bin/sh","-c","toch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 3600"]
		  livenessProbe:
			  exec:
				  command: ["test","-e","/tmp/healthy"]
			  initialDelaySeconds: 1
			  periodSeconds: 3	
		
相關文章
相關標籤/搜索