Kubernetes(k8s)入门、单机版安装、kuberctl指令、k8s服务实例

1.切换root

1.关闭centos自带的防火墙

# systemctl disable firewalld


# systemctl stop firewalld 

2.安装etcd和kubernetes软件(会自动安装docker)

# yum install -y etcd kubernetes

2.修改两处配置

Docker配置文件/etc/sysconfig/docker, OPTIONS=’–selinux-enabled=false –insecure-registry gcr.io’
Kubernetes apiservce配置文件/etc/kubernetes/apiserver,把–admission_control参数钟的ServiceAccount删除

3.启动所有服务

[root@localhost kubernetes]# systemctl start etcd
[root@localhost kubernetes]# systemctl start docker
[root@localhost kubernetes]# systemctl start kube-apiserver
[root@localhost kubernetes]# systemctl start kube-controller-manager
[root@localhost kubernetes]# systemctl start kube-scheduler
[root@localhost kubernetes]# systemctl start kubelet
[root@localhost kubernetes]# systemctl start kube-proxy

测试,查看

部署nginx服务

[root@localhost kubernetes]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED                  SIZE
docker.io/nginx     latest              5699ececb21c        Less than a second ago   109 MB
docker.io/ubuntu    16.04               5e8b97a2a082        24 hours ago             114 MB
hongdada/nginx      v3                  c5cf58738d6b        3 days ago               109 MB
docker.io/busybox   latest              8c811b4aec35        2 weeks ago              1.15 MB
docker.io/tomcat    latest              33e02377a00f        8 weeks ago              554 MB
[root@localhost kubernetes]# kubectl run my-nginx --image=hongdada/nginx:v3 --port=80
deployment "my-nginx" created
[root@localhost kubernetes]# kubectl get pod
NAME                        READY     STATUS              RESTARTS   AGE
my-nginx-3156591236-q1jvn   0/1       ContainerCreating   0          11s

创建gcr.io/google_containers/pause-amd64:3.0镜像

[root@localhost kubernetes]# docker pull googlecontainer/pause-amd64:3.0
Trying to pull repository docker.io/googlecontainer/pause-amd64 ... 
3.0: Pulling from docker.io/googlecontainer/pause-amd64
4f4fb700ef54: Pull complete 
ce150f7a21ec: Pull complete 
Digest: sha256:f04288efc7e65a84be74d4fc63e235ac3c6c603cf832e442e0bd3f240b10a91b
Status: Downloaded newer image for docker.io/googlecontainer/pause-amd64:3.0
[root@localhost kubernetes]# docker tag googlecontainer/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0
[root@localhost kubernetes]# docker image ls
REPOSITORY                              TAG                 IMAGE ID            CREATED                  SIZE
docker.io/nginx                         latest              5699ececb21c        Less than a second ago   109 MB
docker.io/ubuntu                        16.04               5e8b97a2a082        24 hours ago             114 MB
hongdada/nginx                          v3                  c5cf58738d6b        3 days ago               109 MB
docker.io/busybox                       latest              8c811b4aec35        2 weeks ago              1.15 MB
docker.io/tomcat                        latest              33e02377a00f        8 weeks ago              554 MB
docker.io/googlecontainer/pause-amd64   3.0                 99e59f495ffa        2 years ago              747 kB
gcr.io/google_containers/pause-amd64    3.0                 99e59f495ffa        2 years ago              747 kB

kubernetes指令:

# 查看版本
$ 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"}

# 显示集群信息
$ kubectl cluster-info
Kubernetes master is running at http://localhost:8080
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

# 查看集群中有几个Node
$ kubectl get nodes
NAME        STATUS    AGE
127.0.0.1   Ready     8m

# 运行一个镜像
$ kubectl run my-nginx --image=nginx --replicas=2 --port=80
deployment "my-nginx" created

# 查看pod
$ kubectl  get pods
NAME                       READY     STATUS              RESTARTS   AGE
my-nginx-379829228-cwlbb   0/1       ContainerCreating   0          20s
my-nginx-379829228-czk6w   1/1       Running             0          20s

# 查看服务详情信息
$ kubectl  describe pod my-nginx-379829228-cwlbb
Name:        my-nginx-3156591236-q1jvn
Namespace:    default
Node:        127.0.0.1/127.0.0.1
Start Time:    Thu, 07 Jun 2018 05:33:48 +0800
Labels:        pod-template-hash=3156591236
        run=my-nginx
Status:        Pending
IP:        
Controllers:    ReplicaSet/my-nginx-3156591236
Containers:
  my-nginx:
    Container ID:        
    Image:            hongdada/nginx:v3
    Image ID:            
    Port:            80/TCP
    State:            Waiting
      Reason:            ContainerCreating
    Ready:            False
    Restart Count:        0
    Volume Mounts:        <none>
    Environment Variables:    <none>
Conditions:
  Type        Status
  Initialized     True 
  Ready     False 
  PodScheduled     True 
No volumes.
QoS Class:    BestEffort
Tolerations:    <none>
Events:
  FirstSeen    LastSeen    Count    From            SubObjectPath    Type        Reason        Message
  ---------    --------    -----    ----            -------------    --------    ------        -------
  2m        2m        1    {default-scheduler }            Normal        Scheduled    Successfully assigned my-nginx-3156591236-q1jvn to 127.0.0.1
  2m        45s        4    {kubelet 127.0.0.1}            Warning        FailedSync    Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.  details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

  1m    3s    7    {kubelet 127.0.0.1}        Warning    FailedSync    Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image "registry.access.redhat.com/rhel7/pod-infrastructure:latest""

# 查看已部署
$ kubectl  get deployments
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
my-nginx   1         1         1            0           3m

# 删除pod
$ kubectl delete pod my-nginx-3156591236-q1jvn
pod "my-nginx-3156591236-q1jvn" deleted

# 再次查看pod,发现由于replicas机制,pod又生成一个新的
$ kubectl  get pods
NAME                        READY     STATUS              RESTARTS   AGE
my-nginx-3156591236-qm0fq   0/1       ContainerCreating   0          8s

# 删除部署的my-nginx服务。彻底删除pod
$ kubectl delete deployment my-nginx
deployment "my-nginx" deleted

# 再次查看pod
$ kubectl get pods
No resources found.

对比docker命令:

# docker run
$ docker run -d -e DOMAIN=cluster --name my-nginx -p 80:80 nginx
$ kubectl run my-nginx --image=nginx --port=80 --env="DOMAIN=cluster"

# docker ps 
$ docker ps 
$ kubectl get pods

# docker exec
$ docker exec [容器id] ls
$ kubectl exec [pod_id] ls

# docker exec 交互式
$ docker exec -it [容器id] /bin/sh
$ kubectl exec -it [pod_id] -- /bin/sh

# docker info
$ docker info 
$ kubectl cluster-info

yaml文件管理服务:

 用yaml文件来创建服务,创建nginx.yaml文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: my-nginx
spec:
 replicas: 3
 template:
   metadata:
     labels:
       app: nginx
   spec:
     containers:
     - name: nginx
       image: hongdada/nginx:v3
       ports:
       - containerPort: 80

启动管理服务:

# 根据yaml文件创建服务

$ kubectl create -f nginx.yaml 
deployment "my-nginx" created

# 查看deployment

$ kubectl get deployments
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
my-nginx   3         3         3            0           17s

# 查看Pod

$ kubectl get pod
NAME                        READY     STATUS              RESTARTS   AGE
my-nginx-3552297034-60v0h   0/1       ContainerCreating   0          9s
my-nginx-3552297034-pwxfr   0/1       ContainerCreating   0          9s
my-nginx-3552297034-zlxrn   0/1       ContainerCreating   0          9s

# 根据yaml文件删除服务

$ kubectl delete -f nginx.yaml 
deployment "my-nginx" deleted

$ kubectl get pod
No resources found.
$ kubectl get deployment
No resources found.

Service:

我们创建一个nginx服务

[root@localhost kubernetes]# kubectl run my-nginx --image=hongdada/nginx:v3 --port=80
deployment "my-nginx" created
[root@localhost kubernetes]# kubectl expose deployment/my-nginx --type="NodePort" --port 80
service "my-nginx" exposed
[root@localhost kubernetes]# kubectl get services
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1     <none>        443/TCP        16h
my-nginx     10.254.51.51   <nodes>       80:31377/TCP   10s

https://blog.csdn.net/qq_34701586/article/details/78732470

https://www.jianshu.com/p/93f8b074b681

原文地址:https://www.cnblogs.com/hongdada/p/9255182.html