kubeadm安装kubernetes_v1.16.3 (2019-12-01)

################################################################################################################################
                                              kubeadm安装kubernetes_v1.16.3 (2019-12-01)
#安装单master节点                                                    
Kubernetes v1.16.3
calico 3.9
nginx-ingress 1.5.5
Docker 18.09.7

#选择2台服务器:
10.100.100.227  demo-master-a-1       master      123456789
10.100.100.229  demo-worker-a-1       slave       123456789

#查看防火墙的规则
firewall-cmd --list-all
systemctl stop firewalld.service
systemctl stop iptables 

iptables -F
iptables -X
iptables-save 

#关闭系统selinux

#在slave角色worker节点设置主机名
hostnamectl set-hostname demo-worker-a-1 

#添加解析
cat /etc/hosts

10.100.100.227    demo-master-a-1
10.100.100.229    demo-worker-a-1

#worker 节点都要执行
curl -sSL https://kuboard.cn/install-script/v1.16.3/install_kubelet.sh | sh

#设置主机名
hostnamectl set-hostname demo-master-a-1

#关闭系统selinux

#添加解析
cat /etc/hosts
127.0.0.1         demo-master-a-1
10.100.100.227    apiserver.demo
10.100.100.227    demo-master-a-1
10.100.100.229    demo-worker-a-1

#安装基础安装包
curl -sSL https://kuboard.cn/install-script/v1.16.3/install_kubelet.sh | sh

# 以下操作,在 master节点下执行
# 替换 10.100.100.227 为 master 节点实际 IP(请使用内网 IP)
# export 命令只在当前 shell 会话中有效,开启新的 shell 窗口后,如果要继续安装过程,请重新执行此处的 export 命令
# 替换 apiserver.demo 为 您想要的 dnsName
# Kubernetes 容器组所在的网段,该网段安装完成后,由 kubernetes 创建,事先并不存在于您的物理网络中
#如果添加hosts 可以不用执行
# 在 master 节点和 worker 节点都要执行

export MASTER_IP=10.100.100.227
export APISERVER_NAME=apiserver.demo
export POD_SUBNET=10.209.0.1/24
echo "${MASTER_IP}    ${APISERVER_NAME}" >> /etc/hosts

cat /etc/hosts

10.100.100.227    apiserver.demo
10.100.100.227    demo-master-a-1
10.100.100.229    demo-worker-a-1

curl -sSL https://kuboard.cn/install-script/v1.16.3/init_master.sh | sh

# 只在 master 节点执行
# 执行如下命令,等待 3-10 分钟,直到所有的容器组处于 Running 状态
# 查看 master 节点初始化结果

watch kubectl get pod -n kube-system -o wide

kubectl get nodes -o wide

#主节点执行的这个命令,获取的token,
kubeadm token create --print-join-command
# 只在 worker 节点执行
# 替换 x.x.x.x 为 master 节点的内网 IP
export MASTER_IP=x.x.x.x
# 替换 apiserver.demo 为初始化 master 节点时所使用的 APISERVER_NAME
export APISERVER_NAME=apiserver.demo
echo "${MASTER_IP}    ${APISERVER_NAME}" >> /etc/hosts

# 替换为 master 节点上 kubeadm token create 命令的输出
kubeadm join apiserver.demo:6443 --token k5boem.iub5bthvw1756mf6     --discovery-token-ca-cert-hash sha256:6d5259b468b8f8e056e8ce3e76c1e31b5c5dc4ef744edac3f9c9d598397a6f8c

在worker节点执行master 主节点输出的信息
# kubeadm token create 命令的输出

# 以下操作,在 master节点下执行
# 替换 10.100.100.227 为 master 节点实际 IP(请使用内网 IP)
# export 命令只在当前 shell 会话中有效,开启新的 shell 窗口后,如果要继续安装过程,请重新执行此处的 export 命令
# 替换 apiserver.demo 为 您想要的 dnsName
# Kubernetes 容器组所在的网段,该网段安装完成后,由 kubernetes 创建,事先并不存在于您的物理网络中
#如果添加hosts 可以不用执行
# 在 master 节点和 worker 节点都要执行

export MASTER_IP=10.100.100.227
export APISERVER_NAME=apiserver.demo
export POD_SUBNET=10.209.0.1/24
echo "${MASTER_IP}    ${APISERVER_NAME}" >> /etc/hosts

kubeadm join apiserver.demo:6443 --token 5iif0a.wgbde5vcu1x2v7j7     --discovery-token-ca-cert-hash sha256:e79fe1202e8f5df226085db4b183674c8409e76ed5442e987934a3e2fc2b2374

在master 安装 Kubernetes Dashboard
root@demo-master-a-1 ~]# kubectl apply -f https://kuboard.cn/install-script/k8s-dashboard/auth.yaml
serviceaccount/admin-user created
clusterrolebinding.rbac.authorization.k8s.io/admin-user created

[root@demo-master-a-1 ~]# kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
Name:         admin-user-token-n8k2c
Namespace:    kubernetes-dashboard
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: admin-user
              kubernetes.io/service-account.uid: 0fa4febd-6db4-4263-b3b9-a429f473cc78

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1025 bytes
namespace:  20 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6Ikh0cjZYWk81bDY5UTdRa3ZSQ1ZQM1AwQTZ5MVlNRjZHYzJ5blNwbHc5TG8ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLW44azJjIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIwZmE0ZmViZC02ZGI0LTQyNjMtYjNiOS1hNDI5ZjQ3M2NjNzgiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.tq0p2YJH0qXRVVlhJnEH7huwr90EE1-1d7aDYmSzkNQn6UzxPG0QVRey-DO3dFrAWeRhYCDse9t4X4C5UPOSD4FF3eSaA2t19w69W8R2ukzhFSrFTsY7NsZKHAKwI3SMJYYNF15zzvzaZBKo-52rSfmevGFtZFOrVF-bz_3y_LILvWmlBC4tvGZkEW1-OjxYSlhB7pk1rRR93Yu_YbdfjnZT_XmKCM2yIy8lnrQPIoR4uZIqehNh5jia52F_dVX9wpvoPN80-dX2vZOBDITwAsIDSNQMSG5pSap-Qgk9IsuCafbOvHC-dGpGqbHlmHNSL_Fj1KAL5esH865BhDVerw
[root@demo-master-a-1 ~]# 

将127.0.0.1 改成用本机IP访问:

# 推荐使用下面这种方式
$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml
$ vi recommended.yaml
# 修改Service为NodePort类型

#添加这个type: NodePort
cat recommended.yaml
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Namespace
metadata:
  name: kubernetes-dashboard

---

apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard

---

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 443
      targetPort: 8443
  type: NodePort
  selector:
    k8s-app: kubernetes-dashboard

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-certs
  namespace: kubernetes-dashboard
type: Opaque

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-csrf
  namespace: kubernetes-dashboard
type: Opaque
data:
  csrf: ""

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-key-holder
  namespace: kubernetes-dashboard
type: Opaque

---

kind: ConfigMap
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-settings
  namespace: kubernetes-dashboard

---

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
rules:
  # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
  - apiGroups: [""]
    resources: ["secrets"]
    resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
    verbs: ["get", "update", "delete"]
    # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
  - apiGroups: [""]
    resources: ["configmaps"]
    resourceNames: ["kubernetes-dashboard-settings"]
    verbs: ["get", "update"]
    # Allow Dashboard to get metrics.
  - apiGroups: [""]
    resources: ["services"]
    resourceNames: ["heapster", "dashboard-metrics-scraper"]
    verbs: ["proxy"]
  - apiGroups: [""]
    resources: ["services/proxy"]
    resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
    verbs: ["get"]

---

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
rules:
  # Allow Metrics Scraper to get metrics from the Metrics server
  - apiGroups: ["metrics.k8s.io"]
    resources: ["pods", "nodes"]
    verbs: ["get", "list", "watch"]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: kubernetes-dashboard
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kubernetes-dashboard

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kubernetes-dashboard
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kubernetes-dashboard

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      containers:
        - name: kubernetes-dashboard
          image: kubernetesui/dashboard:v2.0.0-beta5
          imagePullPolicy: Always
          ports:
            - containerPort: 8443
              protocol: TCP
          args:
            - --auto-generate-certificates
            - --namespace=kubernetes-dashboard
            # Uncomment the following line to manually specify Kubernetes API server Host
            # If not specified, Dashboard will attempt to auto discover the API server and connect
            # to it. Uncomment only if the default does not work.
            # - --apiserver-host=http://my-address:port
          volumeMounts:
            - name: kubernetes-dashboard-certs
              mountPath: /certs
              # Create on-disk volume to store exec logs
            - mountPath: /tmp
              name: tmp-volume
          livenessProbe:
            httpGet:
              scheme: HTTPS
              path: /
              port: 8443
            initialDelaySeconds: 30
            timeoutSeconds: 30
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            runAsUser: 1001
            runAsGroup: 2001
      volumes:
        - name: kubernetes-dashboard-certs
          secret:
            secretName: kubernetes-dashboard-certs
        - name: tmp-volume
          emptyDir: {}
      serviceAccountName: kubernetes-dashboard
      nodeSelector:
        "beta.kubernetes.io/os": linux
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule

---

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: dashboard-metrics-scraper
  name: dashboard-metrics-scraper
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 8000
      targetPort: 8000
  selector:
    k8s-app: dashboard-metrics-scraper

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: dashboard-metrics-scraper
  name: dashboard-metrics-scraper
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: dashboard-metrics-scraper
  template:
    metadata:
      labels:
        k8s-app: dashboard-metrics-scraper
      annotations:
        seccomp.security.alpha.kubernetes.io/pod: 'runtime/default'
    spec:
      containers:
        - name: dashboard-metrics-scraper
          image: kubernetesui/metrics-scraper:v1.0.1
          ports:
            - containerPort: 8000
              protocol: TCP
          livenessProbe:
            httpGet:
              scheme: HTTP
              path: /
              port: 8000
            initialDelaySeconds: 30
            timeoutSeconds: 30
          volumeMounts:
          - mountPath: /tmp
            name: tmp-volume
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            runAsUser: 1001
            runAsGroup: 2001
      serviceAccountName: kubernetes-dashboard
      nodeSelector:
        "beta.kubernetes.io/os": linux
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
      volumes:
        - name: tmp-volume
          emptyDir: {}
[root@demo-master-a-1 home]# 

https://github.com/cnych/kubernetes-learning/blob/master/docs/index.md

视频
https://www.qikqiak.com/k8s-book/docs/67.Upgrade%E9%9B%86%E7%BE%A4.html

安装
https://www.qikqiak.com/k8strain/k8s-basic/install/

https://github.com/cnych/kubernetes-learning/blob/master/docs/40.ingress.md

######################################################################################################################
                                           
                                   kubeadm安装kubernetes_v1.16.3 (2019-12-01) 安装成功
用此链接安装                                   
https://www.qikqiak.com/k8strain/k8s-basic/install/#dashboard
                                   
hostnamectl set-hostname demo-master-a-1
hostnamectl set-hostname demo-worker-a-1

10.100.100.227    demo-master-a-1
10.100.100.229    demo-worker-a-1

systemctl stop firewalld
systemctl disable firewalld

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

iptables -F
iptables -X
iptables-save 

由于开启内核 ipv4 转发需要加载 br_netfilter 模块,所以加载下该模块
modprobe br_netfilter

创建/etc/sysctl.d/k8s.conf文件,添加如下内容:
cat /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0

sysctl -p /etc/sysctl.d/k8s.conf

安装 ipvs
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

接下来还需要确保各个节点上已经安装了 ipset 软件包

yum -y install ipset

yum -y install ipvsadm

yum install chrony -y

systemctl enable chronyd
systemctl start chronyd
chronyc sources

注释掉 SWAP 的自动挂载,使用free -m确认 swap 已经关闭
swapoff -a

刷新内核
sysctl -p /etc/sysctl.d/k8s.conf

安装 Docke
yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum list docker-ce --showduplicates | sort -r

建议安装18.09版本,是最新验证的版本

yum -y install docker-ce-18.09.9

mkdir -p /etc/docker

配置 Docker 镜像加速器
vi /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors" : [
    "https://ot2k4d59.mirror.aliyuncs.com/"
  ]
}

启动 Docker
systemctl start docker
systemctl enable docker

现在我们就可以来安装 Kubeadm 了,我们这里是通过指定yum 源的方式来进行安装的
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
        https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF

yum源是需要科学上网的,如果不能科学上网的话,我们可以使用阿里云的源进行安装:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
        http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

安装 kubeadm、kubelet、kubectl
# --disableexcludes 禁掉除了kubernetes之外的别的仓库
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

查看版本
kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:15:39Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}

kubelet 设置成开机启动
systemctl enable --now kubelet

初始化集群
master 节点配置 kubeadm 初始化文件

然后接下来在 master 节点配置 kubeadm 初始化文件,可以通过如下命令导出默认的初始化配置:
kubeadm config print init-defaults > kubeadm.yaml

比如修改imageRepository 的值,kube-proxy 的模式为 ipvs,另外需要注意的是我们这里是准备安装 flannel 网络插件的,需要将 networking.podSubnet 设置为10.244.0.0/16

apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 10.151.30.11  # apiserver 节点内网IP
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: ydzs-master  # 默认读取当前master节点的hostname
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: gcr.azk8s.cn/google_containers  # 修改成微软镜像源
kind: ClusterConfiguration
kubernetesVersion: v1.16.2
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16  # Pod 网段,flannel插件需要使用这个网段
  serviceSubnet: 10.96.0.0/12
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs  # kube-proxy 模式

然后使用上面的配置文件进行初始化:
kubeadm init --config kubeadm.yaml

拷贝 kubeconfig 文件
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

添加节点¶
记住初始化集群上面的配置和操作要提前做好,将 master 节点上面的 $HOME/.kube/config 文件拷贝到 node 节点对应的文件中,安装 kubeadm、kubelet、kubectl(可选),然后执行上面初始化完成后提示的 join 命令即可:

kubeadm join 10.151.30.11:6443 --token abcdef.0123456789abcdef \
>     --discovery-token-ca-cert-hash sha256:a292e66049e45264f848186d2fa3582dc360f3b5006cc160f137b5d436e078c2

kubeadm token create --print-join-command 

$ kubectl get nodes
NAME          STATUS     ROLES    AGE    VERSION
ydzs-master   NotReady      master   39m    v1.15.3
ydzs-node1    NotReady   <none>   106s   v1.15.3


######################################################################################################################

学习马哥的目标

查看更详细的信息
kubectl get pods -n kube-system -o wide

查看运行的docker
kubectl get pods -o wide

kubectl get pods -n kube-system
NAME                                      READY   STATUS    RESTARTS   AGE
coredns-667f964f9b-fbjcq                  1/1     Running   0          3d21h
coredns-667f964f9b-jgxs4                  1/1     Running   0          3d21h
etcd-demo-master-a-1                      1/1     Running   0          3d21h
kube-apiserver-demo-master-a-1            1/1     Running   0          3d21h
kube-controller-manager-demo-master-a-1   1/1     Running   2          3d21h
kube-flannel-ds-amd64-8kckc               1/1     Running   0          3d21h
kube-flannel-ds-amd64-x7l59               1/1     Running   0          3d21h
kube-proxy-7bhjl                          1/1     Running   0          3d21h
kube-proxy-8n6q8                          1/1     Running   0          3d21h
kube-scheduler-demo-master-a-1            1/1     Running   1          3d21h
[root@demo-master-a-1 ~]# kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   3d21h

[root@demo-master-a-1 ~]# kubectl create service clusterip ngx-dep --tcp=4321:8765
service/ngx-dep created

查看服务
kubectl get services

查看副本的数量
kubectl get pods 

扩容的方法
[root@demo-master-a-1 ~]# kubectl scale --replicas=3 deployment nginx-deploy
deployment.apps/nginx-deploy scaled

查看
[root@demo-master-a-1 ~]# kubectl get pods 
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-54f57cf6bf-27hkg   1/1     Running   0          8s
nginx-deploy-54f57cf6bf-4rsb9   1/1     Running   0          3d19h
nginx-deploy-54f57cf6bf-9mnxg   1/1     Running   0          3d19h
[root@demo-master-a-1 ~]# 

缩容的方法
[root@demo-master-a-1 ~]# kubectl scale --replicas=2 deployment nginx-deploy
deployment.apps/nginx-deploy scaled

[root@demo-master-a-1 ~]# kubectl get pods 
NAME                            READY   STATUS        RESTARTS   AGE
nginx-deploy-54f57cf6bf-27hkg   0/1     Terminating   0          5m29s
nginx-deploy-54f57cf6bf-4rsb9   1/1     Running       0          3d19h
nginx-deploy-54f57cf6bf-9mnxg   1/1     Running       0          3d19h

[root@demo-master-a-1 ~]# kubectl get pods 
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-54f57cf6bf-4rsb9   1/1     Running   0          3d19h
nginx-deploy-54f57cf6bf-9mnxg   1/1     Running   0          3d19h
[root@demo-master-a-1 ~]# 

通过这个方法映射docker 容器的端口
kubectl expose deployment nginx-deploy --port=7654 --target-port=80 --type=NodePort

kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/nginx-deploy-54f57cf6bf-4rsb9   1/1     Running   0          6d11h
pod/nginx-deploy-54f57cf6bf-5j6v6   1/1     Running   0          2d15h
pod/nginx-deploy-54f57cf6bf-n5p59   1/1     Running   0          2d15h
pod/nginx-deploy-54f57cf6bf-t96r4   1/1     Running   0          2d15h

NAME                                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/kubernetes                      ClusterIP   10.96.0.1       <none>        443/TCP          6d13h
service/nginx-deploy                    NodePort    10.105.127.56   <none>        7654:30631/TCP   3m11s
service/nginx-deploy-54f57cf6bf-4rsb9   ClusterIP   10.98.4.171     <none>        4322/TCP         2d16h
service/ngx-dep                         ClusterIP   10.104.10.249   <none>        4321/TCP         2d16h

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deploy   4/4     4            4           6d11h

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deploy-54f57cf6bf   4         4         4       6d11h
[root@demo-master-a-1 ~]# 

用这个端口就可以访问
http://10.100.100.227:30631/
http://10.100.100.229:30631/

[root@demo-master-a-1 ~]# kubectl get svc
NAME                            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes                      ClusterIP   10.96.0.1       <none>        443/TCP          6d15h
nginx-deploy                    NodePort    10.105.127.56   <none>        7654:30631/TCP   71m
nginx-deploy-54f57cf6bf-4rsb9   ClusterIP   10.98.4.171     <none>        4322/TCP         2d17h
ngx-dep                         ClusterIP   10.104.10.249   <none>        4321/TCP         2d17h
[root@demo-master-a-1 ~]# 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值